第四,五次大作业及期中考试总结
1.前言
按照时间顺序,我们先后完成了第四次大作业,期中考试,第五次大作业。其中第四,五次大作业联系较为紧密,而期中考试的三道题目则有明显的递进关系,因此我在叙述时会按照题目之间的联系来调节次序。
2.设计与分析
(1).期中考试
期中考试中题目是关于点与线(类设计),点线面问题重构(继承与多态),点线面问题再重构(容器类)。
在7-1中,我们需要按照题目要求构造Point类和Line类,并为其赋值属性。
在7-2中,我们需要在7-1的前提下对类设计进行重构,以实现继承与多态的技术性需求。
在7-3中,我们需要添加一个容器类。

由于7-3的代码包括了7-1,7-2,因此我将只展示7-3的代码,如下
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 int flag=0; Scanner in = new Scanner(System.in); GeometryObject G = new GeometryObject(); int choice = in.nextInt(); while(choice != 0) { switch(choice) { case 1: double x1 = in.nextDouble(); double y1 = in.nextDouble(); G.addPoint(x1, y1); break; case 2: double x11 = in.nextDouble(); double y11 = in.nextDouble(); double x22 = in.nextDouble(); double y33 = in.nextDouble(); String a = in.next(); G.addLine(x11, y11, x22, y33, a); break; case 3:flag++; String b = in.next(); if(flag==2) { G.addPlane(b); } break; case 4: int x = in.nextInt(); G.remove(x); } choice = in.nextInt(); } } } abstract class Element { abstract void display(); } class GeometryObject { @SuppressWarnings("rawtypes") ArrayList<Comparable> List = new ArrayList<Comparable>(); public void addPoint(double x1,double y1) { List.add(x1); List.add(y1); System.out.println("("+String.format("%.2f", x1)+","+String.format("%.2f", y1)+")"); } public void addLine(double x1,double y1,double x2,double y2,String a) { List.add(x1); List.add(y1); List.add(x2); List.add(y2); List.add(a); System.out.println("The line's color is:"+a); System.out.println("The line's begin point's Coordinate is:"); System.out.println("("+String.format("%.2f", x1)+","+String.format("%.2f", y1)+")"); System.out.println("The line's end point's Coordinate is:"); System.out.println("("+String.format("%.2f", x2)+","+String.format("%.2f", y2)+")"); System.out.println("The line's length is:"+String.format("%.2f", Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))); } public void addPlane(String a) { List.add(a); System.out.print("The Plane's color is:Green"); } public void remove(int x) { int index=x-1; List.remove(index); } public void show() { for(int i=0;i<List.size();i++) { System.out.println(List.get(i)); } } } class Line extends Element{ private Point p1; private Point p2; private String color; Plane pl = new Plane(); public Line(double x1, double y1, double x2, double y2) { Point p1 = new Point(x1, y1); Point p2 = new Point(x2, y2); this.p1 = p1; this.p2 = p2; } public Line(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } public void setcolor(String a) { this.color = a; } public String getcolor() { return color; } public double getDistance() { double distY = p2.getterY() - p1.getterY(); double distX = p2.getterX() - p1.getterX(); return Math.sqrt(distX*distX+distY*distY); } public void display() { if((p1.getterX()>0&&p1.getterX()<=200)&&(p1.getterY()>0&&p1.getterY()<=200)&&(p2.getterX()>0&&p2.getterX()<=200)&&(p2.getterY()>0&&p2.getterY()<=200)){ p1.display(); p2.display(); System.out.println("The line's color is:"+getcolor()); System.out.println("The line's begin point's Coordinate is:"); p1.display(); System.out.println("The line's end point's Coordinate is:"); p2.display(); System.out.println("The line's length is:"+String.format("%.2f", getDistance())); System.out.print("The Plane's color is:"+getcolor()); } else { System.out.print("Wrong Format"); } } } class Plane extends Element{ private String color; public void setcolor(String a) { this.color = a; } public String getcolor() { return color; } public void display() { System.out.print("The Plane's color is:"+getcolor()); } } class Point extends Element{ private double x; private double y; public Point(double x,double y1) { this.x=x; this.y=y1; } public Point() { // TODO 自动生成的构造函数存根 } public void setterX(double x) { this.x = x; } public void setterY(double y) { this.y = y; } public double getterX() { return x; } public double getterY() { return y; } public void display() { System.out.println("("+String.format("%.2f", getterX())+","+String.format("%.2f", getterY())+")"); } }
期中考试重点是对容器类ArrayList的考察,由于不同类输出不同,因此我们需要在ArrayList中添加对象类的父类。
(2).第四次大作业和第五次大作业
这两次大作业内容大同小异,一个是对四边形进行运算,一个是对五边形进行运算。在四边形中,首次出现了多于点的情况,由此加深了四边形和三角形计算的联系,这一点在之后的五边形中也有进一步体现,但五边形中情况显得更为复杂。而在功能的实现上,两者也有许多相似之处。但相似却并不相同。例如在计算直线与点图形交点形成的区域的面积时,两者相似的都要考虑多余点的情况。以三角形(多余点有两个)为例,在四边形中,我们需要只考虑一个点,而在五边形中,我们则需要考虑两个点。显然,适用于四边形的方法并不适用于五边形,因为五边形类中情况比四边形类多得多,我们必须构建新的方法来进行判断。在五边形的作业中,对比四边形,我大量地使用了ArrayList类进行条件的判断。
功能一:对于四个点是否构成四边形,我的想法是将四边形以每三个点为一个单位相连接,再判断这个单位是否构成三角形,如果任意一个单位都满足三角形,则四个点可以构成四边形。而五边形的判断显然较为复杂。首先,我需要对五个点是否重合进行判断,还要对判断相邻直线是否平行,每隔一条直线的两直线的交点是否在线段的内部。
功能二:对多边形判断凹凸性时,我们都可以用以任意两点构成直线,判断其余点是否均在直线的同一侧,是则为凸多边形。
功能三:对多边形与直线交点面积的计算,我们先要判断多边形的类别吗,是几边形,再在对应的多边形类中通过ArrayList类获取直线与多边形的交点,通过交点计算面积。
功能四:对多边形与多边形的关系,一共需要分9种情况,前一个多边形有三种可能,后一个多边形也有三种可能。由于多边形本质上是由点组成的,所以我们可以将本题分解为判断多个点与直线的关系。
功能六:在判断点与多边形(凸)的位置关系时,我们可以采用面积法,即以点为三角形的一个顶点,按顺序连接多边形的相邻两点,可以得到几个三角形。我们将这几个三角形的面积总和与多边形的面积相比较,如果相等,则一定不在多边形的外面。至于是多边形的内部还是边或点上,我们需要进一步判断上述三角形的面积是否存在为0的情况。
最后,我们还需要特别注意题目的输出要求,例如无法构成多边形输出“not a polygon”,无法构成五边形输出“not a pentagon”。

 
 
 
代码如下
import java.util.Scanner; import java.text.DecimalFormat; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char ty = str.charAt(0); if(ty=='1'||ty=='2'||ty=='3'||ty=='4'||ty=='5') { String wei = str.substring(2,str.length()); switch(ty) { case '1': get x = new get(); x.get1(wei); break; case '2': get3 c = new get3(); c.get4(wei); break; case '3': get5 v = new get5(); v.get6(wei); break; case '4': get7 u = new get7(); u.get8(wei); break; case '5': get9 we = new get9(); we.get10(wei); break; } } else { System.out.print("Wrong Format"); } } } class get { public void get1(String a) { pan pa = new pan(); Fanhui f = new Fanhui(); int i = 0; int flag = 0; int flag1 = 0; int flag2 = 0; double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; double x3 = 0; double y3 = 0; double x4 = 0; double y4 = 0; String pattern = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; for(i=0;i<a.length();i++) { char b = a.charAt(i); char t = a.charAt(i); if(b==',') { flag++; } if(t==' ') { flag1++; } } if(flag==4&&flag1==3) { if(a.matches(pattern)){ double[] aj = new double[12]; aj=f.fanhui(a); x1=aj[0]; y1=aj[1]; x2=aj[2]; y2=aj[3]; x3=aj[4]; y3=aj[5]; x4=aj[6]; y4=aj[7]; if(pa.panduan1(x1,y1,x2,y2)==0&&pa.panduan1(x1,y1,x3,y3)==0&&pa.panduan1(x1,y1,x4,y4)==0&&pa.panduan1(x2,y2,x3,y3)==0&&pa.panduan1(x2,y2,x4,y4)==0&&pa.panduan1(x3,y3,x4,y4)==0) { if(pa.panduan(x1,y1,x2,y2,x3,y3)==1&&pa.panduan(x1,y1,x2,y2,x4,y4)==1&&pa.panduan(x1,y1,x3,y3,x4,y4)==1&&pa.panduan(x2,y2,x3,y3,x4,y4)==1) { flag2=1; System.out.print("true "); } else { System.out.print("false "); } double k12=(y1-y2)/(x1-x2); double k34=(y3-y4)/(x3-x4); double l12=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double l34=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); if(flag2==1) { if((k12==k34&&Math.sqrt(l12-l34)<=0.0000001)||(x1==x2&&x3==x4&&Math.sqrt(l12-l34)<=0.0000001)) { System.out.print("true"); } else{ System.out.print("false"); } } else { System.out.printf("false"); } } else { System.out.print("points coincide"); } } else{ System.out.print("Wrong Format");} } else{ System.out.print("wrong number of points"); } } } class get3 { public void get4(String a) { pan pa = new pan(); Fanhui f = new Fanhui(); int i = 0; int flag = 0; int flag1 = 0; int flag2 = 0; int flag3 = 0; int flag4 = 0; double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; double x3 = 0; double y3 = 0; double x4 = 0; double y4 = 0; String pattern = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; for(i=0;i<a.length();i++) { char b = a.charAt(i); char t = a.charAt(i); if(b==',') { flag++; } if(t==' ') { flag1++; } } if(flag==4&&flag1==3) { if(a.matches(pattern)){ double[] aj = new double[12]; aj=f.fanhui(a); x1=aj[0]; y1=aj[1]; x2=aj[2]; y2=aj[3]; x3=aj[4]; y3=aj[5]; x4=aj[6]; y4=aj[7]; if(pa.panduan(x1,y1,x2,y2,x3,y3)==1&pa.panduan(x1,y1,x2,y2,x4,y4)==1&&pa.panduan(x1,y1,x3,y3,x4,y4)==1&&pa.panduan(x2,y2,x3,y3,x4,y4)==1) { double k12=(y1-y2)/(x1-x2); double k34=(y3-y4)/(x3-x4); double l12=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double l34=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); double l13=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); double l24=Math.sqrt((x2-x4)*(x2-x4)+(y2-y4)*(y2-y4)); double l23=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); //判断是否为平行四边形 if((k12==k34||(x1==x2&&x3==x4))&&Math.sqrt(l12-l34)<0.0001) { flag3=1; } else{ flag3=0; } //判断是否为棱形 if(flag3==1) { if(Math.sqrt(l12-l23)<0.0001) { flag4=1; System.out.print("true "); } else { System.out.print("false "); } } else { System.out.print("false "); } //判断是否为长方形 if(flag3==1) { if(Math.sqrt(l24-l13)<0.0001) { System.out.print("true "); } else { System.out.print("false "); } } else{ System.out.print("false "); } //判断是否为正方形 if(flag4==1) { if(Math.sqrt(l24-l13)<0.0001) { System.out.print("true"); } else { System.out.print("false"); } } else{ System.out.print("false"); } } else { System.out.print("not a quadrilateral"); } } else{ System.out.print("Wrong Format");} } else{ System.out.print("wrong number of points"); } } } class get5 { public void get6(String a) { pan pa = new pan(); Fanhui f = new Fanhui(); int i = 0; int flag = 0; int flag1 = 0; double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; double x3 = 0; double y3 = 0; double x4 = 0; double y4 = 0; String pattern = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; for(i=0;i<a.length();i++) { char b = a.charAt(i); char t = a.charAt(i); if(b==',') { flag++; } if(t==' ') { flag1++; } } if(flag==4&&flag1==3) { if(a.matches(pattern)){ double[] aj = new double[12]; aj=f.fanhui(a); x1=aj[0]; y1=aj[1]; x2=aj[2]; y2=aj[3]; x3=aj[4]; y3=aj[5]; x4=aj[6]; y4=aj[7]; if(pa.panduan1(x1,y1,x2,y2)==0&&pa.panduan1(x1,y1,x3,y3)==0&&pa.panduan1(x1,y1,x4,y4)==0&&pa.panduan1(x2,y2,x3,y3)==0&&pa.panduan1(x2,y2,x4,y4)==0&&pa.panduan1(x3,y3,x4,y4)==0) { if(pa.panduan(x1,y1,x2,y2,x3,y3)==1&&pa.panduan(x1,y1,x2,y2,x4,y4)==1&&pa.panduan(x1,y1,x3,y3,x4,y4)==1&&pa.panduan(x2,y2,x3,y3,x4,y4)==1) { double k13=(y1-y3)/(x1-x3); double l12=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double l34=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)); double l13=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); double l24=Math.sqrt((x2-x4)*(x2-x4)+(y2-y4)*(y2-y4)); double l14=Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4)); double l23=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); double s123=Math.sqrt((l12+l13+l23)*(l12+l23-l13)*(l12+l13-l23)*(l13+l23-l12))/4; double s124=Math.sqrt((l12+l14+l24)*(l14+l24-l12)*(l12+l24-l14)*(l12+l14-l24))/4; double s134=Math.sqrt((l13+l14+l34)*(l14+l34-l13)*(l13+l34-l14)*(l13+l14-l34))/4; double s234=Math.sqrt((l23+l24+l34)*(l23+l24-l34)*(l24+l34-l23)*(l23+l34-l24))/4; if(Math.abs(s123+s134-s124-s234)<=0.0001) { System.out.print("true "+doubleFormat(l12+l23+l34+l14)+" "+doubleFormat(s123+s134)); } else{ if(x1==x2&&((x2>x1&&x4<x1)||(x2<x1&&x4>x1))){ System.out.print("false "+doubleFormat(l12+l23+l34+l14)+" "+doubleFormat(s123+s134) ); } else if(x1!=x2&&((k13*(x2-x1)+y1<y2)&&(k13*(x4-x1)+y1>y4))||((k13*(x2-x1)+y1>y2)&&(k13*(x4-x1)+y1<y4))){ System.out.print("false "+doubleFormat(l12+l23+l34+l14)+" "+doubleFormat(s123+s134)); } else { System.out.print("false "+doubleFormat(l12+l23+l34+l14)+" "+doubleFormat(s124+s234)); } } } else { System.out.print("not a quadrilateral"); } } else { System.out.print("points coincide"); } } else{ System.out.print("Wrong Format");} } else{ System.out.print("wrong number of points"); } } public static Double doubleFormat(double b) { DecimalFormat df = new DecimalFormat("#.000"); Double output = Double.valueOf(df.format(b)); return output; } } class get7 { public void get8(String a) { pan pa = new pan(); calculate cal = new calculate(); Fanhui f = new Fanhui(); int i = 0; int flag = 0; int flag1 = 0; double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; double x3 = 0; double y3 = 0; double x4 = 0; double y4 = 0; double x5 = 0; double y5 = 0; double x6 = 0; double y6 = 0; String pattern = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; for(i=0;i<a.length();i++) { char b = a.charAt(i); char t = a.charAt(i); if(b==',') { flag++; } if(t==' ') { flag1++; } } if(flag==6&&flag1==5) { if(a.matches(pattern)){ double[] aj = new double[12]; aj=f.fanhui(a); x1=aj[0]; y1=aj[1]; x2=aj[2]; y2=aj[3]; x3=aj[4]; y3=aj[5]; x4=aj[6]; y4=aj[7]; x5=aj[8]; y5=aj[9]; x6=aj[10]; y6=aj[11]; if(pa.panduan1(x1,y1,x2,y2)==1) { System.out.println("points coincide"); } else { if(pa.panduan(x3,y3,x4,y4,x5,y5)==1||pa.panduan(x3,y3,x4,y4,x6,y6)==1||pa.panduan(x3,y3,x5,y5,x6,y6)==1||pa.panduan(x4,y4,x5,y5,x6,y6)==1) { double k34=(y3-y4)/(x3-x4); double k45=(y4-y5)/(x4-x5); double k12=(y1-y2)/(x1-x2); double k56=(y5-y6)/(x5-x6); double k36=(y3-y6)/(x3-x6); if(pa.panduan(x3,y3,x4,y4,x5,y5)==1&&pa.panduan(x3,y3,x4,y4,x6,y6)==1&&pa.panduan(x3,y3,x5,y5,x6,y6)==1&&pa.panduan(x4,y4,x5,y5,x6,y6)==1) { if((x1==x2&&x3==x4&&x1==x3)||(x1==x2&&x4==x5&&x1==x4)||(x1==x2&&x5==x6&&x1==x5)||(x1==x2&&x3==x6&&x1==x6)) { System.out.println("The line is coincide with one of the lines"); } else if((k12==k34&&(y1-k12*x1)==(y3-k34*x3))||(k12==k45&&(y1-k12*x1)==(y4-k45*x4))||(k12==k56&&(y1-k12*x1)==(y5-k56*x5))||(k12==k36&&(y1-k12*x1)==(y6-k36*x6))) { System.out.println("The line is coincide with one of the lines"); } else if(x1==x2&&y1!=y2&&((x3>x1&&x4>x1&&x5>x1&&x6>x1)||(x3<x1&&x4<x1&&x5<x1&&x6<x1))){ System.out.println(0); } else if((k12*(x3-x1)+y1<y3&&k12*(x4-x1)+y1<y4&&k12*(x5-x1)+y1<y5&&k12*(x6-x1)+y1<y6)||(k12*(x3-x1)+y1>y3&&k12*(x4-x1)+y1>y4&&k12*(x5-x1)+y1>y5&&k12*(x6-x1)+y1>y6)){ System.out.println(0); } else if(x1==x2&&y1!=y2&&x3==x1&&((x4>x1&&x5>x1&&x6>x1)||(x4<x1&&x5<x1&&x6<x1))) { System.out.println(1); } else if(x1==x2&&y1!=y2&&x4==x1&&((x3>x1&&x5>x1&&x6>x1)||(x3<x1&&x5<x1&&x6<x1))) { System.out.println(1); } else if(x1==x2&&y1!=y2&&x5==x1&&((x3>x1&&x4>x1&&x6>x1)||(x3<x1&&x4<x1&&x6<x1))) { System.out.println(1); } else if(x1==x2&&y1!=y2&&x6==x1&&((x3>x1&&x4>x1&&x5>x1)||(x3<x1&&x4<x1&&x5<x1))) { System.out.println(1); } } } else { System.out.print("not a quadrilateral or triangle"); } } } else{ System.out.print("Wrong Format");} } else{ System.out.print("wrong number of points"); } } } class get9 { public void get10(String a) { calculate cal = new calculate(); pan pa = new pan(); Fanhui f = new Fanhui(); int i = 0; int flag = 0; int flag1 = 0; double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; double x3 = 0; double y3 = 0; double x4 = 0; double y4 = 0; double x5 = 0; double y5 = 0; String pattern = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; for(i=0;i<a.length();i++) { char b = a.charAt(i); char t = a.charAt(i); if(b==',') { flag++; } if(t==' ') { flag1++; } } if(flag==5&&flag1==4) { if(a.matches(pattern)){ double[] aj = new double[12]; aj=f.fanhui(a); x1=aj[0]; y1=aj[1]; x2=aj[2]; y2=aj[3]; x3=aj[4]; y3=aj[5]; x4=aj[6]; y4=aj[7]; x5=aj[8]; y5=aj[9]; if(pa.panduan(x2,y2,x3,y3,x4,y4)==1||pa.panduan(x2,y2,x3,y3,x5,y5)==1||pa.panduan(x2,y2,x4,y4,x5,y5)==1||pa.panduan(x3,y3,x4,y4,x5,y5)==1) { if(pa.panduan(x2,y2,x3,y3,x4,y4)==1&&((pa.panduan1(x2,y2,x5,y5)==1||pa.panduan1(x3,y3,x5,y5)==1||pa.panduan1(x4,y4,x5,y5)==1)||pa.panduan2(x2,y2,x4,y4,x5,y5)==1)) { cal.jisuan3(x1,y1,x2,y2,x3,y3,x4,y4); } else if(pa.panduan(x2,y2,x3,y3,x5,y5)==1&&((pa.panduan1(x2,y2,x4,y4)==1||pa.panduan1(x3,y3,x4,y4)==1||pa.panduan1(x5,y5,x4,y4)==1)||pa.panduan2(x3,y3,x5,y5,x4,y4)==1)) { cal.jisuan3(x1,y1,x2,y2,x3,y3,x5,y5); } else if(pa.panduan(x2,y2,x4,y4,x5,y5)==1&&((pa.panduan1(x2,y2,x3,y3)==1||pa.panduan1(x4,y4,x3,y3)==1||pa.panduan1(x5,y5,x3,y3)==1)||pa.panduan2(x2,y2,x4,y4,x3,y3)==1)) { cal.jisuan3(x1,y1,x2,y2,x4,y4,x5,y5); } else if(pa.panduan(x3,y3,x4,y4,x5,y5)==1&&((pa.panduan1(x3,y3,x2,y2)==1||pa.panduan1(x4,y4,x2,y2)==1||pa.panduan1(x5,y5,x2,y2)==1)||pa.panduan2(x3,y3,x5,y5,x2,y2)==1)) { cal.jisuan3(x1,y1,x3,y3,x4,y4,x5,y5); } else if(pa.panduan(x2,y2,x3,y3,x4,y4)==1&&pa.panduan(x2,y2,x3,y3,x5,y5)==1&&pa.panduan(x2,y2,x4,y4,x5,y5)==1&&pa.panduan(x3,y3,x4,y4,x5,y5)==1) { double l23=cal.jisuan(x2,y2,x3,y3); double l34=cal.jisuan(x3,y3,x4,y4); double l45=cal.jisuan(x4,y4,x5,y5); double l25=cal.jisuan(x2,y2,x5,y5); double l12=cal.jisuan(x1,y1,x2,y2); double l13=cal.jisuan(x1,y1,x3,y3); double l14=cal.jisuan(x1,y1,x4,y4); double l15=cal.jisuan(x1,y1,x5,y5); double l35=cal.jisuan(x3,y3,x5,y5); double s123=cal.jisuan2(l12,l13,l23); double s134=cal.jisuan2(l13,l14,l34); double s145=cal.jisuan2(l14,l15,l45); double s125=cal.jisuan2(l12,l15,l25); double s235=cal.jisuan2(l23,l25,l35); double s345=cal.jisuan2(l34,l35,l45); if(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123!=0&&s134!=0&&s145!=0&&s125!=0) { System.out.println("in the quadrilateral"); } else if((Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123==0&&s134!=0&&s145!=0&&s125!=0)||(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123!=0&&s134==0&&s145!=0&&s125!=0)||(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123!=0&&s134!=0&&s145==0&&s125!=0)||(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123!=0&&s134!=0&&s145!=0&&s125==0)) { System.out.println("on the quadrilateral"); } else if((Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123==0&&s134!=0&&s145!=0&&s125==0)||(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123==0&&s134==0&&s145!=0&&s125!=0)||(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123!=0&&s134==0&&s145==0&&s125!=0)||(Math.abs(s123+s134+s145+s125-s235-s345)<=0.0001&&s123!=0&&s134!=0&&s145==0&&s125==0)) { System.out.println("on the quadrilateral"); } else { System.out.println("outof the quadrilateral"); } } } else { System.out.print("not a quadrilateral or triangle"); } } else{ System.out.print("Wrong Format");} } else{ System.out.print("wrong number of points"); } } } class calculate{ public double jisuan(double x1,double y1,double x2,double y2) { return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } public double jisuan2(double l1,double l2,double l3) { return Math.sqrt((l1+l2+l3)*(l1+l2-l3)*(l2+l3-l1)*(l1+l3-l2))/4; } public void jisuan3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { double l23=jisuan(x2,y2,x3,y3); double l24=jisuan(x2,y2,x4,y4); double l34=jisuan(x3,y3,x4,y4); double l12=jisuan(x1,y1,x2,y2); double l13=jisuan(x1,y1,x3,y3); double l14=jisuan(x1,y1,x4,y4); double s123=jisuan2(l12,l13,l23); double s134=jisuan2(l13,l14,l34); double s124=jisuan2(l12,l14,l24); double s234=jisuan2(l23,l24,l34); if(Math.abs(s123+s134+s124-s234)<=0.0000001&&s123!=0&&s134!=0&&s124!=0) { System.out.println("in the triangle"); } else if((Math.abs(s123+s134+s124-s234)<=0.0000001&&s123!=0&&s134!=0&&s124==0)||(Math.abs(s123+s134+s124-s234)<=0.0000001&&s123!=0&&s134==0&&s124!=0)||(Math.abs(s123+s134+s124-s234)<=0.0000001&&s123==0&&s134!=0&&s124!=0)) { System.out.println("on the triangle"); } else if((Math.abs(s123+s134+s124-s234)<=0.0000001&&s123==0&&s134==0&&s124!=0)||(Math.abs(s123+s134+s124-s234)<=0.0000001&&s123!=0&&s134==0&&s124==0)||(Math.abs(s123+s134+s124-s234)<=0.0000001&&s123==0&&s134!=0&&s124==0)) { System.out.println("on the triangle"); } else { System.out.println("outof the triangle"); } } } class pan{ public int panduan(double x1,double y1,double x2,double y2,double x3,double y3) { double l1=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double l2=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); double l3=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); if((l1+l2>l3&&l1+l3>l2&&l3+l2>l1)&&(Math.abs(l1-l2)<l3&&Math.abs(l1-l3)<l2&&Math.abs(l3-l2)<l1)) { return 1;} else { return 0;} } public int panduan1(double x1,double y1,double x2,double y2) { if(x1==x2&&y1==y2) { return 1;} else { return 0;} } public int panduan2(double x1,double y1,double x2,double y2,double x3,double y3) { double k12=(y1-y2)/(x1-x2); if((x1==x2&&x3==x1&&y3>min(y1,y2)&&y3<max(y1,y2))||(x1!=x2&&k12*(x3-x1)+y1==y3&&x3<max(x1,x2)&&x3>min(x1,x2))) { return 1;} else { return 0;} } public double max(double a,double b) { if(a>b) { return a;} else { return b;} } public double min(double a,double b) { if(a>b) { return b;} else { return a;} } } class Fanhui{ public static double[] fanhui(String a) { double x1 = 0; double y1 = 0; double x2 = 0; double y2 = 0; double x3 = 0; double y3 = 0; double x4 = 0; double y4 = 0; double x5 = 0; double y5 = 0; double x6 = 0; double y6 = 0; int i=0; int j=0; int k=0; int l=0; int m=0; int ik=0; int kl=0; int ag=0; int gk=0; int abc=0; int adc=0; String pattern1 = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; String pattern2 = "(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d+)?"; for(i=0;i<a.length();i++) { char d = a.charAt(i); if(d==',') { String b = a.substring(0,i); x1=Double.parseDouble(b); break; } } for(j=i;j<a.length();j++) { char e = a.charAt(j); if(e==' ') { String d = a.substring(i+1,j); y1=Double.parseDouble(d); break; } } for(k=j;k<a.length();k++) { char gj = a.charAt(k); if(gj==',') { String f = a.substring(j+1,k); x2=Double.parseDouble(f); break; } } for(l=k;l<a.length();l++) { char q = a.charAt(l); if(q==' ') { String p = a.substring(k+1,l); y2=Double.parseDouble(p); break; } } for(m=l;m<a.length();m++) { char r = a.charAt(m); if(r==',') { String g = a.substring(l+1,m); x3=Double.parseDouble(g); break; } } for(ik=m;ik<a.length();ik++) { char u = a.charAt(ik); if(u==' ') { String kg = a.substring(m+1,ik); y3=Double.parseDouble(kg); break; } } for(kl=ik;kl<a.length();kl++) { char ui = a.charAt(kl); if(ui==',') { if(a.matches(pattern1)) { String qw = a.substring(ik+1,kl); x4=Double.parseDouble(qw); String uk = a.substring(kl+1,a.length()); y4=Double.parseDouble(uk); } else { String qw = a.substring(ik+1,kl); x4=Double.parseDouble(qw); } break; } } for(ag=kl;ag<a.length();ag++) { char ui = a.charAt(ag); if(ui==' ') { String uk = a.substring(kl+1,ag); y4=Double.parseDouble(uk); break; } } for(gk=ag;gk<a.length();gk++) { char ui = a.charAt(gk); if(ui==',') { if(a.matches(pattern2)) { String wr = a.substring(ag+1,gk); x5=Double.parseDouble(wr); String uk = a.substring(gk+1,a.length()); y5=Double.parseDouble(uk); } else { String wr = a.substring(ag+1,gk); x5=Double.parseDouble(wr); } break; } } for(abc=gk;abc<a.length();abc++) { char ui = a.charAt(abc); if(ui==' ') { String uk = a.substring(gk+1,abc); y5=Double.parseDouble(uk); break; } } for(adc=abc;adc<a.length();adc++) { char ui = a.charAt(adc); if(ui==',') { String wr = a.substring(abc+1,adc); x6=Double.parseDouble(wr); String ab = a.substring(adc+1,a.length()); y6=Double.parseDouble(ab); break; } } double[] ak = new double[12]; ak[0] = x1; ak[1] = y1; ak[2] = x2; ak[3] = y2; ak[4] = x3; ak[5] = y3; ak[6] = x4; ak[7] = y4; ak[8] = x5; ak[9] = y5; ak[10] = x6; ak[11] = y6; return ak; } }
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); InputData d = new InputData(); ParseInput.paseInput(s, d); int choice = d.getChoice(); ArrayList ps = d.getPoints(); switch (choice) { case 1: handle1(ps); break; case 2: handle2(ps); break; case 3: handle3(ps); break;} } public static void handle1(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon p = new Pentagon(ps.get(0), ps.get(1), ps.get(2),ps.get(3),ps.get(4)); System.out.println(p.isPentagon()); } public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon p = new Pentagon(ps.get(0), ps.get(1), ps.get(2),ps.get(3),ps.get(4)); OutFormat o = new OutFormat(); if(p.isPentagon()) { if(p.isao()) { System.out.println(p.isao()+" "+o.doubleFormat(p.getPerimeter())+" "+o.doubleFormat(p.getArea()));} else { System.out.println(p.isao()); } } else { System.out.println("not a pentagon"); } } public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 7); Line l = new Line(ps.get(0),ps.get(1)); Judge j = new Judge(ps.get(2), ps.get(3), ps.get(4),ps.get(5),ps.get(6)); int i = j.judge(); if(ps.get(0).equals(ps.get(1))) { System.out.println("points coincide"); } else { switch(i) { case 1: Pentagon p = new Pentagon(ps.get(2), ps.get(3), ps.get(4),ps.get(5),ps.get(6)); Line[] pl= p.getSideline(); if(l.isCoincide(pl[0])||l.isCoincide(pl[1])||l.isCoincide(pl[2])||l.isCoincide(pl[3])||l.isCoincide(pl[4])) { System.out.println("The line is coincide with one of the lines"); } else { } break; case 2: Quadrilateral q = j.getQuadrilateral(); Line[] ql= q.getSideline(); if(l.isCoincide(ql[0])||l.isCoincide(ql[1])||l.isCoincide(ql[2])||l.isCoincide(ql[3])) { System.out.println("The line is coincide with one of the lines"); } else { ArrayList<Point> q2=q.getIntersections(l); if(q2.size()==0) { System.out.println(0); } if(q2.size()==1) { System.out.println(1); } if(q2.size()==2) { double[] q3=q.calArea(q2.get(0), q2.get(1)); System.out.println(2+" "+q3[0]+" "+q3[1]); } } break; case 3: Triangle t = j.getTriangle(); Line[] tl= t.getSideline(); if(l.isCoincide(tl[0])||l.isCoincide(tl[1])||l.isCoincide(tl[2])) { System.out.println("The line is coincide with one of the lines"); } else { ArrayList<Point> t2=t.getIntersections(l); if(t2.size()==0) { System.out.println(0); } if(t2.size()==1) { System.out.println(1); } if(t2.size()==2) { double[] t3=t.calArea(t2.get(0), t2.get(1)); System.out.println(2+" "+t3[0]+" "+t3[1]); } } break; case 0: System.out.println("not a polygon"); break; } } } } class InputData { private int choice;;//用户输入的选择项 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 public int getChoice() { return choice; } public void setChoice(int choice) { this.choice = choice; } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { this.points.add(p); } } class Judge { private Point x; private Point y; private Point z; private Point m; private Point n; Pentagon p; Quadrilateral q1; Quadrilateral q2; Quadrilateral q3; Quadrilateral q4; Quadrilateral q5; Quadrilateral q6; Triangle t1; Triangle t2; Triangle t3; Triangle t4; Triangle t5; Triangle t6; Triangle t7; Triangle t8; Triangle t9; Triangle t10; Triangle t11; double s=0; public Judge(Point x,Point y,Point z,Point m,Point n) { this.x = x; this.y = y; this.z = z; this.m = m; this.n = n; } public int judge() { p = new Pentagon(x,y,z,m,n); s=p.getArea(); Quadrilateral q1 = new Quadrilateral(x,z,m,n); Quadrilateral q2 = new Quadrilateral(y,m,n,x); Quadrilateral q3 = new Quadrilateral(z,n,x,y); Quadrilateral q4 = new Quadrilateral(m,x,y,z); Quadrilateral q5 = new Quadrilateral(n,y,z,m); Triangle t1 = new Triangle(x,y,z); Triangle t2 = new Triangle(x,y,m); Triangle t3 = new Triangle(x,y,n); Triangle t4 = new Triangle(x,z,m); Triangle t5 = new Triangle(x,z,n); Triangle t6 = new Triangle(x,m,n); Triangle t7 = new Triangle(y,z,m); Triangle t8 = new Triangle(y,z,n); Triangle t9 = new Triangle(y,m,n); Triangle t10 = new Triangle(z,m,n); ArrayList<Quadrilateral> a = new ArrayList<Quadrilateral>(); a.add(q1);a.add(q2);a.add(q3);a.add(q4);a.add(q5); ArrayList<Quadrilateral> b = new ArrayList<Quadrilateral>(); for(int i = 0;i<a.size();i++) { if(a.get(i).isQuadrilateral()&&(a.get(i).getArea()==s)) { b.add(a.get(i)); q6=a.get(i); break; } } ArrayList<Triangle> c = new ArrayList<Triangle>(); c.add(t1);c.add(t2);c.add(t3);c.add(t4);c.add(t5);c.add(t6);c.add(t7);c.add(t8);c.add(t9);c.add(t10); ArrayList<Triangle> d = new ArrayList<Triangle>(); for(int i=0;i<c.size();i++) { if(c.get(i).isTriangle()&&(c.get(i).getArea()==s)) { d.add(c.get(i)); t11=c.get(i); break; } } if(p.isPentagon()) { return 1; } else if(b.size()!=0) { return 2; } else if(d.size()!=0) { return 3; } else { return 0; } } public Quadrilateral getQuadrilateral() { return q6; } public Triangle getTriangle() { return t11; } } class Line { private Point p1;//线上的第一个点 private Point p2;//线上的第二个点 public Line(Point p1, Point p2) { LineInputError.pointsCoincideError(p1, p2); this.p1 = p1; this.p2 = p2; } public Double getSlope() { return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); } public boolean isOnline(Point x) { if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) { return true; } Line l = new Line(p1, x); if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) { return true; } double b1 = l.getSlope(), b2 = this.getSlope(); return Math.abs(b1 - b2) < 0.00000000001; } public double getDistance(Point x) { double distY = p2.getY() - p1.getY(); double distX = p2.getX() - p1.getX(); return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX) / p1.getDistance(p2); } public boolean isBetween(Point x) { if (!this.isOnline(x)) { return false; } if (x.equals(p1) || x.equals(p2)) { return false; } double d = p2.getDistance(p1); boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d; return b; } public boolean isSameSide(Point x) { return isOnline(x) && !isBetween(x); } public Point getMiddlePoint() { Point p = new Point(); p.setX((p1.getX() + p2.getX()) / 2); p.setY((p1.getY() + p2.getY()) / 2); return p; } public Point getPointA() { return p1; } public Point getPointB() { return p2; } public double getAngle(Line l) { double k2 = getSlope(); double k1 = l.getSlope(); return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度 } public boolean isParallel(Line l) { Double b1 = this.getSlope(); Double b2 = l.getSlope(); if ((b1.isInfinite()) && (b2.isInfinite())) { return true; } else { return (this.getSlope().doubleValue() == l.getSlope().doubleValue()); } } public boolean isCoincide(Line l) { if (!this.isParallel(l)) { return false; } if (this.isOnline(l.p1)) { return true; } return false; } public Point getIntersection(Line l) { if (this.isParallel(l)) { return null; } if (p1.equals(l.p1) || p1.equals(l.p2)) { return p1; } if (p2.equals(l.p1) || p2.equals(l.p2)) { return p2; } Point p3 = l.p1, p4 = l.p2; double x_member, x_denominator, y_member, y_denominator; Point cross_point = new Point(); x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y - p1.x * p3.y; x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x; if (x_denominator == 0) cross_point.x = 0; else cross_point.x = x_member / x_denominator; y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x - p1.y * p3.x; y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y; if (y_denominator == 0) cross_point.y = 0; else cross_point.y = y_member / y_denominator; return cross_point; // 平行返回(0,0) } public boolean Pointplace(Point x,Point y,Point z) { double k = (p1.y-p2.y)/(p1.x-p2.x); if((p1.x==p2.x)&&((x.x>p1.x&&y.x>p1.x&&z.x>p1.x)||(x.x<p1.x&&y.x<p1.x&&z.x<p1.x))) { return true; } else if((p1.x!=p2.x)&&(k*(x.x-p1.x)+p1.y<x.y)&&(k*(y.x-p1.x)+p1.y<y.y)&&(k*(z.x-p1.x)+p1.y<z.y)) { return true; } else if((p1.x!=p2.x)&&(k*(x.x-p1.x)+p1.y>x.y)&&(k*(y.x-p1.x)+p1.y>y.y)&&(k*(z.x-p1.x)+p1.y>z.y)) { return true; } else { return false; } } } class LineInputError { public static void pointsCoincideError(Point p1, Point p2) { if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) { System.out.println("points coincide"); System.exit(0); } } } class OutFormat { public static Double doubleFormat(double b) { DecimalFormat df = new DecimalFormat("#.000"); Double output = Double.valueOf(df.format(b)); return output; } } class ParseInput { public static void paseInput(String s, InputData d) { PointInputError.wrongChoice(s); d.setChoice(getChoice(s)); s = s.substring(2); pasePoints(s, d); } public static int getChoice(String s) { char c = s.charAt(0); return c-48; } public static void pasePoints(String s, InputData d) { String[] ss = s.split(" "); if (ss.length == 0) return; for (int i = 0; i < ss.length; i++) { d.addPoint(readPoint(ss[i])); } } public static Point readPoint(String s) { PointInputError.wrongPointFormat(s); String[] ss = s.split(","); double x = Double.parseDouble(ss[0]); double y = Double.parseDouble(ss[1]); return new Point(x, y); } } class Pentagon { private Point x; private Point y; private Point z; private Point m; private Point n; public Pentagon(Point x, Point y, Point z , Point m, Point n) { this.x = x; this.y = y; this.z = z; this.m = m; this.n = n; } public boolean isPentagon() { ArrayList<Point> ps = new ArrayList<Point>(); int flag=0; ps.add(x);ps.add(y);ps.add(z);ps.add(m);ps.add(n); for(int i=0;i<ps.size();i++) { for(int j=i+1;j<ps.size();j++) { if(ps.get(i).equals(ps.get(j))) { flag++; } } } Line[] l =getSideline(); boolean x1; boolean x2; boolean x3; boolean x4; boolean x5; if(l[0].isCoincide(l[1])) { x1=false; } else { x1=true; } if(l[1].isCoincide(l[2])) { x2=false; } else { x2=true; } if(l[2].isCoincide(l[3])) { x3=false; } else { x3=true; } if(l[3].isCoincide(l[4])) { x4=false; } else { x4=true; } if(l[4].isCoincide(l[0])) { x5=false; } else { x5=true; } boolean ju1; boolean ju2; boolean ju3; boolean ju4; boolean ju5; if(l[0].getIntersection(l[2])!=null) { ju1=last(l[0],l[2],l[0].getIntersection(l[2])); }else { ju1 = true; } if(l[1].getIntersection(l[3])!=null) { ju2=last(l[1],l[3],l[1].getIntersection(l[3])); }else { ju2 = true; } if(l[2].getIntersection(l[4])!=null) { ju3=last(l[2],l[4],l[2].getIntersection(l[4])); }else { ju3 = true; } if(l[3].getIntersection(l[0])!=null) { ju4=last(l[3],l[0],l[3].getIntersection(l[0])); }else { ju4 = true; } if(l[4].getIntersection(l[1])!=null) { ju5=last(l[4],l[1],l[4].getIntersection(l[1])); }else { ju5 = true; } if(flag==0&&x1&&x2&&x3&&x4&&x5&&ju1&&ju2&&ju3&&ju4&&ju5) { return true; } else { return false; } } public boolean isao() { Line l1 = new Line(x,y); Line l2 = new Line(y,z); Line l3 = new Line(z,m); Line l4 = new Line(m,n); Line l5 = new Line(n,x); if(l1.Pointplace(z, m, n)&&l2.Pointplace(x, m, n)&&l3.Pointplace(x, y, n)&&l4.Pointplace(x, y, z)&&l5.Pointplace(y, z, m)) { return true; } else{ return false; } } public double getArea() { Triangle t1 = new Triangle(x, y, m); Triangle t2 = new Triangle(y, z, m); Triangle t3 = new Triangle(m, n, x); return t1.getArea()+t2.getArea()+t3.getArea(); } public double getPerimeter() { double l1=Math.sqrt((this.x.getX()-this.y.getX())*(this.x.getX()-this.y.getX())+(this.x.getY()-this.y.getY())*(this.x.getY()-this.y.getY())); double l2=Math.sqrt((this.x.getX()-this.n.getX())*(this.x.getX()-this.n.getX())+(this.x.getY()-this.n.getY())*(this.x.getY()-this.n.getY())); double l3=Math.sqrt((this.y.getX()-this.z.getX())*(this.y.getX()-this.z.getX())+(this.y.getY()-this.z.getY())*(this.y.getY()-this.z.getY())); double l4=Math.sqrt((this.m.getX()-this.n.getX())*(this.m.getX()-this.n.getX())+(this.m.getY()-this.n.getY())*(this.m.getY()-this.n.getY())); double l5=Math.sqrt((this.m.getX()-this.z.getX())*(this.m.getX()-this.z.getX())+(this.m.getY()-this.z.getY())*(this.m.getY()-this.z.getY())); return (l1+l2+l3+l4+l5); } public int Pointplace(Point p) { Triangle t1 = new Triangle(x, y, p); Triangle t2 = new Triangle(y, z, p); Triangle t3 = new Triangle(z, m, p); Triangle t4 = new Triangle(m, n, p); Triangle t5 = new Triangle(n, x, p); if(((t1.getArea()+t2.getArea()+t3.getArea()+t4.getArea()+t5.getArea())==getArea())&&(t1.getArea()!=0&&t2.getArea()!=0&&t3.getArea()!=0&&t4.getArea()!=0&&t5.getArea()!=0)) { return 1; } else if(((t1.getArea()+t2.getArea()+t3.getArea()+t4.getArea()+t5.getArea())==getArea())&&(t1.getArea()==0||t2.getArea()==0||t3.getArea()==0||t4.getArea()==0||t5.getArea()==0)) { return 2; } else { return 3; } } public Line[] getSideline() { Line line1 = new Line(x, y); Line line2 = new Line(y, z); Line line3 = new Line(z, m); Line line4 = new Line(m, n); Line line5 = new Line(n, x); Line[] lines = { line1, line2, line3 ,line4,line5}; return lines; } public boolean last(Line l1,Line l2,Point p) { if(l1.isBetween(p)||l2.isBetween(p)) { return false; } else { return true; } } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public double getX() { return x; } public double getY() { return y; } public boolean equals(Point p) { boolean b = false; if(this.x==p.getX()&&this.y==p.getY()) { b=true; } return b; } public double getDistance(Point p) { return Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2)); } } class PointInputError { public static void wrongNumberOfPoints(ArrayList ps, int num) { if (ps.size() != num) { System.out.println("wrong number of points"); System.exit(0); } } public static void wrongPointFormat(String s) { if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0); } } public static void wrongChoice(String s) { if (!s.matches("[1-6]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class Quadrilateral { private Point x; private Point y; private Point z; private Point m; public Quadrilateral(Point x,Point y,Point z,Point m) { this.x = x; this.y = y; this.z = z; this.m = m; } public boolean isQuadrilateral() { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, y, m); Triangle t3 = new Triangle(y, z, m); Triangle t4 = new Triangle(x, z, m); if(t1.isTriangle()&&t2.isTriangle()&&t3.isTriangle()&&t4.isTriangle()) { return true; } else { return false; } } public boolean Pointplace(Point p) { Line l = new Line(x, y); if(p.equals(x)||p.equals(y)||p.equals(z)||p.equals(m)) { return true; } else if(l.isBetween(p)) { return true; } else { return false; } } public int isinside(Point p) { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, z, m); Triangle t3 = new Triangle(x, y, p); Triangle t4 = new Triangle(y, z, p); Triangle t5 = new Triangle(z, m, p); Triangle t6 = new Triangle(m, x, p); if(((t1.getArea()+t2.getArea())==(t3.getArea()+t4.getArea()+t5.getArea()+t6.getArea()))&&t3.getArea()!=0&&t4.getArea()!=0&&t5.getArea()!=0&&t6.getArea()!=0) { return 1; } if(((t1.getArea()+t2.getArea())==(t3.getArea()+t4.getArea()+t5.getArea()+t6.getArea()))&&(t3.getArea()==0||t4.getArea()==0||t5.getArea()==0||t6.getArea()==0)) { return 2; } else { return 3; } } public double getArea() { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, z, m); return (t1.getArea()+t2.getArea()); } public Line[] getSideline() { Line line1 = new Line(x, y); Line line2 = new Line(y, z); Line line3 = new Line(z, m); Line line4 = new Line(m, x); Line[] lines = { line1, line2, line3, line4 }; return lines; } public double[] calArea(Point p1,Point p2) { Line[] l = getSideline(); double s1=0; double s2=0; if((p1.equals(x)&&p2.equals(z))||(p1.equals(z)&&p2.equals(x))) { Triangle t = new Triangle(p1,y,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(m)&&p2.equals(y))||(p1.equals(y)&&p2.equals(m))) { Triangle t = new Triangle(p1,x,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(x)&&l[1].isBetween(p2))||(p2.equals(x)&&l[1].isBetween(p1))) { Triangle t = new Triangle(p1,y,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(x)&&l[2].isBetween(p2))||(p2.equals(x)&&l[2].isBetween(p1))) { Triangle t = new Triangle(p1,m,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(y)&&l[2].isBetween(p2))||(p2.equals(y)&&l[2].isBetween(p1))) { Triangle t = new Triangle(p1,z,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(y)&&l[3].isBetween(p2))||(p2.equals(y)&&l[3].isBetween(p1))) { Triangle t = new Triangle(p1,x,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(z)&&l[0].isBetween(p2))||(p2.equals(z)&&l[0].isBetween(p1))) { Triangle t = new Triangle(p1,y,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(z)&&l[3].isBetween(p2))||(p2.equals(z)&&l[3].isBetween(p1))) { Triangle t = new Triangle(p1,m,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(m)&&l[0].isBetween(p2))||(p2.equals(m)&&l[0].isBetween(p1))) { Triangle t = new Triangle(p1,x,p2); s1=t.getArea(); s2=getArea()-s1; } else if((p1.equals(m)&&l[1].isBetween(p2))||(p2.equals(m)&&l[1].isBetween(p1))) { Triangle t = new Triangle(p1,z,p2); s1=t.getArea(); s2=getArea()-s1; } else if((l[0].isBetween(p1)&&l[1].isBetween(p2))&&(l[0].isBetween(p2)&&l[1].isBetween(p1))) { Triangle t = new Triangle(p1,y,p2); s1=t.getArea(); s2=getArea()-s1; } else if((l[1].isBetween(p1)&&l[2].isBetween(p2))&&(l[1].isBetween(p2)&&l[2].isBetween(p1))) { Triangle t = new Triangle(p1,z,p2); s1=t.getArea(); s2=getArea()-s1; } else if((l[2].isBetween(p1)&&l[3].isBetween(p2))&&(l[2].isBetween(p2)&&l[3].isBetween(p1))) { Triangle t = new Triangle(p1,m,p2); s1=t.getArea(); s2=getArea()-s1; } else if((l[3].isBetween(p1)&&l[0].isBetween(p2))&&(l[3].isBetween(p2)&&l[0].isBetween(p1))) { Triangle t = new Triangle(p1,x,p2); s1=t.getArea(); s2=getArea()-s1; } else if(l[0].isBetween(p1)&&l[2].isBetween(p2)) { Triangle t = new Triangle(p1,y,p2); Triangle t1 = new Triangle(p2,z,y); s1=t.getArea()+t1.getArea(); s2=getArea()-s1; } else if((l[0].isBetween(p2)&&l[2].isBetween(p1))) { Triangle t = new Triangle(p1,y,p2); Triangle t1 = new Triangle(p1,z,y); s1=t.getArea()+t1.getArea(); s2=getArea()-s1; } else if(l[1].isBetween(p1)&&l[3].isBetween(p2)) { Triangle t = new Triangle(p1,m,p2); Triangle t1 = new Triangle(p2,z,m); s1=t.getArea()+t1.getArea(); s2=getArea()-s1; } else if((l[1].isBetween(p2)&&l[3].isBetween(p1))) { Triangle t = new Triangle(p1,m,p2); Triangle t1 = new Triangle(p1,z,m); s1=t.getArea()+t1.getArea(); s2=getArea()-s1; } double[] data = new double[2]; if(s1>=s2) { data[0]=s2;data[1]=s1; } if(s1<s2) { data[0]=s1;data[1]=s2; } return data; } public ArrayList<Point> getIntersections(Line l) { Line[] ql = getSideline(); ArrayList<Point> a = new ArrayList<Point>(); ArrayList<Point> b = new ArrayList<Point>(); a.add(ql[0].getIntersection(l));a.add(ql[1].getIntersection(l));a.add(ql[2].getIntersection(l));a.add(ql[3].getIntersection(l)); for(int i=0;i<a.size();i++) { if(a.get(i)==null) { a.remove(a.get(i)); } } for(int i=0;i<4;i++) { for(int j=0;j<a.size();j++) { if(ql[i].isBetween(a.get(j))) { b.add(a.get(j)); } } } for(int i=0;i<a.size();i++) { if(a.get(i).equals(x)||a.get(i).equals(y)||a.get(i).equals(z)||a.get(i).equals(m)) { b.add(a.get(i)); } } for(int i=0;i<b.size();i++) { for(int j=i+1;j<b.size();j++) { if(b.get(i).equals(b.get(j))) { b.remove(b.get(j)); } } } return b; } } class Triangle { private Point x; private Point y; private Point z; public Triangle(Point x, Point y, Point z) { this.x = x; this.y = y; this.z = z; } public boolean isTriangle() { if(x.getDistance(y)+x.getDistance(z)>y.getDistance(z)+0.0001&&x.getDistance(y)+y.getDistance(z)>x.getDistance(z)+0.0001&&x.getDistance(z)+y.getDistance(z)>x.getDistance(y)+0.0001) { return true; } return false; } public Point getMidpoint() { Point p = new Point(); p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3); p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3); return p; } public Line[] getSideline() { Line line1 = new Line(x, y); Line line2 = new Line(y, z); Line line3 = new Line(z, x); Line[] lines = { line1, line2, line3 }; return lines; } public double getArea() { return Math.abs((x.x*y.y+y.x*z.y+z.x*x.y-x.x*z.y-y.x*x.y-z.x*y.y)/2); } public double getPerimeter() { return x.getDistance(y) + y.getDistance(z) + z.getDistance(x); } public int isInside(Point p) { Triangle t1 = new Triangle(x,y,p); Triangle t2 = new Triangle(y,z,p); Triangle t3 = new Triangle(z,x,p); if((t1.getArea()+t2.getArea()+t3.getArea()==getArea())&&(t1.getArea()!=0&&t2.getArea()!=0&&t3.getArea()!=0)){ return 1; } else if((t1.getArea()+t2.getArea()+t3.getArea()==getArea())&&(t1.getArea()!=0||t2.getArea()!=0||t3.getArea()!=0)){ return 2; } else{ return 3; } } public ArrayList<Point> getIntersections(Line l) { ArrayList<Point> ps = new ArrayList<Point>(); ArrayList<Point> pa = new ArrayList<Point>(); Line[] L = getSideline(); ps.add(L[0].getIntersection(l)); ps.add(L[1].getIntersection(l)); ps.add(L[2].getIntersection(l)); for(int i=0;i<ps.size();i++) { if(ps.get(i)==null) { ps.remove(ps.get(i)); } } for(int i = 0;i<3;i++) { for(int j=0;j<ps.size();j++) { if(L[i].isBetween(ps.get(j))) { pa.add(ps.get(j)); } } } for(int i=0;i<ps.size();i++) { if((ps.get(i).equals(x))||(ps.get(i).equals(y))||(ps.get(i).equals(z))) { pa.add(ps.get(i)); } } for(int i = 0;i<pa.size();i++) { for(int j=i+1;j<pa.size();j++) { if(pa.get(i).equals(pa.get(j))) { pa.remove(j); } } } return pa; } public double[] calArea(Point p1, Point p2) { Line[] l = getSideline(); double s1=0; double s2=0; if(p1.equals(x)||p2.equals(x)) { Triangle t1 = new Triangle(p1,z,p2); s1=t1.getArea(); s2=getArea()-s1; } else if(p1.equals(y)||p2.equals(y)) { Triangle t1 = new Triangle(p1,x,p2); s1=t1.getArea(); s2=getArea()-s1; } else if(p1.equals(z)||p2.equals(z)) { Triangle t1 = new Triangle(p1,y,p2); s1=t1.getArea(); s2=getArea()-s1; } else if((l[0].isBetween(p1)&&l[1].isBetween(p2))||(l[0].isBetween(p2)&&l[1].isBetween(p1))) { Triangle t1 = new Triangle(p1,y,p2); s1=t1.getArea(); s2=getArea()-s1; } else if((l[0].isBetween(p1)&&l[2].isBetween(p2))||(l[0].isBetween(p2)&&l[2].isBetween(p1))) { Triangle t1 = new Triangle(p1,x,p2); s1=t1.getArea(); s2=getArea()-s1; } else if((l[1].isBetween(p1)&&l[2].isBetween(p2))||(l[1].isBetween(p2)&&l[2].isBetween(p1))) { Triangle t1 = new Triangle(p1,z,p2); s1=t1.getArea(); s2=getArea()-s1; } double[] data = new double[2]; if(s1>=s2) { data[0]=s2;data[1]=s1; } if(s1<s2) { data[0]=s1;data[1]=s2; } return data; } public boolean judgeLineCoincide(Line l) { return false; } }
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); InputData d = new InputData(); ParseInput.paseInput(s, d); int choice = d.getChoice(); ArrayList ps = d.getPoints(); switch (choice) { case 4: handle4(ps); break; case 5: handle5(ps); break; case 6: handle6(ps); break; } } public static void handle4(ArrayList<Point> ps) { } public static void handle5(ArrayList<Point> ps) { } public static void handle6(ArrayList<Point> ps) { Judge j = new Judge(ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5)); int i = j.judge(); switch(i) { case 1: Pentagon p = new Pentagon(ps.get(1), ps.get(2), ps.get(3),ps.get(4),ps.get(5)); if(p.Pointplace(ps.get(0))==1) { System.out.println("in the pentagon"); } if(p.Pointplace(ps.get(0))==2) { System.out.println("on the pentagon"); } if(p.Pointplace(ps.get(0))==3) { System.out.println("outof the pentagon"); } break; case 2: Quadrilateral q = j.getQuadrilateral(); if(q.isinside(ps.get(0))==1) { System.out.println("in the quadrilateral"); } if(q.isinside(ps.get(0))==2) { System.out.println("on the quadrilateral"); } if(q.isinside(ps.get(0))==3) { System.out.println("outof the quadrilateral"); } break; case 3: Triangle t = j.getTriangle(); if(t.isInside(ps.get(0))==1) { System.out.println("in the triangle"); } if(t.isInside(ps.get(0))==2) { System.out.println("on the triangle"); } if(t.isInside(ps.get(0))==3) { System.out.println("outof the triangle"); } break; case 0: System.out.println("not a polygon"); break; } } } class InputData { private int choice;;//用户输入的选择项 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 public int getChoice() { return choice; } public void setChoice(int choice) { this.choice = choice; } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { this.points.add(p); } } class Judge { private Point x; private Point y; private Point z; private Point m; private Point n; Pentagon p; Quadrilateral q1; Quadrilateral q2; Quadrilateral q3; Quadrilateral q4; Quadrilateral q5; Quadrilateral q6; Triangle t1; Triangle t2; Triangle t3; Triangle t4; Triangle t5; Triangle t6; Triangle t7; Triangle t8; Triangle t9; Triangle t10; Triangle t11; double s=0; public Judge(Point x,Point y,Point z,Point m,Point n) { this.x = x; this.y = y; this.z = z; this.m = m; this.n = n; } public int judge() { p = new Pentagon(x,y,z,m,n); s=p.getArea(); Quadrilateral q1 = new Quadrilateral(x,z,m,n); Quadrilateral q2 = new Quadrilateral(y,m,n,x); Quadrilateral q3 = new Quadrilateral(z,n,x,y); Quadrilateral q4 = new Quadrilateral(m,x,y,z); Quadrilateral q5 = new Quadrilateral(n,y,z,m); Triangle t1 = new Triangle(x,y,z); Triangle t2 = new Triangle(x,y,m); Triangle t3 = new Triangle(x,y,n); Triangle t4 = new Triangle(x,z,m); Triangle t5 = new Triangle(x,z,n); Triangle t6 = new Triangle(x,m,n); Triangle t7 = new Triangle(y,z,m); Triangle t8 = new Triangle(y,z,n); Triangle t9 = new Triangle(y,m,n); Triangle t10 = new Triangle(z,m,n); ArrayList<Quadrilateral> a = new ArrayList<Quadrilateral>(); a.add(q1);a.add(q2);a.add(q3);a.add(q4);a.add(q5); ArrayList<Quadrilateral> b = new ArrayList<Quadrilateral>(); for(int i = 0;i<a.size();i++) { if(a.get(i).isQuadrilateral()&&(a.get(i).getArea()==s)) { b.add(a.get(i)); q6=a.get(i); break; } } ArrayList<Triangle> c = new ArrayList<Triangle>(); c.add(t1);c.add(t2);c.add(t3);c.add(t4);c.add(t5);c.add(t6);c.add(t7);c.add(t8);c.add(t9);c.add(t10); ArrayList<Triangle> d = new ArrayList<Triangle>(); for(int i=0;i<c.size();i++) { if(c.get(i).isTriangle()&&(c.get(i).getArea()==s)) { d.add(c.get(i)); t11=c.get(i); break; } } if(p.isPentagon()) { return 1; } else if(b.size()!=0) { return 2; } else if(d.size()!=0) { return 3; } else { return 0; } } public Quadrilateral getQuadrilateral() { return q6; } public Triangle getTriangle() { return t11; } } class Line { private Point p1;//线上的第一个点 private Point p2;//线上的第二个点 public Line(double x1, double y1, double x2, double y2) { Point p1 = new Point(x1, y1); Point p2 = new Point(x2, y2); LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出 this.p1 = p1; this.p2 = p2; } public Line(Point p1, Point p2) { LineInputError.pointsCoincideError(p1, p2); this.p1 = p1; this.p2 = p2; } public Double getSlope() { return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); } public boolean isOnline(Point x) { if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) { return true; } Line l = new Line(p1, x); if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) { return true; } double b1 = l.getSlope(), b2 = this.getSlope(); return Math.abs(b1 - b2) < 0.00000000001;// b1==b2; } public double getDistance(Point x) { double distY = p2.getY() - p1.getY(); double distX = p2.getX() - p1.getX(); return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX) / p1.getDistance(p2); } public boolean isBetween(Point x) { if (!this.isOnline(x)) { return false; } if (x.equals(p1) || x.equals(p2)) { return false; } double d = p2.getDistance(p1); boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d; return b; } public boolean isSameSide(Point x) { return isOnline(x) && !isBetween(x); } public Point getMiddlePoint() { Point p = new Point(); p.setX((p1.getX() + p2.getX()) / 2); p.setY((p1.getY() + p2.getY()) / 2); return p; } public Point getPointA() { return p1; } public Point getPointB() { return p2; } public double getAngle(Line l) { double k2 = getSlope(); double k1 = l.getSlope(); return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度 } public boolean isParallel(Line l) { Double b1 = this.getSlope(); Double b2 = l.getSlope(); if ((b1.isInfinite()) && (b2.isInfinite())) { return true; } else { return (this.getSlope().doubleValue() == l.getSlope().doubleValue()); } } public boolean isCoincide(Line l) { if (!this.isParallel(l)) { return false; } if (this.isOnline(l.p1)) { return true; } return false; } public Point getIntersection(Line l) { if (this.isParallel(l)) { return null; } if (p1.equals(l.p1) || p1.equals(l.p2)) { return p1; } if (p2.equals(l.p1) || p2.equals(l.p2)) { return p2; } Point p3 = l.p1, p4 = l.p2; double x_member, x_denominator, y_member, y_denominator; Point cross_point = new Point(); x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y - p1.x * p3.y; x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x; if (x_denominator == 0) cross_point.x = 0; else cross_point.x = x_member / x_denominator; y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x - p1.y * p3.x; y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y; if (y_denominator == 0) cross_point.y = 0; else cross_point.y = y_member / y_denominator; return cross_point; // 平行返回(0,0) } public boolean Pointplace(Point x,Point y,Point z) { double k = (p1.y-p2.y)/(p1.x-p2.x); if((p1.x==p2.x)&&((x.x>p1.x&&y.x>p1.x&&z.x>p1.x)||(x.x<p1.x&&y.x<p1.x&&z.x<p1.x))) { return true; } else if((p1.x!=p2.x)&&(k*(x.x-p1.x)+p1.y<x.y)&&(k*(y.x-p1.x)+p1.y<y.y)&&(k*(z.x-p1.x)+p1.y<z.y)) { return true; } else if((p1.x!=p2.x)&&(k*(x.x-p1.x)+p1.y>x.y)&&(k*(y.x-p1.x)+p1.y>y.y)&&(k*(z.x-p1.x)+p1.y>z.y)) { return true; } else { return false; } } } class LineInputError { public static void pointsCoincideError(Point p1, Point p2) { if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) { System.out.println("points coincide"); System.exit(0); } } } class OutFormat { public static Double doubleFormat(double b) { DecimalFormat df = new DecimalFormat("#.000"); Double output = Double.valueOf(df.format(b)); return output; } } class ParseInput { public static void paseInput(String s, InputData d) { PointInputError.wrongChoice(s); d.setChoice(getChoice(s)); s = s.substring(2); pasePoints(s, d); } public static int getChoice(String s) { char c = s.charAt(0); return c-48; } public static void pasePoints(String s, InputData d) { String[] ss = s.split(" "); if (ss.length == 0) return; for (int i = 0; i < ss.length; i++) { d.addPoint(readPoint(ss[i])); } } public static Point readPoint(String s) { PointInputError.wrongPointFormat(s); String[] ss = s.split(","); double x = Double.parseDouble(ss[0]); double y = Double.parseDouble(ss[1]); return new Point(x, y); } } class Pentagon { private Point x; private Point y; private Point z; private Point m; private Point n; public Pentagon(Point x, Point y, Point z , Point m, Point n) { this.x = x; this.y = y; this.z = z; this.m = m; this.n = n; } public boolean isPentagon() { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, y, m); Triangle t3 = new Triangle(x, y, n); Triangle t4 = new Triangle(x, z, m); Triangle t5 = new Triangle(x, z, n); Triangle t6 = new Triangle(x, m, n); Triangle t7 = new Triangle(y, z, m); Triangle t8 = new Triangle(y, z, n); Triangle t9 = new Triangle(y, m, n); Triangle t10 = new Triangle(z, m, n); if(t1.isTriangle()&&t2.isTriangle()&&t3.isTriangle()&&t4.isTriangle()&&t5.isTriangle()&&t6.isTriangle()&&t7.isTriangle()&&t8.isTriangle()&&t9.isTriangle()&&t10.isTriangle()) { return true; } else { return false; } } public boolean isao() { Line l1 = new Line(x,y); Line l2 = new Line(y,z); Line l3 = new Line(z,m); Line l4 = new Line(m,n); Line l5 = new Line(n,x); if(l1.Pointplace(z, m, n)&&l2.Pointplace(x, m, n)&&l3.Pointplace(x, y, n)&&l4.Pointplace(x, y, z)&&l5.Pointplace(y, z, m)) { return true; } else{ return false; } } public double getArea() { Triangle t1 = new Triangle(x, y, m); Triangle t2 = new Triangle(y, z, m); Triangle t3 = new Triangle(m, n, x); return t1.getArea()+t2.getArea()+t3.getArea(); } public double getPerimeter() { double l1=Math.sqrt((this.x.getX()-this.y.getX())*(this.x.getX()-this.y.getX())+(this.x.getY()-this.y.getY())*(this.x.getY()-this.y.getY())); double l2=Math.sqrt((this.x.getX()-this.n.getX())*(this.x.getX()-this.n.getX())+(this.x.getY()-this.n.getY())*(this.x.getY()-this.n.getY())); double l3=Math.sqrt((this.y.getX()-this.z.getX())*(this.y.getX()-this.z.getX())+(this.y.getY()-this.z.getY())*(this.y.getY()-this.z.getY())); double l4=Math.sqrt((this.m.getX()-this.n.getX())*(this.m.getX()-this.n.getX())+(this.m.getY()-this.n.getY())*(this.m.getY()-this.n.getY())); double l5=Math.sqrt((this.m.getX()-this.z.getX())*(this.m.getX()-this.z.getX())+(this.m.getY()-this.z.getY())*(this.m.getY()-this.z.getY())); return (l1+l2+l3+l4+l5); } public int Pointplace(Point p) { Triangle t1 = new Triangle(x, y, p); Triangle t2 = new Triangle(y, z, p); Triangle t3 = new Triangle(z, m, p); Triangle t4 = new Triangle(m, n, p); Triangle t5 = new Triangle(n, x, p); double s1=t1.getArea()+t2.getArea()+t3.getArea()+t4.getArea()+t5.getArea(); double s2=getArea(); if((Math.abs(s1-s2)<=0.0001)&&(t1.getArea()!=0&&t2.getArea()!=0&&t3.getArea()!=0&&t4.getArea()!=0&&t5.getArea()!=0)) { return 1; } else if((Math.abs(s1-s2)<=0.0001)&&(t1.getArea()==0||t2.getArea()==0||t3.getArea()==0||t4.getArea()==0||t5.getArea()==0)) { return 2; } else { return 3; } } public Line[] getSideline() { Line line1 = new Line(x, y); Line line2 = new Line(y, z); Line line3 = new Line(z, m); Line line4 = new Line(m, n); Line line5 = new Line(n, x); Line[] lines = { line1, line2, line3 ,line4,line5}; return lines; } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } public double getX() { return x; } public double getY() { return y; } public boolean equals(Point p) { boolean b = false; if(this.x==p.getX()&&this.y==p.getY()) { b=true; } return b; } public double getDistance(Point p) { return Math.sqrt(Math.pow(this.x-p.getX(),2)+Math.pow(this.y-p.getY(),2)); } } class PointInputError { public static void wrongNumberOfPoints(ArrayList ps, int num) { if (ps.size() != num) { System.out.println("wrong number of points"); System.exit(0); } } public static void wrongPointFormat(String s) { if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { System.out.println("Wrong Format"); System.exit(0); } } public static void wrongChoice(String s) { if (!s.matches("[1-6]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class Quadrilateral { private Point x; private Point y; private Point z; private Point m; public Quadrilateral(Point x,Point y,Point z,Point m) { this.x = x; this.y = y; this.z = z; this.m = m; } public boolean isQuadrilateral() { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, y, m); Triangle t3 = new Triangle(y, z, m); Triangle t4 = new Triangle(x, z, m); if(t1.isTriangle()&&t2.isTriangle()&&t3.isTriangle()&&t4.isTriangle()) { return true; } else { return false; } } public boolean Pointplace(Point p) { Line l = new Line(x, y); if(p.equals(x)||p.equals(y)||p.equals(z)||p.equals(m)) { return true; } else if(l.isBetween(p)) { return true; } else { return false; } } public int isinside(Point p) { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, z, m); Triangle t3 = new Triangle(x, y, p); Triangle t4 = new Triangle(y, z, p); Triangle t5 = new Triangle(z, m, p); Triangle t6 = new Triangle(m, x, p); double s1=t1.getArea()+t2.getArea(); double s2=t3.getArea()+t4.getArea()+t5.getArea()+t6.getArea(); if((Math.abs(s1-s2)<=0.0001)&&t3.getArea()!=0&&t4.getArea()!=0&&t5.getArea()!=0&&t6.getArea()!=0) { return 1; } if((Math.abs(s1-s2)<=0.0001)&&(t3.getArea()==0||t4.getArea()==0||t5.getArea()==0||t6.getArea()==0)) { return 2; } else { return 3; } } public double getArea() { Triangle t1 = new Triangle(x, y, z); Triangle t2 = new Triangle(x, z, m); return (t1.getArea()+t2.getArea()); } public Line[] getSideline() { Line line1 = new Line(x, y); Line line2 = new Line(y, z); Line line3 = new Line(z, m); Line line4 = new Line(m, x); Line[] lines = { line1, line2, line3, line4 }; return lines; } } class Triangle { private Point x; private Point y; private Point z; public Triangle(Point x, Point y, Point z) { this.x = x; this.y = y; this.z = z; } public boolean isTriangle() { if(x.getDistance(y)+x.getDistance(z)>y.getDistance(z)+0.0001&&x.getDistance(y)+y.getDistance(z)>x.getDistance(z)+0.0001&&x.getDistance(z)+y.getDistance(z)>x.getDistance(y)+0.0001) { return true; } return false; } public Point getMidpoint() { Point p = new Point(); p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3); p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3); return p; } public Line[] getSideline() { Line line1 = new Line(x, y); Line line2 = new Line(y, z); Line line3 = new Line(z, x); Line[] lines = { line1, line2, line3 }; return lines; } public double getArea() { return Math.abs((x.x*y.y+y.x*z.y+z.x*x.y-x.x*z.y-y.x*x.y-z.x*y.y)/2); } public double getPerimeter() { return x.getDistance(y) + y.getDistance(z) + z.getDistance(x); } public int isInside(Point p) { Triangle t1 = new Triangle(x,y,p); Triangle t2 = new Triangle(y,z,p); Triangle t3 = new Triangle(z,x,p); double s1=t1.getArea()+t2.getArea()+t3.getArea(); double s2=getArea(); if((Math.abs(s1-s2)<=0.00001)&&(t1.getArea()!=0&&t2.getArea()!=0&&t3.getArea()!=0)){ return 1; } else if((Math.abs(s1-s2)<=0.00001)&&(t1.getArea()==0||t2.getArea()==0||t3.getArea()==0)){ return 2; } else{ return 3; } } }
3.踩坑心得
(1)期中考试中,我们需要特别注意输出的最后一行是不换行的。
(2)在四,五次大作业中,我们依然需要注意精度问题,同时各个功能的要求不同,同一个情况输出也有可能不同。例如无法构成多边形输出“not a polygon”,无法构成五边形输出“not a pentagon”。
(3)判断四边形可以任意三点构成三角形来判断,但是五边形不能当然地认为可以继承,五边形需要更精确的判断方法。
4.改进建议
(1) 回想算法是如何实现的,思考能否在不影响功能的情况下进一步缩减代码长度
5.总结
(1)通过这几次的作业,我对类的的理解进一步加深了。在三角形中,我虽然运用了类,但是却只是粗略的运用,并没有真正理解分类的含义。在五边形中,我将功能细分给了对应的类,简化了许多操作。
(2)学会了如何使用ArrayList类
                
            
        
浙公网安备 33010602011771号