现在的位置: 首页 > 综合 > 正文

C++已知两点求角度

2017年10月04日 ⁄ 综合 ⁄ 共 341字 ⁄ 字号 评论关闭

        C++已知两点求角度:

float angle(float x1, float y1, float x2, float y2)
{
	float angle_temp;
	float xx, yy;

	xx = x2 - x1;
	yy = y2 - y1;

	if (xx == 0.0)
		angle_temp = PI / 2.0;
	else
		angle_temp = atan(fabs(yy / xx));

	if ((xx < 0.0) && (yy >= 0.0))
		angle_temp = PI - angle_temp;
	else if ((xx < 0.0) && (yy < 0.0))
		angle_temp = PI + angle_temp;
	else if ((xx >= 0.0) && (yy < 0.0))
		angle_temp = PI * 2.0 - angle_temp;

	return (angle_temp);
}

抱歉!评论已关闭.