判断两线段是否平行

//判断两条线段是否相交的测试代码 //对于垂直的线段暂时没有作考虑,如果需要后续加上判断即可,应该不难 //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轴的情况没有考虑到。

转载于:https://www.cnblogs.com/ileimingGG/archive/2010/07/08/7435820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值