import java.applet.Applet; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; public class Segment { Complex[] z=new Complex[2]; int[] INDEX=new int[3]; Color COLOR; public Segment() {} public Segment(Complex z1,Complex z2) { this.z[0]=z1; this.z[1]=z2; INDEX[0]=0; INDEX[1]=0; INDEX[2]=0; COLOR=Color.white; } public GeneralPath toGeneralPath() { GeneralPath gp=new GeneralPath(); gp.moveTo((float)(z[0].x),(float)(z[0].y)); gp.lineTo((float)(z[1].x),(float)(z[1].y)); return(gp); } public double length() { return(Complex.dist(z[0],z[1])); } public void print() { z[0].print(); z[1].print(); } public static Segment segMerge(Segment S1,Segment S2) { int k=Complex.zeroOriented(S1.z[0],S1.z[1],S2.z[0]); if(k==0) return(null); k=Complex.zeroOriented(S1.z[0],S1.z[1],S2.z[1]); if(k==0) return(null); Complex z2=Complex.minus(S1.z[1],S1.z[0]); Complex z3=Complex.minus(S2.z[0],S1.z[0]); Complex z4=Complex.minus(S2.z[1],S1.z[0]); Complex z1=new Complex(0,0); if(z2.norm()<.000001) return(null); z3=Complex.divide(z3,z2); z4=Complex.divide(z4,z2); z2=new Complex(1,0); double r1=z1.x; double r2=z2.x; double r3=z3.x; double r4=z4.x; if(Math.max(r1,r2)Math.max(r3,r4)) return(null); //segments disjoint double s1=Math.min(Math.min(Math.min(r1,r2),r3),r4); double s2=Math.max(Math.max(Math.max(r1,r2),r3),r4); Segment T=new Segment(); if(r1s2-.00000001) T.z[1]=S1.z[0]; if(r2>s2-.00000001) T.z[1]=S1.z[1]; if(r3>s2-.00000001) T.z[1]=S2.z[0]; if(r4>s2-.00000001) T.z[1]=S2.z[1]; return(T); } public Segment chopX(double X) { //both points to the left if((z[0].x>=X)&&(z[1].x>=X)) return(null); //both points to the right if((z[0].x<=-X)&&(z[1].x<=-X)) return(null); //both points inside if((Math.abs(z[0].x)=X)) { w[1]=new Complex(X,0); w[2]=new Complex(X,1); w[3]=V.findCross2(z[0],z[1],w[1],w[2]); return(new Segment(z[0],w[3])); } if((Math.abs(z[1].x)=X)) { w[1]=new Complex(X,0); w[2]=new Complex(X,1); w[3]=V.findCross2(z[0],z[1],w[1],w[2]); return(new Segment(z[1],w[3])); } if((Math.abs(z[0].x)=X)&&(z[1].x<=-X)) { w[1]=new Complex(X,0); w[2]=new Complex(X,1); w[3]=V.findCross2(z[0],z[1],w[1],w[2]); w[1]=new Complex(-X,0); w[2]=new Complex(-X,1); w[4]=V.findCross2(z[0],z[1],w[1],w[2]); return(new Segment(w[3],w[4])); } if((z[1].x>=X)&&(z[0].x<=-X)) { w[1]=new Complex(X,0); w[2]=new Complex(X,1); w[3]=V.findCross2(z[0],z[1],w[1],w[2]); w[1]=new Complex(-X,0); w[2]=new Complex(-X,1); w[4]=V.findCross2(z[0],z[1],w[1],w[2]); return(new Segment(w[3],w[4])); } return(null); } public Segment chopY(double Y) { if(this==null) return(null); Segment S=new Segment(new Complex(z[0].y,z[0].x),new Complex(z[1].y,z[1].x)); Segment T=S.chopX(Y); if(T==null) return(null); T=new Segment(new Complex(T.z[0].y,T.z[0].x),new Complex(T.z[1].y,T.z[1].x)); return(T); } public Segment chop(double X,double Y) { if(this==null) return(null); Segment S1=chopX(X); if(S1==null) return(null); Segment S2=S1.chopY(Y); return(S2); } }