我有一组先前定义的浮点值(一组角度) 我需要一个函数,它将两个角度作为输入并返回它们之间的所有角度。 用户首先在float中输入一组角度,然后用户可以输入任意两个角度(我的程序应返回这些值之间的所有角度) 例如 30,310(返回所有角度> = 30和< 310 但 310,30也应该是有效的(角度应该环绕)
提前致谢
答案 0 :(得分:3)
我得到了你的要求。对于角度列表中的每个角度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
答案 1 :(得分:2)
我猜 类似 :
for( int f = start; f != end; f = (f + 1) % 360 ) {
// do something with f
}
答案 2 :(得分:2)
你的意思是说你的程序应该返回先前输入的一组角度中存在的所有角度吗?
在这种情况下,您只需要比较存储的值和两个输入角度。像 -
这样的东西for(i = 0; i&lt; array_length; i ++)
{
if(array[i] >= value1 && array[i] <= value2)
{
cout << array[i];
}
}
更好的方法可能是对先前存储的角度进行排序。在这种情况下,您不需要遍历存储的值。
如果你需要获得两个角度之间的所有角度,那么这是无限的(如果你不考虑整数值)
答案 3 :(得分:1)
这是一个打印给定范围之间所有角度的函数。希望这会有所帮助:
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;
}