4

We are coding in C++, have half a circle, starting a a certain point (e.g. (0,-310)) and finishing at a certain point (0,310). We have the radius, and we have the equation X^2 + Y^2 = r^2. Now we are trying to calculate some (say 10+) points on the line of this circle.

Hence, we are trying to create an increment that will calculate the Y/X values between these points, using the equation shown above to make sure that all the points calculated are on the line of the circle.

Once we have these points, we are trying to put them into several complex equations to calculate angles of a robot arm that is to draw this shape. This is not really the priority, but I thought I should include our overall aim in the question.

How to create an increment to calculate all the coordinates on line of the half circle between our two start points?
Then put these values into the equations in the code above to calculate the angles of the robot arm. looking for a way to do this without calculating each point individually, i.e. create an increment that will do it in one go.

This is kind of what we are aiming for, to calculate the points in bold.

Rory Duncan
  • 55
  • 1
  • 1
  • 6
  • 2
    What do you need help with, resolution of points? – Thomas Matthews Mar 20 '13 at 14:13
  • 2
    You probably would be better using polar coordinates - e.g. http://janjorissen.be/drawing-a-perfect-circle-with-the-curveto-function/ (I know it's Flash, not C++, but you can see the idea) – Roger Rowland Mar 20 '13 at 14:13
  • 1
    I fail to see what this question has to do with C++? – Cheers and hth. - Alf Mar 20 '13 at 14:24
  • Did I understand right, that you need coords of bold points in this picture http://imagehost.spark-media.ru/i4/22F8BA8E-744C-E120-0A89-F8E7AD5376A7.png/2013-03-20_18h35_56.png ? – borisbn Mar 20 '13 at 14:35
  • What is the question? there is no question here. You have to add a question. – default Mar 20 '13 at 14:42
  • Okay 'borisbn' that is exactly what I am trying to do. And we are coding this in C++, and trying to create an increment to calculate all the co-ordinates on line of the half circle between our two start points. Make more sense? – Rory Duncan Mar 20 '13 at 14:45

3 Answers3

5

Do the points need to be evenly spaced? If not, then you could just use your formula directly:

// assume half-circle centered at (0,0) and radius=310
double r = 310.0;
int n = 10;
for( int i=0; i<n; i++ )
{
   double x = i*r/n;
   double y = sqrt( r*r - x*x );
   // both (x,y) and (x,-y) are points on the half-circle
}

Once this is working, you could also play with the distribution of x values to approximate even spacing around the circle.

If your circle is not centered at (0,0) then just offset the computed (x,y) by the actual center.

Eric
  • 11,392
  • 13
  • 57
  • 100
  • Thats brilliant thanks, will give it a go. Cheers! – Rory Duncan Mar 20 '13 at 15:23
  • Okay, I tried this. While the X values change, the Y values stay stuck at 310 throughout. Is there any way to change this? – Rory Duncan Mar 20 '13 at 15:33
  • @RoryDuncan -- as x gets closer to r, then x*x gets closer to r*r so sqrt(r*r - x*x) gets closer to zero, there's no way for it to stay at 310. Must be a coding error. – Eric Mar 20 '13 at 16:03
  • You were right, coding error on my part. Thanks again – Rory Duncan Mar 20 '13 at 16:24
  • I am not really sure what you mean about "you could also play with the distribution of x values to approximate even spacing around the circle." How do I go about this? I have the results but unless I ask for a large number of results, then the coordinates seem to stop halfway round the circle. How do I get it so that it shows results evenly spaced all the way round the half circle? Thanks – Rory Duncan Mar 20 '13 at 16:51
4

The points of a circle can be determined using the formulas:

x = radius * cos(angle)  
y = radius * sin(angle)

You will have to determine the piece, portion, or arc of the circle you are drawing and determine the starting angle and ending angle.

Otherwise, search SO and the web for "arc drawing algorithm c++".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • there are a multitude of ways to skin a cat, **indcluding** using the formula provided by the OP. i would use neither that formula nor the one you present. anyway, the phrasing "uses the formulas..." indicates that you think this is "the" way to do it, but that just ain't so, there are many ways, and your chosen one happens to be about the most inefficient of them all. – Cheers and hth. - Alf Mar 20 '13 at 14:21
  • 1
    Rephrased so as not to imply this is **the most efficient** method. More efficient algorithms can be found by searching the web for arc drawing algorithms. – Thomas Matthews Mar 20 '13 at 14:25
  • Thanks, will have a look about an give them a go. :) – Rory Duncan Mar 20 '13 at 14:46
2

you can do it by changing your equation to be of Theta (the angle) like that:
X = X0 + Cos(Theta)*r
Y = Y0 + Sin(Theta)*r

while in your case (X0,Y0) = (0,0), r = 310 and Theta range is between -180 - 180 (if your cos and sin in degrees) or between -Phi - Phi (if cos and sin in radii).

Now if you want 10 points you need to take your Theta range, and split it to 10 and cal X,Y for each of those values.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94