nchu-software-oop-2022第4、5次作业分析及期中考试总结
直入主题
第四次:
7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)
//7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II import java.util.Scanner; import java.util.regex.Matcher; /*Matcher 类: Matcher 对象是对输入字符串进行解释和匹配操作的引擎。 与Pattern 类一样,Matcher 也没有公共构造方法。 你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。*/ import java.util.regex.Pattern; /*Pattern 类: pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。 要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法, 它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。*/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); Pattern p = Pattern.compile("[0-9]+");//Patter.comile(String regex) int sum;//每一行的总和 while (!str.equals("end")) { Matcher m = p.matcher(str);//Pattern.matcher(String) sum = 0; while (m.find()) { //find()有p内的正则内容则返回真 sum += Integer.parseInt(m.group()); } System.out.println(sum); str = in.nextLine();//下一行 } } }
这题比较简单,注意学习正则表达式。
7-2 点线形系列4-凸四边形的计算
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner in = new Scanner(System.in); String str; str = in.nextLine(); int i = (int)str.charAt(0)-48; if(i<1||i>5||str.charAt(1)!=':') { System.out.println("Wrong Format"); System.exit(0); } str=str.substring(2); if((!str.matches("((-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?),(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?))((\\s)(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?),(-|[+])?((([1-9]\\d*)|[0])(\\.\\d+)?))*\\s?"))) { System.out.println("Wrong Format"); System.exit(0); } switch(i) { case 1:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(\\.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(\\.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) { System.out.println("points coincide"); System.exit(0); } else System.out.println(q.isQuadrilateral()+" "+q.isParallelogram()); } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 2:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) System.out.println("points coincide"); else if(!q.isQuadrilateral()) System.out.println("not a quadrilateral"); else System.out.println(q.isDiamond()+" "+q.isRectangle()+" "+q.isSquare()); } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 3:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7])); if(q.isCoincide()) { System.out.println("points coincide"); System.exit(0); }else if(!q.isQuadrilateral()) System.out.println("not a quadrilateral"); else { if(q.isBump()) System.out.println(q.isBump()+" "+q.format(q.circumference())+" "+q.format(q.Area1())); else System.out.println(q.isBump()+" "+q.format(q.circumference())+" "+q.format(q.Area2())); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){3}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } break; } case 4:{ if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){5}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7]),Float.parseFloat(a[8]),Float.parseFloat(a[9]),Float.parseFloat(a[10]),Float.parseFloat(a[11])); if(a[0].equals(a[2])&&a[1].equals(a[3])) { System.out.println("points coincide"); System.exit(0); }else if(!q.isComposition()) { System.out.println("not a quadrilateral or triangle"); System.exit(0); }else if(q.isECoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3]))) { System.out.println("The line is coincide with one of the lines"); System.exit(0); } else if(q.isQuadrilateral()) { q.four(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3])); }else { q.tree(Float.parseFloat(a[0]),Float.parseFloat(a[1]),Float.parseFloat(a[2]),Float.parseFloat(a[3])); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){5}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); } break; } case 5:{//面积法 if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){4}")) { String[] a = str.split(",|\\s"); Quadrilateral q = new Quadrilateral(Float.parseFloat(a[2]),Float.parseFloat(a[3]),Float.parseFloat(a[4]),Float.parseFloat(a[5]),Float.parseFloat(a[6]),Float.parseFloat(a[7]),Float.parseFloat(a[8]),Float.parseFloat(a[9])); if(!q.isComposition()) System.out.println("not a quadrilateral or triangle"); else if(q.isQuadrilateral()&&q.isDCoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) //四边形点在边上 System.out.println("on the quadrilateral"); else if(!q.isQuadrilateral()&&q.isDCoincide(Float.parseFloat(a[0]),Float.parseFloat(a[1])))//三角形点在边上 System.out.println("on the triangle"); else if(q.isQuadrilateral()) { if(q.Area3(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) System.out.println("in the quadrilateral"); else System.out.println("outof the quadrilateral"); }else { if(q.Area3(Float.parseFloat(a[0]),Float.parseFloat(a[1]))) System.out.println("in the triangle"); else System.out.println("outof the triangle"); } } else { if(str.matches("((-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?))((\\s)(-|[+])?(([1-9]\\d*|[0])(.\\d+)?),(-|[+])?(([1-9]\\d*|[0])(.\\d+)?)){4}(\\s)")) { System.out.println("Wrong Format"); System.exit(0); } System.out.println("wrong number of points"); System.exit(0); } } break; } } } class Quadrilateral{ private float x1,y1,x2,y2,x3,y3,x4,y4; public Quadrilateral() { super(); // TODO 自动生成的构造函数存根 } public Quadrilateral(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { super(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.x3 = x3; this.y3 = y3; this.x4 = x4; this.y4 = y4; } public boolean isCoincide(){//是否重合 if((x1==x2&&y1==y2)||(x2==x3&&y2==y3)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1)) return true; return false; } public boolean isQuadrilateral() {//是否是四边形 Line a1 = new Line(x2,y2,x3,y3,x1,y1,x4,y4); Line a2 = new Line(x4,y4,x3,y3,x1,y1,x2,y2); if((x1==x3&&y1==y3)||(x2==x4&&y2==y4)||(x1==x2&&y1==y2)||(x4==x3&&y4==y3)||a1.isDot3()||a2.isDot3()) return false; else if((x1==x2&&x1==x3)||(x1==x2&&x1==x4)||(x1==x3&&x1==x4)||(x3==x2&&x3==x4)) return false; else if(Math.abs((y1-y2)/(x1-x2)-(y1-y3)/(x1-x3))<0.01||Math.abs((y1-y2)/(x1-x2)-(y1-y4)/(x1-x4))<0.01||Math.abs((y1-y4)/(x1-x4)-(y1-y3)/(x1-x3))<0.01||Math.abs((y3-y2)/(x3-x2)-(y2-y4)/(x2-x4))<0.01) return false; else return true; } public boolean isParallelogram() {//是否是平行四边形 if(isQuadrilateral()) { if((x1==x2&&x3==x4||(y1-y2)/(x1-x2)==(y4-y3)/(x4-x3))&&(x3==x2&&x1==x4||(y3-y2)/(x3-x2)==(y4-y1)/(x4-x1))) return true; else return false; } else return false; } public boolean isDiamond() {//是否是菱形 if(isParallelogram()) { if(((y1==y3&&x2==x4)||(y2==y4&&x1==x3))||(((y1-y3)/(x1-x3))*((y4-y2)/(x4-x2))==-1)) return true; else return false; } else return false; } public boolean isRectangle() {//是否是矩形 if(isParallelogram()) { if((x1==x2&&y2==y3)||(y1==y2&&x2==x3)||((y1-y2)/(x1-x2))*((y3-y2)/(x3-x2))==-1) return true; else return false; } else return false; } public boolean isSquare() {//是否是正方形 if(isRectangle()&&isDiamond()) return true; else return false; } public boolean isBump() {//凹为false 凸为true double x = -((x2-x4)*(x3*y1-x1*y3)-(x1-x3)*(x4*y2-x2*y4))/((y2-y4)*(x1-x3)-(y1-y3)*(x2-x4)); double y = -((y2-y4)*(y3*x1-y1*x3)-(y1-y3)*(y4*x2-y2*x4))/((x2-x4)*(y1-y3)-(x1-x3)*(y2-y4)); if ((((x>=x3&&x<=x1)||(x<=x3&&x>=x1))&&((y>=y3&&y<=y1)||(y<=y3&&y>=y1)))&&(((x>=x2&&x<=x4)||(x<=x2&&x>=x4))&&((y>=y2&&y<=y4)||(y<=y2&&y>=y4)))) return true; else return false; } public boolean isComposition() {//是否为三角形和四边形 if(isQuadrilateral()) return true; if((x1==x2&&y1==y2&&x3==x2&&y3==y2)||(x1==x2&&y1==y2&&x4==x2&&y4==y2)||(x1==x4&&y1==y4&&x3==x4&&y3==y4)||(x4==x2&&y4==y2&&x3==x4&&y3==y4)) return false; if((x1==x2&&x2==x3&&x3==x4)||(y1-y3)/(x1-x3)==(y2-y4)/(x2-x4)) return false; if((x1==x2&&y1==y2)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)||(x4==x1&&y4==y1)) return true; if((x1==x2&&x2==x3||Math.abs((y1-y2)/(x1-x2)-(y2-y3)/(x2-x3))<0.01)&&((x1>=x2&&x2>=x3)||x3>=x2&&x2>=x1)&&((y1>=y2&&y2>=y3)||y3>=y2&&y2>=y1)) return true; if((x1==x2&&x2==x4||Math.abs((y1-y2)/(x1-x2)-(y2-y4)/(x2-x4))<0.01)&&((x4>=x1&&x1>=x2)||x1>=x2&&x2>=x4)&&((y4>=y1&&y1>=y2)||y2>=y1&&y1>=y4)) return true; if((x4==x2&&x2==x3||Math.abs((y4-y2)/(x4-x2)-(y2-y3)/(x2-x3))<0.01)&&((x4>=x3&&x3>=x2)||x2>=x3&&x3>=x4)&&((y4>=y3&&y3>=y2)||y2>=y3&&y3>=y4)) return true; if((x1==x4&&x4==x3||Math.abs((y1-y4)/(x1-x4)-(y4-y3)/(x4-x3))<0.01)&&((x1>=x4&&x4>=x3)||x3>=x4&&x4>=x1)&&((y1>=y4&&y4>=y3)||y3>=y4&&y4>=y1)) return true; return false; } public boolean isECoincide(float x5,float y5,float x6,float y6) {//是否边重合 if((x1==x2&&x5==x6&&x2==x5&&y1!=y2)||(x3==x2&&x5==x6&&x2==x5&&y3!=y2)||(x3==x4&&x5==x6&&x4==x5&&y3!=y4)||(x1==x4&&x5==x6&&x4==x5&&y1!=y4)) return true; Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x4,y4); Line a4 = new Line(x5,y5,x6,y6,x4,y4,x1,y1); if( a1.isCoincide()||a2.isCoincide()||a3.isCoincide()||a4.isCoincide() ) return true; return false; } public boolean isDCoincide(float x5,float y5) {//是否三点共线 if((x1==x2&&x5==x1&&((x5>=x1&&x5<=x2)||(x5<=x1&&x5>=x2))&&((y5>=y1&&y5<=y2)||(y5<=y1&&y5>=y2)))||(x3==x2&&x5==x2&&((x5>=x3&&x5<=x2)||(x5<=x3&&x5>=x2))&&((y5>=y3&&y5<=y2)||(y5<=y3&&y5>=y2)))||(x3==x4&&x5==x3&&((x5>=x3&&x5<=x4)||(x5<=x3&&x5>=x4))&&((y5>=y3&&y5<=y4)||(y5<=y3&&y5>=y4)))||(x1==x4&&x5==x4&&((x5>=x1&&x5<=x4)||(x5<=x1&&x5>=x4))&&((y5>=y1&&y5<=y4)||(y5<=y1&&y5>=y4)))) return true; if((x1!=x2&&y1!=y2&&(y1-y2)/(x1-x2)==(y5-y1)/(x5-x1)&&((x5>=x1&&x5<=x2)||(x5<=x1&&x5>=x2))&&((y5>=y1&&y5<=y2)||(y5<=y1&&y5>=y2)))||(x3!=x2&&y3!=y2&&(y3-y2)/(x3-x2)==(y5-y2)/(x5-x2)&&((x5>=x3&&x5<=x2)||(x5<=x3&&x5>=x2))&&((y5>=y3&&y5<=y2)||(y5<=y3&&y5>=y2)))||(x3!=x4&&y3!=y4&&(y3-y4)/(x3-x4)==(y5-y3)/(x5-x3)&&((x5>=x3&&x5<=x4)||(x5<=x3&&x5>=x4))&&((y5>=y3&&y5<=y4)||(y5<=y3&&y5>=y4)))||(x1!=x4&&y1!=y4&&(y1-y4)/(x1-x4)==(y5-y4)/(x5-x4)&&((x5>=x1&&x5<=x4)||(x5<=x1&&x5>=x4))&&((y5>=y1&&y5<=y4)||(y5<=y1&&y5>=y4)))) return true; return false; } public void tree(float x5,float y5,float x6,float y6) { if((x1==x2&&y1==y2)) { x2=x4; y2=y4; } if((x1==x3&&y1==y3)||(x2==x3&&y2==y3)) { x3=x4; y3=y4; } if((x1==x2&&x2==x3||(y1-y2)/(x1-x2)==(y2-y3)/(x2-x3))&&((x1>=x2&&x2>=x3)||x3>=x2&&x2>=x1)&&((y1>=y2&&y2>=y3)||y3>=y2&&y2>=y1)) { x2=x4; y2=y4; } if((x1==x2&&x2==x4||(y1-y2)/(x1-x2)==(y2-y4)/(x2-x4))&&((x4>=x1&&x1>=x2)||x1>=x2&&x2>=x4)&&((y4>=y1&&y1>=y2)||y2>=y1&&y1>=y4)) { x1=x4; y1=y4; } if((x4==x2&&x2==x3||(y4-y2)/(x4-x2)==(y2-y3)/(x2-x3))&&((x4>=x3&&x3>=x2)||x2>=x3&&x3>=x4)&&((y4>=y3&&y3>=y2)||y2>=y3&&y3>=y4)) { x3=x4; y3=y4; } Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x1,y1); double s1,s2; if(a1.isCoincide()||a2.isCoincide()||a3.isCoincide()) System.out.println("The point is on the edge of the triangle"); else if((a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&!a3.isDot())||(a1.getxValue()==a3.getxValue()&&a1.getyValue()==a3.getyValue()&&!a2.isDot())||(a3.getxValue()==a2.getxValue()&&a3.getyValue()==a2.getyValue()&&!a1.isDot())) System.out.println(1); else if(!a1.isDot()&&!a2.isDot()&&!a3.isDot()) System.out.println(0); else { if(a1.isDot()&&a2.isDot()&&!a3.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x2,k3 = y2; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else if(a1.isDot()&&a3.isDot()&&!a2.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x1,k3 = y1; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else if(a2.isDot()&&a3.isDot()&&!a1.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x3,k3 = y3; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } else { if(a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&a3.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a3.getxValue(); float k2 = a3.getyValue(); float g3 = x1,k3 = y1; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } else if(a3.getxValue()==a2.getxValue()&&a3.getyValue()==a2.getyValue()&&a1.isDot()) { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x2,k3 = y2; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; }else { float g1 = a1.getxValue(); float k1 = a1.getyValue(); float g2 = a2.getxValue(); float k2 = a2.getyValue(); float g3 = x3,k3 = y3; s1=Area(g1,k1,g2,k2,g3,k3); s2=Area(x1,y1,x2,y2,x3,y3)-s1; } if(s1<=s2) System.out.println("2 " + new DecimalFormat("0.0##").format(s1) + " " + new DecimalFormat("0.0##").format(s2)); else System.out.println("2 " + new DecimalFormat("0.0##").format(s2) + " " + new DecimalFormat("0.0##").format(s1)); } } } public void four(float x5,float y5,float x6,float y6) {// 直线与四边形 Line a1 = new Line(x5,y5,x6,y6,x1,y1,x2,y2); Line a2 = new Line(x5,y5,x6,y6,x2,y2,x3,y3); Line a3 = new Line(x5,y5,x6,y6,x3,y3,x4,y4); Line a4 = new Line(x5,y5,x6,y6,x4,y4,x1,y1); double s1=0,u=0; if(!a1.isDot()&&!a2.isDot()&&!a3.isDot()&&!a4.isDot()) System.out.println(0); else if((a1.getxValue()==a2.getxValue()&&a1.getyValue()==a2.getyValue()&&!a3.isDot()&&!a4.isDot())||(a2.getxValue()==a3.getxValue()&&a2.getyValue()==a3.getyValue()&&!a1.isDot()&&!a4.isDot()) ||(a3.getxValue()==a4.getxValue()&&a3.getyValue()==a4.getyValue()&&!a1.isDot()&&!a4.isDot())||(a1.getxValue()==a4.getxValue()&&a1.getyValue()==a4.getyValue()&&!a2.isDot()&&!a3.isDot())) { System.out.println(1); }else { if(a3.isDot()&&a1.isDot()) { s1 =0.5 * Math.abs(x1 * a1.getyValue() + x4 * y1 + a1.getxValue() * y4 - x1 * y4 - x4 * a1.getyValue() -a1.getxValue() * y1) + 0.5 * Math.abs(x4 * a1.getyValue() + a3.getxValue() * y4 + a1.getxValue() * a3.getyValue() - x4 * a3.getyValue() - a3.getxValue() * a1.getyValue() -a1.getxValue() * y4); u=1; } if(u==1) { if(s1<Area1()-s1) System.out.println("2 "+format(s1)+" "+format(Area1()-s1)); else System.out.println("2 "+format(Area1()-s1)+" "+format(s1)); } } } public String format(double a) {//格式化 return new DecimalFormat("0.0##").format(a); } public double Area(float x1, float y1, float x2, float y2, float x3, float y3) {//三角形面积 return 0.5 * Math.abs(x1 * y3 + x2 * y1 + x3 * y2 - x1 * y2 - x2 * y3 -x3 * y1); } public double Area1() {//凸形面积 return 0.5*Math.abs((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2)); } public double Area2() {//凹形面积 return 0.5*Math.abs((x3-x1)*(y4-y2)-(y3-y1)*(x4-x2)); } public boolean Area3(float x5,float y5) {//面积是否相等 面积法 double a = 0.5 * Math.abs(x1 * y3 + x2 * y1 + x3 * y2 - x1 * y2 - x2 * y3 -x3 * y1) + 0.5 * Math.abs(x1 * y3 + x4 * y1 + x3 * y4 - x1 * y4 - x4 * y3 -x3 * y1); double b = 0.5 * Math.abs(x1 * y5 + x2 * y1 + x5 * y2 - x1 * y2 - x2 * y5 -x5 * y1) + 0.5 * Math.abs(x2 * y3 + x5 * y2 + x3 * y5 - x2 * y5 - x5 * y3 -x3 * y2) + 0.5 * Math.abs(x4 * y3 + x5 * y4 + x3 * y5 - x4 * y5 - x5 * y3 -x3 * y4) + 0.5 * Math.abs(x1 * y4 + x5 * y1 + x4 * y5 - x1 * y5 - x5 * y4 -x4 * y1); if(Math.abs(a-b)<0.01) return true; return false; } public double circumference() {//四边形周长 return Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5) + Math.pow((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2),0.5) + Math.pow((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4),0.5) +Math.pow((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4),0.5); } } class Line{ private float x1,x2,x3,x4,y1,y2,y3,y4; public Line(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) { super(); this.x1 = x1; this.x2 = x2; this.x3 = x3; this.x4 = x4; this.y1 = y1; this.y2 = y2; this.y3 = y3; this.y4 = y4; } public Line() { super(); // TODO 自动生成的构造函数存根 } boolean isDot() { double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01) return false; else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))) return true; else return false; } boolean isDot3() { double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); if(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01) return false; else if (((x>=x3&&x<=x4)||(x<=x3&&x>=x4))&&((y>=y3&&y<=y4)||(y<=y3&&y>=y4))&&((x>=x1&&x<=x2)||(x<=x1&&x>=x2))&&((y>=y1&&y<=y2)||(y<=y1&&y>=y2))) return true; else return false; } boolean isCoincide(){ if(Math.abs((y4-y3)*x1+(x3-x4)*y1+x4*y3-y4*x3)/Math.sqrt((y4-y3)*(y4-y3)+(x4-x3)*(x4-x3))<0.01&&(Math.abs((y1-y2)/(x1-x2)-(y3-y4)/(x3-x4))<0.01||((x1==x2)&&(x3==x4)))) return true; else return false; } float getxValue(){ return -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4)); } float getyValue(){ return -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4)); } }
这题是大作业的连续剧,比较难,再做之前要打好前面类设计的基础,多加类设计的练习,这次写的不好,并不规范。



还没学到类的继承,导致代码冗余量高。

凹四边形的面积算法不是很完整,有部分情况没有算出来。
7-3 设计一个银行业务类
这一次作业是面向样例编程,只有一个测试点,没难度
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); BankBusiness B = new BankBusiness(); B.welcome(); String name, passport; name = in.next(); passport = in.next(); B.kaihu(name, passport); // System.out.println(B.getName() + " " + B.getPassword()); passport = in.next(); float num = in.nextFloat(); B.deposit(passport, num); passport = in.next(); num = in.nextFloat(); B.withdraw(passport, num);//第一次 passport = in.next(); num = in.nextFloat(); B.withdraw(passport, num);//第二次 passport = in.next(); num = in.nextFloat(); B.withdraw(passport, num);//第三次 B.welcomeNext(); } } class BankBusiness { public static String bankName = "中国银行"; private String name, password; private float balance; public static void welcome() { System.out.println(bankName + "欢迎您的到来!"); } public static void welcomeNext() { System.out.println("请收好您的证件和物品,欢迎您下次光临!"); } public String getName() { return this.name; } public String getPassword() { return this.password; } void kaihu(String name, String password) { this.name = name; this.password = password; this.balance = 0; } public void deposit(String password, float num) { if (this.password.matches(password)) { this.balance += num; System.out.println("您的余额有" + this.balance + "元。"); } else { System.out.println("您的密码错误!"); } } public void withdraw(String password, float num) { if (this.password.matches(password)) { if (num > this.balance) { System.out.println("您的余额不足!"); } else { this.balance = this.balance - num; System.out.println("请取走钞票,您的余额还有" + this.balance + "元。"); } } else { System.out.println("您的密码错误!"); } } }
nchu-software-oop-2022-5
第五次大作业,太难了,前面的类和算法没设计好的话,这两题根本写不了。
7-1 点线形系列5-凸五边形的计算-1
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; import java.net.*; import java.text.*; public class Main{ static double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0,x5=0,y5=0,x6=0,y6=0,x7=0,y7=0; static Point t; public static void main(String[] args){ Scanner bjsn=new Scanner(System.in); String s=bjsn.nextLine(); int len=s.length(); int cnt=0; //基本格式判断 if(len==10){ System.out.println("Wrong Format"); return; } for(int i=0;i<len;i++){ if(s.charAt(i)==','){ cnt++; } } for(int i=1;i<len;i++){ if(s.charAt(i)==','&&s.charAt(i-1)==','){ System.out.println("Wrong Format"); return; } } if(cnt==0){ System.out.println("Wrong Format"); return; } if(s.charAt(0)<'1'||s.charAt(0)>'3'||s.charAt(1)!=':'||(s.charAt(len-1)<'0'||s.charAt(len-1)>'9')){ System.out.println("Wrong Format"); return; } int pos=2; if((s.charAt(pos)!='+'&&s.charAt(pos)!='-')&&(s.charAt(pos)<'0'||s.charAt(pos)>'9')){ System.out.println("Wrong Format"); return; } if(s.charAt(2)=='-'&&s.charAt(15)=='-'){ System.out.println("Wrong Format"); return; } String sss=s.substring(2,len); String ss[]=sss.split(" "); String str[]=null; for(String Str:ss){//正则输入法判断格式并提取数字 str=Str.split(","); for(String tt:str){ if(!tt.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")){ System.out.println("Wrong Format"); return; } } } Line line = new Line(); Trangle trangle = new Trangle(); Quadrilateral quadrilateral = new Quadrilateral(); Pentagon pentagon = new Pentagon(); switch (s.charAt(0)){ case '1': if(ss.length!=5) { if (cnt != ss.length) System.out.println("Wrong Format"); else System.out.println("wrong number of points"); return; } str=ss[0].split(",");x1=Double.parseDouble(str[0]);y1=Double.parseDouble(str[1]); str=ss[1].split(",");x2=Double.parseDouble(str[0]);y2=Double.parseDouble(str[1]); str=ss[2].split(",");x3=Double.parseDouble(str[0]);y3=Double.parseDouble(str[1]); str=ss[3].split(",");x4=Double.parseDouble(str[0]);y4=Double.parseDouble(str[1]); str=ss[4].split(",");x5=Double.parseDouble(str[0]);y5=Double.parseDouble(str[1]); if(line.cross(x1,y1,x2,y2,x3,y3,x4,y4)||line.cross(x1,y1,x2,y2,x4,y4,x5,y5)||line.cross(x2,y2,x3,y3,x4,y4,x5,y5)||line.cross(x2,y2,x3,y3,x1,y1,x5,y5)||line.cross(x3,y3,x4,y4,x1,y1,x5,y5)||line.cross(x3,y3,x4,y4,x1,y1,x2,y2)||line.cross(x4,y4,x5,y5,x1,y1,x2,y2)||line.cross(x4,y4,x5,y5,x2,y2,x3,y3)||line.cross(x5,y5,x1,y1,x2,y2,x3,y3)||line.cross(x5,y5,x1,y1,x3,y3,x4,y4)){ System.out.println("false");//只要有相交就不算 return; } if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)||line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)||line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)||line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)||line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ System.out.println("false");//只要有平行就不算 return; } System.out.println("true"); break; case '2': if(ss.length!=5) { if (cnt != ss.length) System.out.println("Wrong Format"); else System.out.println("wrong number of points"); return; } str=ss[0].split(",");x1=Double.parseDouble(str[0]);y1=Double.parseDouble(str[1]); str=ss[1].split(",");x2=Double.parseDouble(str[0]);y2=Double.parseDouble(str[1]); str=ss[2].split(",");x3=Double.parseDouble(str[0]);y3=Double.parseDouble(str[1]); str=ss[3].split(",");x4=Double.parseDouble(str[0]);y4=Double.parseDouble(str[1]); str=ss[4].split(",");x5=Double.parseDouble(str[0]);y5=Double.parseDouble(str[1]); if(line.cross(x1,y1,x2,y2,x3,y3,x4,y4)||line.cross(x1,y1,x2,y2,x4,y4,x5,y5)||line.cross(x2,y2,x3,y3,x4,y4,x5,y5)||line.cross(x2,y2,x3,y3,x1,y1,x5,y5)||line.cross(x3,y3,x4,y4,x1,y1,x5,y5)){ System.out.println("not a pentagon"); return; } if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)&&line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ System.out.println("not a pentagon"); return; } if((line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&!line.check(x1,y1,x2,y2,x3,y3))||(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&!line.check(x2,y2,x3,y3,x4,y4))||(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)&&!line.check(x3,y3,x4,y4,x5,y5))||(line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)&&!line.check(x4,y4,x5,y5,x1,y1))||(line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)&&!line.check(x5,y5,x1,y1,x2,y2))){ System.out.println("not a pentagon"); return; } if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)||line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)||line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)||line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)||line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ System.out.println("not a pentagon"); return; } if(line.isPointCoincide(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)>0){ System.out.println("not a pentagon"); return; }//枚举第五个点,看它是否在由前四个点组成的四边形中,如在则为凹四边形 if(pentagon.isTPentagon(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)&& pentagon.isTPentagon(x1,y1,x3,y3,x4,y4,x5,y5,x2,y2)&& pentagon.isTPentagon(x1,y1,x2,y2,x4,y4,x5,y5,x3,y3)&& pentagon.isTPentagon(x1,y1,x2,y2,x3,y3,x5,y5,x4,y4)&& pentagon.isTPentagon(x2,y2,x3,y3,x4,y4,x5,y5,x1,y1)){ double C=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3))+Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))+Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5))+Math.sqrt((x1-x5)*(x1-x5)+(y1-y5)*(y1-y5)); double S= trangle.qinjiushao(x1,y1,x2,y2,x3,y3)+ trangle.qinjiushao(x1,y1,x3,y3,x4,y4)+ trangle.qinjiushao(x1,y1,x4,y4,x5,y5); BigDecimal Cc= new BigDecimal(C); BigDecimal Ss= new BigDecimal(S); C=Cc.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); S=Ss.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println("true "+C+" "+S); } else{ System.out.println("false"); } break; case '3': if(ss.length!=7) { if (cnt != ss.length) System.out.println("Wrong Format"); else System.out.println("wrong number of points"); return; } str=ss[0].split(",");x6=Double.parseDouble(str[0]);y6=Double.parseDouble(str[1]); str=ss[1].split(",");x7=Double.parseDouble(str[0]);y7=Double.parseDouble(str[1]); str=ss[2].split(",");x1=Double.parseDouble(str[0]);y1=Double.parseDouble(str[1]); str=ss[3].split(",");x2=Double.parseDouble(str[0]);y2=Double.parseDouble(str[1]); str=ss[4].split(",");x3=Double.parseDouble(str[0]);y3=Double.parseDouble(str[1]); str=ss[5].split(",");x4=Double.parseDouble(str[0]);y4=Double.parseDouble(str[1]); str=ss[6].split(",");x5=Double.parseDouble(str[0]);y5=Double.parseDouble(str[1]); if(line.cross(x1,y1,x2,y2,x3,y3,x4,y4)||line.cross(x1,y1,x2,y2,x4,y4,x5,y5)||line.cross(x2,y2,x3,y3,x4,y4,x5,y5)||line.cross(x2,y2,x3,y3,x1,y1,x5,y5)||line.cross(x3,y3,x4,y4,x1,y1,x5,y5)||line.cross(x3,y3,x4,y4,x1,y1,x2,y2)||line.cross(x4,y4,x5,y5,x1,y1,x2,y2)||line.cross(x4,y4,x5,y5,x2,y2,x3,y3)||line.cross(x5,y5,x1,y1,x2,y2,x3,y3)||line.cross(x5,y5,x1,y1,x3,y3,x4,y4)){ System.out.println("not a pentagon"); return; } if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)&&line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ System.out.println("not a pentagon"); return; } if(line.isPointCoincide(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)>3){ System.out.println("not a pentagon"); return; } if(line.pointCoincide(x6,y6,x7,y7)){ System.out.println("points coincide"); return; } if(line.lineCoincide(x1,y1,x2,y2,x6,y6,x7,y7)||line.lineCoincide(x2,y2,x3,y3,x6,y6,x7,y7)||line.lineCoincide(x3,y3,x4,y4,x6,y6,x7,y7)||line.lineCoincide(x4,y4,x5,y5,x6,y6,x7,y7)||line.lineCoincide(x1,y1,x5,y5,x6,y6,x7,y7)){ System.out.println("The line is coincide with one of the lines"); return; } if((line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0&&line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x5,y5)<=0)||(line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0&&line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x5,y5)>=0)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x3,y3)<0&&line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x5,y5)<0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x3,y3)>0&&line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x5,y5)>0)) { System.out.println("0"); } else{ System.out.println("1"); } return; } if(line.isPointCoincide(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)==2||line.isPointCoincide(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)==3){ LinkedHashSet<Point>set = new LinkedHashSet<>(); set.add(new Point(x1,y1)); set.add(new Point(x2,y2)); set.add(new Point(x3,y3)); set.add(new Point(x4,y4)); set.add(new Point(x5,y5)); Point zb[] = new Point[3]; int posIndex=0; for(Point k:set){ zb[posIndex++]=k; } x1=zb[0].x;y1=zb[0].y; x2=zb[1].x;y2=zb[1].y; x3=zb[2].x;y3=zb[2].y; if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x1,y1,x2,y2,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x2,y2,x1,y1,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x2,y2,x1,y1,x6,y6,x7,y7); } } else if(line.isPointCoincide(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)==1){ LinkedHashSet<Point>set = new LinkedHashSet<>(); set.add(new Point(x1,y1)); set.add(new Point(x2,y2)); set.add(new Point(x3,y3)); set.add(new Point(x4,y4)); set.add(new Point(x5,y5)); Point zb[] = new Point[4]; int posIndex=0; for(Point k:set){ zb[posIndex++]=k; } x1=zb[0].x;y1=zb[0].y; x2=zb[1].x;y2=zb[1].y; x3=zb[2].x;y3=zb[2].y; x4=zb[3].x;y4=zb[3].y; if(line.numParad(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)==3){ if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0&&line.loc(x6,y6,x7,y7,x4,y4)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0&&line.loc(x6,y6,x7,y7,x4,y4)<=0)){ trangle.sTrangle(x1,y1,x3,y3,x4,y4,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x3,y3)<0&&line.loc(x6,y6,x7,y7,x4,y4)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x3,y3)>0&&line.loc(x6,y6,x7,y7,x4,y4)<=0)){ trangle.sTrangle(x3,y3,x1,y1,x4,y4,x6,y6,x7,y7); } else{ trangle.sTrangle(x4,y4,x3,y3,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x4,y4)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x4,y4)<=0)){ trangle.sTrangle(x1,y1,x2,y2,x4,y4,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x4,y4)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x4,y4)<=0)){ trangle.sTrangle(x2,y2,x1,y1,x4,y4,x6,y6,x7,y7); } else{ trangle.sTrangle(x4,y4,x2,y2,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x3,y3,x4,y4,x4,y4,x1,y1)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x1,y1,x2,y2,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x2,y2,x1,y1,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x2,y2,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x4,y4,x1,y1,x1,y1,x2,y2)){ if((line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x4,y4,x2,y2,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x2,y2,x4,y4,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x2,y2,x4,y4,x6,y6,x7,y7); } } } else { quadrilateral.getQua(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6,x7,y7); } } else{ if(line.numParad(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)==2){ if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x5,y5)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x5,y5)<=0)){ trangle.sTrangle(x1,y1,x4,y4,x5,y5,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x5,y5)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x5,y5)<=0)){ trangle.sTrangle(x4,y4,x1,y1,x5,y5,x6,y6,x7,y7); } else{ trangle.sTrangle(x5,y5,x4,y4,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x5,y5)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x5,y5)<=0)){ trangle.sTrangle(x1,y1,x2,y2,x5,y5,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x5,y5)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x5,y5)<=0)){ trangle.sTrangle(x2,y2,x1,y1,x5,y5,x6,y6,x7,y7); } else{ trangle.sTrangle(x5,y5,x2,y2,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x1,y1,x2,y2,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x2,y2,x1,y1,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x2,y2,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)&&line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ if((line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x4,y4,x2,y2,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x2,y2)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x2,y2)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x2,y2,x4,y4,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x2,y2,x4,y4,x6,y6,x7,y7); } } else if(line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)&&line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ if((line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x5,y5)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x5,y5)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x4,y4,x5,y5,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x5,y5)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x5,y5)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x5,y5,x4,y4,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x5,y5,x4,y4,x6,y6,x7,y7); } } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x5,y5)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x5,y5)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x1,y1,x5,y5,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x5,y5)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x5,y5)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x5,y5,x1,y1,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x5,y5,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x1,y1,x5,y5,x5,y5,x4,y4)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x1,y1,x4,y4,x3,y3,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x3,y3)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x3,y3)<=0)){ trangle.sTrangle(x4,y4,x1,y1,x3,y3,x6,y6,x7,y7); } else{ trangle.sTrangle(x3,y3,x4,y4,x1,y1,x6,y6,x7,y7); } } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x1,y1,x5,y5,x5,y5,x4,y4)){ if((line.loc(x6,y6,x7,y7,x1,y1)<0&&line.loc(x6,y6,x7,y7,x4,y4)>=0&&line.loc(x6,y6,x7,y7,x2,y2)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)>0&&line.loc(x6,y6,x7,y7,x4,y4)<=0&&line.loc(x6,y6,x7,y7,x2,y2)<=0)){ trangle.sTrangle(x1,y1,x4,y4,x2,y2,x6,y6,x7,y7); } else if((line.loc(x6,y6,x7,y7,x1,y1)>=0&&line.loc(x6,y6,x7,y7,x4,y4)<0&&line.loc(x6,y6,x7,y7,x2,y2)>=0)||(line.loc(x6,y6,x7,y7,x1,y1)<=0&&line.loc(x6,y6,x7,y7,x4,y4)>0&&line.loc(x6,y6,x7,y7,x2,y2)<=0)){ trangle.sTrangle(x4,y4,x1,y1,x2,y2,x6,y6,x7,y7); } else{ trangle.sTrangle(x2,y2,x4,y4,x1,y1,x6,y6,x7,y7); } } } else if(line.numParad(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5)==1){ if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ quadrilateral.getQua(x1,y1,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7); } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ quadrilateral.getQua(x1,y1,x2,y2,x4,y4,x5,y5,x6,y6,x7,y7); } else if(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ quadrilateral.getQua(x1,y1,x2,y2,x3,y3,x5,y5,x6,y6,x7,y7); } else if(line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)){ quadrilateral.getQua(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6,x7,y7); } else if(line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ quadrilateral.getQua(x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7); } } else{ pentagon.getPen(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7); } } } } } class Point{//点类 double x,y; public Point(double x,double y){ this.x=x; this.y=y; } @Override public boolean equals(Object t){//判重 if(this == t){ return true; } if(t == null || getClass() != t.getClass()){ return false; } Point point = (Point) t; return x == point.x && y == point.y &&Objects.equals(x,point.x) && Objects.equals(y,point.y); } @Override public int hashCode(){//重写hash函数 return Objects.hash(x,y); } } class Line{//线类 boolean check(double x1,double y1,double x2,double y2,double x3,double y3){//点是否在线段上 if(isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ if((x2>x1&&x3<x2)||(x2<x1&&x3>x2)){ return false; } } return true; } boolean cross(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//判断是否相交 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A34=y3-y4; double B34=x4-x3; double C34=x3*y4-x4*y3; double D=A12*B34-A34*B12; if(Math.abs(D)>=0.0000001){ double x=(B12*C34-B34*C12)/D; double y=(A34*C12-A12*C34)/D; if(x<Math.max(x1,x2)&&x>Math.min(x1,x2)&&y<Math.max(y1,y2)&&y>Math.min(y1,y2)){ return true; } } return false; } boolean isParad(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//是否平行 if((y2-y1)*(x4-x3)==(y4-y3)*(x2-x1)){ return true; } return false; } int numParad(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//平行边的组数 int cnt=0; if(isParad(x1,y1,x2,y2,x2,y2,x3,y3))cnt++; if(isParad(x2,y2,x3,y3,x3,y3,x4,y4))cnt++; if(isParad(x3,y3,x4,y4,x4,y4,x5,y5))cnt++; if(isParad(x4,y4,x5,y5,x5,y5,x1,y1))cnt++; if(isParad(x5,y5,x1,y1,x1,y1,x2,y2))cnt++; return cnt; } boolean pointCoincide(double x1,double y1,double x2,double y2){//两点是否重合 if(x1-x2==0&&y1-y2==0){ return true; } return false; } int isPointCoincide(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//重点组数 int cnt=0; if(pointCoincide(x1,y1,x2,y2))cnt++; if(pointCoincide(x1,y1,x3,y3))cnt++; if(pointCoincide(x1,y1,x4,y4))cnt++; if(pointCoincide(x1,y1,x5,y5))cnt++; if(pointCoincide(x2,y2,x3,y3))cnt++; if(pointCoincide(x2,y2,x4,y4))cnt++; if(pointCoincide(x2,y2,x5,y5))cnt++; if(pointCoincide(x3,y3,x4,y4))cnt++; if(pointCoincide(x3,y3,x5,y5))cnt++; if(pointCoincide(x4,y4,x5,y5))cnt++; return cnt; } int loc(double x1,double y1,double x2,double y2,double x3,double y3){//点与线的位置关系 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; if(Math.abs(A12*x3+B12*y3+C12)<0.0000001){ return 0; } else if(A12*x3+B12*y3+C12<0){ return -1; } else{ return 1; } } boolean lineCoincide(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//两线是否重合 if(isParad(x1, y1, x2, y2, x3, y3, x4, y4)){ if(loc(x1, y1, x2, y2, x3, y3)==0&&loc(x1, y1, x2, y2, x4, y4)==0&&!pointCoincide(x3,y3,x4,y4)&&!pointCoincide(x1,y1,x2,y2)){ return true; } } return false; } double pointDis(double x1,double y1,double x2,double y2,double x3,double y3){//点线距离 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double D=Math.sqrt(A12*A12+B12*B12); double res=(A12*x3+B12*y3+C12)/D; return res; } } class Trangle{//三角形类 void sTrangle(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//求并输出面积 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A13=y1-y3; double B13=x3-x1; double C13=x1*y3-x3*y1; double A45=y4-y5; double B45=x5-x4; double C45=x4*y5-x5*y4; double D12_45=A12*B45-A45*B12; double D13_45=A13*B45-A45*B12; double X12_45=(B12*C45-B45*C12)/D12_45; double Y12_45=(A45*C12-A12*C45)/D12_45; double X13_45=(B13*C45-B45*C13)/D13_45; double Y13_45=(A45*C13-A13*C45)/D13_45; double L14=Math.sqrt((X12_45-x1)*(X12_45-x1)+(Y12_45-y1)*(Y12_45-y1)); double L15=Math.sqrt((X13_45-x1)*(X13_45-x1)+(Y13_45-y1)*(Y13_45-y1)); double L12=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double L13=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); double f=L14*L15/(L12*L13); double S=qinjiushao(x1, y1, x2, y2, x3, y3); double rmin=Math.min(S*f,S-S*f); double rmax=Math.max(S*f,S-S*f); BigDecimal smn=new BigDecimal(rmin); BigDecimal smx=new BigDecimal(rmax); rmin=smn.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); rmax=smx.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println("2 "+rmin+" "+rmax); } double qinjiushao(double x1,double y1,double x2,double y2,double x3,double y3){//求面积 double a=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double b=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); double c=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); double p=(a+b+c)/2; double s=Math.sqrt(p*(p-a)*(p-b)*(p-c)); return s; } } class Quadrilateral{//四边形类 void getQua(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x6,double y6,double x7,double y7){//得到距离 Line line = new Line(); double[] X = new double[4]; double[] Y = new double[4]; double[] dis = new double[4]; double[] Dis = new double[4]; int[] loc = new int[4]; int[] pre = new int[4]; boolean[] used = new boolean[4]; dis[0] = line.pointDis(x6, y6, x7, y7, x1, y1); Dis[0] = line.pointDis(x6, y6, x7, y7, x1, y1); X[0] = x1; Y[0] = y1; dis[1] = line.pointDis(x6, y6, x7, y7, x2, y2); Dis[1] = line.pointDis(x6, y6, x7, y7, x2, y2); X[1] = x2; Y[1] = y2; dis[2] = line.pointDis(x6, y6, x7, y7, x3, y3); Dis[2] = line.pointDis(x6, y6, x7, y7, x3, y3); X[2] = x3; Y[2] = y3; dis[3] = line.pointDis(x6, y6, x7, y7, x4, y4); Dis[3] = line.pointDis(x6, y6, x7, y7, x4, y4); X[3] = x4; Y[3] = y4; Arrays.sort(dis); for (int i = 0; i < 4; i++) { loc[i] = 0; used[i]=false; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if(Dis[i] == dis[j] && !used[j]){ used[j]=true; loc[i]=j; pre[j]=i; break; } } } if(line.pointCoincide(X[pre[0]],Y[pre[0]],x1,y1)){ if(line.loc(x6,y6,x7,y7,x1,y1)*line.loc(x6,y6,x7,y7,x2,y2)>0){ getS(x1,y1,x4,y4,x3,y3,x2,y2,x6,y6,x7,y7); } else{ getS(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6,x7,y7); } } else if(line.pointCoincide(X[pre[0]],Y[pre[0]],x2,y2)){ if(line.loc(x6,y6,x7,y7,x2,y2)*line.loc(x6,y6,x7,y7,x3,y3)>0) { getS(x2, y2, x1, y1, x4, y4,x3, y3, x6, y6, x7, y7); } else{ getS(x2, y2, x3, y3, x4, y4, x1, y1, x6, y6, x7, y7); } } else if(line.pointCoincide(X[pre[0]],Y[pre[0]],x3,y3)){ if(line.loc(x6,y6,x7,y7,x3,y3)*line.loc(x6,y6,x7,y7,x4,y4)>0) { getS(x3, y3, x2, y2, x1, y1, x4, y4, x6, y6, x7, y7); } else{ getS(x3, y3, x4, y4, x1, y1, x2, y2, x6, y6, x7, y7); } } else{ if(line.loc(x6,y6,x7,y7,x3,y3)*line.loc(x6,y6,x7,y7,x4,y4)>0) { getS(x4, y4, x1, y1, x2, y2, x3, y3, x6, y6, x7, y7); } else{ getS(x4, y4, x3, y3, x2, y2, x1, y1, x6, y6, x7, y7); } } } void getS(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5,double x6,double y6){//求面积并输出 Trangle trangle=new Trangle(); double S=trangle.qinjiushao(x1, y1, x2, y2, x4, y4)+trangle.qinjiushao(x2, y2, x3, y3, x4, y4); Line line=new Line(); int loc1=line.loc(x5, y5, x6, y6, x1, y1); int loc2=line.loc(x5, y5, x6, y6, x2, y2); int loc3=line.loc(x5, y5, x6, y6, x3, y3); int loc4=line.loc(x5, y5, x6, y6, x4, y4); double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A14=y1-y4; double B14=x4-x1; double C14=x1*y4-x4*y1; double A23=y2-y3; double B23=x3-x2; double C23=x2*y3-x3*y2; double A34=y3-y4; double B34=x4-x3; double C34=x3*y4-x4*y3; double A56=y5-y6; double B56=x6-x5; double C56=x5*y6-x6*y5; double D56_12=A56*B12-A12*B56; double D56_14=A56*B14-A14*B56; double D56_23=A56*B23-A23*B56; double D56_34=A56*B34-A34*B56; double sl=0,sr=0; if((loc2<=0&&loc3>0&&loc4<=0)||(loc2>=0&&loc3<0&&loc4>=0)){ double X56_23=(B56*C23-B23*C56)/D56_23; double Y56_23=(A23*C56-A56*C23)/D56_23; double X56_34=(B56*C34-B34*C56)/D56_34; double Y56_34=(A34*C56-A56*C34)/D56_34; sl=trangle.qinjiushao(x3,y3,X56_23,Y56_23,X56_34,Y56_34); sr=S-sl; } else if((loc2<0&&loc3>=0)||(loc2>0&&loc3<=0)){ double X56_12=(B56*C12-B12*C56)/D56_12; double Y56_12=(A12*C56-A56*C12)/D56_12; double X56_23=(B56*C23-B23*C56)/D56_23; double Y56_23=(A23*C56-A56*C23)/D56_23; sl= trangle.qinjiushao(x2,y2,X56_12,Y56_12,X56_23,Y56_23); sr=S-sl; } else if((loc1<0&&loc2>0&&loc4<0&&loc3>0)||(loc1>0&&loc2<0&&loc3<0&&loc4>0)){ double X56_12=(B56*C12-B12*C56)/D56_12; double Y56_12=(A12*C56-A56*C12)/D56_12; double X56_34=(B56*C34-B34*C56)/D56_34; double Y56_34=(A34*C56-A56*C34)/D56_34; sl= trangle.qinjiushao(x2,y2,X56_12,Y56_12,X56_34,Y56_34)+ trangle.qinjiushao(x2,y2,x3,y3,X56_34,Y56_34); sr=S-sl; } else{ double X56_14=(B56*C14-B14*C56)/D56_14; double Y56_14=(A14*C56-A56*C14)/D56_14; double X56_12=(B56*C12-B12*C56)/D56_12; double Y56_12=(A12*C56-A56*C12)/D56_12; sl= trangle.qinjiushao(x1,y1,X56_12,Y56_12,X56_14,Y56_14); sr=S-sl; } double rmin=Math.min(sl,sr); double rmax=Math.max(sl,sr); BigDecimal smn = new BigDecimal(rmin); BigDecimal smx = new BigDecimal(rmax); rmin = smn.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); rmax = smx.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println("2 " + rmin + " " + rmax); } } class Pentagon {//五边形类 void getPen(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5,double x6,double y6,double x7,double y7){//求点距,同四边形 Line line = new Line(); double[] X = new double[5]; double[] Y = new double[5]; double[] dis = new double[5]; double[] Dis = new double[5]; int[] loc = new int[5]; int[] pre = new int[5]; boolean[] used = new boolean[5]; dis[0] = line.pointDis(x6, y6, x7, y7, x1, y1); Dis[0] = line.pointDis(x6, y6, x7, y7, x1, y1); X[0] = x1; Y[0] = y1; dis[1] = line.pointDis(x6, y6, x7, y7, x2, y2); Dis[1] = line.pointDis(x6, y6, x7, y7, x2, y2); X[1] = x2; Y[1] = y2; dis[2] = line.pointDis(x6, y6, x7, y7, x3, y3); Dis[2] = line.pointDis(x6, y6, x7, y7, x3, y3); X[2] = x3; Y[2] = y3; dis[3] = line.pointDis(x6, y6, x7, y7, x4, y4); Dis[3] = line.pointDis(x6, y6, x7, y7, x4, y4); X[3] = x4; Y[3] = y4; dis[4] = line.pointDis(x6, y6, x7, y7, x5, y5); Dis[4] = line.pointDis(x6, y6, x7, y7, x5, y5); X[4] = x5; Y[4] = y5; Arrays.sort(dis); for (int i = 0; i < 5; i++) { loc[i] = 0; used[i]=false; } for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if(Dis[i] == dis[j] && !used[j]){ used[j] = true; loc[i] = j; pre[j] = i; break; } } } if(line.pointCoincide(X[pre[0]],Y[pre[0]],x1,y1)){ getS(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7); } else if(line.pointCoincide(X[pre[0]],Y[pre[0]],x2,y2)){ getS(x2,y2,x3,y3,x4,y4,x5,y5,x1,y1,x6,y6,x7,y7); } else if(line.pointCoincide(X[pre[0]],Y[pre[0]],x3,y3)){ getS(x3,y3,x4,y4,x5,y5,x1,y1,x2,y2,x6,y6,x7,y7); } else if(line.pointCoincide(X[pre[0]],Y[pre[0]],x3,y3)){ getS(x4,y4,x5,y5,x1,y1,x2,y2,x3,y3,x6,y6,x7,y7); } else{ getS(x5,y5,x1,y1,x2,y2,x3,y3,x4,y4,x6,y6,x7,y7); } } void getS(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x5, double y5, double x6, double y6,double x7,double y7) {//求面积并输出 Line line = new Line(); Trangle trangle = new Trangle(); double S=trangle.qinjiushao(x1, y1, x2, y2, x3, y3) + trangle.qinjiushao(x1, y1, x3, y3, x4, y4) + trangle.qinjiushao(x1,y1,x4,y4,x5,y5); int loc1=line.loc(x6,y6,x7,y7,x1,y1); int loc2=line.loc(x6,y6,x7,y7,x2,y2); int loc3=line.loc(x6,y6,x7,y7,x3,y3); int loc4=line.loc(x6,y6,x7,y7,x4,y4); int loc5=line.loc(x6,y6,x7,y7,x5,y5); double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A45=y4-y5; double B45=x5-x4; double C45=x4*y5-x5*y4; double A23=y2-y3; double B23=x3-x2; double C23=x2*y3-x3*y2; double A34=y3-y4; double B34=x4-x3; double C34=x3*y4-x4*y3; double A15=y1-y5; double B15=x5-x1; double C15=x1*y5-x5*y1; double A67=y6-y7; double B67=x7-x6; double C67=x6*y7-x7*y6; double D67_12=A67*B12-A12*B67; double D67_23=A67*B23-A23*B67; double D67_34=A67*B34-A34*B67; double D67_45=A67*B45-A45*B67; double D67_15=A67*B15-A15*B67; double sl=0,sr=0; if((loc3<0&&loc4>=0&&loc2>=0)||(loc3>0&&loc4<=0&&loc2<=0)){ double X67_23=(B67*C23-B23*C67)/D67_23; double Y67_23=(A23*C67-A67*C23)/D67_23; double X67_34=(B67*C34-B34*C67)/D67_34; double Y67_34=(A34*B67-A67*B34)/D67_34; sl= trangle.qinjiushao(x3,y3,X67_23,Y67_23,X67_34,Y67_34); sr=S-sl; } else if((loc4<0&&loc3>=0&&loc5>=0)||(loc4>0&&loc3<=0&&loc5<=0)){ double X67_34=(B67*C34-B34*C67)/D67_34; double Y67_34=(A34*B67-A67*B34)/D67_34; double X67_45=(B67*C45-B45*C67)/D67_45; double Y67_45=(A45*C67-A67*C45)/D67_45; sl= trangle.qinjiushao(x4,y4,X67_34,Y67_34,X67_45,Y67_45); sr=S-sl; } else if((loc3<0&&loc4<0&&loc2>=0&&loc5>=0)||(loc3>0&&loc4>0&&loc5<=0&&loc2<=0)){ double X67_23=(B67*C23-B23*C67)/D67_23; double Y67_23=(A23*C67-A67*C23)/D67_23; double X67_45=(B67*C45-B45*C67)/D67_45; double Y67_45=(A45*C67-A67*C45)/D67_45; sl= trangle.qinjiushao(x3,y3,x4,y4,X67_23,Y67_23)+ trangle.qinjiushao(x4,y4,X67_23,Y67_23,X67_45,Y67_45); sr=S-sl; } else if((loc2<0&&loc4<0&&loc5>=0)||(loc2>0&&loc4>0&&loc5<=0)){ double X67_45=(B67*C45-B45*C67)/D67_45; double Y67_45=(A45*C67-A67*C45)/D67_45; double X67_12=(B67*C12-B12*C67)/D67_12; double Y67_12=(A12*C67-A67*C12)/D67_12; sl= trangle.qinjiushao(x1,y1,x5,y5,X67_12,Y67_12)+ trangle.qinjiushao(x5,y5,X67_12,Y67_12,X67_45,Y67_45); sr=S-sl; } else if((loc5<=0&&loc2>0)||(loc5>=0&&loc2<0)){ double X67_23=(B67*C23-B23*C67)/D67_23; double Y67_23=(A23*C67-A67*C23)/D67_23; double X67_15=(B67*C15-B15*C67)/D67_15; double Y67_15=(A15*C67-A67*C15)/D67_15; sl= trangle.qinjiushao(x1,y1,x2,y2,X67_15,Y67_15)+ trangle.qinjiushao(x2,y2,X67_15,Y67_15,X67_23,Y67_23); sr=S-sl; } else{ double X67_12=(B67*C12-B12*C67)/D67_12; double Y67_12=(A12*C67-A67*C12)/D67_12; double X67_15=(B67*C15-B15*C67)/D67_15; double Y67_15=(A15*C67-A67*C15)/D67_15; sl= trangle.qinjiushao(X67_12,Y67_12,X67_15,Y67_15,x1,y1); sr=S-sl; } double rmin=Math.min(sl,sr); double rmax=Math.max(sl,sr); BigDecimal smn = new BigDecimal(rmin); BigDecimal smx = new BigDecimal(rmax); rmin = smn.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); rmax = smx.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println("2 " + rmin + " " + rmax); } boolean isTPentagon(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x5, double y5) {//判断是否为凹五边形 Trangle trangle = new Trangle(); double S = trangle.qinjiushao(x1, y1, x2, y2, x3, y3) + trangle.qinjiushao(x1, y1, x4, y4, x3, y3); double SS = trangle.qinjiushao(x1, y1, x2, y2, x5, y5) + trangle.qinjiushao(x5, y5, x2, y2, x3, y3) + trangle.qinjiushao(x4, y4, x5, y5, x3, y3) + trangle.qinjiushao(x1, y1, x4, y4, x5, y5); if (Math.abs(S - SS) < 0.0000001 || S - SS > 0) { return false; } return true; } }
上代码上分析



分析得代码的平均复杂度仍处于高位,代码结构可进一步优化。
总结:一道非常考验人劝退的题,非常需要耐心分析设计,但用上类的方法以后,思路清晰了,避免了功能代码的冗余,减少代码行。
7-2 点线形系列5-凸五边形的计算-2
import java.math.BigDecimal; import java.util.*; import java.lang.*; public class Main{ Point t; public static void main(String[] args){ double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0,x5=0,y5=0,x6=0,y6=0,x7=0,y7=0,x8=0,y8=0,x9=0,y9=0,x10=0,y10=0; Scanner bjsn=new Scanner(System.in); String s=bjsn.nextLine(); int len=s.length(); int cnt=0; //基本格式判断 for(int i=0;i<len;i++){ if(s.charAt(i)==','){ cnt++; } } if(cnt==0){ System.out.println("Wrong Format"); return; } if(s.charAt(0)<'4'||s.charAt(0)>'6'||s.charAt(1)!=':'||(s.charAt(len-1)<'0'||s.charAt(len-1)>'9')){ System.out.println("Wrong Format"); return; } int pos=2; if((s.charAt(pos)!='+'&&s.charAt(pos)!='-')&&(s.charAt(pos)<'0'||s.charAt(pos)>'9')){ System.out.println("Wrong Format"); return; } String sss=s.substring(2,len); String ss[]=sss.split(" "); String str[]=null; for(String Str:ss){//正则输入法判断格式并提取数字 str=Str.split(","); for(String tt:str){ if(!tt.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")){ System.out.println("Wrong Format"); return; } } } Line line = new Line(); Triangle triangle = new Triangle(); Quadrilateral quadrilateral = new Quadrilateral(); Pentagon pentagon = new Pentagon(); switch (s.charAt(0)){ case '4'://位置关系判断 if(ss.length!=10) { if (cnt != ss.length) System.out.println("Wrong Format"); else System.out.println("wrong number of points"); return; } str=ss[0].split(",");x1=Double.parseDouble(str[0]);y1=Double.parseDouble(str[1]); str=ss[1].split(",");x2=Double.parseDouble(str[0]);y2=Double.parseDouble(str[1]); str=ss[2].split(",");x3=Double.parseDouble(str[0]);y3=Double.parseDouble(str[1]); str=ss[3].split(",");x4=Double.parseDouble(str[0]);y4=Double.parseDouble(str[1]); str=ss[4].split(",");x5=Double.parseDouble(str[0]);y5=Double.parseDouble(str[1]); str=ss[5].split(",");x6=Double.parseDouble(str[0]);y6=Double.parseDouble(str[1]); str=ss[6].split(",");x7=Double.parseDouble(str[0]);y7=Double.parseDouble(str[1]); str=ss[7].split(",");x8=Double.parseDouble(str[0]);y8=Double.parseDouble(str[1]); str=ss[8].split(",");x9=Double.parseDouble(str[0]);y9=Double.parseDouble(str[1]); str=ss[9].split(",");x10=Double.parseDouble(str[0]);y10=Double.parseDouble(str[1]); Point[] res1 = xingzhuang(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5); Point[] res2 = xingzhuang(x6,y6,x7,y7,x8,y8,x9,y9,x10,y10); int ans1=0,tot1=0,ans2=0,tot2=0,lco=0,cco=0,pco=0; for(int i=0;i< res1.length;i++){ for(int j=0;j< res2.length;j++){ if(line.lineCoincide(res1[i].x,res1[i].y,res1[(i+1)% res1.length].x,res1[(i+1)% res1.length].y,res2[j].x,res2[j].y,res2[(j+1)% res2.length].x,res2[(j+1)% res2.length].y)){ lco++; } if(line.cross1(res1[i].x,res1[i].y,res1[(i+1)% res1.length].x,res1[(i+1)% res1.length].y,res2[j].x,res2[j].y,res2[(j+1)% res2.length].x,res2[(j+1)% res2.length].y)){ cco++; } if(line.pointCoincide(res1[i].x,res1[i].y,res2[j].x,res2[j].y)){ pco++; } } } if(res1.length==3&&res2.length==3){//(3,3),(3,4),(3,5),(4,3),(4,4),(4,5),(5,3),(5,4),(5,5)9种情况遍历 for(int i = 0; i <= 2; i++){ if(triangle.inTriangle(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res1[i].x,res1[i].y)>0)ans1++; else if(triangle.inTriangle(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 2; i++){ if(triangle.inTriangle(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res2[i].x,res2[i].y)>0)ans2++; else if(triangle.inTriangle(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==3&&tot2==3)System.out.println("the previous triangle coincides with the following triangle"); else if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco>0)System.out.println("the previous triangle is connected to the following triangle");else if(cco>0)System.out.println("the previous triangle is interlaced with the following triangle");else System.out.println("no overlapping area between the previous triangle and the following triangle");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==3&&tot2==0)System.out.println("the previous triangle is inside the following triangle");else if(tot1==0&&tot2==3)System.out.println("the previous triangle contains the following triangle");else if(cco>0)System.out.println("the previous triangle is interlaced with the following triangle");else System.out.println("the previous triangle is connected to the following triangle");} else if(ans1+tot1==3){System.out.println("the previous triangle is inside the following triangle");} else if(ans2+tot2==3){System.out.println("the previous triangle contains the following triangle");} else {System.out.println("the previous triangle is interlaced with the following triangle");} } else if(res1.length==3&&res2.length==4){ for(int i = 0; i <= 2; i++){ if(quadrilateral.inQuarilateral(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res1[i].x,res1[i].y)>0)ans1++; else if(quadrilateral.inQuarilateral(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 3; i++){ if(triangle.inTriangle(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res2[i].x,res2[i].y)>0)ans2++; else if(triangle.inTriangle(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res2[i].x,res2[i].y)==0)tot2++; } if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco==0)System.out.println("the previous triangle is connected to the following quadrilateral");else if(cco>0){System.out.println("the previous triangle is interlaced with the following quadrilateral");}else System.out.println("no overlapping area between the previous triangle and the following quadrilateral");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==3&&tot2==0)System.out.println("the previous triangle is inside the following quadrilateral");else if(tot1==0&&tot2==4)System.out.println("the previous triangle contains the following quadrilateral");else if(cco>0)System.out.println("the previous triangle is interlaced with the following quadrilateral");else if(pco==3)System.out.println("the previous triangle is inside the following quadrilateral");else System.out.println("the previous triangle is connected to the following quadrilateral");} else if(ans1+tot1==3){System.out.println("the previous triangle is inside the following quadrilateral");} else if(ans2+tot2==4){System.out.println("the previous triangle contains the following quadrilateral");} else {System.out.println("the previous triangle is interlaced with the following quadrilateral");} } else if(res1.length==3&&res2.length==5){ for(int i = 0; i <= 2; i++){ if(pentagon.inPentagon(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res2[4].x,res2[4].y,res1[i].x,res1[i].y)>0)ans1++; else if(pentagon.inPentagon(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res2[4].x,res2[4].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 4; i++){ if(triangle.inTriangle(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res2[i].x,res2[i].y)>0)ans2++; else if(triangle.inTriangle(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res2[i].x,res2[i].y)==0)tot2++; } if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco==0)System.out.println("the previous triangle is connected to the following pentagon");else if(cco>0){System.out.println("the previous triangle is interlaced with the following pentagon");}else System.out.println("no overlapping area between the previous triangle and the following pentagon");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==3&&tot2==0)System.out.println("the previous triangle is inside the following pentagon");else if(tot1==0&&tot2==5)System.out.println("the previous triangle contains the following pentagon");else if(cco>0)System.out.println("the previous triangle is interlaced with the following pentagon");else if(pco==3)System.out.println("the previous triangle is inside the following pentagon");else System.out.println("the previous triangle is connected to the following pentagon");} else if(ans1+tot1==3){System.out.println("the previous triangle is inside the following pentagon");} else if(ans2+tot2==5){System.out.println("the previous triangle contains the following pentagon");} else {System.out.println("the previous triangle is interlaced with the following pentagon");} } else if(res1.length==4&&res2.length==3){ for(int i = 0; i <= 3; i++){ if(triangle.inTriangle(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res1[i].x,res1[i].y)>0)ans1++; else if(triangle.inTriangle(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 2; i++){ if(quadrilateral.inQuarilateral(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res2[i].x,res2[i].y)>0)ans2++; else if(quadrilateral.inQuarilateral(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==4&&tot2==3)System.out.println("the previous quadrilateral coincides with the following triangle"); else if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco>0)System.out.println("the previous quadrilateral is connected to the following triangle");else if(cco>0)System.out.println("the previous quadrilateral is interlaced with the following triangle");else System.out.println("no overlapping area between the previous quadrilateral and the following triangle");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==4&&tot2==0)System.out.println("the previous quadrilateral is inside the following triangle");else if(tot1==0&&tot2==3)System.out.println("the previous quadrilateral contains the following triangle");else if(cco>0)System.out.println("the previous quadrilateral is interlaced with the following triangle");else if(pco==3)System.out.println("the previous quadrilateral contains the following triangle");else System.out.println("the previous quadrilateral is connected to the following triangle");} else if(ans1+tot1==4){System.out.println("the previous quadrilateral is inside the following triangle");} else if(ans2+tot2==3){System.out.println("the previous quadrilateral contains the following triangle");} else {System.out.println("the previous quadrilateral is interlaced with the following triangle");} } else if(res1.length==4&&res2.length==4){ for(int i = 0; i <= 3; i++){ if(quadrilateral.inQuarilateral(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res1[i].x,res1[i].y)>0)ans1++; else if(quadrilateral.inQuarilateral(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 3; i++){ if(quadrilateral.inQuarilateral(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res2[i].x,res2[i].y)>0)ans2++; else if(quadrilateral.inQuarilateral(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==4&&tot2==4)System.out.println("the previous quadrilateral coincides with the following quadrilateral"); else if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco>0)System.out.println("the previous quadrilateral is connected to the following quadrilateral");else if(cco>0)System.out.println("the previous quadrilateral is interlaced with the following quadrilateral");else System.out.println("no overlapping area between the previous quadrilateral and the following quadrilateral");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==4&&tot2==0)System.out.println("the previous quadrilateral is inside the following quadrilateral");else if(tot1==0&&tot2==4)System.out.println("the previous quadrilateral contains the following quadrilateral");else if(cco>0)System.out.println("the previous quadrilateral is interlaced with the following quadrilateral");else System.out.println("the previous quadrilateral is connected to the following quadrilateral");} else if(ans1+tot1==4){System.out.println("the previous quadrilateral is inside the following quadrilateral");} else if(ans2+tot2==4){System.out.println("the previous quadrilateral contains the following quadrilateral");} else {System.out.println("the previous quadrilateral is interlaced with the following quadrilateral");} } else if(res1.length==4&&res2.length==5){ for(int i = 0; i <= 3; i++){ if(pentagon.inPentagon(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res2[4].x,res2[4].y,res1[i].x,res1[i].y)>0)ans1++; else if(pentagon.inPentagon(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res2[4].x,res2[4].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 4; i++){ if(quadrilateral.inQuarilateral(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res2[i].x,res2[i].y)>0)ans2++; else if(quadrilateral.inQuarilateral(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==4&&tot2==5)System.out.println("the previous quadrilateral is interlaced with the following pentagon"); else if((tot1==4&&tot2==4&&ans1==0&&ans2==0)||(tot1+ans1==4&&ans2==0)){System.out.println("the previous quadrilateral is inside the following pentagon");} else if(ans2+tot2==5&&ans1==0){System.out.println("the previous quadrilateral contains the following pentagon");} else if((pco==1&&ans1==0&&ans2==0&&tot1==1&&tot2==1)||(lco==1&&ans1==0&&ans2==0)){System.out.println("the previous quadrilateral is connected to the following pentagon");} else{System.out.println("the previous quadrilateral is interlaced with the following pentagon");} } else if(res1.length==5&&res2.length==3){ for(int i = 0; i <= 4; i++){ if(triangle.inTriangle(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res1[i].x,res1[i].y)>0)ans1++; else if(triangle.inTriangle(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 2; i++){ if(pentagon.inPentagon(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res1[4].x,res1[4].y,res2[i].x,res2[i].y)>0)ans2++; else if(pentagon.inPentagon(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res1[4].x,res1[4].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==5&&tot2==3)System.out.println("the previous pentagon coincides with the following triangle"); else if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco>0)System.out.println("the previous pentagon is connected to the following triangle");else if(cco>0)System.out.println("the previous pentagon is interlaced with the following triangle");else System.out.println("no overlapping area between the previous pentagon and the following triangle");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==5&&tot2==0)System.out.println("the previous pentagon is inside the following triangle");else if(tot1==0&&tot2==3)System.out.println("the previous pentagon contains the following triangle");else if(cco>0)System.out.println("the previous pentagon is interlaced with the following triangle");else if(pco==3)System.out.println("the previous pentagon contains the following triangle");else System.out.println("the previous pentagon is connected to the following triangle");} else if(ans1+tot1==5){System.out.println("the previous pentagon is inside the following triangle");} else if(ans2+tot2==3){System.out.println("the previous pentagon contains the following triangle");} else {System.out.println("the previous pentagon is interlaced with the following triangle");} } else if(res1.length==5&&res2.length==4){ for(int i = 0; i <= 4; i++){ if(quadrilateral.inQuarilateral(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res1[i].x,res1[i].y)>0)ans1++; else if(quadrilateral.inQuarilateral(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 3; i++){ if(pentagon.inPentagon(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res1[4].x,res1[4].y,res2[i].x,res2[i].y)>0)ans2++; else if(pentagon.inPentagon(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res1[4].x,res1[4].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==5&&tot2==4)System.out.println("the previous pentagon coincides with the following quadrilateral"); else if(ans1==0&&ans2==0&&tot1==0&&tot2==0){if(lco>0&&cco>0)System.out.println("the previous pentagon is connected to the following quadrilateral");else if(cco>0)System.out.println("the previous pentagon is interlaced with the following quadrilateral");else System.out.println("no overlapping area between the previous pentagon and the following quadrilateral");} else if((tot1>0||tot2>0)&&ans1==0&&ans2==0){if(tot1==5&&tot2==0)System.out.println("the previous pentagon is inside the following quadrilateral");else if(tot1==0&&tot2==4)System.out.println("the previous pentagon contains the following quadrilateral");else if(cco>0)System.out.println("the previous pentagon is interlaced with the following quadrilateral");else if(pco==4)System.out.println("the previous pentagon contains the following quadrilateral");else System.out.println("the previous pentagon is connected to the following quadrilateral");} else if(ans1+tot1==5){System.out.println("the previous pentagon is inside the following quadrilateral");} else if(ans2+tot2==4){System.out.println("the previous pentagon contains the following quadrilateral");} else {System.out.println("the previous pentagon is interlaced with the following quadrilateral");} } else{ for(int i = 0; i <= 4; i++){ if(pentagon.inPentagon(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res2[4].x,res2[4].y,res1[i].x,res1[i].y)>0)ans1++; else if(pentagon.inPentagon(res2[0].x,res2[0].y,res2[1].x,res2[1].y,res2[2].x,res2[2].y,res2[3].x,res2[3].y,res2[4].x,res2[4].y,res1[i].x,res1[i].y)==0)tot1++; } for(int i = 0; i <= 4; i++){ if(pentagon.inPentagon(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res1[4].x,res1[4].y,res2[i].x,res2[i].y)>0)ans2++; else if(pentagon.inPentagon(res1[0].x,res1[0].y,res1[1].x,res1[1].y,res1[2].x,res1[2].y,res1[3].x,res1[3].y,res1[4].x,res1[4].y,res2[i].x,res2[i].y)==0)tot2++; } if(tot1==5&&tot2==5)System.out.println("the previous pentagon coincides with the following pentagon"); else System.out.println("the previous pentagon is connected to the following pentagon"); } break; case '5': if(ss.length!=10) { if (cnt != ss.length) System.out.println("Wrong Format"); else System.out.println("wrong number of points"); return; } str=ss[0].split(",");x1=Double.parseDouble(str[0]);y1=Double.parseDouble(str[1]); str=ss[1].split(",");x2=Double.parseDouble(str[0]);y2=Double.parseDouble(str[1]); str=ss[2].split(",");x3=Double.parseDouble(str[0]);y3=Double.parseDouble(str[1]); str=ss[3].split(",");x4=Double.parseDouble(str[0]);y4=Double.parseDouble(str[1]); str=ss[4].split(",");x5=Double.parseDouble(str[0]);y5=Double.parseDouble(str[1]); str=ss[5].split(",");x6=Double.parseDouble(str[0]);y6=Double.parseDouble(str[1]); str=ss[6].split(",");x7=Double.parseDouble(str[0]);y7=Double.parseDouble(str[1]); str=ss[7].split(",");x8=Double.parseDouble(str[0]);y8=Double.parseDouble(str[1]); str=ss[8].split(",");x9=Double.parseDouble(str[0]);y9=Double.parseDouble(str[1]); str=ss[9].split(",");x10=Double.parseDouble(str[0]);y10=Double.parseDouble(str[1]); int kk=0; Point[] pol1 = xingzhuang(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5); Point[] pol2 = xingzhuang(x6,y6,x7,y7,x8,y8,x9,y9,x10,y10); LinkedHashSet<Point>cover = new LinkedHashSet<>(); for(int i = 0; i < pol1.length ;i++){ for(int j = 0; j < pol2.length ;j++){ if(line.cross(pol1[i].x,pol1[i].y,pol1[(i+1)%pol1.length].x,pol1[(i+1)% pol1.length].y,pol2[j].x,pol2[j].y,pol2[(j+1)%pol2.length].x,pol2[(j+1)% pol2.length].y)){ cover.add(line.tlpoint(pol1[i].x,pol1[i].y,pol1[(i+1)%pol1.length].x,pol1[(i+1)% pol1.length].y,pol2[j].x,pol2[j].y,pol2[(j+1)%pol2.length].x,pol2[(j+1)% pol2.length].y)); } if(line.pointCoincide(pol1[i].x,pol1[i].y,pol2[j].x,pol2[j].y)){ cover.add(new Point(pol1[i].x,pol1[i].y)); } if(pol1.length==3){ if(triangle.inTriangle(pol1[0].x,pol1[0].y,pol1[1].x,pol1[1].y,pol1[2].x,pol1[2].y,pol2[j].x,pol2[j].y)==1){ cover.add(new Point(pol2[j].x,pol2[j].y)); } } else if(pol1.length==4){ if(quadrilateral.inQuarilateral(pol1[0].x,pol1[0].y,pol1[1].x,pol1[1].y,pol1[2].x,pol1[2].y,pol1[3].x,pol1[3].y,pol2[j].x,pol2[j].y)==1){ cover.add(new Point(pol2[j].x,pol2[j].y)); } } else if(pol1.length==5){ if(pentagon.inPentagon(pol1[0].x,pol1[0].y,pol1[1].x,pol1[1].y,pol1[2].x,pol1[2].y,pol1[3].x,pol1[3].y,pol1[4].x,pol1[4].y,pol2[j].x,pol2[j].y)==1){ cover.add(new Point(pol2[j].x,pol2[j].y)); } } if(pol2.length==3){ if(triangle.inTriangle(pol2[0].x,pol2[0].y,pol2[1].x,pol2[1].y,pol2[2].x,pol2[2].y,pol1[i].x,pol1[i].y)==1){ cover.add(new Point(pol1[i].x,pol1[i].y)); } } else if(pol2.length==4){ if(quadrilateral.inQuarilateral(pol2[0].x,pol2[0].y,pol2[1].x,pol2[1].y,pol2[2].x,pol2[2].y,pol2[3].x,pol2[3].y,pol1[i].x,pol1[i].y)==1){ cover.add(new Point(pol1[i].x,pol1[i].y)); } } else if(pol2.length==5){ if(pentagon.inPentagon(pol2[0].x,pol2[0].y,pol2[1].x,pol2[1].y,pol2[2].x,pol2[2].y,pol2[3].x,pol2[3].y,pol2[4].x,pol2[4].y,pol1[i].x,pol1[i].y)==1){ cover.add(new Point(pol1[i].x,pol1[i].y)); } } } } for(int i=0;i< pol1.length;i++){//遍历得到交点 for(int j=0;j<pol2.length;j++){ if(line.loc(pol2[j].x,pol2[j].y,pol2[(j+1)% pol2.length].x,pol2[(j+1)% pol2.length].y,pol1[i].x,pol1[i].y)==0){ if(line.isOnLine(pol2[j].x,pol2[j].y,pol2[(j+1)% pol2.length].x,pol2[(j+1)% pol2.length].y,pol1[i].x,pol1[i].y)) { cover.add(new Point(pol1[i].x, pol1[i].y)); } } } } for(int i=0;i< pol2.length;i++){ for(int j=0;j<pol1.length;j++){ if(line.loc(pol1[j].x,pol1[j].y,pol1[(j+1)% pol1.length].x,pol1[(j+1)% pol1.length].y,pol2[i].x,pol2[i].y)==0){ if(line.isOnLine(pol1[j].x,pol1[j].y,pol1[(j+1)% pol1.length].x,pol1[(j+1)% pol1.length].y,pol2[i].x,pol2[i].y)) { cover.add(new Point(pol2[i].x, pol2[i].y)); } } } } Point[] coverPol = new Point[cover.size()]; kk=0; for(Point t:cover){ coverPol[kk++] = t; } double[] theta = new double[coverPol.length]; double[] theTa = new double[coverPol.length]; double[] X = new double[coverPol.length]; double[] Y = new double[coverPol.length]; double x=coverPol[0].x; double y=coverPol[0].y; coverPol[0].x=0; coverPol[0].y=0; for(int i=1;i<coverPol.length;i++){ coverPol[i].x -= x; coverPol[i].y -= y; X[i] = coverPol[i].x; Y[i] = coverPol[i].y; theta[i] = Math.atan2(coverPol[i].y,coverPol[i].x); theTa[i] = Math.atan2(coverPol[i].y,coverPol[i].x); } Arrays.sort(theta); boolean[] used = new boolean[coverPol.length]; int[] loc = new int[coverPol.length]; int[] pre = new int[coverPol.length]; for(int i=0;i<coverPol.length;i++){ for(int j=0;j<coverPol.length;j++){ if(theTa[i] == theta[j] && !used[j]){ used[j] = true; loc[i] = j; pre[j] = i; break; } } } double[] xx = new double[coverPol.length-1]; double[] yy = new double[coverPol.length-1]; int k = 0; for(int i=0;i<coverPol.length;i++){ if(!line.pointCoincide(X[pre[i]],Y[pre[i]],0.0,0.0)){ xx[k] = X[pre[i]]; yy[k] = Y[pre[i]]; k++; } } double S=0; for(int i=0;i<coverPol.length-2;i++){//求面积 S += triangle.qinjiushao(0, 0, xx[i], yy[i], xx[(i+1)%(coverPol.length-1)], yy[(i+1)%(coverPol.length-1)]); } BigDecimal Ss = new BigDecimal(S); S = Ss.setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(S); break; case '6': if(ss.length!=6) { if (cnt != ss.length) System.out.println("Wrong Format"); else System.out.println("wrong number of points"); return; } str=ss[0].split(",");x6=Double.parseDouble(str[0]);y6=Double.parseDouble(str[1]); str=ss[1].split(",");x1=Double.parseDouble(str[0]);y1=Double.parseDouble(str[1]); str=ss[2].split(",");x2=Double.parseDouble(str[0]);y2=Double.parseDouble(str[1]); str=ss[3].split(",");x3=Double.parseDouble(str[0]);y3=Double.parseDouble(str[1]); str=ss[4].split(",");x4=Double.parseDouble(str[0]);y4=Double.parseDouble(str[1]); str=ss[5].split(",");x5=Double.parseDouble(str[0]);y5=Double.parseDouble(str[1]); LinkedHashSet<Point>set6 = new LinkedHashSet<>(); set6.add(new Point(x1,y1)); set6.add(new Point(x2,y2)); set6.add(new Point(x3,y3)); set6.add(new Point(x4,y4)); set6.add(new Point(x5,y5)); if(set6.size()==3){ Point[] case6 = new Point[3]; int q=0; for(Point r:set6){ case6[q++]=r; } x1=case6[0].x;y1=case6[0].y; x2=case6[1].x;y2=case6[1].y; x3=case6[2].x;y3=case6[2].y; triangle.Case6(x1,y1,x2,y2,x3,y3,x6,y6); } else if(set6.size()==4){ Point[] case6 = new Point[4]; int q=0; for(Point r:set6){ case6[q++]=r; } x1=case6[0].x;y1=case6[0].y; x2=case6[1].x;y2=case6[1].y; x3=case6[2].x;y3=case6[2].y; x4=case6[3].x;y4=case6[3].y; if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ triangle.Case6(x1,y1,x3,y3,x4,y4,x6,y6); } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ triangle.Case6(x1,y1,x2,y2,x4,y4,x6,y6); } else if(line.isParad(x3,y3,x4,y4,x4,y4,x1,y1)){ triangle.Case6(x1,y1,x2,y2,x3,y3,x6,y6); } else if(line.isParad(x4,y4,x1,y1,x1,y1,x2,y2)){ triangle.Case6(x2,y2,x3,y3,x4,y4,x6,y6); } else{ quadrilateral.Case6(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6); } } else{ if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x1,y1,x5,y5,x5,y5,x4,y4)){ triangle.Case6(x1,y1,x3,y3,x4,y4,x6,y6); } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ triangle.Case6(x1,y1,x3,y3,x5,y5,x6,y6); } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)){ triangle.Case6(x1,y1,x2,y2,x4,y4,x6,y6); } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ triangle.Case6(x1,y1,x4,y4,x5,y5,x6,y6); } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ triangle.Case6(x1,y1,x2,y2,x5,y5,x6,y6); } else if(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)){ triangle.Case6(x1,y1,x3,y3,x4,y4,x6,y6); } else if(line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)&&line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ triangle.Case6(x2,y2,x3,y3,x4,y4,x6,y6); } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ quadrilateral.Case6(x1,y1,x3,y3,x4,y4,x5,y5,x6,y6); } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ quadrilateral.Case6(x1,y1,x2,y2,x4,y4,x5,y5,x6,y6); } else if(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ quadrilateral.Case6(x1,y1,x2,y2,x3,y3,x5,y5,x6,y6); } else if(line.isParad(x1,y1,x5,y5,x5,y5,x4,y4)){ quadrilateral.Case6(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6); } else{ pentagon.Case6(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6); } } } } static Point[] getPoint(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//得到去重、去平行后的点集 Line line = new Line(); Triangle triangle = new Triangle(); Quadrilateral quadrilateral = new Quadrilateral(); Pentagon pentagon = new Pentagon(); LinkedHashSet<Point>set = new LinkedHashSet<>(); set.add(new Point(x1,y1)); set.add(new Point(x2,y2)); set.add(new Point(x3,y3)); set.add(new Point(x4,y4)); set.add(new Point(x5,y5)); if(set.size()==3){ Point[] res = new Point[3]; int k=0; for(Point r:set){ res[k++]=r; } return res; } else if(set.size()==4){ Point[] res = new Point[4]; int k=0; for(Point r:set){ res[k++]=r; } return res; } else{ Point[] res = new Point[5]; int k=0; for(Point r:set){ res[k++]=r; } return res; } } static Point[] xingzhuang(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//得到有效点集 Line line = new Line(); Triangle triangle = new Triangle(); Quadrilateral quadrilateral = new Quadrilateral(); Pentagon pentagon = new Pentagon(); Point[] res = new Point[5]; res = getPoint(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5); if(res.length==3){ return res; } else if(res.length==4){ x1=res[0].x;y1=res[0].y; x2=res[1].x;y2=res[1].y; x3=res[2].x;y3=res[2].y; x4=res[3].x;y4=res[3].y; if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x3,y3); res3[2]=new Point(x4,y4); return res3; } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x2,y2); res3[2]=new Point(x4,y4); return res3; } else if(line.isParad(x3,y3,x4,y4,x4,y4,x1,y1)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x2,y2); res3[2]=new Point(x3,y3); return res3; } else if(line.isParad(x4,y4,x1,y1,x1,y1,x2,y2)){ Point[] res3 = new Point[3]; res3[0]=new Point(x2,y2); res3[1]=new Point(x3,y3); res3[4]=new Point(x4,y4); return res3; } else{ Point[] res4 = new Point[4]; res4[0]=new Point(x1,y1); res4[1]=new Point(x2,y2); res4[2]=new Point(x3,y3); res4[3]=new Point(x4,y4); return res4; } } else{ if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x1,y1,x5,y5,x5,y5,x4,y4)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x3,y3); res3[2]=new Point(x4,y4); return res3; } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x3,y3); res3[2]=new Point(x5,y5); return res3; } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x2,y2); res3[2]=new Point(x4,y4); return res3; } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)&&line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x4,y4); res3[2]=new Point(x5,y5); return res3; } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)&&line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x2,y2); res3[2]=new Point(x5,y5); return res3; } else if(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)&&line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)){ Point[] res3 = new Point[3]; res3[0]=new Point(x1,y1); res3[1]=new Point(x2,y2); res3[2]=new Point(x3,y3); return res3; } else if(line.isParad(x4,y4,x5,y5,x5,y5,x1,y1)&&line.isParad(x5,y5,x1,y1,x1,y1,x2,y2)){ Point[] res3 = new Point[3]; res3[0]=new Point(x2,y2); res3[1]=new Point(x3,y3); res3[2]=new Point(x4,y4); return res3; } else if(line.isParad(x1,y1,x2,y2,x2,y2,x3,y3)){ Point[] res4 = new Point[4]; res4[0]=new Point(x1,y1); res4[1]=new Point(x3,y3); res4[2]=new Point(x4,y4); res4[3]=new Point(x5,y5); return res4; } else if(line.isParad(x2,y2,x3,y3,x3,y3,x4,y4)){ Point[] res4 = new Point[4]; res4[0]=new Point(x1,y1); res4[1]=new Point(x2,y2); res4[2]=new Point(x4,y4); res4[3]=new Point(x5,y5); return res4; } else if(line.isParad(x3,y3,x4,y4,x4,y4,x5,y5)){ Point[] res4 = new Point[4]; res4[0]=new Point(x1,y1); res4[1]=new Point(x2,y2); res4[2]=new Point(x3,y3); res4[3]=new Point(x5,y5); return res4; } else if(line.isParad(x1,y1,x5,y5,x5,y5,x4,y4)){ Point[] res4 = new Point[4]; res4[0]=new Point(x1,y1); res4[1]=new Point(x2,y2); res4[2]=new Point(x3,y3); res4[3]=new Point(x4,y4); return res4; } else{ Point[] res5 = new Point[5]; res5[0]=new Point(x1,y1); res5[1]=new Point(x2,y2); res5[2]=new Point(x3,y3); res5[3]=new Point(x4,y4); res5[4]=new Point(x5,y5); return res5; } } } } class Point{//点类 double x,y; public Point(double x,double y){ this.x=x; this.y=y; } @Override public boolean equals(Object t){ if(this == t){ return true; } if(t == null || getClass() != t.getClass()){ return false; } Point point = (Point) t; return x == point.x && y == point.y &&Objects.equals(x,point.x) && Objects.equals(y,point.y); } @Override public int hashCode(){ return Objects.hash(x,y); } } class Line{//线类 boolean cross(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//判断线段是否相交 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A34=y3-y4; double B34=x4-x3; double C34=x3*y4-x4*y3; double D=A12*B34-A34*B12; if(Math.abs(D)>=0.0000001){ double x=(B12*C34-B34*C12)/D; double y=(A34*C12-A12*C34)/D; if(((x<=Math.max(x1,x2)&&x>=Math.min(x1,x2)&&y<Math.max(y1,y2)&&y>Math.min(y1,y2))||(x<Math.max(x1,x2)&&x>Math.min(x1,x2)&&y<=Math.max(y1,y2)&&y>=Math.min(y1,y2)))&&((x<=Math.max(x3,x4)&&x>=Math.min(x3,x4)&&y<Math.max(y3,y4)&&y>Math.min(y3,y4))||(x<Math.max(x3,x4)&&x>Math.min(x3,x4)&&y<=Math.max(y3,y4)&&y>=Math.min(y3,y4)))){ return true; } } return false; } boolean cross1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){ double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A34=y3-y4; double B34=x4-x3; double C34=x3*y4-x4*y3; double D=A12*B34-A34*B12; if(Math.abs(D)>=0.0000001){ double x=(B12*C34-B34*C12)/D; double y=(A34*C12-A12*C34)/D; if((x<=Math.max(x1,x2)&&x>=Math.min(x1,x2)&&y<Math.max(y1,y2)&&y>Math.min(y1,y2))||(x<Math.max(x1,x2)&&x>Math.min(x1,x2)&&y<=Math.max(y1,y2)&&y>=Math.min(y1,y2))){ return true; } } return false; } boolean isParad(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//是否平行 if((y2-y1)*(x4-x3)==(y4-y3)*(x2-x1)){ return true; } return false; } boolean pointCoincide(double x1,double y1,double x2,double y2){//点重 if(x1-x2==0&&y1-y2==0){ return true; } return false; } int loc(double x1,double y1,double x2,double y2,double x3,double y3){//位置关系 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; if(Math.abs(A12*x3+B12*y3+C12)<0.0000001){ return 0; } else if(A12*x3+B12*y3+C12<0){ return -1; } else{ return 1; } } boolean isOnLine(double x1,double y1,double x2,double y2,double x3,double y3){//是否在线段上 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; if(Math.abs(A12*x3+B12*y3+C12)<0.0000001){ if(x3>=Math.min(x1,x2)&&x3<=Math.max(x1,x2)&&y3>=Math.min(y1,y2)&&y3<=Math.max(y1,y2)){ return true; } } return false; } boolean lineCoincide(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//线重合 if(isParad(x1, y1, x2, y2, x3, y3, x4, y4)){ if(loc(x1, y1, x2, y2, x3, y3)==0&&loc(x1, y1, x2, y2, x4, y4)==0&&!pointCoincide(x3,y3,x4,y4)&&!pointCoincide(x1,y1,x2,y2)){ return true; } } return false; } Point tlpoint(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//得到交点 double A12=y1-y2; double B12=x2-x1; double C12=x1*y2-x2*y1; double A34=y3-y4; double B34=x4-x3; double C34=x3*y4-x4*y3; double D=A12*B34-A34*B12; double x=(B12*C34-B34*C12)/D; double y=(A34*C12-A12*C34)/D; return new Point(x,y); } } class Triangle{//三角形类 double qinjiushao(double x1,double y1,double x2,double y2,double x3,double y3){//求三角形面积 double a=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); double b=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); double c=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); double p=(a+b+c)/2; double s=Math.sqrt(p*(p-a)*(p-b)*(p-c)); return s; } int inTriangle(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//是否在三角形内 double s1=qinjiushao(x1,y1,x2,y2,x4,y4); double s2=qinjiushao(x1,y1,x3,y3,x4,y4); double s3=qinjiushao(x2,y2,x3,y3,x4,y4); double s=qinjiushao(x1,y1,x2,y2,x3,y3); if(Math.abs(s1+s2+s3-s)<0.0001){ if(Math.abs(s1)<0.0001||Math.abs(s2)<0.0001||Math.abs(s3)<0.0001){ return 0; } else{ return 1; } } else{ return -1; } } void Case6(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//是否在三角形内(case6) if(inTriangle(x1,y1,x2,y2,x3,y3,x4,y4)==1){ System.out.println("in the triangle"); } else if(inTriangle(x1, y1, x2, y2, x3, y3, x4, y4)==0){ System.out.println("on the triangle"); } else{ System.out.println("outof the triangle"); } } } class Quadrilateral{//四边形类 int inQuarilateral(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//是否在四边形内 Triangle triangle = new Triangle(); double s1=triangle.qinjiushao(x1,y1,x2,y2,x5,y5); double s2=triangle.qinjiushao(x2,y2,x3,y3,x5,y5); double s3=triangle.qinjiushao(x3,y3,x4,y4,x5,y5); double s4=triangle.qinjiushao(x1,y1,x4,y4,x5,y5); double s=triangle.qinjiushao(x1,y1,x2,y2,x3,y3)+ triangle.qinjiushao(x1,y1,x3,y3,x4,y4); if(Math.abs(s1+s2+s3+s4-s)<0.0001){ if(Math.abs(s1)<0.0001||Math.abs(s2)<0.0001||Math.abs(s3)<0.0001||Math.abs(s4)<0.0001){ return 0; } return 1; } else{ return -1; } } void Case6(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5){//是否在四边形内/边上/外(case6) if(inQuarilateral(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5)==1){ System.out.println("in the quadrilateral"); } else if(inQuarilateral(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5)==0){ System.out.println("on the quadrilateral"); } else{ System.out.println("outof the quadrilateral"); } } } class Pentagon {//五边形类 int inPentagon(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5,double x6,double y6){ Triangle triangle = new Triangle(); double s1=triangle.qinjiushao(x1,y1,x2,y2,x6,y6); double s2=triangle.qinjiushao(x2,y2,x3,y3,x6,y6); double s3=triangle.qinjiushao(x3,y3,x4,y4,x6,y6); double s4=triangle.qinjiushao(x4,y4,x5,y5,x6,y6); double s5=triangle.qinjiushao(x1,y1,x5,y5,x6,y6); double s=triangle.qinjiushao(x1,y1,x2,y2,x3,y3)+ triangle.qinjiushao(x1,y1,x3,y3,x4,y4)+ triangle.qinjiushao(x1,y1,x4,y4,x5,y5); if(Math.abs(s1+s2+s3+s4+s5-s)<0.0000001){ if(Math.abs(s1)<0.0000001||Math.abs(s2)<0.0000001||Math.abs(s3)<0.0000001||Math.abs(s4)<0.0000001||Math.abs(s5)<0.0000001){ return 0; } return 1; } else{ return -1; } } void Case6(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5,double x6,double y6){ if(inPentagon(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6)==1){ System.out.println("in the pentagon"); } else if(inPentagon(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6)==0){ System.out.println("on the pentagon"); } else{ System.out.println("outof the pentagon"); } } }




写代码一定要耐心冷静,不然容易思路混乱。
2022-下-面向对象-期中考试
下图是三题的类

7-1 点与线(类设计)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double x1=input.nextDouble(); double y1=input.nextDouble(); double x2=input.nextDouble(); double y2=input.nextDouble(); String color=input.next(); if(x1>0&&x1<=200&&x2>0&&x2<=200&&y1>0&&y1<=200&&y2>0&&y2<=200) { Point p1=new Point(x1,y1); Point p2=new Point(x2,y2); Line l1=new Line(p1, p2, color); l1.display(); } else { System.out.print("Wrong Format"); } } } class Point{ private double x; private double y; 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 void display() { System.out.println("("+String.format("%.2f", x)+","+String.format("%.2f",y)+")"); } } class Line{ private Point point1; private Point point2; private String color; public Line(Point p1,Point p2,String color) { this.point1=p1; this.point2=p2; this.color=color; } public void setPoint1(Point point1) { this.point1=point1; } public void setPoint2(Point point2) { this.point2=point2; } public void setColor(String color) { this.color=color; } public Point getPoint1() { return point1; } public Point getPoint2() { return point2; } public String getColor() { return color; } public double getDistance() { double distance=Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); return distance; } public void display() { System.out.println("The line's color is:"+color); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.println("The line's length is:"+String.format("%.2f",getDistance())); } }

7-2 点线面问题重构(继承与多态)
import java.util.Scanner; public class Main { public static void main(String[] args) { double a,b,c,d; Scanner in = new Scanner(System.in); a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); d = in.nextDouble(); String color = in.next(); if(a<=0 || a>200 || b<=0 || b>200 || c<=0 || c>200 ||d<=0 || d>200 ) { System.out.println("Wrong Format"); System.exit(0); } Point p1 = new Point(a, b); Point p2 = new Point(c, d); Line line = new Line(p1, p2, color); Plane plane = new Plane(color); Element element = new Element(); element = p1;//起点Point element.display(); element = p2;//终点Point element.display(); element = line;//线段 element.display(); element = plane;//面 element.display(); } } class Point extends Element{ private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point() { } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } @Override public void display() { System.out.println("("+String.format("%.2f", this.x)+","+String.format("%.2f", this.y)+")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; public Line() { } public Line(Point point1, Point point2, String color) { this.point1 = point1; this.point2 = point2; this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); getPoint1().display(); System.out.println("The line's end point's Coordinate is:"); getPoint2().display(); System.out.println("The line's length is:"+String.format("%.2f", getDistance()));; } public double getDistance() { return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2)); } } class Plane extends Element{ private String color; public Plane() { } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The Plane's color is:"+this.color); } } class Element { public void display() { } }

7-3 点线面问题再重构(容器类)
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { double a,b,c,d; Scanner in = new Scanner(System.in); GeometryObject g = new GeometryObject(); int choice = in.nextInt(); while(choice != 0) { switch(choice) { case 1://insert Point object into list a = in.nextDouble(); b = in.nextDouble(); if(a<=0 || a>200 || b<=0 || b>200) { System.out.println("Wrong Format"); System.exit(0); } Point p1 = new Point(); p1.setX(a);p1.setY(b); g.add(p1); break; case 2://insert Line object into list Line line = new Line(); a = in.nextDouble(); b = in.nextDouble(); c = in.nextDouble(); d = in.nextDouble(); String color = in.next(); if(a<=0 || a>200 || b<=0 || b>200 || c<=0 || c>200 || d<=0 || d>200) { System.out.println("Wrong Format"); System.exit(0); } Point p2 = new Point(a, b); Point p3 = new Point(c, d); line.setPoint1(p2);line.setPoint2(p3);line.setColor(color); g.add(line); break; case 3://insert Plane object into list Plane plane = new Plane(); String co = in.next(); plane.setColor(co); g.add(plane); break; case 4://delete index - 1 object from list int index = in.nextInt(); g.remove(index); } choice = in.nextInt(); } for(Element e: g.getEs()) { e.display(); } } } class Point extends Element{ private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point() { } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } @Override public void display() { System.out.println("("+String.format("%.2f", this.x)+","+String.format("%.2f", this.y)+")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; public Line() { } public Line(Point point1, Point point2, String color) { this.point1 = point1; this.point2 = point2; this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The line's color is:"+this.color); System.out.println("The line's begin point's Coordinate is:"); getPoint1().display(); System.out.println("The line's end point's Coordinate is:"); getPoint2().display(); System.out.println("The line's length is:"+String.format("%.2f", getDistance()));; } public double getDistance() { return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2)); } } class Plane extends Element{ private String color; public Plane() { } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { System.out.println("The Plane's color is:"+this.color); } } class Element { public void display() { System.out.println("a"); } } class GeometryObject { private ArrayList<Element> es = new ArrayList<Element>(); public GeometryObject() { } public void add(Element element) { es.add(element); } public void remove(int index) { if(index>=1 && index<=es.size()) es.remove(index-1); } public ArrayList<Element> getEs() { return es; } }

读取字符串,next()和nextLine()有区别,
总结:写代码前画好类图。画出类图可以具象化编程思路,对类的设计有所帮助。
多使用继承和抽象。对于多个具有相似属性的类,类似功能可以添加到抽象类里,
既可缩短代码方便代码阅读,又可简化编程。
每次做题,都能学到新东西。这次及作业我感受到类、继承、抽象等面向对象程序设计方法的好处。调试代码的过程需要耐心,同时,这几次作业还让我意识到,想要学好java(其它专业课也一样),必须系统、广泛地学习,接受新的思维和方法,不能钻牛角尖。

浙公网安备 33010602011771号