java计算两条线段交点
2、设y=ax+b,通过斜率可求出a的值,通过坐标可以求出b的值,这样两个线段所在直线的两个方程式就得到了;
3、通过两个方程式求出交点,再判断交点是不是落在两线段交点的区域中
代码:
public static Point getCrossPoint(Lseg lsegA,Lseg lsegB){ float x; float y; float x1=lsegA.getA().getX(); float y1=lsegA.getA().getY(); float x2=lsegA.getB().getX(); float y2=lsegA.getB().getY(); float x3=lsegB.getA().getX(); float y3=lsegB.getA().getY(); float x4=lsegB.getB().getX(); float y4=lsegB.getB().getY(); float k1=Float.MAX_VALUE; float k2=Float.MAX_VALUE; boolean flag1=false; boolean flag2=false; if((x1-x2)==0) flag1=true; if((x3-x4)==0) flag2=true; if(!flag1) k1=(y1-y2)/(x1-x2); if(!flag2) k2=(y3-y4)/(x3-x4); if(k1==k2) return null; if(flag1){ if(flag2) return null; x=x1; if(k2==0){ y=y3; }else{ y=k2*(x-x4)+y4; } }else if(flag2){ x=x3; if(k1==0){ y=y1; }else{ y=k1*(x-x2)+y2; } }else{ if(k1==0){ y=y1; x=(y-y4)/k2+x4; }else if(k2==0){ y=y3; x=(y-y2)/k1+x2; }else{ x=(k1*x2-k2*x4+y4-y2)/(k1-k2); y=k1*(x-x2)+y2; } } if(between(x1,x2,x)&&between(y1,y2,y)&&between(x3,x4,x)&&between(y3,y4,y)){ Point point=new Point(); point.setX(x); point.setY(y); if(point.equals(lsegA.getA())||point.equals(lsegA.getB())) return null; return point; }else{ return null; } } public static boolean between(float a,float b,float target){ if(target>=a-0.01&&target<=b+0.01||target<=a+0.01&&target>=b-0.01) return true; else return false; }}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。