Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: How confident are you that you could have done this?

Author: Joseph Tadeusz

Date: 10:02:35 02/11/06

Go up one level in this thread


#include <math.h>

int main() {
  double r, // radius cone
         h, // height cone
         R, // radius sphere
  x,y,centre, volume_sphere;
  double maxd=0,displ,limit,pi=3.14159265,submerged;
  double slope;
  double alpha;	// angle of cone with x axis
  double bestR,bestx,besty,bestcentre,bestlimit,bestsubmerged;
  double bestvolume_sphere,bestslope;

  printf("enter cone radius...\n");
  scanf("%lf",&r);
  printf("enter cone height...\n");
  scanf("%lf",&h);

  slope = r/h;
  alpha = atan(slope);

  for(R = r / 4; R < 4 * r; R += .0001) {// go from small to big sphere
    y = R * cos(alpha); 		// y coordinate of contact
    if(y > r)                         // outside cone
      break;
    x = (h / r) * y;     		// x coordinate of contact
    centre = x + R * sin(alpha); 	// x coordinate of centre of sphere
    limit = centre + R;       	// x coordinate of far out limit of sphere
    if(limit < h)              	// too deep inside cone
      continue;
    if(limit > 4 * R + h)        	// too far outside cone
      break;
    submerged = 2 * R - (limit - h);	// x linesegment of sphere inside cone

    // displacement:  http://www.lmnoeng.com/Volume/CylConeSphere.htm
    displ = ( pi / 3 ) * submerged * submerged * ( 1.5 * 2 * R - submerged);

    if(displ >= maxd) {
      maxd = displ;
      volume_sphere = 1.3333333*pi*R*R*R;
      bestR = R;
      bestx = x;
      besty = y;
      bestcentre = centre;
      bestlimit = limit;
      bestsubmerged = submerged;
      bestvolume_sphere = volume_sphere;
      bestslope = slope;
    }
  }
      printf("displacement=%f radius=%f x=%f y=%f  centre=%f limit=%lf \
submerged=%f volume_sphere=%f\n",\
maxd,bestR,bestx,besty,bestcentre,bestlimit,bestsubmerged, bestvolume_sphere);
      printf("formula for slope: y = %f * x\n",bestslope);
      printf("formula for circle: y = sqrt(%f * %f - (x - %f)^2)\n",\
bestR,bestR,bestcentre);
  return 0;
}

Here's my version of the cone/sphere problem. The formulas in the output can be
used in a plotting utility like kplot to get a picture.
I can't guarantee that it's correct.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.