0

So the idea is that I want to get 20 points in X,Y coordinates between two clicks on a cirlce. The only criteria is that the points always have to be between the smaller angle between the 2 clicks.

Lets say the midpoint of the circle is c.x,c.y, radius is c.r and the two clicks are p1.x,p1.y and p2.x,p2.y

What I tried to do so far is get the angle of both clicks form the X axis with respect to the centre of the circle.

float from = std::fmod(atan2(p1.y - c.y, p1.x - c.x), 2 * M_PI); //Update range to 0-2pi
float to = std::fmod(atan2(p2.y - c.y, p2.x - c.x), 2 * M_PI);

And then get the distance between the two clicks. And calculate the coordinates of the points in a loop.

float fi =  from + ((to - from)* i / 20); // 20 points needed
vertices[i] = vec2(c.x + c.r * cosf(fi), c.y + c.r * sinf(fi)); // x = cx+r*cos(fi), y=cy+r*sin(fi)

The problem with this approach is that in some cases it returns the outer curve of the cirlce. Picture shows 3 such curves on 3 clicks. The calculated curves are shown in white, the blue is the desired output. render

Gabor
  • 5
  • 2
  • Is the `fMod` in your code a typo for [`std::fmod`](https://en.cppreference.com/w/cpp/numeric/math/fmod) or is that another function? In any case, please edit the question to include a [mre]. – Bob__ Mar 29 '20 at 11:36
  • yes it is the same I'm sorry – Gabor Mar 29 '20 at 11:45
  • Related: https://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles – Bob__ Mar 29 '20 at 12:08

1 Answers1

0

You need to calculate the difference, so it is in the range [-PI;PI].

You can do it like this:

float from = atan2(p1.y - pos.y, p1.x - pos.x);
float to = atan2(p2.y - pos.y, p2.x - pos.x);
float diff = to - from;

if (diff<-PI) {
    diff += 2*PI;
} else if (diff>PI) {
    diff -= 2*PI;
}

Then you can calculate the "interpolated" angle like this:

float fi = from + diff*t;  // t is a number between 0 and 1

This works, because atan2 returns an angle between -PI and PI, so the simple difference will be between -2*PI and 2*PI. And the if will restrict this range to be between -PI and PI (adding/subtracting 2*PI doesn't change the actual, "visible" angle).

geza
  • 26,117
  • 6
  • 47
  • 111