Blog2:nchu-software-oop-2022-4+5+期中( 二 )


这题题目难度还好,主要根据前面三角形的类和方法,在其基础上添加修改 。
import java.util.Scanner;class Point{double x;double y;int flag=0;int exist=1;//1:存在 0:不存在 2:交点无数个public boolean pointchonghe(Point A,Point B)//判断两点重合{if(A.x==B.x&&A.y==B.y)return true;elsereturn false;}public boolean Pointscoincide(Point A,Point B,Point C,Point D)//判断四边形四点重合{if(A.x==B.x&&A.y==B.y||A.x==C.x&&A.y==C.y||A.x==D.x&&A.y==D.y||B.x==C.x&&B.y==C.y||B.x==D.x&&B.y==D.y||C.x==D.x&&C.y==D.y)return true;elsereturn false;}}class Line{Point p1=new Point();Point p2=new Point();double a;double b;double c;double D;int flag;//交点是否在线段上public void L(Line l)//一般式系数{l.a=l.p2.y-l.p1.y;l.b=l.p1.x-l.p2.x;l.c=l.p2.x*l.p1.y-l.p1.x*l.p2.y;}public void D(Point p1,Point p2,Line lp1p2)//两点间距离{lp1p2.D=Math.sqrt(Math.pow(lp1p2.p1.x-lp1p2.p2.x,2)+Math.pow(lp1p2.p1.y-lp1p2.p2.y,2));lp1p2.D=Math.round(lp1p2.D*1000000000)/(double)1000000000;}public void distanse(Line l)//求线段距离D{l.D=Math.sqrt(Math.pow(l.p1.x-l.p2.x,2)+Math.pow(l.p1.y-l.p2.y,2));l.D=Math.round(l.D*1000000000)/(double)1000000000;}public boolean zhixian(Point X,Line l)//点是否在直线上{L(l);if(l.a*X.x+l.b*X.y+l.c==0)return true;elsereturn false;}public boolean linechonghe(Line l1,Line l2)//判断是否两线重合{L(l1);L(l2);if(l1.b==0&&l2.b==0){if(l1.c/l1.a==l2.c/l2.a)return true;elsereturn false;}else if(l1.a==0&&l2.a==0){if(l1.c/l1.b==l2.c/l2.b)return true;elsereturn false;}else{if(l1.c/l1.a==l2.c/l2.a&&l1.c/l1.b==l2.c/l2.b)return true;elsereturn false;}}public boolean pingxing(Line l1,Line l2)//判断是否平行(有无交点){L(l1);L(l2);if(l1.b==0&&l2.b==0)return true;else if(l1.b!=0&&l2.b!=0&&(l1.a/l1.b==l2.a/l2.b))return true;elsereturn false;}public boolean Quadrilateral(Line lAB,Point C,Point D)//判断是否四边形{L(lAB);if((!zhixian(C,lAB))&&(!zhixian(D,lAB))&&((lAB.a*C.x+lAB.b*C.y+lAB.c<0)&&(lAB.a*D.x+lAB.b*D.y+lAB.c<0)||(lAB.a*C.x+lAB.b*C.y+lAB.c>0)&&(lAB.a*D.x+lAB.b*D.y+lAB.c>0)))return true;elsereturn false;}public boolean Quadrilateral1(Point A,Point B,Point C,Point D,Quadrilateral Q)//凹{if((!zhixian(C,Q.l1))&&(!zhixian(D,Q.l1))&&(!zhixian(D,Q.l6))&&(!zhixian(D,Q.l2))){Point O=new Point();Line lAO=new Line();lAO.p1=A;lAO.p2=O;Line lBO=new Line();lBO.p1=B;lBO.p2=O;Line lCO=new Line();lCO.p1=C;lCO.p2=O;Line lDO=new Line();lDO.p1=D;lDO.p2=O;Triangle t=new Triangle();t.jiaodian(Q.l5,Q.l6,O);//AC和BD交点if(t.pointwhere(O,A,C,lAO,lCO,Q.l6)&&!t.pointwhere(O,B,D,lBO,lDO,Q.l5))//交点在线段AC和不在BD上{Q.l5.flag=0;Q.l6.flag=1;return true;}else if(!t.pointwhere(O,A,C,lAO,lCO,Q.l6)&&t.pointwhere(O,B,D,lBO,lDO,Q.l5))//交点不在线段AC和在BD上{Q.l5.flag=1;Q.l6.flag=0;return true;}elsereturn false;}elsereturn false;}public boolean Quadrilateral2(Point A,Point B,Point C,Point D,Quadrilateral Q)//凸{if((!zhixian(C,Q.l1))&&(!zhixian(D,Q.l1))&&(!zhixian(A,Q.l3))&&(!zhixian(D,Q.l2))&&(!A.Pointscoincide(A,B,C,D))){Point O=new Point();Line lAO=new Line();lAO.p1=A;lAO.p2=O;Line lBO=new Line();lBO.p1=B;lBO.p2=O;Line lCO=new Line();lCO.p1=C;lCO.p2=O;Line lDO=new Line();lDO.p1=D;lDO.p2=O;Triangle t=new Triangle();t.jiaodian(Q.l5,Q.l6,O);//AC和BD交点if(O.exist==1&&t.pointwhere(O,A,C,lAO,lCO,Q.l6)&&t.pointwhere(O,B,D,lBO,lDO,Q.l5))//交点在线段AC和BD上return true;elsereturn false;}elsereturn false;}public boolean pingxingQuadrilateral(Line l1,Line l2)//判断是否平行四边形{distanse(l1);distanse(l2);if(pingxing(l1,l2)&&l1.D==l2.D)return true;elsereturn false;}}class Triangle{Line l1=new Line();Line l2=new Line();Line l3=new Line();double S;int num;int flag=0;//1:存在,0:不存在public boolean sanjiaox(Line l1,Line l2,Line l3)//判断是否为三角形{l1.distanse(l1);l2.distanse(l2);l3.distanse(l3);if((l1.D+l2.D>l3.D&&l1.D<=l3.D&&l2.D<=l3.D)||(l1.D+l3.D>l2.D&&l1.D<=l2.D&&l3.D<=l2.D)||(l2.D+l3.D>l1.D&&l2.D<=l1.D&&l2.D<=l1.D))return true;elsereturn false;}public void whichsanjiaox(Point C,Point D,Point E,Point F,Line lAB,Triangle tDEF,Triangle tCEF,Triangle tCDF,Triangle tCDE,Quadrilateral Q)//判断四个点构成哪种三角形{if(sanjiaox(tDEF.l1,tDEF.l2,tDEF.l3)&&pointwhere(C,D,F,Q.l1,Q.l4,Q.l5)){if(lAB.linechonghe(lAB,Q.l1)||lAB.linechonghe(lAB,Q.l2)||lAB.linechonghe(lAB,Q.l3)||lAB.linechonghe(lAB,Q.l4)){System.out.print("The line is coincide with one of the lines");return;}elsetDEF.flag=1;}else if(sanjiaox(tCEF.l1,tCEF.l2,tCEF.l3)&&pointwhere(D,C,E,Q.l1,Q.l2,Q.l6)){if(lAB.linechonghe(lAB,Q.l1)||lAB.linechonghe(lAB,Q.l2)||lAB.linechonghe(lAB,Q.l3)||lAB.linechonghe(lAB,Q.l4)){System.out.print("The line is coincide with one of the lines");return;}elsetCEF.flag=1;}else if(sanjiaox(tCDF.l1,tCDF.l2,tCDF.l3)&&pointwhere(E,D,F,Q.l2,Q.l3,Q.l5)){if(lAB.linechonghe(lAB,Q.l1)||lAB.linechonghe(lAB,Q.l2)||lAB.linechonghe(lAB,Q.l3)||lAB.linechonghe(lAB,Q.l4)){System.out.print("The line is coincide with one of the lines");return;}elsetCDF.flag=1;}else if(sanjiaox(tCDE.l1,tCDE.l2,tCDE.l3)&&pointwhere(F,C,E,Q.l4,Q.l3,Q.l6)){if(lAB.linechonghe(lAB,Q.l1)||lAB.linechonghe(lAB,Q.l2)||lAB.linechonghe(lAB,Q.l3)||lAB.linechonghe(lAB,Q.l4)){System.out.print("The line is coincide with one of the lines");return;}elsetCDE.flag=1;}}public void whichsanjiaox1(Point C,Point D,Point E,Point F,Triangle tDEF,Triangle tCEF,Triangle tCDF,Triangle tCDE,Quadrilateral Q)//判断哪种三角形{if(sanjiaox(tDEF.l1,tDEF.l2,tDEF.l3)&&pointwhere(C,D,F,Q.l1,Q.l4,Q.l5))tDEF.flag=1;else if(sanjiaox(tCEF.l1,tCEF.l2,tCEF.l3)&&pointwhere(D,C,E,Q.l1,Q.l2,Q.l6))tCEF.flag=1;else if(sanjiaox(tCDF.l1,tCDF.l2,tCDF.l3)&&pointwhere(E,D,F,Q.l2,Q.l3,Q.l5))tCDF.flag=1;else if(sanjiaox(tCDE.l1,tCDE.l2,tCDE.l3)&&pointwhere(F,C,E,Q.l4,Q.l3,Q.l6))tCDE.flag=1;}public void jiaodian(Line l1,Line l2,Point O)//两条直线交点{if(l1.pingxing(l1,l2)){O.exist=0;}else{l1.L(l1);l2.L(l2);O.x=(l1.b*l2.c-l2.b*l1.c)/(l1.a*l2.b-l2.a*l1.b);O.y=(l2.a*l1.c-l1.a*l2.c)/(l1.a*l2.b-l2.a*l1.b);O.x=Math.round(O.x*1000000000)/(double)1000000000;O.y=Math.round(O.y*1000000000)/(double)1000000000;}}public boolean pointwhere(Point A,Point B,Point C,Line lAB,Line lAC,Line lBC)//点A是否在线段BC上{if(A.pointchonghe(A,B)||A.pointchonghe(A,C))return true;else{lAB.D(A,B,lAB);lAC.D(A,C,lAC);lBC.D(B,C,lBC);if(lAB.D+lAC.D-lBC.D<0.000001)return true;elsereturn false;}}public void jiaodiannum(Point P1,Point P2,Point P3,Line lAB,Triangle t)//交点数(线和三角形){t.num=0;P1.flag=0;P2.flag=0;P3.flag=0;t.jiaodian(lAB,t.l1,P1);Line lp11=new Line();lp11.p1=P1;lp11.p2=t.l1.p1;Line lp12=new Line();lp12.p1=P1;lp12.p2=t.l1.p2;if(P1.exist==1&&t.pointwhere(P1, t.l1.p1, t.l1.p2,lp11,lp12,t.l1)){t.num++;P1.flag=1;//交点在线段上}t.jiaodian(lAB,t.l2,P2);Line lp21=new Line();lp21.p1=P2;lp21.p2=t.l2.p1;Line lp22=new Line();lp22.p1=P2;lp22.p2=t.l2.p2;if(P2.exist==1&&t.pointwhere(P2,t.l2.p1,t.l2.p2,lp21,lp22,t.l2)){if(P1.flag==0||(!P1.pointchonghe(P1,P2)&&P1.flag==1)){t.num++;P2.flag=1;}}t.jiaodian(lAB,t.l3,P3);Line lp31=new Line();lp31.p1=P3;lp31.p2=t.l3.p1;Line lp32=new Line();lp32.p1=P3;lp32.p2=t.l3.p2;if(P3.exist==1&&t.pointwhere(P3,t.l3.p1,t.l3.p2,lp31,lp32,t.l3)){if(P1.flag==0&&P2.flag==0){t.num++;P3.flag=1;}else if(P1.flag==1&&P2.flag==0&&!P1.pointchonghe(P1,P3)){t.num++;P3.flag=1;}else if(P1.flag==0&&P2.flag==1&&!P2.pointchonghe(P2,P3)){t.num++;P3.flag=1;}}}public void sanjiaoxmianji(Triangle t)//三角形面积{t.l1.distanse(t.l1);t.l2.distanse(t.l2);t.l3.distanse(t.l3);double x=(t.l1.D+t.l2.D+t.l3.D)/2;t.S=Math.sqrt(x*(x-t.l1.D)*(x-t.l2.D)*(x-t.l3.D));}public void xiaomianji(Point P1,Point P2,Point P3,Triangle t,Triangle T)//切割小三角形面积{if(P1.flag==1&&P2.flag==1)//三角形CP1P2{t.l1.p1=T.l1.p2;t.l1.p2=P1;t.l2.p1=T.l1.p2;t.l2.p2=P2;t.l3.p1=P1;t.l3.p2=P2;t.sanjiaoxmianji(t);}else if(P1.flag==1&&P3.flag==1){t.l1.p1=T.l1.p1;t.l1.p2=P1;t.l2.p1=T.l1.p1;t.l2.p2=P3;t.l3.p1=P1;t.l3.p2=P3;t.sanjiaoxmianji(t);}else if(P2.flag==1&&P3.flag==1){t.l1.p1=T.l2.p2;t.l1.p2=P2;t.l2.p1=T.l2.p2;t.l2.p2=P3;t.l3.p1=P2;t.l3.p2=P3;t.sanjiaoxmianji(t);}}public void xiaomianji1(Point P1,Point P2,Point P3,Point P4,Triangle t,Quadrilateral Q)//四边形切割小三角形面积{if(P1.flag==1&&P2.flag==1)//三角形DP1P2{t.l1.p1=Q.l1.p2;t.l1.p2=P1;t.l2.p1=Q.l1.p2;t.l2.p2=P2;t.l3.p1=P1;t.l3.p2=P2;t.sanjiaoxmianji(t);}else if(P1.flag==1&&P4.flag==1)//CP1P4{t.l1.p1=Q.l1.p1;t.l1.p2=P1;t.l2.p1=Q.l1.p1;t.l2.p2=P4;t.l3.p1=P1;t.l3.p2=P4;t.sanjiaoxmianji(t);}else if(P2.flag==1&&P3.flag==1)//EP2P3{t.l1.p1=Q.l2.p2;t.l1.p2=P2;t.l2.p1=Q.l2.p2;t.l2.p2=P3;t.l3.p1=P2;t.l3.p2=P3;t.sanjiaoxmianji(t);}else if(P3.flag==1&&P4.flag==1)//FP3P4{t.l1.p1=Q.l3.p2;t.l1.p2=P3;t.l2.p1=Q.l3.p2;t.l2.p2=P4;t.l3.p1=P3;t.l3.p2=P4;t.sanjiaoxmianji(t);}}}class Quadrilateral{Line l1=new Line();Line l2=new Line();Line l3=new Line();Line l4=new Line();Line l5=new Line();//BDLine l6=new Line();//ACdouble C;double S;int num;int flag;//1:存在 0:不存在int aotu;//1:凸 0:凹public boolean lingxing(Quadrilateral Q)//菱形{if(!Q.l1.pingxingQuadrilateral(Q.l1,Q.l3))return false;else{Q.l1.distanse(Q.l1);Q.l2.distanse(Q.l2);if(Q.l1.D==Q.l2.D)return true;elsereturn false;}}public boolean zhijiao(Quadrilateral Q)//判断直角{Q.l1.distanse(Q.l1);Q.l4.distanse(Q.l4);Q.l5.distanse(Q.l5);if(Q.l1.D*Q.l1.D+Q.l4.D*Q.l4.D-Q.l5.D*Q.l5.D<0.000000000001)return true;elsereturn false;}public boolean juxing(Quadrilateral Q)//矩形{if(!Q.l1.pingxingQuadrilateral(Q.l1,Q.l3))return false;else{if(zhijiao(Q))return true;elsereturn false;}}public boolean zhengfangxing(Quadrilateral Q)//正方形{if(!Q.juxing(Q))return false;else{Q.l1.distanse(Q.l1);Q.l2.distanse(Q.l2);if(Q.l1.D==Q.l2.D)return true;elsereturn false;}}public void Quadrilateralzhouchang(Quadrilateral Q)//四边形周长{Q.l1.distanse(Q.l1);Q.l2.distanse(Q.l2);Q.l3.distanse(Q.l3);Q.l4.distanse(Q.l4);Q.C=Q.l1.D+Q.l2.D+Q.l3.D+Q.l4.D;}public void Quadrilateralmianji(Quadrilateral Q)//凸四边形面积{Q.l1.distanse(Q.l1);Q.l2.distanse(Q.l2);Q.l3.distanse(Q.l3);Q.l4.distanse(Q.l4);Q.l5.distanse(Q.l5);double x1=(l1.D+l4.D+l5.D)/2;double s1=Math.sqrt(x1*(x1-l1.D)*(x1-l4.D)*(x1-l5.D));double x2=(l2.D+l3.D+l5.D)/2;double s2=Math.sqrt(x2*(x2-l2.D)*(x2-l3.D)*(x2-l5.D));Q.S=s1+s2;}public void Quadrilateralmianji1(Quadrilateral Q)//凹四边形面积{if(Q.l5.flag==0&&Q.l6.flag==1){Quadrilateralmianji(Q);}else if(Q.l5.flag==1&&Q.l6.flag==0){Q.l1.distanse(Q.l1);Q.l2.distanse(Q.l2);Q.l3.distanse(Q.l3);Q.l4.distanse(Q.l4);Q.l6.distanse(Q.l6);double x1=(l1.D+l2.D+l6.D)/2;double s1=Math.sqrt(x1*(x1-l1.D)*(x1-l2.D)*(x1-l6.D));double x2=(l3.D+l4.D+l6.D)/2;double s2=Math.sqrt(x2*(x2-l3.D)*(x2-l4.D)*(x2-l6.D));Q.S=s1+s2;}}public void Quadrilateralnum(Point P1,Point P2,Point P3,Point P4,Line lAB,Triangle T,Quadrilateral Q)//交点数(线和四边形){Q.num=0;P1.flag=0;P2.flag=0;P3.flag=0;P4.flag=0;T.jiaodian(lAB,Q.l1,P1);Line lp11=new Line();lp11.p1=P1;lp11.p2=Q.l1.p1;Line lp12=new Line();lp12.p1=P1;lp12.p2=Q.l1.p2;if(P1.exist==1&&T.pointwhere(P1,Q.l1.p1,Q.l1.p2,lp11,lp12,Q.l1)){Q.num++;P1.flag=1;//交点在线段上}T.jiaodian(lAB,Q.l2,P2);Line lp21=new Line();lp21.p1=P2;lp21.p2=Q.l2.p1;Line lp22=new Line();lp22.p1=P2;lp22.p2=Q.l2.p2;if(P2.exist==1&&T.pointwhere(P2,Q.l2.p1,Q.l2.p2,lp21,lp22,Q.l2)){if(P1.flag==0||(!P1.pointchonghe(P1,P2)&&P1.flag==1)){Q.num++;P2.flag=1;}}T.jiaodian(lAB,Q.l3,P3);Line lp31=new Line();lp31.p1=P3;lp31.p2=Q.l3.p1;Line lp32=new Line();lp32.p1=P3;lp32.p2=Q.l3.p2;if(P3.exist==1&&T.pointwhere(P3,Q.l3.p1,Q.l3.p2,lp31,lp32,Q.l3)){if(P1.flag==0&&P2.flag==0){Q.num++;P3.flag=1;}else if(P1.flag==1&&P2.flag==0&&!P1.pointchonghe(P1,P3)){Q.num++;P3.flag=1;}else if(P1.flag==0&&P2.flag==1&&!P2.pointchonghe(P2,P3)){Q.num++;P3.flag=1;}}T.jiaodian(lAB,Q.l4,P4);Line lp41=new Line();lp41.p1=P4;lp41.p2=Q.l4.p1;Line lp42=new Line();lp42.p1=P4;lp42.p2=Q.l4.p2;if(P4.exist==1&&T.pointwhere(P4,Q.l4.p1,Q.l4.p2,lp41,lp42,Q.l4)){if(P1.flag==0&&P2.flag==0&&P3.flag==0){Q.num++;P4.flag=1;}else if(P1.flag==1&&P2.flag==0&&P3.flag==0&&!P1.pointchonghe(P1,P4)){Q.num++;P4.flag=1;}else if(P1.flag==0&&P2.flag==1&&P3.flag==0&&!P2.pointchonghe(P2,P4)){Q.num++;P4.flag=1;}else if(P1.flag==0&&P2.flag==0&&P3.flag==1&&!P3.pointchonghe(P3,P4)){Q.num++;P4.flag=1;}}}}public class Main{public static void main(String[] args){Scanner in=new Scanner(System.in);String a=in.nextLine();int num=(int)a.charAt(0)-(int)'0';String[] tokens=a.split(" ");int length=tokens.length;for(int i=0;i<length;i++){if(i==0){if(!tokens[i].matches("^\\d:[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?),[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")){System.out.print("Wrong Format");return;}}else if(i!=0){if(!tokens[i].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?),[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")){System.out.print("Wrong Format");return;}}}if(((num==1||num==2||num==3)&&length!=4)||(num==4&&length!=6)||(num==5&&length!=5)){System.out.print("wrong number of points");return;}String[] arr=a.split(" |:|,");switch(num){case 1:{Line lAB=new Line();Point C=new Point();Point D=new Point();lAB.p1.x=Double.valueOf(arr[1]);lAB.p1.y=Double.valueOf(arr[2]);lAB.p2.x=Double.valueOf(arr[3]);lAB.p2.y=Double.valueOf(arr[4]);C.x=Double.valueOf(arr[5]);C.y=Double.valueOf(arr[6]);D.x=Double.valueOf(arr[7]);D.y=Double.valueOf(arr[8]);Line lCD=new Line();lCD.p1=C;lCD.p2=D;Line lBC=new Line();lBC.p1=lAB.p2;lBC.p2=C;if(lAB.p1.Pointscoincide(lAB.p1,lAB.p2,C,D)){System.out.print("points coincide");return;}if(lAB.Quadrilateral(lAB,C,D)&&lBC.Quadrilateral(lBC,lAB.p1,D)){System.out.print("true");if(lAB.pingxingQuadrilateral(lAB,lCD))System.out.print(" true");elseSystem.out.print(" false");}elseSystem.out.print("false false");break;}case 2:{Quadrilateral Q=new Quadrilateral();Q.l1.p1.x=Double.valueOf(arr[1]);Q.l1.p1.y=Double.valueOf(arr[2]);Q.l1.p2.x=Double.valueOf(arr[3]);Q.l1.p2.y=Double.valueOf(arr[4]);Q.l2.p1=Q.l1.p2;Q.l2.p2.x=Double.valueOf(arr[5]);Q.l2.p2.y=Double.valueOf(arr[6]);Q.l3.p1=Q.l2.p2;Q.l3.p2.x=Double.valueOf(arr[7]);Q.l3.p2.y=Double.valueOf(arr[8]);Q.l4.p1=Q.l1.p1;Q.l4.p2=Q.l3.p2;Q.l5.p1=Q.l1.p2;Q.l5.p2=Q.l4.p2;if(Q.l1.p1.Pointscoincide(Q.l1.p1,Q.l1.p2,Q.l3.p1,Q.l3.p2)){System.out.print("not a quadrilateral");return;}if(!Q.l1.Quadrilateral(Q.l1,Q.l3.p1,Q.l3.p2)||!Q.l2.Quadrilateral(Q.l2,Q.l4.p1,Q.l4.p2)){System.out.print("not a quadrilateral");return;}if(Q.lingxing(Q))System.out.print("true");elseSystem.out.print("false");if(Q.juxing(Q))System.out.print(" true");elseSystem.out.print(" false");if(Q.zhengfangxing(Q))System.out.print(" true");elseSystem.out.print(" false");break;}case 3:{Quadrilateral Q=new Quadrilateral();Q.l1.p1.x=Double.valueOf(arr[1]);Q.l1.p1.y=Double.valueOf(arr[2]);Q.l1.p2.x=Double.valueOf(arr[3]);Q.l1.p2.y=Double.valueOf(arr[4]);Q.l2.p1=Q.l1.p2;Q.l2.p2.x=Double.valueOf(arr[5]);Q.l2.p2.y=Double.valueOf(arr[6]);Q.l3.p1=Q.l2.p2;Q.l3.p2.x=Double.valueOf(arr[7]);Q.l3.p2.y=Double.valueOf(arr[8]);Q.l4.p1=Q.l1.p1;Q.l4.p2=Q.l3.p2;Q.l5.p1=Q.l1.p2;Q.l5.p2=Q.l4.p2;Q.l6.p1=Q.l1.p1;Q.l6.p2=Q.l2.p2;Point A=new Point();Point B=new Point();Point C=new Point();Point D=new Point();A.x=Double.valueOf(arr[1]);A.y=Double.valueOf(arr[2]);B.x=Double.valueOf(arr[3]);B.y=Double.valueOf(arr[4]);C.x=Double.valueOf(arr[5]);C.y=Double.valueOf(arr[6]);D.x=Double.valueOf(arr[7]);D.y=Double.valueOf(arr[8]);if(Q.l1.p1.Pointscoincide(Q.l1.p1,Q.l1.p2,Q.l3.p1,Q.l3.p2)){System.out.print("not a quadrilateral");return;}int X;if(Q.l1.Quadrilateral(Q.l1,Q.l3.p1,Q.l3.p2)&&Q.l2.Quadrilateral(Q.l2,Q.l4.p1,Q.l4.p2)&&!Q.l6.zhixian(D,Q.l6)&&!Q.l6.zhixian(B,Q.l6))//涵盖凸四边形和D点凹进去的四边形{Point o=new Point();Line lBo=new Line();lBo.p1=B;lBo.p2=o;Line lDo=new Line();lDo.p1=D;lDo.p2=o;Line lAo=new Line();lAo.p1=A;lAo.p2=o;Line lCo=new Line();lCo.p1=C;lCo.p2=o;Triangle t=new Triangle();t.jiaodian(Q.l5,Q.l6,o);if(!t.pointwhere(o,Q.l5.p1,Q.l5.p2,lBo,lDo,Q.l5)&&t.pointwhere(o,Q.l6.p1,Q.l6.p2,lAo,lCo,Q.l6)){X=0;Q.l5.flag=0;Q.l6.flag=1;System.out.print("false ");}else{X=1;System.out.print("true ");}}else{if(Q.l5.Quadrilateral1(A,B,C,D,Q))//凹四边形{X=0;System.out.print("false ");}else{System.out.print("not a quadrilateral");return;}}Q.Quadrilateralzhouchang(Q);//周长面积System.out.print(Math.round(Q.C*1000)/(double)1000);if(X==1){System.out.print(" ");Q.Quadrilateralmianji(Q);System.out.print(Math.round(Q.S*1000)/(double)1000);}else if(X==0){System.out.print(" ");Q.Quadrilateralmianji1(Q);System.out.print(Math.round(Q.S*1000)/(double)1000);}break;}case 4:{Point A=new Point();Point B=new Point();A.x=Double.valueOf(arr[1]);A.y=Double.valueOf(arr[2]);B.x=Double.valueOf(arr[3]);B.y=Double.valueOf(arr[4]);Line lAB=new Line();lAB.p1=A;lAB.p2=B;if(A.pointchonghe(A,B)){System.out.print("points coincide");return;}Point C=new Point();Point D=new Point();Point E=new Point();Point F=new Point();C.x=Double.valueOf(arr[5]);C.y=Double.valueOf(arr[6]);D.x=Double.valueOf(arr[7]);D.y=Double.valueOf(arr[8]);E.x=Double.valueOf(arr[9]);E.y=Double.valueOf(arr[10]);F.x=Double.valueOf(arr[11]);F.y=Double.valueOf(arr[12]);Quadrilateral Q=new Quadrilateral();Q.l1.p1=C;Q.l1.p2=D;Q.l2.p1=D;Q.l2.p2=E;Q.l3.p1=E;Q.l3.p2=F;Q.l4.p1=C;Q.l4.p2=F;Q.l5.p1=D;Q.l5.p2=F;Q.l6.p1=C;Q.l6.p2=E;Triangle tDEF=new Triangle();tDEF.l1.p1=D;tDEF.l1.p2=E;tDEF.l2.p1=E;tDEF.l2.p2=F;tDEF.l3.p1=D;tDEF.l3.p2=F;Triangle tCEF=new Triangle();tCEF.l1.p1=C;tCEF.l1.p2=E;tCEF.l2.p1=E;tCEF.l2.p2=F;tCEF.l3.p1=C;tCEF.l3.p2=F;Triangle tCDF=new Triangle();tCDF.l1.p1=C;tCDF.l1.p2=D;tCDF.l2.p1=D;tCDF.l2.p2=F;tCDF.l3.p1=C;tCDF.l3.p2=F;Triangle tCDE=new Triangle();tCDE.l1.p1=C;tCDE.l1.p2=D;tCDE.l2.p1=D;tCDE.l2.p2=E;tCDE.l3.p1=C;tCDE.l3.p2=E;Point P1=new Point();Point P2=new Point();Point P3=new Point();Point P4=new Point();tDEF.whichsanjiaox(C,D,E,F,lAB,tDEF,tCEF,tCDF,tCDE,Q);//判断是哪三个点构成三角形if(tDEF.flag==1||C.pointchonghe(C,E))//求直线AB与三角形交点数{if(C.pointchonghe(C,E)&&lAB.linechonghe(lAB,Q.l5)){System.out.print("The line is coincide with one of the lines");return;}tDEF.jiaodiannum(P1,P2,P3,lAB,tDEF);System.out.print(tDEF.num);if(tDEF.num==2){System.out.print(" ");tDEF.sanjiaoxmianji(tDEF);Triangle t=new Triangle();tDEF.xiaomianji(P1,P2,P3,t,tDEF);//切割小三角形面积double s=tDEF.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}}else if(tCEF.flag==1||D.pointchonghe(D,F)){if(D.pointchonghe(D,F)&&lAB.linechonghe(lAB,Q.l6)){System.out.print("The line is coincide with one of the lines");return;}tCEF.jiaodiannum(P1,P2,P3,lAB,tCEF);System.out.print(tCEF.num);if(tCEF.num==2){System.out.print(" ");tCEF.sanjiaoxmianji(tCEF);Triangle t=new Triangle();tCEF.xiaomianji(P1,P2,P3,t,tCEF);//切割小三角形面积double s=tCEF.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}}else if(tCDF.flag==1||E.pointchonghe(E,C)){if(C.pointchonghe(C,E)&&lAB.linechonghe(lAB,Q.l5)){System.out.print("The line is coincide with one of the lines");return;}tCDF.jiaodiannum(P1,P2,P3,lAB,tCDF);System.out.print(tCDF.num);if(tCDF.num==2){System.out.print(" ");tCDF.sanjiaoxmianji(tCDF);Triangle t=new Triangle();tCDF.xiaomianji(P1,P2,P3,t,tCDF);//切割小三角形面积double s=tCDF.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}}else if(tCDE.flag==1||F.pointchonghe(F,D)){if(D.pointchonghe(D,F)&&lAB.linechonghe(lAB,Q.l6)){System.out.print("The line is coincide with one of the lines");return;}tCDE.jiaodiannum(P1,P2,P3,lAB,tCDE);System.out.print(tCDE.num);if(tCDE.num==2){System.out.print(" ");tCDE.sanjiaoxmianji(tCDE);Triangle t=new Triangle();tCDE.xiaomianji(P1,P2,P3,t,tCDE);//切割小三角形面积double s=tCDE.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}}else if(Q.l1.Quadrilateral2(C,D,E,F,Q)){if(lAB.linechonghe(lAB,Q.l1)||lAB.linechonghe(lAB,Q.l2)||lAB.linechonghe(lAB,Q.l3)||lAB.linechonghe(lAB,Q.l4)){System.out.print("The line is coincide with one of the lines");return;}Triangle t=new Triangle();Q.Quadrilateralnum(P1,P2,P3,P4,lAB,t,Q);System.out.print(Q.num);if(Q.num==2){System.out.print(" ");Q.Quadrilateralmianji(Q);//大四边形面积if(P1.flag==1&&P2.flag==1){t.xiaomianji1(P1,P2,P3,P4,t,Q);double s=Q.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}else if(P1.flag==1&&P3.flag==1){Triangle tCFP3=new Triangle();tCFP3.l1.p1=C;tCFP3.l1.p2=F;tCFP3.l2.p1=C;tCFP3.l2.p2=P3;tCFP3.l3.p1=F;tCFP3.l3.p2=P3;tCFP3.sanjiaoxmianji(tCFP3);Triangle tCP1P3=new Triangle();tCP1P3.l1.p1=C;tCP1P3.l1.p2=P1;tCP1P3.l2.p1=C;tCP1P3.l2.p2=P3;tCP1P3.l3.p1=P1;tCP1P3.l3.p2=P3;tCP1P3.sanjiaoxmianji(tCP1P3);double s1=tCFP3.S+tCP1P3.S;Triangle tDP1P3=new Triangle();tDP1P3.l1.p1=D;tDP1P3.l1.p2=P1;tDP1P3.l2.p1=D;tDP1P3.l2.p2=P3;tDP1P3.l3.p1=P1;tDP1P3.l3.p2=P3;tDP1P3.sanjiaoxmianji(tDP1P3);Triangle tDEP3=new Triangle();tDEP3.l1.p1=D;tDEP3.l1.p2=E;tDEP3.l2.p1=D;tDEP3.l2.p2=P3;tDEP3.l3.p1=E;tDEP3.l3.p2=P3;tDEP3.sanjiaoxmianji(tDEP3);double s2=tDEP3.S+tDP1P3.S;if(s1<s2)System.out.print(Math.round(s1*1000)/(double)1000+" "+Math.round(s2*1000)/(double)1000);elseSystem.out.print(Math.round(s2*1000)/(double)1000+" "+Math.round(s1*1000)/(double)1000);}else if(P1.flag==1&&P4.flag==1){t.xiaomianji1(P1,P2,P3,P4,t,Q);double s=Q.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}else if(P2.flag==1&&P3.flag==1){t.xiaomianji1(P1,P2,P3,P4,t,Q);double s=Q.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}else if(P2.flag==1&&P4.flag==1){Triangle tCDP2=new Triangle();tCDP2.l1.p1=C;tCDP2.l1.p2=D;tCDP2.l2.p1=C;tCDP2.l2.p2=P2;tCDP2.l3.p1=D;tCDP2.l3.p2=P2;tCDP2.sanjiaoxmianji(tCDP2);Triangle tCP2P4=new Triangle();tCP2P4.l1.p1=C;tCP2P4.l1.p2=P2;tCP2P4.l2.p1=C;tCP2P4.l2.p2=P4;tCP2P4.l3.p1=P2;tCP2P4.l3.p2=P4;tCP2P4.sanjiaoxmianji(tCP2P4);double s1=tCDP2.S+tCP2P4.S;Triangle tFP2P4=new Triangle();tFP2P4.l1.p1=F;tFP2P4.l1.p2=P2;tFP2P4.l2.p1=F;tFP2P4.l2.p2=P4;tFP2P4.l3.p1=P2;tFP2P4.l3.p2=P4;tFP2P4.sanjiaoxmianji(tFP2P4);Triangle tEFP2=new Triangle();tEFP2.l1.p1=E;tEFP2.l1.p2=F;tEFP2.l2.p1=E;tEFP2.l2.p2=P2;tEFP2.l3.p1=F;tEFP2.l3.p2=P2;tEFP2.sanjiaoxmianji(tEFP2);double s2=tFP2P4.S+tEFP2.S;if(s1<s2)System.out.print(Math.round(s1*1000)/(double)1000+" "+Math.round(s2*1000)/(double)1000);elseSystem.out.print(Math.round(s2*1000)/(double)1000+" "+Math.round(s1*1000)/(double)1000);}else if(P3.flag==1&&P4.flag==1){t.xiaomianji1(P1,P2,P3,P4,t,Q);double s=Q.S-t.S;if(t.S<s)System.out.print(Math.round(t.S*1000)/(double)1000+" "+Math.round(s*1000)/(double)1000);elseSystem.out.print(Math.round(s*1000)/(double)1000+" "+Math.round(t.S*1000)/(double)1000);}}}else//判断非三角形四边形情况{System.out.print("not a quadrilateral or triangle");}break;}case 5:{Point A=new Point();Point C=new Point();Point D=new Point();Point E=new Point();Point F=new Point();A.x=Double.valueOf(arr[1]);A.y=Double.valueOf(arr[2]);C.x=Double.valueOf(arr[3]);C.y=Double.valueOf(arr[4]);D.x=Double.valueOf(arr[5]);D.y=Double.valueOf(arr[6]);E.x=Double.valueOf(arr[7]);E.y=Double.valueOf(arr[8]);F.x=Double.valueOf(arr[9]);F.y=Double.valueOf(arr[10]);Quadrilateral Q=new Quadrilateral();Q.l1.p1=C;Q.l1.p2=D;Q.l2.p1=D;Q.l2.p2=E;Q.l3.p1=E;Q.l3.p2=F;Q.l4.p1=C;Q.l4.p2=F;Q.l5.p1=D;Q.l5.p2=F;Q.l6.p1=C;Q.l6.p2=E;Triangle tDEF=new Triangle();tDEF.l1.p1=D;tDEF.l1.p2=E;tDEF.l2.p1=E;tDEF.l2.p2=F;tDEF.l3.p1=D;tDEF.l3.p2=F;Triangle tCEF=new Triangle();tCEF.l1.p1=C;tCEF.l1.p2=E;tCEF.l2.p1=E;tCEF.l2.p2=F;tCEF.l3.p1=C;tCEF.l3.p2=F;Triangle tCDF=new Triangle();tCDF.l1.p1=C;tCDF.l1.p2=D;tCDF.l2.p1=D;tCDF.l2.p2=F;tCDF.l3.p1=C;tCDF.l3.p2=F;Triangle tCDE=new Triangle();tCDE.l1.p1=C;tCDE.l1.p2=D;tCDE.l2.p1=D;tCDE.l2.p2=E;tCDE.l3.p1=C;tCDE.l3.p2=E;Point P1=new Point();Point P2=new Point();Point P3=new Point();Point P4=new Point();Line lAC=new Line();lAC.p1=A;lAC.p2=C;Line lAD=new Line();lAD.p1=A;lAD.p2=D;Line lAE=new Line();lAE.p1=A;lAE.p2=E;Line lAF=new Line();lAF.p1=A;lAF.p2=F;int num1=0,num2=0,num3=0,num4=0;tDEF.whichsanjiaox1(C,D,E,F,tDEF,tCEF,tCDF,tCDE,Q);//判断是哪三个点构成三角形if(tDEF.flag==1||C.pointchonghe(C,E))//求直线与三角形交点数{if(tDEF.pointwhere(A,D,E,lAD,lAE,Q.l2)||tDEF.pointwhere(A,E,F,lAE,lAF,Q.l3)||tDEF.pointwhere(A,D,F,lAD,lAF,Q.l5)){System.out.print("on the triangle");return;}tDEF.jiaodiannum(P1,P2,P3,lAD,tDEF);num1=tDEF.num;tDEF.jiaodiannum(P1,P2,P3,lAE,tDEF);num2=tDEF.num;tDEF.jiaodiannum(P1,P2,P3,lAF,tDEF);num3=tDEF.num;if(num1==2&&num2==2&&num3==2)System.out.print("in the triangle");elseSystem.out.print("outof the triangle");}else if(tCEF.flag==1||D.pointchonghe(D,F)){if(tCEF.pointwhere(A,C,E,lAC,lAE,Q.l6)||tCEF.pointwhere(A,E,F,lAE,lAF,Q.l3)||tCEF.pointwhere(A,C,F,lAC,lAF,Q.l4)){System.out.print("on the triangle");return;}tCEF.jiaodiannum(P1,P2,P3,lAC,tCEF);num1=tCEF.num;tCEF.jiaodiannum(P1,P2,P3,lAE,tCEF);num2=tCEF.num;tCEF.jiaodiannum(P1,P2,P3,lAF,tCEF);num3=tCEF.num;if(num1==2&&num2==2&&num3==2)System.out.print("in the triangle");elseSystem.out.print("outof the triangle");}else if(tCDF.flag==1||E.pointchonghe(E,C)){if(tCDF.pointwhere(A,C,D,lAC,lAD,Q.l1)||tCDF.pointwhere(A,D,F,lAD,lAF,Q.l5)||tCDF.pointwhere(A,C,F,lAC,lAF,Q.l4)){System.out.print("on the triangle");return;}tCDF.jiaodiannum(P1,P2,P3,lAC,tCDF);num1=tCDF.num;tCDF.jiaodiannum(P1,P2,P3,lAD,tCDF);num2=tCDF.num;tCDF.jiaodiannum(P1,P2,P3,lAF,tCDF);num3=tCDF.num;if(num1==2&&num2==2&&num3==2)System.out.print("in the triangle");elseSystem.out.print("outof the triangle");}else if(tCDE.flag==1||F.pointchonghe(F,D)){if(tCDE.pointwhere(A,C,D,lAC,lAD,Q.l1)|tCDE.pointwhere(A,D,E,lAD,lAE,Q.l2)||tCDE.pointwhere(A,C,E,lAC,lAE,Q.l6)){System.out.print("on the triangle");return;}tCDE.jiaodiannum(P1,P2,P3,lAC,tCDE);num1=tCDE.num;tCDE.jiaodiannum(P1,P2,P3,lAD,tCDE);num2=tCDE.num;tCDE.jiaodiannum(P1,P2,P3,lAE,tCDE);num3=tCDE.num;if(num1==2&&num2==2&&num3==2)System.out.print("in the triangle");elseSystem.out.print("outof the triangle");}else if(Q.l1.Quadrilateral2(C,D,E,F,Q)){Triangle t=new Triangle();if(t.pointwhere(A,C,D,lAC,lAD,Q.l1)||t.pointwhere(A,D,E,lAD,lAE,Q.l2)||t.pointwhere(A,E,F,lAE,lAF,Q.l3)||t.pointwhere(A,C,F,lAC,lAF,Q.l4)){System.out.print("on the quadrilateral");return;}Triangle tACD=new Triangle();tACD.l1.p1=A;tACD.l1.p2=C;tACD.l2.p1=A;tACD.l2.p2=D;tACD.l3.p1=C;tACD.l3.p2=D;Triangle tADE=new Triangle();tADE.l1.p1=A;tADE.l1.p2=D;tADE.l2.p1=A;tADE.l2.p2=E;tADE.l3.p1=D;tADE.l3.p2=E;Triangle tAEF=new Triangle();tAEF.l1.p1=A;tAEF.l1.p2=E;tAEF.l2.p1=A;tAEF.l2.p2=F;tAEF.l3.p1=E;tAEF.l3.p2=F;Triangle tACF=new Triangle();tACF.l1.p1=A;tACF.l1.p2=C;tACF.l2.p1=A;tACF.l2.p2=F;tACF.l3.p1=C;tACF.l3.p2=F;Q.Quadrilateralmianji(Q);//四边形面积tACD.sanjiaoxmianji(tACD);tADE.sanjiaoxmianji(tADE);tAEF.sanjiaoxmianji(tAEF);tACF.sanjiaoxmianji(tACF);double S;S=tACD.S+tADE.S+tAEF.S+tACF.S;if(S-Q.S<0.001)System.out.print("in the quadrilateral");elseSystem.out.print("outof the quadrilateral");}elseSystem.out.print("not a quadrilateral or triangle");/*Q.Quadrilateralnum(P1,P2,P3,P4,lAC,t,Q);num1=Q.num;Q.Quadrilateralnum(P1,P2,P3,P4,lAD,t,Q);num2=Q.num;Q.Quadrilateralnum(P1,P2,P3,P4,lAE,t,Q);num3=Q.num;Q.Quadrilateralnum(P1,P2,P3,P4,lAF,t,Q);num4=Q.num;System.out.println(num1+" "+num2+" "+num3+" "+num4);if(num1==2&&num2==2&&num3==2&&num4==2)System.out.print("in the quadrilateral");elseSystem.out.print("outof the quadrilateral");}elseSystem.out.print("not a quadrilateral or triangle");*/break;}}}}

经验总结扩展阅读