判断两线段是否平行

本文提供了一种判断两线段是否相交的简单算法实现,并邀请读者审查代码逻辑及改进建议,特别指出未考虑垂直线段及斜率计算精度问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//判断两条线段是否相交的测试代码 //对于垂直的线段暂时没有作考虑,如果需要后续加上判断即可,应该不难 //author:challenKing //data:2010-07-06 class test { public static void main(String[] args) { boolean xiangjiao = xiangjiao(11,11,12,12,21,21,22,22); System.out.println(xiangjiao); } //分别给出两条线段的两个点,判断是否相交 static boolean xiangjiao(int x11, int y11, int x12, int y12, int x21, int y21, int x22, int y22) { float slope1=0,slope2=0,slope3=0,slope4=0; if((x12>x11&&y12>y11)||(x12<x11&&y12<y11)){ int y = Math.abs(y12-y11); int x = Math.abs(x12-x11); slope1 = (float)y/x; } if((x22>x21&&y22>y21)||(x22<x21&&y22<y21)){ int y = Math.abs(y22-y21); int x = Math.abs(x22-x21); slope2 = (float)y/x; } if((x11>x12&&y11<y12) || (x11<x12&y11>y12)){ int y = y12-y11; int x = x12-x11; slope3 = (float)y/x; } if((x21>x22&&y21<y22) || (x21<x22&y21>y22)){ int y = y22-y21; int x = x22-x21; slope4 = (float)y/x; } if(slope1!=0&&slope2!=0){ if(slope1 == slope2){ return true; } else return false; } else if(slope3!=0&&slope4!=0){ if(slope3 == slope4){ return true; } else return false; } else return false; } }

遇到了一个笔试的题目试着做一做,也不知道是否正确或者逻辑是否清晰,忘看了的大大们给出意见吧!谢谢!

我想还有一个问题就是求斜率的时候是用的除法,应该会存在精度的问题!还有就是当线段平行x轴或者y轴的情况没有考虑到。

抱歉,我可以回答你的问题。以下是 Line 类的实现代码: ```c++ #include <iostream> #include <cmath> using namespace std; class Line { private: double a, b, c; // ax + by + c = 0 public: Line(double aa = 0, double bb = 0, double cc = 0) { // initialize with coefficients a = aa, b = bb, c = cc; if (a == 0 && b == 0) b = 1; // if invalid, set b to be 1 } Line(double x1, double y1, double x2, double y2) { // initialize with two points if (x1 == x2) { a = 1, b = 0, c = -x1; // vertical line } else { a = (y2 - y1) / (x1 - x2); b = -1; c = y1 - a * x1; } } double slope() { // calculate slope if (b == 0) return INFINITY; else return -a / b; } double intercept() { // calculate intercept if (b == 0) return INFINITY; else return -c / b; } int getRelPosTo(Line l) { // calculate relative position to another line if (fabs(a * l.b - b * l.a) < 1e-9 && fabs(b * l.c - c * l.b) < 1e-9) return COINCIDENT; if (fabs(a * l.b - b * l.a) < 1e-9) return PARALLEL; else return INTERSECT; } const int COINCIDENT = 0; const int PARALLEL = 1; const int INTERSECT = 2; }; int main() { Line l1(1, 2, 3); // 1x + 2y + 3 = 0 cout << "Slope: " << l1.slope() << endl; // -0.5 cout << "Intercept: " << l1.intercept() << endl; // -1.5 Line l2(0, 1, 1); // y + 1 = 0, vertical line passing through (1, 0) cout << "Slope: " << l2.slope() << endl; // INF cout << "Intercept: " << l2.intercept() << endl; // -1 Line l3(2, 3, 4, 5); // y = x + 1 cout << "Slope: " << l3.slope() << endl; // 1 cout << "Intercept: " << l3.intercept() << endl; // 1 cout << "Relative position to l1: " << l2.getRelPosTo(l1) << endl; // PARALLEL cout << "Relative position to l3: " << l1.getRelPosTo(l3) << endl; // INTERSECT return 0; } ``` 注意到我们在函数中使用了 `1e-9` 的精度来比较两个实数是否相等,这是因为浮点数在内存中的存储方式所决定的,相等的两个数可能会略微不同。在实际使用中,我们也可以根据题目要求来调整精度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值