c++计算两个角度之间的所有角度

发布于 2024-09-25 15:30:23 字数 179 浏览 2 评论 0原文

我有一组先前定义的浮点值(一组角度) 我需要一个函数,它接受两个角度作为输入并返回它们之间的所有角度。 用户首先输入一组浮动角度,然后用户可以输入任意两个角度(我的程序应该返回这些值之间的所有角度) 例如 30,310(返回所有角度 >=30 且 <310 但 310,30 也应该有效(角度应该环绕)

提前致谢

I have a set of previously defined float values(a set of angles)
I need a function which takes two angles as input and returns all the angles between them.
The user first enters a set of angles in float and then the user can enter any two angles(my program should return all the angles between these values)
For example
30,310(return all angles >=30 and <310
but
310,30 should also be valid(angles should wrap around)

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〃温暖了心ぐ 2024-10-02 15:30:23

我明白你在问什么。对于角度列表中的每个角度 A,您想知道 A 是否包含在由角度 B 和 C 定义的扇区中。如果 B>C,则该扇区从角度 B 开始,并围绕 0 度标记结束在 A 处。

这里有一些代码可以满足您的要求:

#include <vector>
#include <iostream>
#include <cmath>

bool angleIsBetween(float angle, float start, float end)
{
    // If angle is outside the range [0,360), convert it to the
    // equivalent angle inside that range.
    angle = fmod(angle, 360.0f);

    // If start>end, then the angle range wraps around 0.
    return (start<=end) ? (angle>=start && angle<=end)
                        : (angle>=start || angle<=end);
}

int main()
{
    float angles[] = {0.0, 180.0, 30};
    size_t nAngles = sizeof(angles)/sizeof(angles[0]);

    for (size_t i=0; i<nAngles; ++i)
    {
        std::cout << angleIsBetween(angles[i], 30.0, 310.0) << " ";
        std::cout << angleIsBetween(angles[i], 310.0, 30) << " ";
    }
    std::cout << "\n";

    return 0;
}

输出:0 1 1 0 1 1

I get what you're asking. For each angle A in a list of angles, you want to know if A is included in the sector defined by the angles B and C. If B>C, then the sector starts at angle B and wraps around the 0 degree mark to end at A.

Here's some code that does what you're asking for:

#include <vector>
#include <iostream>
#include <cmath>

bool angleIsBetween(float angle, float start, float end)
{
    // If angle is outside the range [0,360), convert it to the
    // equivalent angle inside that range.
    angle = fmod(angle, 360.0f);

    // If start>end, then the angle range wraps around 0.
    return (start<=end) ? (angle>=start && angle<=end)
                        : (angle>=start || angle<=end);
}

int main()
{
    float angles[] = {0.0, 180.0, 30};
    size_t nAngles = sizeof(angles)/sizeof(angles[0]);

    for (size_t i=0; i<nAngles; ++i)
    {
        std::cout << angleIsBetween(angles[i], 30.0, 310.0) << " ";
        std::cout << angleIsBetween(angles[i], 310.0, 30) << " ";
    }
    std::cout << "\n";

    return 0;
}

This outputs: 0 1 1 0 1 1

从﹋此江山别 2024-10-02 15:30:23

我猜类似

for( int f = start; f != end; f = (f + 1) % 360 ) {
    // do something with f
}

I am guessing something like:

for( int f = start; f != end; f = (f + 1) % 360 ) {
    // do something with f
}
夏有森光若流苏 2024-10-02 15:30:23

您的意思是说您的程序应该返回先前输入的角度集中存在的所有角度吗?

在这种情况下,您只需比较存储的值和两个输入角度。类似 -

for(i=0; i < array_length; i++)

{

   if(array[i] >= value1 && array[i] <= value2)

  {
     cout << array[i];
  }

}

更好的方法可能是对先前存储的角度进行排序。在这种情况下,您不需要遍历所有存储的值。

如果您需要获取两个角度之间的所有角度,那么这是无限的(如果您不只考虑整数值)

Do you mean to say your program should return all the angles present in previously entered set of angles?

In that case you just need to compare the stored values and the two input angles. Something like-

for(i=0; i < array_length; i++)

{

   if(array[i] >= value1 && array[i] <= value2)

  {
     cout << array[i];
  }

}

A better way may be to sort the previously stored angles. In that case you won't need to traverse all through the stored values.

If you need to get all the angles between two angles, then that is infinite(if you are not considering only integer values)

玻璃人 2024-10-02 15:30:23

这是一个打印给定范围内所有角度的函数。希望这有帮助:

void angles(double a1, double a2) {
    int deg1, min1, sec1, deg2, min2, sec2;
    double const mult = 0.0166666667;
    double angle;
    if (a1 == (int)a1) {
        deg1 = a1; min1 = 0; sec1 = 0;
    } else {
        deg1 = a1;
        min1 = (int)(60 * (a1 - (int)a1));
        sec1 = (int)(60 * ((60 * (a1 - (int)a1)) - min1) + 0.5);
    }
    if (a2 == (int)a2) {
        deg2 = a2 - 1; min2 = 59; sec2 = 60;
    } else {
        deg2 = a2;
        min2 = (int)(60 * (a2 - (int)a2));
        sec2 = (int)(60 * ((60 * (a2 - (int)a2)) - min2) + 0.5);
        if (sec2 == 0) {
            sec2 = 60;
            min2--;
        }
    }
    if (deg1 <= deg2) {
        cout << deg1 << " " << min1 << " " << sec1 << " < " << deg2 << " " << min2 << " " << sec2 << endl;
        while (deg1 <= deg2) {
            if (deg1 < deg2) {
                while (min1 < 60) {
                    while (sec1 < 60) {
                        angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                        cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                        sec1++;
                    }
                    sec1 = 0;
                    min1++;
                }
            } else {
                if (min1 < min2) {
                    while (min1 <= min2) {
                        if (sec1 < sec2) {
                            while (sec1 < 60) {
                                angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                                cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                                sec1++;
                            }
                            sec1 = 0;
                            min1++;
                        } else {
                            while (sec1 <= sec2) {
                                angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                                cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                                sec1++;
                            }
                            sec1 = 0;
                            min1++;
                        }
                    }
                } else {
                    while (min1 < 60) {
                        while (sec1 < 60) {
                            angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                            cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                            sec1++;
                        }
                        sec1 = 0;
                        min1++;
                    }
                }
            }
            min1 = 0;
            deg1++;
        }
    }
}

int main() {
    angles(40.3472, 40.5);
    return 0;
}

Here's a function that prints all angles between a given range. Hope this helps:

void angles(double a1, double a2) {
    int deg1, min1, sec1, deg2, min2, sec2;
    double const mult = 0.0166666667;
    double angle;
    if (a1 == (int)a1) {
        deg1 = a1; min1 = 0; sec1 = 0;
    } else {
        deg1 = a1;
        min1 = (int)(60 * (a1 - (int)a1));
        sec1 = (int)(60 * ((60 * (a1 - (int)a1)) - min1) + 0.5);
    }
    if (a2 == (int)a2) {
        deg2 = a2 - 1; min2 = 59; sec2 = 60;
    } else {
        deg2 = a2;
        min2 = (int)(60 * (a2 - (int)a2));
        sec2 = (int)(60 * ((60 * (a2 - (int)a2)) - min2) + 0.5);
        if (sec2 == 0) {
            sec2 = 60;
            min2--;
        }
    }
    if (deg1 <= deg2) {
        cout << deg1 << " " << min1 << " " << sec1 << " < " << deg2 << " " << min2 << " " << sec2 << endl;
        while (deg1 <= deg2) {
            if (deg1 < deg2) {
                while (min1 < 60) {
                    while (sec1 < 60) {
                        angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                        cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                        sec1++;
                    }
                    sec1 = 0;
                    min1++;
                }
            } else {
                if (min1 < min2) {
                    while (min1 <= min2) {
                        if (sec1 < sec2) {
                            while (sec1 < 60) {
                                angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                                cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                                sec1++;
                            }
                            sec1 = 0;
                            min1++;
                        } else {
                            while (sec1 <= sec2) {
                                angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                                cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                                sec1++;
                            }
                            sec1 = 0;
                            min1++;
                        }
                    }
                } else {
                    while (min1 < 60) {
                        while (sec1 < 60) {
                            angle = deg1 + (min1 * mult) + (sec1 * mult * mult);
                            cout << deg1 << " " << min1 << " " << sec1 << " = " << angle << endl;
                            sec1++;
                        }
                        sec1 = 0;
                        min1++;
                    }
                }
            }
            min1 = 0;
            deg1++;
        }
    }
}

int main() {
    angles(40.3472, 40.5);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文