判断两直线正交 平行问题

方法一: 用内积外积判断正交平行时,形参为线段

#include<iostream>
#include<cmath>
using namespace std;

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)//实数判断是否为0的方法

class Point//建立point类,里面包含很多point的运算符
{
	public:
		double x, y;
		
		Point(double x = 0, double y = 0): x(x), y(y) {}
		//定义point中的特定运算符
		Point operator + (Point p) { return Point(x + p.x, y + p.y); }
		Point operator - (Point p) { return Point(x - p.x, y - p.y); }
		Point operator * (double a) { return Point(a * x, a * y); }
		Point operator / (double a) { return Point(x / a, y / a); }

		double abs() { return sqrt(norm()); }//求每个点的模
		double norm() { return x * x + y * y; }//求出每个点的横坐标平方+纵坐标平方
		
		bool operator < (const Point &p) const {
			return x != p.x ? x < p.x : y < p.y;
		}

		bool operator == (const Point &p) const {
			return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
		}
};

typedef Point Vector;

struct Segment
{
	Point p1, p2;//一个线段包含两个已知点,一个点又对应一个坐标
};
double dot(Vector a, Vector b) 
{
	return a.x * b.x + a.y * b.y;
}
double cross(Vector a, Vector b) 
{
	return a.x*b.y - a.y*b.x;
}
//用内积判断正交
bool isOrthogonal(Segment s1, Segment s2)
{
	return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0);
}
//用外积判断平行
bool isParallel(Segment s1, Segment s2)
{
	return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0);
}

int main()
{
	int q;
	cin>>q;
	Segment s1,s2;
	while(q--)
	{
		cin>>s1.p1.x>>s1.p1.y>>s1.p2.x>>s1.p2.y>>s2.p1.x>>s2.p1.y>>s2.p2.x>>s2.p2.y;
		if(isOrthogonal(s1,s2) )
		{
			cout<<1<<endl;
		} 
		else if(isParallel(s1,s2))
		{
			cout<<2<<endl;
		} 
		else 
		{
			cout<<0<<endl;
		}
	}
}

方法二: 用内积外积判断正交平行时,形参为向量

#include<iostream>
#include<cmath>
using namespace std;

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

class Point {
	public:
		double x, y;
		
		Point(double x = 0, double y = 0): x(x), y(y) {}

		Point operator + (Point p) { return Point(x + p.x, y + p.y); }
		Point operator - (Point p) { return Point(x - p.x, y - p.y); }
		Point operator * (double a) { return Point(a * x, a * y); }
		Point operator / (double a) { return Point(x / a, y / a); }

		double abs() { return sqrt(norm()); }
		double norm() { return x * x + y * y; }
		
		bool operator < (const Point &p) const {
			return x != p.x ? x < p.x : y < p.y;
		}

		bool operator == (const Point &p) const {
			return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
		}
};

typedef Point Vector;

double dot(Vector a, Vector b) {
	return a.x * b.x + a.y * b.y;
}

double cross(Vector a, Vector b) {
	return a.x*b.y - a.y*b.x;
}
//内积判断正交
bool isOrthogonal(Vector a, Vector b){
	return equals(dot(a, b), 0.0);
}
//外积判断平行
bool isParallel(Vector a, Vector b)
{
	return equals(cross(a, b), 0.0);
}
int main()
{
	int q;
	cin>>q;
	Point p0, p1, p2, p3;
	while(q--)
	{
		cin>>p0.x>>p0.y>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y;
		if(isOrthogonal(p0-p1, p2-p3) )
		{
			cout<<1<<endl;
		} 
		else if(isParallel(p0-p1, p2-p3))
		{
			cout<<2<<endl;
		} 
		else 
		{
			cout<<0<<endl;
		}
	}
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

henulmh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值