前言:
第四次作业:这次作业难度适中,主要考察了对于正则表达式、类的结构、对于四边形的数学问题的解决、继承的使用。
第五次作业:这次作业难度比较大,五边形对于四边形和三角形来说,需要考虑的可能性明显增大,需要解决的数学问题的逻辑也明显更难,更加考验对于继承的使用和合理的构建类。
期中考试:这次考试难度并不算高,主要考查了对于类的设计,继承和多态,类的重构(建立一个容器类)。
第四五次作业中,由于我并没有构建好各个类的关系,也没有合理的使用类的继承,在编写代码的时候存在许多问题,尤其是第五次的作业难度和复杂程度明显提升的情况下,仅仅得了六十多分。在
设计与分析:
第四次作业:
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出”not a quadrilateral”
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出”The line is coincide with one of the lines”。若后四个点不符合四边形或三角形的输入,输出”not a quadrilateral or triangle”。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出”on the triangle或者on the quadrilateral”。若后四个点不符合四边形或三角形,输出”not a quadrilateral or triangle”。
输入格式:
基本格式:选项+”:”+坐标x+”,”+坐标y+” “+坐标x+”,”+坐标y。点的x、y坐标之间以英文”,”分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出”Wrong Format”。
如果符合基本格式,但输入点的数量不符合要求,输出”wrong number of points”。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
选项1、2、3中,若四边形四个点中有重合点,输出”points coincide”。
选项4中,若前两个输入线的点重合,输出”points coincide”。
分析: 这次作业的难点主要在于如何判断出输入的数据是否是四边形还是三角形或者是不能构成多边形。
我的代码:
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { double[] hhh = new double[100]; Data da =new Data(); Shujuchuli sh = new Shujuchuli(); da.shuru(); if(!da.panduan()){ System.out.println("wrong number of points"); return; } da.fenge(); switch (da.n){ case 1: da.chonghe(); //System.out.println(da.hhh[0]+""+da.hhh[1]+""+da.hhh[2]+""+da.hhh[3]+""+da.hhh[4]+""+da.hhh[5]); if(!sh.sibianx(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.print("false false"); } else { System.out.print("true "); if(sh.pingxingsib(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.println("true"); } else { System.out.println("false"); } } break; case 2: if(!sh.sibianx(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.println("not a quadrilateral"); return; } else { if (sh.lingxing(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.print("true "); } else{ System.out.print("false "); } if(sh.juxing(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.print("true "); } else{ System.out.print("false "); } if(sh.zhengfang(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.print("true"); } else{ System.out.print("false"); } } break; case 3: if(!sh.sibianx(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])){ System.out.println("not a quadrilateral"); return; } else{ sh.tu(da.hhh[0],da.hhh[1],da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7]); } break; case 4: boolean book2 = false; if(da.hhh[0]==da.hhh[2]&&da.hhh[1]==da.hhh[3]){ System.out.println("points coincid"); return; } if(sh.zaizai(da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7],da.hhh[8],da.hhh[9])&&sh.zaizai(da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7],da.hhh[10],da.hhh[11])&&!sh.sibianx(da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7],da.hhh[8],da.hhh[9],da.hhh[10],da.hhh[11])){ System.out.println("not a quadrilateral or triangle"); return; } for (int i =4;i<=8;i=i+2) { if(da.hhh[i] == da.hhh[i+2]&&da.hhh[i+1] == da.hhh[i+3]){ book2 = true; da.hhh[i] = da.hhh[10]; da.hhh[i+1] = da.hhh[11]; } if(da.hhh[i]==da.hhh[10]&&da.hhh[i+1]==da.hhh[11]){ book2 =true; } } if(da.hhh[4]==da.hhh[10]&&da.hhh[5]==da.hhh[11]){ book2 = true; } //System.out.println(da.hhh[4]+" "+da.hhh[5]+" "+da.hhh[6]+" "+da.hhh[7]+" "+da.hhh[8]+" "+da.hhh[9]+" "); //sh.sanjiaoqiege(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9]); if(book2){ if(sh.panduan(da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9])) { sh.sanjiaoqiege(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9]); } else System.out.println("not a quadrilateral or triangle"); } if(!book2) { if (sh.sanjiao(da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9], da.hhh[10], da.hhh[11])) { for (int i = 4; i <= 8; i = i + 2) { if (sh.x0 == da.hhh[i] && sh.y0 == da.hhh[i + 1]) { da.hhh[i] = da.hhh[10]; da.hhh[i + 1] = da.hhh[11]; } } sh.sanjiaoqiege(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9]); return; } if (!sh.sanjiao(da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9], da.hhh[10], da.hhh[11])) { if (!sh.sibianx(da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9], da.hhh[10], da.hhh[11])) { System.out.println("not a quadrilateral or triangle"); return; } sh.tuqiege(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9], da.hhh[10], da.hhh[11]); } } break; case 5: book2 = false; if(sh.zaizai(da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7])&&sh.zaizai(da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[8],da.hhh[9])&&!sh.sibianx(da.hhh[2],da.hhh[3],da.hhh[4],da.hhh[5],da.hhh[6],da.hhh[7],da.hhh[8],da.hhh[9])){ System.out.println("not a quadrilateral or triangle"); return; } for (int i =2;i<=6;i=i+2) { if(da.hhh[i] == da.hhh[i+2]&&da.hhh[i+1] == da.hhh[i+3]){ book2 = true; da.hhh[i] = da.hhh[8]; da.hhh[i+1] = da.hhh[9]; } } if(book2){ if(sh.panduan(da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9])) { sh.pansanjiao(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7]); } else System.out.println("not a quadrilateral or triangle"); return; } if(!book2) { if (sh.sanjiao(da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9])) { for (int i = 2; i <= 6; i = i + 2) { if (sh.x0 == da.hhh[i] && sh.y0 == da.hhh[i + 1]) { da.hhh[i] = da.hhh[8]; da.hhh[i + 1] = da.hhh[9]; break; } } sh.pansanjiao(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7]); return; } if (!sh.sanjiao(da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9])) { if (!sh.sibianx(da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9])) { System.out.println("not a quadrilateral or triangle"); return; } sh.pansibian(da.hhh[0], da.hhh[1], da.hhh[2], da.hhh[3], da.hhh[4], da.hhh[5], da.hhh[6], da.hhh[7], da.hhh[8], da.hhh[9]); } } } } } class Shujuchuli { double a, b, c; //构成直线方程的参数 double x0, y0; boolean flat = false,flat1 =false; double zzx[] = new double[100]; double zzy[] = new double[100]; int n= 1; //x1,y1 x2,y2两点的距离 public double dianjuli(double x1, double y1, double x2, double y2) { return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } //x1,y1 x2,y2两点所成直线的方程的基本数据 public void fangc(double x1, double y1, double x2, double y2) { a = y2 - y1; b = x1 - x2; c = x2 * y1 - x1 * y2; } //判断x1,y1 x2,y2和x3,y3 x4,y4两条线段是否垂直:A1A2+B1B2=0 public boolean chuizhi(double x1, double y1, double x2, double y2, double x3, double y3){ boolean book =false; double num =(y2-y1)*(y3-y2)+(x1-x2)*(x2-x3); if(Math.abs(num)<0.001){ book = true; } return book; } //x3,y3到x1,y1 x2,y2直线的距离 public double dianxianju(double x1, double y1, double x2, double y2, double x3, double y3) { a = y2 - y1; b = x1 - x2; c = x2 * y1 - x1 * y2; return Math.abs(a * x3 + b * y3 + c) / Math.sqrt(a * a + b * b); } //判断x1,y1 x2,y2和x3,y3 x4,y4两条线段的交点 public boolean jiaodian(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double b1, b2, D1, D2, D; boolean book = true; b1 = (y2 - y1) * x1 + (x1 - x2) * y1; b2 = (y4 - y3) * x3 + (x3 - x4) * y3; D = (x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1); D1 = b2 * (x2 - x1) - b1 * (x4 - x3); D2 = b2 * (y2 - y1) - b1 * (y4 - y3); x0 = D1 / D; y0 = D2 / D; // System.out.println(x0+" "+y0); if(zaizai1(x1,y1,x2,y2,x0,y0)&&zaizai1(x3,y3,x4,y4,x0,y0)){ book = false; } return book; } //判断x3,y3是否在x1,y1 x2,y2所构成的直线上 public boolean zaizai(double x1, double y1, double x2, double y2, double x3, double y3) { double h = dianxianju(x1, y1, x2, y2, x3, y3); boolean book = false; if (Math.abs(h) < 0.01) { book = true; } return book; } //判断x1,y1 x2,y2 和 x3,y3和x4,y4是否平行 public boolean pingxing(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double n = (y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4); boolean book; book = Math.abs(n) < 0.001; return book; } //&&(x2 * y1 - x1 * y2 != x4 * y3 - x3 * y4) public boolean xianchonghe(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double n = (y1 - y2) * (x3 - x4) - (x1 - x2) * (y3 - y4); boolean book; if (n==0 && (x2 * y1 - x1 * y2 == x4 * y3 - x3 * y4)) { book = true; } else book = false; return book; } //判断是否是四边形 public boolean sibianx(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { boolean book; if(zaizai(x1, y1, x2, y2, x3, y3)||zaizai(x1, y1, x2, y2, x4, y4)||zaizai(x2,y2,x3,y3,x4,y4)||zaizai(x1,y1,x3,y3,x4,y4)){ book = false; } else { if (pingxing(x1, y1, x2, y2, x3, y3, x4, y4)) { if(!pingxing(x1,y1,x4,y4,x2,y2,x3,y3)){ book = jiaodian(x1, y1, x4, y4, x2, y2, x3, y3); } else { book = jiaodian(x1, y1, x2, y2, x3, y3, x4, y4); } } else { book = jiaodian(x1, y1, x2, y2, x3, y3, x4, y4); } } return book; } //判断是否是平行四边形 public boolean pingxingsib(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ boolean book = false; if(pingxing(x1, y1, x2, y2, x3, y3, x4, y4)&&pingxing(x1, y1, x4, y4, x2, y2, x3, y3)){ book = true; } return book; } //判断是否是菱形 public boolean lingxing(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ boolean book = false; double L1,L2; if(pingxingsib(x1, y1, x2, y2, x3, y3, x4, y4)){ L1 = dianjuli(x1, y1, x2, y2); L2 = dianjuli(x2, y2, x3, y3); if(Math.abs(L1-L2)<0.001){ book = true; } } return book; } //判断是否是矩形 public boolean juxing(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ boolean book =false; if(pingxingsib(x1, y1, x2, y2, x3, y3, x4, y4)){ if(chuizhi(x1, y1, x2, y2, x3, y3)){ book = true; } } return book; } //判断是否是正方形 public boolean zhengfang(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ boolean book =false; if(lingxing(x1, y1, x2, y2, x3, y3, x4, y4)){ if(chuizhi(x1, y1, x2, y2, x3, y3)){ book = true; } } return book; } //三角形面积 public double ss(double x1,double y1,double x2,double y2,double x3,double y3){ double h = dianxianju(x1,y1,x2,y2,x3,y3); double L = dianjuli(x1,y1,x2,y2); return h*L/2; } //求x2,y2点的角度 public double jiaodu(double x1,double y1,double x2,double y2,double x3,double y3){ double A,B,C,cos,du; A = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); B = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); C = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); cos = (A*A+C*C-B*B)/(2*A*C); du=Math.acos(cos); return du; } //s1 = S(p1,p2,p3)+ S(p3,p4,p1) //s2 = S(p2,p3,p4)+S(p4,p1,p2) //判断凹凸四边形,并对相应数据处理 public void tu(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ double s1,s2,c,a1,a2,a3,a4,a; s1 = ss(x1,y1,x2,y2,x3,y3)+ss(x1,y1,x4,y4,x3,y3); s2 = ss(x4,y4,x2,y2,x3,y3)+ss(x1,y1,x2,y2,x4,y4); if(Math.abs(s1-s2)<0.001){ System.out.print("true "); String m1 = new DecimalFormat("#0.0##").format(s1); c = dianjuli(x1,y1,x2,y2)+dianjuli(x3,y3,x2,y2)+dianjuli(x3,y3,x4,y4)+dianjuli(x1,y1,x4,y4); String m2 = new DecimalFormat("#0.0##").format(c); System.out.println(m2+" "+m1); } else { System.out.print("false "); a1 = jiaodu(x1,y1,x2,y2,x3,y3); a2 = jiaodu(x2,y2,x3,y3,x4,y4); a3 = jiaodu(x3,y3,x4,y4,x1,y1); a4 = jiaodu(x4,y4,x1,y1,x2,y2); a = Math.max(Math.max(a1,a2),Math.max(a3,a4)); c = dianjuli(x1,y1,x2,y2)+dianjuli(x3,y3,x2,y2)+dianjuli(x3,y3,x4,y4)+dianjuli(x1,y1,x4,y4); String m4 = new DecimalFormat("#0.0##").format(c); System.out.print(m4+" "); if(a==a1||a==a3){ String m3 = new DecimalFormat("#0.0##").format(s2); System.out.println(m3); } if(a==a2||a==a4){ String m3 = new DecimalFormat("#0.0##").format(s1); System.out.println(m3); } } } //凸四边形的面积 public double tuss(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ return ss(x1,y1,x2,y2,x3,y3)+ss(x1,y1,x4,y4,x3,y3); } public Dian qiujiaodian(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, Dian d) { double b1, b2, D1, D2, D; boolean book = true; b1 = (y2 - y1) * x1 + (x1 - x2) * y1; b2 = (y4 - y3) * x3 + (x3 - x4) * y3; D = (x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1); D1 = b2 * (x2 - x1) - b1 * (x4 - x3); D2 = b2 * (y2 - y1) - b1 * (y4 - y3); x0 = D1 / D; y0 = D2 / D; d.x=x0; d.y=y0; return d; } // public void tuqiege(double x,double y,double xx,double yy,double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ double sx[] = new double[100]; double sy[] = new double[100]; double x0[] = new double[100]; double y0[] = new double[100]; int num = 0; int l[] = new int[100]; //顶点标记 sx[0] = x4; sy[0] = y4; sx[1] = x1; sx[2] = x2; sx[3] = x3; sx[4] = x4; sy[1] = y1; sy[2] = y2; sy[3] = y3; sy[4] = y4; sx[5] = x1; sy[5] = y1; if(xianchonghe(x,y,xx,yy,x1,y1,x2,y2)||xianchonghe(x,y,xx,yy,x1,y1,x4,y4)||xianchonghe(x,y,xx,yy,x3,y3,x2,y2)||xianchonghe(x,y,xx,yy,x4,y4,x3,y3)){ System.out.println("The line is coincide with one of the lines"); System.exit(0); } for(int i=1;i<=4;i++){ l[i]=0; } for(int i=1;i<4;i++){ for (int j=i+1;j<=4;j++){ if(j!=i+2) if (jiaodian1(x, y, xx, yy, sx[i], sy[i], sx[j], sy[j])) { num++; l[i]++; l[j]++; } } } Dian d1 = new Dian(); Dian d2 = new Dian(); //System.out.println(num); switch (num){ case 0: System.out.println(num); break; case 2: double s1=0,s2=0; if(flat){ System.out.println("1"); System.exit(0); } System.out.print("2 "); if(Math.max(Math.max(l[1],l[2]),Math.max(l[3],l[4]))==2){ if(l[1]==2){ d1=qiujiaodian(x,y,xx,yy,x1,y1,x4,y4,d1); d2=qiujiaodian(x,y,xx,yy,x1,y1,x2,y2,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x1,y1); s2 =tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; } if(l[2]==2){ d1=qiujiaodian(x,y,xx,yy,x2,y2,x1,y1,d1); d2=qiujiaodian(x,y,xx,yy,x2,y2,x3,y3,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x2,y2); s2 =tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; } if(l[3]==2){ d1=qiujiaodian(x,y,xx,yy,x3,y3,x2,y2,d1); d2=qiujiaodian(x,y,xx,yy,x3,y3,x4,y4,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x3,y3); s2 =tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; } if(l[4]==2){ d1=qiujiaodian(x,y,xx,yy,x4,y4,x1,y1,d1); d2=qiujiaodian(x,y,xx,yy,x4,y4,x3,y3,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x4,y4); s2 =tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; } String m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); String m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); System.exit(0); } if(jiaodian1(x,y,xx,yy,x1,y1,x2,y2)){ d1 = qiujiaodian(x,y,xx,yy,x1,y1,x2,y2,d1); d2 = qiujiaodian(x,y,xx,yy,x3,y3,x4,y4,d2); s1 = tuss(x1,y1,x4,y4,d1.x,d1.y,d2.x,d2.y); s2 = tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; String m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); String m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); System.exit(0); } if(jiaodian1(x,y,xx,yy,x1,y1,x4,y4)){ d1 = qiujiaodian(x,y,xx,yy,x1,y1,x4,y4,d1); d2 = qiujiaodian(x,y,xx,yy,x3,y3,x2,y2,d2); s1 = tuss(x1,y1,x2,y2,d1.x,d1.y,d2.x,d2.y); s2 = tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; String m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); String m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); System.exit(0); } break; case 3: System.out.print("2 "); s1=0;s2=0; l[0]=l[4]; l[5]=l[1]; for (int i =1;i<=4;i++){ if(l[i]==2){ if(l[i+1]==1){ s1 = ss(sx[i],sy[i],sx[i+1],sy[i+1],zzx[1],zzy[1]); s2 = tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; break; } if(l[i-1]==1){ s1 = ss(sx[i],sy[i],sx[i-1],sy[i-1],zzx[1],zzy[1]); s2 = tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; break; } } } String m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); String m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); break; case 4: System.out.print("2 "); s1=0;s2=0; for (int i=1;i<=2;i++){ if(l[i]==2){ s1 = ss(sx[i],sy[i],sx[i+2],sy[i+2],sx[i+1],sy[i+1]); s2 = tuss(x1,y1,x2,y2,x3,y3,x4,y4)-s1; } } m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); break; } } public boolean jiaodian2(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double b1, b2, D1, D2, D; boolean book = false; b1 = (y2 - y1) * x1 + (x1 - x2) * y1; b2 = (y4 - y3) * x3 + (x3 - x4) * y3; D = (x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1); D1 = b2 * (x2 - x1) - b1 * (x4 - x3); D2 = b2 * (y2 - y1) - b1 * (y4 - y3); x0 = D1 / D; y0 = D2 / D; if(x0<=Math.max(x1,x2)&&x0>=Math.min(x1,x2)&&x0<=Math.max(x3,x4)&&x0>=Math.min(x3,x4)&&y0<=Math.max(y1,y2)&&y0>=Math.min(y1,y2)&&y0<=Math.max(y3,y4)&&y0>=Math.min(y3,y4)){ book = true; } return book; } public boolean sanjiao(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ if(zaizai(x1, y1, x2, y2, x4, y4)||zaizai(x1, y1, x2, y2, x3, y3)||zaizai(x4, y4, x2, y2, x3, y3)||zaizai(x1,y1,x3,y3,x4,y4)){ if(!jiaodian2(x1,y1,x3,y3,x2,y2,x4,y4)){ flat1 = false; } else { flat1 = true; } } return flat1; } public boolean panduan(double x1,double y1,double x2,double y2,double x3,double y3){ boolean book = false; if(!(x2==x3&&y2==y3)) { double h = dianxianju(x2, y2, x3, y3,x1,y1); //System.out.println(h); if (h != 0) { book = true; } } return book; } public boolean jiaodian1(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double b1, b2, D1, D2, D; boolean book = false; if(pingxing(x1,y1,x2,y2,x3,y3,x4,y4)){ return false; } b1 = (y2 - y1) * x1 + (x1 - x2) * y1; b2 = (y4 - y3) * x3 + (x3 - x4) * y3; D = (x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1); D1 = b2 * (x2 - x1) - b1 * (x4 - x3); D2 = b2 * (y2 - y1) - b1 * (y4 - y3); x0 = D1 / D; y0 = D2 / D; zzx[n] = x0; zzy[n] = y0; n++; if(x0<=Math.max(x3,x4)&&x0>=Math.min(x3,x4)&&y0<=Math.max(y3,y4)&&y0>=Math.min(y3,y4)){ book = true; if((x0==x3&&y0==y3)||(x0==x4&&y0==x4)){ flat = true; } } return book; } public void sanjiaoqiege(double x,double y,double xx,double yy,double x1, double y1, double x2, double y2, double x3, double y3){ flat =false; if(xianchonghe(x,y,xx,yy,x1,y1,x2,y2)||xianchonghe(x,y,xx,yy,x3,y3,x2,y2)||xianchonghe(x,y,xx,yy,x1,y1,x3,y3)){ System.out.println("The line is coincide with one of the lines"); System.exit(0); } double sx[] = new double[100]; double sy[] = new double[100]; int num = 0; double s1=0,s2=0; int l[] = new int[100]; //顶点标记 sx[0] = x3; sx[1] = x1; sx[2] = x2; sx[3] = x3; sx[4] = x1; sy[0] = y3; sy[1] = y1; sy[2] = y2; sy[3] = y3; sy[4] = y1; Dian d1 = new Dian(); Dian d2 = new Dian(); for(int i=1;i<=3;i++){ l[i]=0; } for(int i=1;i<3;i++){ for (int j=i+1;j<=3;j++){ if (jiaodian1(x, y, xx, yy, sx[i], sy[i], sx[j], sy[j])) { num++; l[i]++; l[j]++; } } } if(num==0){ System.out.println("0"); } if(num==2){ if(flat){ System.out.println("1"); return; } System.out.print("2 "); if(l[1]==2){ d1=qiujiaodian(x,y,xx,yy,x1,y1,x2,y2,d1); d2=qiujiaodian(x,y,xx,yy,x1,y1,x3,y3,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x1,y1); s2 =ss(x1, y1, x2, y2, x3, y3)-s1; } if(l[2]==2){ d1=qiujiaodian(x,y,xx,yy,x2,y2,x1,y1,d1); d2=qiujiaodian(x,y,xx,yy,x2,y2,x3,y3,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x2,y2); s2 =ss(x1, y1, x2, y2, x3, y3)-s1; } if(l[3]==2){ d1=qiujiaodian(x,y,xx,yy,x3,y3,x1,y1,d1); d2=qiujiaodian(x,y,xx,yy,x2,y2,x3,y3,d2); s1 =ss(d1.x,d1.y,d2.x,d2.y,x3,y3); s2 =ss(x1, y1, x2, y2, x3, y3)-s1; } String m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); String m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); System.exit(0); } if(num==3){ System.out.print("2 "); for(int i=1;i<=3;i++){ if(zaizai(x,y,xx,yy,sx[i],sy[i])){ d1=qiujiaodian(x,y,xx,yy,sx[i+1],sy[i+1],sx[i-1],sy[i-1],d1); s1 = ss(d1.x,d1.y,sx[i],sy[i],sx[i+1],sy[i+1]); s2 = ss(x1, y1, x2, y2, x3, y3)-s1; } } String m1 = new DecimalFormat("#0.0##").format(Math.max(s1,s2)); String m2 = new DecimalFormat("#0.0##").format(Math.min(s1,s2)); System.out.println(m2+" "+m1); System.exit(0); } } public boolean zaizai1(double x1, double y1, double x2, double y2, double x3, double y3) { double h = dianxianju(x1, y1, x2, y2, x3, y3); boolean book = false; if (Math.abs(h) < 0.00001) { if((x3<=Math.max(x1,x2)&&x3>=Math.min(x1,x2))&&(y3<=Math.max(y1,y2)&&y3>=Math.min(y1,y2))) { book = true; } } return book; } public void pansanjiao(double x,double y,double x1,double y1,double x2,double y2,double x3,double y3){ double s1,s2; if(zaizai1(x1,y1,x2,y2,x,y)||zaizai1(x1,y1,x3,y3,x,y)||zaizai1(x2,y2,x3,y3,x,y)){ System.out.println("on the triangle"); System.exit(0); } s1 = dianxianju(x1,y1,x2,y2,x,y)*dianjuli(x1,y1,x2,y2)/2+dianxianju(x1,y1,x3,y3,x,y)*dianjuli(x1,y1,x3,y3)/2+dianxianju(x3,y3,x2,y2,x,y)*dianjuli(x3,y3,x2,y2)/2; s2 = ss(x1,y1,x2,y2,x3,y3); //System.out.println(x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+y3); //System.out.println(s1+" "+s2); if(Math.abs(s1-s2)<0.001){ System.out.println("in the triangle"); } else { System.out.println("outof the triangle"); } } public void pansibian(double x,double y,double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){ double s1,s2; if(zaizai1(x1,y1,x2,y2,x,y)||zaizai1(x1,y1,x4,y4,x,y)||zaizai1(x2,y2,x3,y3,x,y)||zaizai1(x3,y3,x4,y4,x,y)){ System.out.println("on the quadrilateral"); System.exit(0); } s1 = dianxianju(x1,y1,x2,y2,x,y)*dianjuli(x1,y1,x2,y2)/2+dianxianju(x1,y1,x4,y4,x,y)*dianjuli(x1,y1,x4,y4)/2+dianxianju(x3,y3,x2,y2,x,y)*dianjuli(x3,y3,x2,y2)/2+dianxianju(x3,y3,x4,y4,x,y)*dianjuli(x3,y3,x4,y4)/2; s2 = tuss(x1,y1,x2,y2,x3,y3,x4,y4); if(Math.abs(s1-s2)<0.01){ System.out.println("in the quadrilateral"); } else { System.out.println("outof the quadrilateral"); } } } class Dian { double x,y; } class Data { String str; boolean book; double[] hhh = new double[100]; int n; public void shuru(){ Scanner sc = new Scanner(System.in); str = sc.nextLine(); String abc[] = str.split(" "); n = (int) str.charAt(0) - '0'; if(!str.matches("[1-5]:([+-]?(0|[1-9]\\d*)(\\.\\d+)?,[+-]?(0|[1-9]\\d*)(\\.\\d+)? )*[+-]?(0|[1-9]\\d*)(\\.\\d+)?,[+-]?(0|[1-9]\\d*)(\\.\\d+)? ?")){ System.out.println("Wrong Format"); System.exit(0); } } public void fenge() { int num = 0; str =str.substring(2); //System.out.println(str); String abc[] = str.split(" "); for (String i : abc) { String zx[] = i.split((",")); for (String j : zx) { hhh[num] = Double.parseDouble(j); num++; } } } public boolean panduan(){ String abc[] = str.split(" "); switch (n){ case 1: case 2: case 3: if(abc.length==4){ book = true; } break; case 4: if(abc.length==6){ book = true; } break; case 5: if(abc.length==5){ book = true; } break; } return book; } //判断是否又重合的点 0 2 4 6 01234567 public void chonghe(){ for(int i=0;i<5;i=i+2){ for(int j=i+2;j<7;j=j+2){ if(hhh[i]==hhh[j]&&hhh[i+1]==hhh[j+1]){ System.out.println("points coincide"); System.exit(0); } } } } }
View Code
由于没有构建好好的继承关系,许多方法写的冗余重复,导致代码长度过长,类图
原文地址:http://www.cnblogs.com/zzx2856243510/p/16836481.html