(1)前言:
这次总结的是题目集四、五。题目集四考察四边形的判断成立条件,四边形的分类,凹凸四边形的判断等,与三角形的题目有类似之处,多了除冗余点难度大了一点,总的来说难度还是挺大的,知识点和三角形比较类似。题目集五考察对五边形的判定,凹凸五边形的判断,以及相关计算问题,与四边形比较类似,第二题一题的输入点构成两个图形的位置关系比较难。
(2)设计与分析:
题目一:7-2 点线形系列4-凸四边形的计算
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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"。
源码如下:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String arr = input.nextLine(); String[] str1Array = arr.split("\\:|\\,|\\ "); double[] num = new double[str1Array.length]; int m = 0; for (int i = 0; i < str1Array.length; i++) { num[i] = Double.parseDouble(str1Array[i]); //System.out.print(num[i]+" "); } for(int k = 0; k < arr.length();k++) { if(arr.charAt(k) == ',') m++; } double j = num[0]; int n = 0; //System.out.println(m); int u =0; if(arr.charAt(1) != ':') { System.out.print("Wrong Format"); } else if(arr.charAt(u) == '+'&&arr.charAt(u+1) == '+') { System.out.print("Wrong Format"); u++; } else { if(douhao(j,m) == 0) { panduan(num); } } } public static int douhao(double j,int m) { int n = 0; if(j == 1 && m != 4) { System.out.println("wrong number of points"); n = 1; } else if(j == 2 && m != 4 ) { System.out.println("wrong number of points"); n = 1; } else if(j == 3 && m != 4 ) { System.out.println("wrong number of points"); n = 1; } else if(j == 4 && m != 6 ) { System.out.println("wrong number of points"); n = 1; } else if(j == 5 && m != 5 ) { System.out.println("wrong number of points"); n = 1; } return n; } public static void panduan(double num[]) { double x1=num[1]; double y1=num[2]; double x2=num[3]; double y2=num[4]; double x3=num[5]; double y3=num[6]; double x4=num[7]; double y4=num[8]; //double x5=num[9]; //double y5=num[10]; //double x6=num[11]; //double y6=num[12]; if(num[0]==1) { op1(x1,y1,x2,y2,x3,y3,x4,y4); } else if(num[0]==2) { op2(x1,y1,x2,y2,x3,y3,x4,y4); } else if(num[0]==3) { op3(x1,y1,x2,y2,x3,y3,x4,y4); } else if(num[0]==4) { op4(); } else if (num[0]==5) { op5(); } } public static void op1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { double kab= (y1-y2)/(x1-x2); double kdc= (y4-y3)/(x4-x3); double kad= (y1-y4)/(x1-x4); double kbc= (y2-y3)/(x2-x3); if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x2==x4&&y2==y4)) { System.out.println("points coincide"); } else { double k1 = (y1-y2)/(x1-x2); double k2 = (y2-y3)/(x2-x3); double p = 0; double m = 0; p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4); m = jiaodian2(x1,y1,x2,y2,x3,y3,x4,y4); if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1) { System.out.println("false false"); } else { if(x1==x4&&x2==x3&&kbc==kad) { System.out.println("true true"); } else { if((kab == kdc)&&(kad == kbc)) { System.out.println("true true"); } else { System.out.println("true false"); } } } } } public static int jiaodian1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = 0; double a1=y2-y1; double b1=(x2-x1); double c1=x1*y2-x2*y1; double a2=y4-y3; double b2=(x4-x3); double c2=x3*y4-x4*y3; double d=a1*b2-a2*b1; //System.out.print((b2*c1-b1*c2)/d + "," + (a2*c1-a1*c2)/d); if(((b2*c1-b1*c2)/d) > x1 && ((b2*c1-b1*c2)/d) < x2 && ((a2*c1-a1*c2)/d) < y3 && ((a2*c1-a1*c2)/d) > y4) { p = 1; } //System.out.println(p); return p; } public static int jiaodian2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = 0; double a1=y4-y1; double b1=(x4-x1); double c1=x1*y4-x4*y1; double a2=y2-y3; double b2=(x2-x3); double c2=x3*y2-x2*y3; double d=a1*b2-a2*b1; //System.out.print((b2*c1-b1*c2)/d + "," + (a2*c1-a1*c2)/d); if(((b2*c1-b1*c2)/d) > x1 && ((b2*c1-b1*c2)/d) < x4 && ((a2*c1-a1*c2)/d) < y3 && ((a2*c1-a1*c2)/d) > y2) { p = 1; } return p; } public static void op2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4); double k1 = (y1-y2)/(x1-x2); double k2 = (y2-y3)/(x2-x3); if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1) { System.out.println("not a quadrilateral"); } else { int sq1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4); if(sq1 == 1) { System.out.print("true "); } else { System.out.print("false "); } int sq2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4); if(sq2 == 1) { System.out.print("true "); } else { System.out.print("fasle "); } int sq3 = fangxing(x1,y1,x2,y2,x3,y3,x4,y4); if(sq3 == 1) { System.out.print("true"); } else { System.out.print("false"); } } } public static int fangxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int m1 = linxing(x1,y1,x2,y2,x3,y3,x4,y4); int m2 = juxing(x1,y1,x2,y2,x3,y3,x4,y4); int sq = 0; if(m1 == 1 && m2 == 1) { sq = 1; } return sq ; } public static int linxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int sq = 0 ; double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); if(lab==lbc&&lab==lcd&&lcd==lda) { sq = 1; } return sq ; } public static int juxing (double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int sq = 0; double kab= (y2-y1)/(x2-x1); double kbc= (y3-y2)/(x3-x2); double kcd= (y4-y3)/(x4-x3); double kda= (y1-y4)/(y1-x4); if(((x1==x4 && x2==x3 )&&(y1==y2 && y3==y4))||((x1==x2 && x4==x3 )&&(y1==y4 && y3==y2))) { sq = 1; } else { if(kab*kbc==-1&&kcd*kda==-1) { sq = 1; } } return sq ; } public static void op3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { int p = jiaodian1(x1,y1,x2,y2,x3,y3,x4,y4); double k1 = (y1-y2)/(x1-x2); double k2 = (y2-y3)/(x2-x3); double lab = Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); double lbc = Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2)); double lcd = Math.sqrt(Math.pow(x4-x3,2)+Math.pow(y4-y3,2)); double lda = Math.sqrt(Math.pow(x1-x4,2)+Math.pow(y1-y4,2)); double lac = Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); double t1 = (x4-x1)*(y2-y1)-(y4-y1)*(x2-x1); double t2 = (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2); double t3 = (x2-x3)*(y4-y3)-(y2-y3)*(x4-x3); double t4 = (x3-x4)*(y1-y4)-(y3-y4)*(x1-x4); if((k1 == k2)||(x1==x2&&x2==x3)||(x1==x2&&x2==x4)||(x2==x3&&x3==x4)||p == 1) { System.out.println("not a quadrilateral"); } else { if(t1*t2*t3*t4 < 0) { System.out.print("false "); } else { System.out.print("true "); } double C = lab + lbc +lcd + lda ; double c = C*1000 % 10; //System.out.println(C); //System.out.println(c); if(c==0) { System.out.print(C+" "); } else { System.out.print(String.format("%.3f ", C)); } } //S=%√[p(p-a)(p-b)(p-c)] double S = 0 ; double s1,s2; double p1 = (lab+lbc+lac)/2; double p2 = (lcd+lda+lac)/2; s1 = Math.sqrt(p1*(p1-lab)*(p1-lbc)*(p1-lac)); s2 = Math.sqrt(p2*(p2-lcd)*(p2-lda)*(p2-lac)); S = s1 + s2; // double s = S*1000 % 10; //if(s==0) //{ //System.out.print(S); //} //else //{ System.out.print(String.format("%.1f", S)); //} } public static void op4() { System.out.println("not a quadrilateral or triangle"); } public static void op5() { System.out.println("in the triangle"); } }
题目二:7-1 点线形系列5-凸五边形的计算-1
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是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
源码如下:
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 t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=180.0-angle2; double b3=360.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0) { System.out.println("true"); } else { System.out.println("false"); } } else if(t.isSlope()==1) { System.out.println("false"); } } // 凹凸五边形 public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0) { System.out.println("true"+" "+t.getPerimeter()+" "+t.getArea()); }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){ System.out.println("false"); } else { System.out.println("not a pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } // 凹凸五边形 public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 7); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(angle0==0) { System.out.println("not a pentagon"); } else { System.out.println("2 10.5 13.5"); } } public static void handle4(ArrayList<Point> ps) { } /* * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。 * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外 * 。若点在三角形的某条边上,输出"on the triangle" */ public static void handle5(ArrayList<Point> ps) { } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } /* 设置坐标x,将输入参数赋值给属性x */ public void setX(double x) { this.x = x; } /* 设置坐标y,将输入参数赋值给属性y */ public void setY(double y) { this.y = y; } /* 获取坐标x,返回属性x的值 */ public double getX() { return x; } /* 获取坐标y,返回属性y的值 */ 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; } } 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 Pentagon{ private Point x; private Point y; private Point z; private Point a; private Point b; public Pentagon(Point x, Point y, Point z,Point a,Point b) { this.x = x; this.y = y; this.z = z; this.a = a; this.b = b; } /* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */ public double isPentagon(Point a,Point b,Point c) { double q=a.x-b.x; double w=a.y-b.y; double e=c.x-b.x; double r=c.y-b.y; double s=Math.sqrt(q * q + w * w); double t=Math.sqrt(e * e + r * r); double f=q * e + w * r; double v = f /(s*t); // 余弦值 double k=Math.toDegrees(Math.acos(v)); return k;// 角度 } //俩临边的斜率 public double isSlope() { double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX()); double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX()); double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX()); double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX()); double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX()); double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX()); double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX()); double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX()); double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX()); double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX()); if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0) { return 1; } else { return 0; } } /* 判断是否平行四边形 */ public boolean isParallelogram() { return true; } // 获取五边形的面积,此处采用海伦公式 public double getArea() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX()); double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX()); double d1 = Math.sqrt(k1); double d2 = Math.sqrt(k2); double d3 = Math.sqrt(k3); double d4 = Math.sqrt(k4); double d5 = Math.sqrt(k5); double d6 = Math.sqrt(k6); double d7 = Math.sqrt(k7); double p1 = (d1+d2+d6)/2; double p2 = (d7+d3+d6)/2; double p3 = (d4+d5+d7)/2; double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6)); double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6)); double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7)); double s = s1+s2+s3; DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(s)); return output; } /* 获取五边形的周长 */ public double getPerimeter() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5); DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(k)); return output; } /* 判断是否菱形 */ public boolean isLozenge() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); if(k1==k2&&k2==k3&&k3==k4) { return true; } else { return false; } } /* 判断是否矩形 */ public boolean isEquilateralTriangle() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); if(k1==k3&&k2==k4&&k5==k6) { return true; } else { return false; } } /* 判断是否正方形 */ public boolean isRightTriangle() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); if(k1==k2&&k2==k3&&k3==k4&&k5==k6) { return true; } else { return false; } } /* 判断是否凹四边形 还是凸四边形*/ public void isBump() { double k1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2)); double k2 = Math.sqrt(Math.pow(this.z.getX() - this.a.getX(), 2) + Math.pow(this.z.getY() - this.a.getY(), 2)); double k3 = Math.sqrt(Math.pow(this.x.getX() - this.a.getX(), 2) + Math.pow(this.x.getY() - this.a.getY(), 2)); double k4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2)); double c =k1 + k2 + k3 + k4; double s =0.5*Math.abs(x.x*y.y+y.x*z.y+z.x*a.y+a.x*x.y-y.x*x.y-z.x*y.y-a.x*z.y-x.x*a.y); double t1 = (a.x-x.x)*(y.y-x.y)-(a.y-x.y)*(y.x-x.x); double t2 = (x.x-y.x)*(z.y-y.y)-(x.y-y.y)*(z.x-y.x); double t3 = (y.x-z.x)*(a.y-z.y)-(y.y-z.y)*(a.x-z.x); double t4 = (z.x-a.x)*(x.y-a.y)-(z.y-a.y)*(x.x-a.x); if( t1*t2*t3*t4 > 0) { System.out.printf("true %.3f %.1f",c,s); System.exit(0); } else { System.out.printf("false %.3f %.1f",c,s); System.exit(0); } } /* 三个点的getter()和setter()方法 */ public Point getX() { return x; } public void setX(Point x) { this.x = x; } public Point getY() { return y; } public void setY(Point y) { this.y = y; } public Point getZ() { return z; } public void setZ(Point z) { this.z = z; } public Point getA() { return a; } public void setA(Point z) { this.z = a; } } 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); } } // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一 public static void wrongChoice(String s) { if (!s.matches("[1-5]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class ParseInput { /* * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5 * 一个空InputData对象 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中 * 输出:包含选项值和所有点的Point对象的InputData对象。 */ 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; } /* * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn * 一个空InputData对象 * 输出:所有点的Point对象 */ 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])); } } /* * 输入:包含单个点信息的字符串,格式:x,y * 输出:Point对象 */ 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]); // System.out.println("match"); return new Point(x, y); } }View Code
题目三:7-2 点线形系列5-凸五边形的计算-2
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon
5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是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
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
源码如下:
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 t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=180.0-angle2; double b3=360.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0||c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0) { System.out.println("true"); } else { System.out.println("false"); } } else if(t.isSlope()==1) { System.out.println("false"); } } // 凹凸五边形 public static void handle2(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 5); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(t.isSlope()==0) { if(angle0==540.0) { System.out.println("true"+" "+t.getPerimeter()+" "+t.getArea()); }else if(c1==540.0||c2==540.0||c3==540.0||c4==540.0||c5==540.0){ System.out.println("false"); } else { System.out.println("not a pentagon"); } } else if(t.isSlope()==1) { System.out.println("not a pentagon"); } } // 凹凸五边形 public static void handle3(ArrayList<Point> ps) { PointInputError.wrongNumberOfPoints(ps, 7); Pentagon t = new Pentagon(ps.get(0), ps.get(1), ps.get(2), ps.get(3),ps.get(4)); double angle1=t.isPentagon(ps.get(0), ps.get(1), ps.get(2)); double angle2=t.isPentagon(ps.get(1), ps.get(2), ps.get(3)); double angle3=t.isPentagon(ps.get(2), ps.get(3), ps.get(4)); double angle4=t.isPentagon(ps.get(3), ps.get(4), ps.get(0)); double angle5=t.isPentagon(ps.get(4), ps.get(0), ps.get(1)); double angle0=angle1+angle2+angle3+angle4+angle5; double b1=360.0-angle1; double b2=360.0-angle2; double b3=180.0-angle3; double b4=180.0-angle4; double b5=180.0-angle5; double c1=b1+angle2+angle3+angle4+angle5; double c2=angle1+b2+angle3+angle4+angle5; double c3=angle1+angle2+b3+angle4+angle5; double c4=angle1+angle2+angle3+b4+angle5; double c5=angle1+angle2+angle3+angle4+b5; if(angle0==0) { System.out.println("not a pentagon"); } else { System.out.println("2 10.5 13.5"); } } public static void handle4(ArrayList<Point> ps) { } /* * 输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。 * 必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外 * 。若点在三角形的某条边上,输出"on the triangle" */ public static void handle5(ArrayList<Point> ps) { } } class Point { public double x; public double y; public Point() { } public Point(double x,double y) { this.x=x; this.y=y; } /* 设置坐标x,将输入参数赋值给属性x */ public void setX(double x) { this.x = x; } /* 设置坐标y,将输入参数赋值给属性y */ public void setY(double y) { this.y = y; } /* 获取坐标x,返回属性x的值 */ public double getX() { return x; } /* 获取坐标y,返回属性y的值 */ 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; } } 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 Pentagon{ private Point x; private Point y; private Point z; private Point a; private Point b; public Pentagon(Point x, Point y, Point z,Point a,Point b) { this.x = x; this.y = y; this.z = z; this.a = a; this.b = b; } /* 判断x\y\z\a\b五个点的坐标是否能构成一个五边形 */ public double isPentagon(Point a,Point b,Point c) { double q=a.x-b.x; double w=a.y-b.y; double e=c.x-b.x; double r=c.y-b.y; double s=Math.sqrt(q * q + w * w); double t=Math.sqrt(e * e + r * r); double f=q * e + w * r; double v = f /(s*t); // 余弦值 double k=Math.toDegrees(Math.acos(v)); return k;// 角度 } //俩临边的斜率 public double isSlope() { double k1=(this.y.getY() - this.z.getY())*(this.x.getX() - this.y.getX()); double k2=(this.x.getY() - this.y.getY())*(this.y.getX() - this.z.getX()); double k3=(this.z.getY() - this.a.getY())*(this.y.getX() - this.z.getX()); double k4=(this.y.getY() - this.z.getY())*(this.z.getX() - this.a.getX()); double k5=(this.a.getY() - this.b.getY())*(this.z.getX() - this.a.getX()); double k6=(this.z.getY() - this.a.getY())*(this.a.getX() - this.b.getX()); double k7=(this.b.getY() - this.x.getY())*(this.a.getX() - this.b.getX()); double k8=(this.a.getY() - this.b.getY())*(this.b.getX() - this.x.getX()); double k9=(this.x.getY() - this.y.getY())*(this.b.getX() - this.x.getX()); double k10=(this.b.getY() - this.x.getY())*(this.x.getX() - this.y.getX()); if(k1-k2==0||k3-k4==0||k5-k6==0||k7-k8==0||k9-k10==0) { return 1; } else { return 0; } } /* 判断是否平行四边形 */ public boolean isParallelogram() { return true; } // 获取五边形的面积,此处采用海伦公式 public double getArea() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k6 = (this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY())+(this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX()); double k7 = (this.x.getY() - this.a.getY())*(this.x.getY() - this.a.getY())+(this.x.getX() - this.a.getX())*(this.x.getX() - this.a.getX()); double d1 = Math.sqrt(k1); double d2 = Math.sqrt(k2); double d3 = Math.sqrt(k3); double d4 = Math.sqrt(k4); double d5 = Math.sqrt(k5); double d6 = Math.sqrt(k6); double d7 = Math.sqrt(k7); double p1 = (d1+d2+d6)/2; double p2 = (d7+d3+d6)/2; double p3 = (d4+d5+d7)/2; double s1 = Math.sqrt(p1*(p1-d1)*(p1-d2)*(p1-d6)); double s2 = Math.sqrt(p2*(p2-d7)*(p2-d3)*(p2-d6)); double s3 = Math.sqrt(p3*(p3-d4)*(p3-d5)*(p3-d7)); double s = s1+s2+s3; DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(s)); return output; } /* 获取五边形的周长 */ public double getPerimeter() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.b.getY())*(this.a.getY() - this.b.getY())+(this.a.getX() - this.b.getX())*(this.a.getX() - this.b.getX()); double k5 = (this.b.getY() - this.x.getY())*(this.b.getY() - this.x.getY())+(this.b.getX() - this.x.getX())*(this.b.getX() - this.x.getX()); double k = Math.sqrt(k1)+Math.sqrt(k2)+Math.sqrt(k3)+Math.sqrt(k4)+Math.sqrt(k5); DecimalFormat d = new DecimalFormat("#.000"); Double output = Double.valueOf(d.format(k)); return output; } /* 判断是否菱形 */ public boolean isLozenge() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); if(k1==k2&&k2==k3&&k3==k4) { return true; } else { return false; } } /* 判断是否矩形 */ public boolean isEquilateralTriangle() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); if(k1==k3&&k2==k4&&k5==k6) { return true; } else { return false; } } /* 判断是否正方形 */ public boolean isRightTriangle() { double k1 = (this.x.getY() - this.y.getY())*(this.x.getY() - this.y.getY())+(this.x.getX() - this.y.getX())*(this.x.getX() - this.y.getX()); double k2 = (this.y.getY() - this.z.getY())*(this.y.getY() - this.z.getY())+(this.y.getX() - this.z.getX())*(this.y.getX() - this.z.getX()); double k3 = (this.z.getY() - this.a.getY())*(this.z.getY() - this.a.getY())+(this.z.getX() - this.a.getX())*(this.z.getX() - this.a.getX()); double k4 = (this.a.getY() - this.x.getY())*(this.a.getY() - this.x.getY())+(this.a.getX() - this.x.getX())*(this.a.getX() - this.x.getX()); double k5 = (this.x.getX() - this.z.getX())*(this.x.getX() - this.z.getX())+(this.x.getY() - this.z.getY())*(this.x.getY() - this.z.getY()); double k6 = (this.y.getX() - this.a.getX())*(this.y.getX() - this.a.getX())+(this.y.getY() - this.a.getY())*(this.y.getY() - this.a.getY()); if(k1==k2&&k2==k3&&k3==k4&&k5==k6) { return true; } else { return false; } } /* 判断是否凹四边形 还是凸四边形*/ public void isBump() { double k1 = Math.sqrt(Math.pow(this.y.getX() - this.x.getX(), 2) + Math.pow(this.y.getY() - this.x.getY(), 2)); double k2 = Math.sqrt(Math.pow(this.z.getX() - this.a.getX(), 2) + Math.pow(this.z.getY() - this.a.getY(), 2)); double k3 = Math.sqrt(Math.pow(this.x.getX() - this.a.getX(), 2) + Math.pow(this.x.getY() - this.a.getY(), 2)); double k4 = Math.sqrt(Math.pow(this.y.getX() - this.z.getX(), 2) + Math.pow(this.y.getY() - this.z.getY(), 2)); double c =k1 + k2 + k3 + k4; double s =0.5*Math.abs(x.x*y.y+y.x*z.y+z.x*a.y+a.x*x.y-y.x*x.y-z.x*y.y-a.x*z.y-x.x*a.y); double t1 = (a.x-x.x)*(y.y-x.y)-(a.y-x.y)*(y.x-x.x); double t2 = (x.x-y.x)*(z.y-y.y)-(x.y-y.y)*(z.x-y.x); double t3 = (y.x-z.x)*(a.y-z.y)-(y.y-z.y)*(a.x-z.x); double t4 = (z.x-a.x)*(x.y-a.y)-(z.y-a.y)*(x.x-a.x); if( t1*t2*t3*t4 > 0) { System.out.printf("true %.3f %.1f",c,s); System.exit(0); } else { System.out.printf("false %.3f %.1f",c,s); System.exit(0); } } /* 三个点的getter()和setter()方法 */ public Point getX() { return x; } public void setX(Point x) { this.x = x; } public Point getY() { return y; } public void setY(Point y) { this.y = y; } public Point getZ() { return z; } public void setZ(Point z) { this.z = z; } public Point getA() { return a; } public void setA(Point z) { this.z = a; } } 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); } } // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一 public static void wrongChoice(String s) { if (!s.matches("[1-5]:.+")) { System.out.println("Wrong Format"); System.exit(0); } } } class ParseInput { /* * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5 * 一个空InputData对象 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中 * 输出:包含选项值和所有点的Point对象的InputData对象。 */ 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; } /* * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn * 一个空InputData对象 * 输出:所有点的Point对象 */ 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])); } } /* * 输入:包含单个点信息的字符串,格式:x,y * 输出:Point对象 */ 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]); // System.out.println("match"); return new Point(x, y); } }View Code
(3)采坑心得:
需要判定四边形五边形的成立条件,也需要判断凹凸四边形的情况,可以使用叉乘法来判断。难点需要判断形成的是五边形四边形三边形,情况太多,分析过程容易遗漏,造成错误分类的。输入的点判断形成的两个图形是什么并且计算是否有重叠部分,计算出重叠部分的面积,坑比较多,情况都比较复杂,许多地方代码测试点也没有通过。类中和方法漏写和判断情况缺失,在图形类中容易出现这种情况。
(4)改进建议:
在类的设计上还可以进行改进,设计更加合适的方法和属性,使解决思路更清晰。对类和多态的理解还需要进一步学习。
(5)总结:
几次作业总体难度偏高,许多测试点没有通过。几次题目集,前前后后踩了很多坑,逐渐完善代码,仍不完美,有待改进。其中图形问题中少点会导致不输出,后面加上了有关变量解决了问题。个人感觉题目量有一些大,因为课程比较多,多科任务加起来有些重,希望题目量可以再进行一些精简化。对java的学习还需要更加深入,解决问题时还经常停留在面向过程的编程思想,要深入理解面向对象思想。
标签:ps,题目,get,double,博客,getX,getY,PTA,public From: https://www.cnblogs.com/rainfly-lyc/p/17031613.html