plot points on circle between 2 points.

Started by
5 comments, last by AngleWyrm 16 years, 9 months ago
Hi guys, I have some code that plots points on a circle: for (float theta = 0; theta <= 2 * PI; theta += PI / (rRadius * rAccuracy)) { float x = rRadius * cos(theta); float y = rRadius * sin(theta); } How would I instead plot points between 2 points on circle? ie I have 2 points on a circle, how do I plot points between these points? I figure I need to convert these points to theta values (Do you know how to do this) and then use the loop from theta1 to theta2. thanks!
Advertisement
EDIT: i wrote something wrong, better take a look here.

http://www.teacherschoice.com.au/Maths_Library/Coordinates/polar_-_rectangular_conversion.htm
If you look at your (kneeride's) code snippet posted in the OP, you'll see a conversion from the angle theta to a point (x,y) like so:

x := R * cos( theta )
y := R * sin( theta )

What you want to do is the inverse function, namely convert from a point to an angle. Unfortunately you have a sine _and_ a cosine of the unknown variable theta; but there is a correspondence

tan( theta ) = sin( theta ) / cos( theta )

that can be used to isolate theta.

Since this computation is to be used w/ caution due to its division by zero possibility and its periodicity, your programming API typically offers a function atan2(y,x) that considers these aspects as well.
Does this look like I'm on the right track? I found some code deep in my library. it looks similar to what I need. (I however have no idea why y is before x in the parameters)

double x(double y, double x){  if (x > 0)  {    return atan2(y,x);  }  else if (x > 0)  {    return G_PI + atan2(y,x);  }  else // x == 0  {    if (y >= 0)     {      return G_PI / 2;    }    else    {      return -(G_PI / 2);    }  }}
The atan2 function already does all neccessary case distinctions for you (okay, I don't really know the standard at this point, but AFAIK it does).

However, atan2 returns a value between -pi and +pi, and perhaps you would be more happy w/ values between 0 and 2pi. Then a downstream add-on
  if(theta<0) {     theta += TWO_PI;  }

would be appropriate.

Besides that there is a typo in your snippet w/ distincting x<0.
cool. thanks for tips
You could also just add PI to all results, and get a range of 0 to 2PI. This changes where 0 radians is (opposite direction) though.
--"I'm not at home right now, but" = lights on, but no ones home

This topic is closed to new replies.

Advertisement