【问题标题】:Find the points between two points on a circle in the smaller angle在较小角度的圆上找到两点之间的点
【发布时间】:2020-03-29 11:15:23
【问题描述】:

所以我的想法是,我想在两次单击圆环之间的 X、Y 坐标中获得 20 个点。唯一的标准是这些点必须始终位于 2 次点击之间的较小角度之间。

假设圆的中点是c.x,c.y,半径是c.r,两次点击是p1.x,p1.y > 和 p2.x,p2.y

到目前为止,我尝试做的是获取两次点击相对于圆心的 X 轴角度。

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

然后得到两次点击之间的距离。并循环计算各点的坐标。

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)

这种方法的问题在于,在某些情况下它会返回圆的外曲线。图片显示了 3 次点击后的 3 条这样的曲线。计算的曲线以白色显示,蓝色是所需的输出。 render

【问题讨论】:

标签: c++ math geometry angle atan2


【解决方案1】:

你需要计算差值,所以它在 [-PI;PI] 范围内。

你可以这样做:

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;
}

然后你可以像这样计算“插值”角度:

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

这是可行的,因为atan2 返回 -PI 和 PI 之间的角度,所以简单的区别将在 -2*PI 和 2*PI 之间。 if 会将这个范围限制在 -PI 和 PI 之间(加/减 2*PI 不会改变实际的“可见”角度)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2020-02-18
    相关资源
    最近更新 更多