简体   繁体   中英

Find the points between two points on a circle in the smaller angle

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 cx,cy , radius is cr 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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM