c ++ 转向问题
#include <iostream>
#include <math.h>
using namespace std;
class point
{
public:
//*默认构造
point(){x = 0,y = 0;}
//*赋值函数
void fuzhi(int x1,int y1);
//*两点间距离的
double juli(point r);
//*判断拐角是否是直角
bool ifzhijiao(point r1,point r3);
//*判断是否在一条直线上
bool ifzhixian(point r1,point r3);
//判断最终拐向
bool guaixiang(point r1,point r3);
//*点成
int diancheng(point r);
//*相减
void jian(point r);
//*将p2移动到原点,p1,p3对应移动
void movetozero(point &r1,point &r3);
//*判断一个点在第几象限
float whatxian(point p);
//*判断是否有点在坐标轴上
bool ifzuobiaozhou(point p);
//*判断点在那个坐标轴上
int whatzhou(point p);
int x;
int y;
};
//赋值函数
void point::fuzhi(int x1,int y1)
{
x = x1;
y = y1;
}
//判断两点间距离
double point::juli(point r)
{
double a2 = 0;
a2 = (r.y - y)*(r.y - y) + (r.x - x)*(r.x - x);
return sqrt(a2);
}
//判断拐角是否是直角
bool point::ifzhijiao(point r1,point r2)
{
r1.jian(*this);
r2.jian(*this);
return !r1.diancheng(r2);
}
//相减
void point::jian(point r)
{
x -= r.x;
y -= r.y;
}
//点成
int point::diancheng(point r)
{
return (x*r.x + y*r.y);
}
//判断是否在一条直线上
bool point::ifzhixian(point r1,point r3)
{
// cout << r1.juli(r3) << endl;
// cout << juli(r1) << endl;
// cout << juli(r3) << endl;
if (fabs(r1.juli(r3) - juli(r1) - juli(r3))<=0.01 ){
return 1;
}else{
return 0;
}
}
//判断最终拐向
bool point::guaixiang(point r1,point r3)
{
if(ifzhixian(r1,r3)){
cout << "TOWARDS" << endl;
}else{
//把p2移动到原点
movetozero(r1,r3);
if(ifzhijiao(r1,r3)){//拐角为直角
if(ifzuobiaozhou(r1)){//p1在坐标轴上
if(((whatzhou(r1) - whatzhou(r3))==1)||((whatzhou(r1) - whatzhou(r3))==-3))
{
// cout << "(whatzhou(r1)" << whatzhou(r1) << endl;
// cout << "(whatzhou(r3)" << whatzhou(r3) << endl;
// cout << "A的横坐标: " << r1.x << endl;
// cout << "A的纵坐标: " << r1.y << endl;
// cout << "C的横坐标: " << r3.x <<endl;
// cout << "C的纵坐标: " << r3.y << endl;
cout << "LIFT" << endl;
}else{
cout << "RIGHT" << endl;
}
}else{
if((whatxian(r1) - whatxian(r3) == 1)||(whatxian(r1) - whatxian(r3) == -3 )){
cout << "LEFT" << endl;
}else{
cout << "RIGHT" << endl;
}
}
}else{
cout << "既不直走,又不拐90度的弯" << endl;
}
}
return 0;
}
//将p2移动到原点,p1,p3对应移动
void point::movetozero(point &r1,point &r3)
{
r1.x -= x;
r1.y -= y;
r3.x -= x;
r3.y -= y;
x = 0;
y = 0;
}
//判断一个点在第几象限,若是x轴正方向则为1.1,负方向1.2 y轴正方向2.1,负方向2.2
float point::whatxian(point p)
{
//原点
if(ifzuobiaozhou(p)){
cout << "改点在坐标轴上" << endl;
return (float)0.1;
}
if(p.x == 0 && p.y == 0){
return 0;
}else if(p.x > 0 && p.y >0){//第一象限
return 1;
}else if(p.x >0 && p.y < 0){//第四象限
return 4;
}else if(p.x <0 && p.y < 0){//第三象限
return 3;
}else if(p.x < 0 && p.y > 0){//第二象限
return 2;
}
return -1;
}
//判断是否有点在坐标轴上
bool point::ifzuobiaozhou(point p)
{
return (p.x == 0 || p.y == 0);
}
//判断点在那个坐标轴上
int point::whatzhou(point p)
{
if(p.y == 0){
if(p.x > 0){
return 1;
}else{
return 3;
}
}else if(p.x == 0){
if(p.y > 0){
return 2;
}else{
return 4;
}
}
else{
cout << "改点不在坐标轴上 " << endl;
return 0;
}
}
int main()
{
//赋值
point p[3];
for(int i = 0;i <= 2;i++){
int x,y;
cout << "第" << i+1 <<"个点的横坐标:" ;
cin >> x;
cout << "第" << i+1 <<"个点的纵坐标:" ;
cin >> y;
p[i].fuzhi(x,y);
}
p[1].guaixiang(p[0],p[2]);
return 0;
}
//写得粗略了些
//如有bug,告诉我,再改,,