在坐标系中从一个角度旋转到另一个角度所经过的象限 (直接看简洁版)
数学基本还给老师了所以基本全都是用逻辑实现的。旋转分为顺时针和逆时针旋转。
顺时针旋转
int GetAngleQuadrantClockwise(float fAngle)
{
int iQuadrant = 0;
if (fAngle == 0)
{
iQuadrant = 4;
}
else if (fAngle == 270)
{
iQuadrant = 3;
}
else if (fAngle == 180)
{
iQuadrant = 2;
}
else if (fAngle == 90)
{
iQuadrant = 1;
}
else if (fAngle > 0 && fAngle < 90)
{
iQuadrant = 1;
}
else if (fAngle > 90 && fAngle < 180)
{
iQuadrant = 2;
}
else if (fAngle > 180 && fAngle < 270)
{
iQuadrant = 3;
}
else if (fAngle > 270 && fAngle < 360)
{
iQuadrant = 4;
}
return iQuadrant;
}
//顺时针转所经过的象限
void clockwise (float start_angle, float end_angle, std::vector<int>& vecQuadrant)
{
bool bSign = false;
if (GetAngleQuadrantClockwise(start_angle) == GetAngleQuadrantClockwise(end_angle)) //在同一个象限了
{
vecQuadrant.push_back(GetAngleQuadrantClockwise(start_angle));
start_angle -= 90;
if (start_angle < 0)
{
start_angle += 360;
}
bSign = true;
}
do
{
vecQuadrant.push_back(GetAngleQuadrantClockwise(start_angle));
start_angle -= 90;
if (start_angle < 0)
{
start_angle += 360;
}
} while (GetAngleQuadrantClockwise(start_angle) != GetAngleQuadrantClockwise(end_angle));
if (!bSign)
{
if (start_angle != 90 && start_angle != 0 && start_angle != 180 && start_angle != 270)
{
vecQuadrant.push_back(GetAngleQuadrantClockwise(start_angle));
}
}
}
int main()
{
std::vector<int> Test;
float x = 180;
float y = 90;
if (x < 0)
{
x += 360;
}
if (y < 0)
{
y += 360;
}
//接受两个角度,返回一个保存象限的vector
clockwise(180 , 90, Test);
return 0;
}
逆时针转旋转
int GetAngleQuadrantAnticlockwise(float fAngle)
{
int iQuadrant = 0;
if (fAngle == 0)
{
iQuadrant = 1;
}
else if (fAngle == 270)
{
iQuadrant = 4;
}
else if (fAngle == 180)
{
iQuadrant = 3;
}
else if (fAngle == 90)
{
iQuadrant = 2;
}
else if (fAngle > 0 && fAngle < 90)
{
iQuadrant = 1;
}
else if (fAngle > 90 && fAngle < 180)
{
iQuadrant = 2;
}
else if (fAngle > 180 && fAngle < 270)
{
iQuadrant = 3;
}
else if (fAngle > 270 && fAngle < 360)
{
iQuadrant = 4;
}
return iQuadrant;
}
//逆时针
void anticlockwise(float start_angle, float end_angle, std::vector<int>& vecQuadrant)
{
bool bSign = false;
if (GetAngleQuadrantAnticlockwise(start_angle) == GetAngleQuadrantAnticlockwise(end_angle)) //在同一个象限了
{
vecQuadrant.push_back(GetAngleQuadrantAnticlockwise(start_angle));
start_angle += 90;
if (start_angle >= 360)
{
start_angle -= 360;
}
bSign = true;
}
do
{
vecQuadrant.push_back(GetAngleQuadrantAnticlockwise(start_angle));
start_angle += 90;
if (start_angle >= 360)
{
start_angle -= 360;
}
} while (GetAngleQuadrantAnticlockwise(start_angle) != GetAngleQuadrantAnticlockwise(end_angle));
if (!bSign)
{
if (start_angle != 90 && start_angle != 0 && start_angle != 180 && start_angle != 270)
{
vecQuadrant.push_back(GetAngleQuadrantAnticlockwise(start_angle));
}
}
}
int main()
{
std::vector<int> Test;
float x = 180;
float y = 90;
if (x < 0)
{
x += 360;
}
if (y < 0)
{
y += 360;
}
//接受两个角度,返回一个保存象限的vector
anticlockwise(180 , 90, Test);
return 0;
}
简洁版
int GetAngleQuadrantClockwise(float fAngle)
{
int iQuadrant = 0;
if (fAngle > 0 && fAngle <= 90)
{
iQuadrant = 1;
}
else if (fAngle > 90 && fAngle <= 180)
{
iQuadrant = 2;
}
else if (fAngle > 180 && fAngle <= 270)
{
iQuadrant = 3;
}
else if ((fAngle > 270 && fAngle < 360) || fAngle == 0)
{
iQuadrant = 4;
}
return iQuadrant;
}
int GetAngleQuadrantAnticlockwise(float fAngle)
{
int iQuadrant = 0;
if ((fAngle > 0 && fAngle < 90) || fAngle == 0)
{
iQuadrant = 1;
}
else if (fAngle >= 90 && fAngle < 180)
{
iQuadrant = 2;
}
else if (fAngle >= 180 && fAngle < 270)
{
iQuadrant = 3;
}
else if (fAngle >= 270 && fAngle < 360)
{
iQuadrant = 4;
}
return iQuadrant;
}
//顺时针转所经过的象限
void clockwise(float start_angle, float end_angle, std::vector<int> &vecQuadrant)
{
std::vector<int> vecTest = {0, 1, 2, 3, 4};
int iStart = GetAngleQuadrantClockwise(start_angle);
int iEnd = GetAngleQuadrantClockwise(end_angle);
vecQuadrant.push_back(iStart);
iStart--;
if (iStart == 0)
{
iStart = 4;
}
while (iStart != iEnd)
{
vecQuadrant.push_back(iStart);
iStart--;
if (iStart == 0)
{
iStart = 4;
}
}
}
//逆时针转所经过的象限
void anticlockwise(float start_angle, float end_angle, std::vector<int> &vecQuadrant)
{
std::vector<int> vecTest = {0, 1, 2, 3, 4};
int iStart = GetAngleQuadrantAnticlockwise(start_angle);
int iEnd = GetAngleQuadrantAnticlockwise(end_angle);
vecQuadrant.push_back(iStart);
iStart++;
if (iStart > 4)
{
iStart = 1;
}
while (iStart != iEnd)
{
vecQuadrant.push_back(iStart);
iStart++;
if (iStart > 4)
{
iStart = 1;
}
}
}