第二次博客作业

前言:

  本次博客是对第四次、第五次作业和期中考试的一次总结,第四次、第五次作业相对于前几次的作业难度有大幅度提升,完成代码后对于整体框架仍不是很清晰,代码也修改过多次。

 

设计与分析:

题目集4(7-2)

用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。

输入样例1:

选项1,点重合。例如:

1:-1,-1 -1,-1 1,2 1,-2
 

输出样例:

在这里给出相应的输出。例如:

points coincide
 

输入样例2:

不符合基本格式。例如:

1:-1,-1 1,2 -1,1 ++1,0
 

输出样例:

在这里给出相应的输出。例如:

Wrong Format
 

输入样例3:

选项1,输入点数量不对。例如:

1:-1,-1 -1,2 
 

输出样例:

在这里给出相应的输出。例如:

wrong number of points
 

输入样例4:

选项1,正确输入判断。例如:

1:-1,-1 -1,1 1,2 1,-2
 

输出样例:

在这里给出相应的输出。例如:

true false
 

输入样例5:

选项2,输入点不构成四边形。例如:

2:10,10 1,1 0,0 1,20
 

输出样例:

在这里给出相应的输出。例如:

not a quadrilateral
 

输入样例6:

选项2,正方形。例如:

2:0,0 0,80 80,80 80,0
 

输出样例:

在这里给出相应的输出。例如:

true true true
 

输入样例7:

选项2。例如:

2:0,0 -10,80 0,160 -10,80
 

输出样例:

在这里给出相应的输出。例如:

not a quadrilateral
 

输入样例8:

选项3,凸四边形。例如:

3:-1,-1 -1,1 1,2 1,-2
 

输出样例:

在这里给出相应的输出。例如:

true 10.472 6.0
 

输入样例9:

选项3,。例如:

3:0,0 -10,100 0,99 10,100
 

输出样例:

在这里给出相应的输出。例如:

false 221.097 990.0

SourceMonito报表

 

 

 类图:

完整代码:

复制代码
  1 import java.text.DecimalFormat;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner sc = new Scanner(System.in);
  7         String s = sc.nextLine();
  8         String s2[] = null;
  9         String s1[] = null;
 10         String s3[] = null;
 11         String s4 = "";
 12         double[] d = new double[30];
 13 
 14         if (!s.matches("^[1-5]:(([+-]?(0|(0\\.\\d+)|[1-9]\\d*(\\.\\d+)?)),([+-]?(0|(0\\.\\d+)|[1-9]\\d*(\\.\\d+)?))\\s?)+$")) {
 15             System.out.println("Wrong Format");
 16             System.exit(0);
 17         }
 18         int index = 0;
 19         s3 = s.split(":");
 20         s1 = s3[1].split(" ");
 21         for (int i = 0; i < s1.length; i++) {
 22             s2 = s1[i].split(",");
 23             for (int j = 0; j < s2.length; j++) {
 24                 if (!check(s2[j])) {
 25                     System.out.println("Wrong Format");
 26                     System.exit(0);
 27                 }
 28                 d[index++] = Double.valueOf(s2[j]);
 29             }
 30         }
 31         int num = 0;
 32         Point[] parr = new Point[10];
 33         for (int i = 0; i < index; i++) {
 34             if (i % 2 == 0) {
 35                 parr[num++] = new Point(d[i], d[i + 1]);
 36             }
 37         }
 38 
 39         switch (s.charAt(0)) {
 40             case '1':
 41                 if (index == 8) {
 42                     if (parr[0].equal(parr[1]) || parr[0].equal(parr[3]) || parr[1].equal(parr[2]) || parr[0].equal(parr[2]) || parr[1].equal(parr[3]) || parr[2].equal(parr[3])) {
 43                         System.out.println("points coincide");
 44                         System.exit(0);
 45                     }
 46                     Quadrilateral q = new Quadrilateral(parr[0], parr[1], parr[2], parr[3]);
 47                     if (q.isQuadrilateral()) {
 48                         System.out.print("true ");
 49                     } else {
 50                         System.out.print("false ");
 51                     }
 52                     if (q.isParallelQuadrilateral()) {
 53                         System.out.println("true");
 54                     } else {
 55                         System.out.println("false");
 56                     }
 57                 } else {
 58                     System.out.println("wrong number of points");
 59                 }
 60                 break;
 61             case '2':
 62                 if (index == 8) {
 63                     if (parr[0].equal(parr[1]) || parr[0].equal(parr[3]) || parr[1].equal(parr[2]) || parr[0].equal(parr[2]) || parr[1].equal(parr[3]) || parr[2].equal(parr[3])) {
 64                         System.out.println("not a quadrilateral");
 65                         System.exit(0);
 66                     }
 67                     Quadrilateral q = new Quadrilateral(parr[0], parr[1], parr[2], parr[3]);
 68                     if (!q.isQuadrilateral()) {
 69                         System.out.println("not a quadrilateral");
 70                         System.exit(0);
 71                     }
 72                     System.out.println(q.isDiamond() + " " + q.isRectangle() + " " + q.isSquare());
 73                 } else {
 74                     System.out.println("wrong number of points");
 75                 }
 76                 break;
 77             case '3':
 78                 if (index == 8) {
 79                     if (parr[0].equal(parr[1]) || parr[0].equal(parr[3]) || parr[1].equal(parr[2]) || parr[0].equal(parr[2]) || parr[1].equal(parr[3]) || parr[2].equal(parr[3])) {
 80                         System.out.println("points coincide");
 81                         System.exit(0);
 82                     }
 83                     Quadrilateral q = new Quadrilateral(parr[0], parr[1], parr[2], parr[3]);
 84                     if (!q.isQuadrilateral()) {
 85                         System.out.println("not a quadrilateral");
 86                         System.exit(0);
 87                     }
 88                     System.out.println(q.isConvexQuadrilateral() + " " + new DecimalFormat("0.0##").format(q.sideLength()) + " " + new DecimalFormat("0.0##").format(q.area()));
 89                 } else {
 90                     System.out.println("wrong number of points");
 91                 }
 92                 break;
 93             case '4':
 94                 if (index == 12) {
 95                     Line l = new Line(parr[0], parr[1]);
 96                     Quadrilateral q = new Quadrilateral(parr[2], parr[3], parr[4], parr[5]);
 97                     if (!q.isQuadrilateral()) {
 98                         if (parr[2].equal(parr[5])) {
 99                             Delta D = new Delta(parr[2], parr[3], parr[4]);
100                             judgeDelta(D);
101                             if (parr[0].equal(parr[1])) {
102                             System.out.println("points coincide");
103                             System.exit(0);
104                             }
105                             D.intersection_point(l);
106                         } else if (parr[2].equal(parr[4]) || parr[4].equal(parr[5])) {
107                             Delta D = new Delta(parr[2], parr[3], parr[5]);
108                             judgeDelta(D);
109                             if (parr[0].equal(parr[1])) {
110                             System.out.println("points coincide");
111                             System.exit(0);
112                             }
113                             D.intersection_point(l);
114                         } else if (parr[2].equal(parr[3]) || parr[3].equal(parr[4]) || parr[3].equal(parr[5])) {
115                             Delta D = new Delta(parr[2], parr[4], parr[5]);
116                             judgeDelta(D);
117                             if (parr[0].equal(parr[1])) {
118                             System.out.println("points coincide");
119                             System.exit(0);
120                             }
121                             D.intersection_point(l);
122                         } else if (parr[3].equal(parr[4]) || parr[3].equal(parr[5])) {
123                             Delta D = new Delta(parr[2], parr[4], parr[5]);
124                             judgeDelta(D);
125                             if (parr[0].equal(parr[1])) {
126                             System.out.println("points coincide");
127                             System.exit(0);
128                             }
129                             D.intersection_point(l);
130                         } else if (q.lines[0].getSlope() == q.lines[1].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope()) {
131                             Line temp = new Line(parr[2], parr[4]);
132                             if (!temp.IsMid(parr[3])) {
133                                 System.out.println("not a quadrilateral or triangle");
134                                 System.exit(0);
135                             }
136                             Delta D = new Delta(parr[2], parr[4], parr[5]);
137                             judgeDelta(D);
138                             if (parr[0].equal(parr[1])) {
139                             System.out.println("points coincide");
140                             System.exit(0);
141                             }
142                             D.intersection_point(l);
143                         } else if (q.lines[0].getSlope() == q.lines[3].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope()) {
144                             Line temp = new Line(parr[3], parr[5]);
145                             if (!temp.IsMid(parr[2])) {
146                                 System.out.println("not a quadrilateral or triangle");
147                                 System.exit(0);
148                             }
149                             Delta D = new Delta(parr[3], parr[4], parr[5]);
150                             judgeDelta(D);
151                             if (parr[0].equal(parr[1])) {
152                             System.out.println("points coincide");
153                             System.exit(0);
154                             }
155                             D.intersection_point(l);
156                         } else if (q.lines[1].getSlope() == q.lines[2].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope()) {
157                             Line temp = new Line(parr[3], parr[5]);
158                             if (!temp.IsMid(parr[4])) {
159                                 System.out.println("not a quadrilateral or triangle");
160                                 System.exit(0);
161                             }
162                             Delta D = new Delta(parr[2], parr[3], parr[5]);
163                             judgeDelta(D);
164                             if (parr[0].equal(parr[1])) {
165                             System.out.println("points coincide");
166                             System.exit(0);
167                             }
168                             D.intersection_point(l);
169                         } else if (q.lines[2].getSlope() == q.lines[3].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope()) {
170                             Line temp = new Line(parr[2], parr[4]);
171                             if (!temp.IsMid(parr[5])) {
172                                 System.out.println("not a quadrilateral or triangle");
173                                 System.exit(0);
174                             }
175                             Delta D = new Delta(parr[2], parr[3], parr[4]);
176                             judgeDelta(D);
177                             if (parr[0].equal(parr[1])) {
178                             System.out.println("points coincide");
179                             System.exit(0);
180                             }
181                             D.intersection_point(l);
182                         } else if (q.lines[2].getSlope() == q.lines[3].getSlope() && q.lines[0].getSlope() == q.lines[1].getSlope() || q.lines[0].getSlope() == q.lines[3].getSlope() && q.lines[1].getSlope() == q.lines[2].getSlope()){
183                             System.out.println("not a quadrilateral or triangle");
184                             System.exit(0);
185                         }else{
186                             System.out.println("not a quadrilateral or triangle");
187                             System.exit(0);
188                         }
189                        
190                     } else {
191                         q.InterSection_Line(l);
192                     }
193                 } else {
194                     System.out.println("wrong number of points");
195                 }
196                 break;
197             case '5':
198                 if (index == 10) {
199                     Quadrilateral q = new Quadrilateral(parr[1], parr[2], parr[3], parr[4]);
200                     if (!q.isQuadrilateral()) {
201                         if (parr[1].equal(parr[2])) {
202                             Delta D = new Delta(parr[1], parr[3], parr[4]);
203                             judgeDelta(D);
204                             D.IsInTheTriangle(parr[0]);
205                         } else if (parr[1].equal(parr[3]) || parr[2].equal(parr[3])) {
206                             Delta D = new Delta(parr[1], parr[2], parr[4]);
207                             judgeDelta(D);
208                             D.IsInTheTriangle(parr[0]);
209                         } else if (parr[1].equal(parr[4]) || parr[2].equal(parr[4]) || parr[3].equal(parr[4])) {
210                             Delta D = new Delta(parr[1], parr[2], parr[3]);
211                             judgeDelta(D);
212                             D.IsInTheTriangle(parr[0]);
213                         } else if (q.lines[0].getSlope() == q.lines[1].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope()) {
214                             Line temp = new Line(parr[1], parr[3]);
215                             if (!temp.IsMid(parr[2])) {
216                                 System.out.println("not a quadrilateral or triangle");
217                                 System.exit(0);
218                             }
219                             Delta D = new Delta(parr[1], parr[3], parr[4]);
220                             judgeDelta(D);
221                             D.IsInTheTriangle(parr[0]);
222                         } else if (q.lines[0].getSlope() == q.lines[3].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope()) {
223                             Line temp = new Line(parr[2], parr[4]);
224                             if (!temp.IsMid(parr[1])) {
225                                 System.out.println("not a quadrilateral or triangle");
226                                 System.exit(0);
227                             }
228                             Delta D = new Delta(parr[2], parr[3], parr[4]);
229                             judgeDelta(D);
230                             D.IsInTheTriangle(parr[0]);
231                         } else if (q.lines[1].getSlope() == q.lines[2].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope()) {
232                             Line temp = new Line(parr[2], parr[4]);
233                             if (!temp.IsMid(parr[3])) {
234                                 System.out.println("not a quadrilateral or triangle");
235                                 System.exit(0);
236                             }
237                             Delta D = new Delta(parr[1], parr[2], parr[4]);
238                             judgeDelta(D);
239                             D.IsInTheTriangle(parr[0]);
240                         } else if (q.lines[2].getSlope() == q.lines[3].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope()) {
241                             Line temp = new Line(parr[1], parr[3]);
242                             if (!temp.IsMid(parr[4])) {
243                                 System.out.println("not a quadrilateral or triangle");
244                                 System.exit(0);
245                             }
246                             Delta D = new Delta(parr[1], parr[2], parr[3]);
247                             judgeDelta(D);
248                             D.IsInTheTriangle(parr[0]);
249                         } else if (q.lines[2].getSlope() == q.lines[3].getSlope() && q.lines[0].getSlope() == q.lines[1].getSlope() || q.lines[0].getSlope() == q.lines[3].getSlope() && q.lines[1].getSlope() == q.lines[2].getSlope()) {
250                             System.out.println("not a quadrilateral or triangle");
251                             System.exit(0);
252                         }else{
253                             System.out.println("not a quadrilateral or triangle");
254                             System.exit(0);
255                         }
256                     } else {
257                         q.IsInTheQuadrilateral(parr[0]);
258                     }
259                 } else {
260                     System.out.println("wrong number of points");
261                 }
262                 break;
263             default:
264                 System.out.println("Wrong Format");
265         }
266 
267     }
268 
269     public static boolean check(String str) {
270         return str.matches("^[+-]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$");
271     }
272 
273     public static void judgeDelta(Delta D) {
274         if (!D.isTriangle()) {
275             System.out.println("not a quadrilateral or triangle");
276             System.exit(0);
277         }
278     }
279 }
280 
281 class Point {
282     double x, y;
283 
284     Point() {
285     }
286 
287     Point(double x, double y) {
288         this.x = x;
289         this.y = y;
290     }
291 
292     double distance(Point p) {
293         return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
294     }
295 
296     boolean equal(Point p) {
297         if (this.x == p.x && this.y == p.y) {
298             return true;
299         } else {
300             return false;
301         }
302     }
303 }
304 
305 class Line {
306     Point p1, p2;
307     double a, b, c;
308     double length;
309 
310 
311     Line() {
312     }
313 
314     Line(Point p1, Point p2) {
315         this.p1 = p1;
316         this.p2 = p2;
317         this.a = p2.y - p1.y;
318         this.b = p1.x - p2.x;
319         this.c = p2.x * p1.y - p1.x * p2.y;
320         this.length = p1.distance(p2);
321     }
322 
323 
324     public double getSlope() {
325         if (b == 0) {
326             return 9999;
327         } else {
328             return -a / b;
329         }
330     }
331 
332     public double getDistance(Point p) {
333         return Math.abs(this.a * p.x + this.b * p.y + c) / Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
334     }
335 
336     public boolean isParallel(Line l) {
337         if (this.getSlope() == l.getSlope())
338             return true;
339         else
340             return false;
341     }
342 
343     public boolean IsSame(Line l) {
344         if (this.getSlope() == l.getSlope() && this.getDistance(l.p1) == 0)
345             return true;
346         else
347             return false;
348     }
349 
350     public boolean IsMid(Point p) {
351         if (this.b != 0) {
352             if ((this.p2.x >= p.x && this.p1.x <= p.x) || (this.p2.x <= p.x && this.p1.x >= p.x)) {
353                 return true;
354             } else {
355                 return false;
356             }
357         } else {
358             if ((this.p2.y >= p.y && this.p1.y <= p.y) || (this.p2.y <= p.y && this.p1.y >= p.y)) {
359                 return true;
360             } else {
361                 return false;
362             }
363         }
364     }
365 
366     public double getLength() {
367         this.length = this.p1.distance(this.p2);
368         return this.length;
369     }
370 
371     public Point InterSection(Line l) {
372         double temp = this.a * l.b - l.a * this.b;
373       
374         double x = (this.b * l.c - l.b * this.c) / temp;
375         double y = (l.a * this.c - this.a * l.c) / temp;
376         Point p = new Point(x, y);
377         return p;
378     }
379 
380     public double Calculate(Point p) {
381         return this.a * p.x + this.b * p.y + c;
382     }
383 
384     //点在线的同一侧
385     public boolean The_same_side(Point p1, Point p2) {
386         if ((Calculate(p1) > 0 && Calculate(p2) > 0) || (Calculate(p1) < 0 && Calculate(p2) < 0)) {
387             return true;
388         } else {
389             return false;
390         }
391     }
392 }
393 
394 class Delta {
395     Point p3, p4, p5;
396     Line l1, l2, l3;
397 
398     Delta() {
399     }
400 
401     Delta(Point p1, Point p2, Point p3) {
402         this.p3 = p1;
403         this.p4 = p2;
404         this.p5 = p3;
405         this.l1 = new Line(this.p3, this.p4);
406         this.l2 = new Line(this.p4, this.p5);
407         this.l3 = new Line(this.p3, this.p5);
408     }
409 
410     public boolean isTriangle() {
411         if (this.p3.equal(this.p4) || this.p3.equal(this.p5) || this.p4.equal(this.p5)) {
412             return false;
413         }
414         if (this.l1.getDistance(this.p5) < 0.00001 || this.l2.getDistance(this.p3) < 0.00001 || this.l3.getDistance(this.p4) < 0.00001) {
415             return false;
416         }
417         return true;
418     }
419 
420 
421     public static double area(Point p1, Point p2, Point p3) {
422         double[] length = new double[3];
423         length[0] = p1.distance(p2);
424         length[1] = p2.distance(p3);
425         length[2] = p3.distance(p1);
426         double p = (length[1] + length[0] + length[2]) / 2;
427         return Math.sqrt(p * (p - length[0]) * (p - length[1]) * (p - length[2]));
428     }
429 
430 
431     void intersection_point(Line l) {
432         Point temp1 = new Point();
433         Point temp2 = new Point();
434         Point temp3 = new Point();
435         temp1 = l.InterSection(this.l1);
436         temp2 = l.InterSection(this.l2);
437         temp3 = l.InterSection(this.l3);
438         int num1 = 0;
439         int num2 = 0;
440         int num3 = 0;
441         if (l.IsSame(this.l3) || l.IsSame(this.l2) || l.IsSame(this.l1)) {
442             System.out.println("The line is coincide with one of the lines");
443             return;
444         }
445         if (this.l1.IsMid(temp1)) {
446             num1++;
447         }
448         if (this.l2.IsMid(temp2)) {
449             num2++;
450         }
451         if (this.l3.IsMid(temp3)) {
452             num3++;
453         }
454         if (num1 + num2 + num3 == 2) {
455             if (num1 == 1 && num2 == 1) {
456                 if (temp1.equal(temp2)) {
457                     System.out.println("1");
458                 } else {
459                     Line tempLine = new Line(temp1, temp2);
460                     double areaAll = 0.5 * l1.length * l1.getDistance(this.p5);
461                     double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.p4);
462                     Show_area(area, areaAll - area);
463                 }
464             } else if (num1 == 1 && num3 == 1) {
465                 if (temp1.equal(temp3)) {
466                     System.out.println("1");
467                 } else {
468                     Line tempLine = new Line(temp1, temp3);
469                     double areaAll = 0.5 * l1.length * l1.getDistance(this.p5);
470                     double area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(this.p3);
471                     Show_area(area, areaAll - area);
472                 }
473             } else if (num2 == 1 && num3 == 1) {
474                 if (temp2.equal(temp3)) {
475                     System.out.println("1");
476                 } else {
477                     Line tempLine = new Line(temp2, temp3);
478                     double areaAll = 0.5 * l1.length * l1.getDistance(this.p5);
479                     double area = 0.5 * temp3.distance(temp2) * tempLine.getDistance(this.p5);
480                     Show_area(area, areaAll - area);
481                 }
482             }
483         } else if (num1 + num2 + num3 == 3) {
484             if (temp1.equal(temp2)) {
485                 Line tempLine = new Line(temp1, temp3);
486                 double areaAll = 0.5 * l1.length * l1.getDistance(this.p5);
487                 double area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(this.p3);
488                 Show_area(area, areaAll - area);
489             } else if (temp1.equal(temp3)) {
490                 Line tempLine = new Line(temp2, temp3);
491                 double areaAll = 0.5 * l1.length * l1.getDistance(this.p5);
492                 double area = 0.5 * temp3.distance(temp2) * tempLine.getDistance(this.p5);
493                 Show_area(area, areaAll - area);
494             } else if (temp2.equal(temp3)) {
495                 Line tempLine = new Line(temp1, temp2);
496                 double areaAll = 0.5 * l1.length * l1.getDistance(this.p5);
497                 double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.p4);
498                 Show_area(area, areaAll - area);
499             }
500         } else {
501             System.out.println(num1 + num2 + num3);
502         }
503     }
504 
505     public static void Show_area(double area1, double area2) {
506         if (area1 > area2) {
507             System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
508         } else {
509             System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
510         }
511     }
512 
513 
514     public void IsInTheTriangle(Point p) {
515         Point temp1 = new Point();
516         Point temp2 = new Point();
517         Line l = new Line();
518         l.p1 = p;
519         l.a = this.l1.a;
520         l.b = this.l1.b;
521         l.c = -l.a * l.p1.x - l.b * l.p1.y;
522         temp1 = l.InterSection(this.l2);
523         temp2 = l.InterSection(this.l3);
524         l.p1 = temp1;
525         l.p2 = temp2;
526         if ((this.l1.getDistance(p) == 0 && l1.IsMid(p)) || (this.l2.getDistance(p) == 0 && l2.IsMid(p)) || (l3.IsMid(p) && this.l3.getDistance(p) == 0)) {
527             System.out.println("on the triangle");
528             return;
529         }
530         if ((this.l1.getDistance(p) == 0 && !l1.IsMid(p)) || (this.l2.getDistance(p) == 0 && !l2.IsMid(p)) || (!l3.IsMid(p) && this.l3.getDistance(p) == 0)) {
531             System.out.println("outof the triangle");
532             return;
533         }
534         if (temp1.equal(temp2)) {
535             System.out.println("outof the triangle");
536             return;
537         }
538         if (!this.l2.IsMid(temp1) || !this.l3.IsMid(temp2)) {
539             System.out.println("outof the triangle");
540             return;
541         }
542         if (this.l2.IsMid(temp1) && this.l3.IsMid(temp2)) {
543             if (l.IsMid(p)) {
544                 System.out.println("in the triangle");
545                 return;
546             } else {
547                 System.out.println("outof the triangle");
548                 return;
549             }
550         }
551     }
552 }
553 
554 class Quadrilateral {
555     public Point[] points;
556     public Line[] lines;
557 
558     Quadrilateral() {
559     }
560 
561     Quadrilateral(Point p1, Point p2, Point p3, Point p4) {
562         this.points = new Point[4];
563         this.lines = new Line[4];
564         this.points[0] = p1;
565         this.points[1] = p2;
566         this.points[2] = p3;
567         this.points[3] = p4;
568         this.lines[0] = new Line(p1, p2);
569         this.lines[1] = new Line(p2, p3);
570         this.lines[2] = new Line(p3, p4);
571         this.lines[3] = new Line(p4, p1);
572 
573 //        if (p1.equal(p2) || p1.equal(p3) || p1.equal(p4) || p2.equal(p3) || p2.equal(p4) || p3.equal(p4)) {
574 //            System.out.println("points coincide");
575 //            System.exit(0);
576 //        }
577 //        if (this.lines[0].isParallel(this.lines[1]) || this.lines[0].isParallel(this.lines[3]) || this.lines[2].isParallel(this.lines[1]) || this.lines[2].isParallel(this.lines[3])) {
578 //            System.out.println("not a quadrilateral");
579 //            System.exit(0);
580 //        }
581 //
582 //        Point p0 = this.lines[0].InterSection(this.lines[2]);
583 //        Point p5 = this.lines[1].InterSection(this.lines[3]);
584 //        if (p0 != null && (this.lines[0].IsMid(p0) && this.lines[2].IsMid(p0)) || p5 != null && (this.lines[1].IsMid(p5) && this.lines[3].IsMid(p5))) {
585 //            System.out.println("not a quadrilateral");
586 //            System.exit(0);
587 //        }
588     }
589 
590     public boolean isQuadrilateral() {
591         if (this.lines[0].isParallel(this.lines[1]) || this.lines[0].isParallel(this.lines[3]) || this.lines[2].isParallel(this.lines[1]) || this.lines[2].isParallel(this.lines[3])) {
592             return false;
593         }
594 
595         Point p0 = null;
596         Point p5 = null;
597         if(!this.lines[0].isParallel(this.lines[2])){
598             p0 = this.lines[0].InterSection(this.lines[2]);
599         }
600         if(!this.lines[1].isParallel(this.lines[3])){
601             p5 = this.lines[1].InterSection(this.lines[3]);
602         }
603         if (p0 != null && (this.lines[0].IsMid(p0) && this.lines[2].IsMid(p0)) || p5 != null && (this.lines[1].IsMid(p5) && this.lines[3].IsMid(p5))) {
604             return false;
605         }
606         return true;
607     }
608 
609     //是否为平行四边形
610     public boolean isParallelQuadrilateral() {
611         return this.lines[0].isParallel(this.lines[2]) && this.lines[1].isParallel(this.lines[3]);
612     }
613 
614     //是否为菱形
615     public boolean isDiamond() {
616         boolean flag = this.lines[1].length == this.lines[0].length && this.lines[1].length == this.lines[2].length && this.lines[2].length == this.lines[3].length;
617         return this.isParallelQuadrilateral() && flag;
618     }
619 
620     //是否为矩形
621     public boolean isRectangle() {
622         return this.points[0].distance(this.points[2]) == this.points[1].distance(this.points[3]);
623     }
624 
625     //是否为正方形
626     public boolean isSquare() {
627         return this.isDiamond() && this.isRectangle();
628     }
629 
630     //是否为凸四边形
631     public boolean isConvexQuadrilateral() {
632         double[] s = new double[4];
633         s[0] = Delta.area(this.points[0], this.points[1], this.points[2]);
634         s[1] = Delta.area(this.points[1], this.points[2], this.points[3]);
635         s[2] = Delta.area(this.points[0], this.points[1], this.points[3]);
636         s[3] = Delta.area(this.points[0], this.points[2], this.points[3]);
637         return Math.abs(s[0] + s[1] - s[2] - s[3]) < 0.0001;
638     }
639 
640     //四边形周长
641     public double sideLength() {
642         return this.lines[0].length + this.lines[1].length + this.lines[2].length + this.lines[3].length;
643     }
644 
645     //四边形面积
646     public double area() {
647         double[] s = new double[4];
648         s[0] = Delta.area(this.points[0], this.points[1], this.points[2]);
649         s[1] = Delta.area(this.points[0], this.points[2], this.points[3]);
650 
651         s[2] = Delta.area(this.points[0], this.points[1], this.points[3]);
652         s[3] = Delta.area(this.points[1], this.points[2], this.points[3]);
653 
654         return Math.min(s[0] + s[1], s[2] + s[3]);
655     }
656 
657     //线切割四边形
658     public void InterSection_Line(Line l) {
659         int num1 = 0;
660         int num2 = 0;
661         int num3 = 0;
662         int num4 = 0;
663         Point temp1;
664         Point temp2;
665         Point temp3;
666         Point temp4;
667         temp1 = l.InterSection(this.lines[0]);
668         temp2 = l.InterSection(this.lines[1]);
669         temp3 = l.InterSection(this.lines[2]);
670         temp4 = l.InterSection(this.lines[3]);
671         if (l.IsSame(this.lines[0]) || l.IsSame(this.lines[1]) || l.IsSame(this.lines[2]) || l.IsSame(this.lines[3])) {
672             System.out.println("The line is coincide with one of the lines");
673             return;
674         }
675         if (this.lines[0].IsMid(temp1)) {
676             num1++;
677         }
678         if (this.lines[1].IsMid(temp2)) {
679             num2++;
680         }
681         if (this.lines[2].IsMid(temp3)) {
682             num3++;
683         }
684         if (this.lines[3].IsMid(temp4)) {
685             num4++;
686         }
687         int sum = num1 + num2 + num3 + num4;
688         double areaAll = area();
689         if (sum == 2) {
690             if (num1 == 1 && num2 == 1) {
691                 if (temp1.equal(temp2)) {
692                     System.out.println("1");
693                 } else {
694                     Line tempLine = new Line(temp1, temp2);
695                     double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.points[1]);
696                     Delta.Show_area(area, areaAll);
697                 }
698             } else if (num1 == 1 && num3 == 1) {
699                 Line tempLine = new Line(temp1, temp3);
700                 Quadrilateral q = new Quadrilateral(temp1, temp3, this.points[2], this.points[1]);
701                 double area = q.area();
702                 Delta.Show_area(area, areaAll - area);
703             } else if (num1 == 1 && num4 == 1) {
704                 if (temp1.equal(temp4)) {
705                     System.out.println("1");
706                 } else {
707                     Line tempLine = new Line(temp1, temp4);
708                     //Quadrilateral q = new Quadrilateral(temp1, temp3, this.points[1], this.points[2]);
709                     double area = 0.5 * temp1.distance(temp4) * tempLine.getDistance(this.points[0]);
710                     Delta.Show_area(area, areaAll - area);
711                 }
712             } else if (num2 == 1 && num3 == 1) {
713                 if (temp2.equal(temp3)) {
714                     System.out.println("1");
715                 } else {
716                     Line tempLine = new Line(temp2, temp3);
717                     //Quadrilateral q = new Quadrilateral(temp1, temp3, this.points[1], this.points[2]);
718                     double area = 0.5 * temp2.distance(temp3) * tempLine.getDistance(this.points[2]);
719                     Delta.Show_area(area, areaAll - area);
720                 }
721             } else if (num2 == 1 && num4 == 1) {
722                 Line tempLine = new Line(temp2, temp4);
723                 Quadrilateral q = new Quadrilateral(temp2, temp4, this.points[0], this.points[1]);
724                 double area = q.area();
725                 Delta.Show_area(area, areaAll - area);
726             } else if (num3 == 1 && num4 == 1) {
727                 if (temp3.equal(temp4)) {
728                     System.out.println("1");
729                 } else {
730                     Line tempLine = new Line(temp3, temp4);
731                     //Quadrilateral q = new Quadrilateral(temp2, temp4, this.points[0], this.points[1]);
732                     double area = 0.5 * temp3.distance(temp4) * tempLine.getDistance(this.points[3]);
733                     Delta.Show_area(area, areaAll - area);
734                 }
735             }
736         } else if (sum == 3) {
737             if (num1 == 1 && num2 == 1 && num3 == 1) {
738                 if (temp1.equal(temp2)) {
739                     Line tempLine = new Line(temp1, temp3);
740                     double area = 0.5 * tempLine.getDistance(this.points[2]) * tempLine.length;
741                     Delta.Show_area(area, areaAll - area);
742                 } else if (temp2.equal(temp3)) {
743                     Line tempLine = new Line(temp1, temp2);
744                     double area = 0.5 * tempLine.getDistance(this.points[1]) * tempLine.length;
745                     Delta.Show_area(area, areaAll - area);
746                 }
747             } else if (num1 == 1 && num2 == 1 && num4 == 1) {
748                 if (temp1.equal(temp2)) {
749                     Line tempLine = new Line(temp1, temp4);
750                     double area = 0.5 * tempLine.getDistance(this.points[0]) * tempLine.length;
751                     Delta.Show_area(area, areaAll - area);
752                 } else if (temp1.equal(temp4)) {
753                     Line tempLine = new Line(temp1, temp2);
754                     double area = 0.5 * tempLine.getDistance(this.points[1]) * tempLine.length;
755                     Delta.Show_area(area, areaAll - area);
756                 }
757             } else if (num1 == 1 && num3 == 1 && num4 == 1) {
758                 if (temp1.equal(temp4)) {
759                     Line tempLine = new Line(temp1, temp2);
760                     double area = 0.5 * tempLine.getDistance(this.points[1]) * tempLine.length;
761                     Delta.Show_area(area, areaAll - area);
762                 } else if (temp3.equal(temp4)) {
763                     Line tempLine = new Line(temp1, temp4);
764                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[0]);
765                     Delta.Show_area(area, areaAll - area);
766                 }
767             } else if (num2 == 1 && num3 == 1 && num4 == 1) {
768                 if (temp2.equal(temp3)) {
769                     Line tempLine = new Line(temp2, temp4);
770                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[3]);
771                     Delta.Show_area(area, areaAll - area);
772                 } else if (temp3.equal(temp4)) {
773                     Line tempLine = new Line(temp2, temp3);
774                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[2]);
775                     Delta.Show_area(area, areaAll - area);
776                 }
777             }
778         } else if (sum == 4) {
779             if (this.isConvexQuadrilateral()) {
780                 if (temp1.equal(temp2) && temp3.equal(temp4)) {
781                     Line tempLine = new Line(temp1, temp3);
782                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[0]);
783                     Delta.Show_area(area, areaAll - area);
784                 } else if (temp1.equal(temp4) && temp3.equal(temp2)) {
785                     Line tempLine = new Line(temp1, temp2);
786                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[1]);
787                     Delta.Show_area(area, areaAll - area);
788                 }
789             } else {
790                 Line tempLine1 = new Line(this.points[0], this.points[2]);
791                 Line tempLine2 = new Line(this.points[1], this.points[3]);
792                 if (tempLine1.The_same_side(this.points[1], this.points[3])) {
793 
794                 } else if (tempLine2.The_same_side(this.points[0], this.points[3])) {
795 
796                 }
797 
798             }
799         }
800     }
801 
802     //点是否在四边形内
803 public void IsInTheQuadrilateral(Point p) {
804         Point temp1 = null;
805         Point temp2 = null;
806         Point temp3 = null;
807         Line l = new Line();
808         l.p1 = p;
809         l.a = this.lines[0].a;
810         l.b = this.lines[0].b;
811         l.c = -l.a * l.p1.x - l.b * l.p1.y;
812         temp1 = l.InterSection(this.lines[1]);
813         temp2 = l.InterSection(this.lines[2]);
814         temp3 = l.InterSection(this.lines[3]);
815         int temp=0;
816         if ((this.lines[0].getDistance(p) == 0 && this.lines[0].IsMid(p)) || (this.lines[1].getDistance(p) == 0 && this.lines[1].IsMid(p)) || (this.lines[2].getDistance(p) == 0 && this.lines[2].IsMid(p)) || (this.lines[3].getDistance(p) == 0 && this.lines[3].IsMid(p))) {
817             System.out.println("on the quadrilateral");
818             return;
819         }
820         if ((this.lines[0].getDistance(p) == 0 && !this.lines[0].IsMid(p)) || (this.lines[1].getDistance(p) == 0 && !this.lines[1].IsMid(p)) || (this.lines[2].getDistance(p) == 0 && !this.lines[2].IsMid(p)) || (this.lines[3].getDistance(p) == 0 && !this.lines[3].IsMid(p))) {
821 //             System.out.println("outof the quadrilateral");
822 //             return;
823             
824         }
825         if (temp1.equal(temp2) || temp2.equal(temp3)) {
826 //             System.out.println("outof the quadrilateral");
827 //             return;
828         }
829         if ((this.lines[1].IsMid(temp1) && this.lines[2].IsMid(temp2))) {
830             l.p2 = temp2;
831             l.p1 = temp1;
832             if (l.IsMid(p)) {
833 //                 System.out.println("in the quadrilateral");
834 //                 return;
835                 temp++;
836             } else {
837 //                 System.out.println("outof the quadrilateral");
838 //                 return;
839             }
840         }
841     if(this.lines[1].IsMid(temp1) && this.lines[3].IsMid(temp3)){
842             l.p2 = temp3;
843             l.p1 = temp1;
844             if (l.IsMid(p)) {
845 //                 System.out.println("in the quadrilateral");
846 //                 return;
847                 temp++;
848             } else {
849 //                 System.out.println("outof the quadrilateral");
850 //                 return;
851             }
852         }
853     if(this.lines[3].IsMid(temp3) && this.lines[2].IsMid(temp2)){
854             l.p2 = temp3;
855             l.p1 = temp2;
856             if (l.IsMid(p)) {
857 //                 System.out.println("in the quadrilateral");
858 //                 return;
859                 temp++;
860             } else {
861 //                 System.out.println("outof the quadrilateral");
862 //                 return;
863             }
864         }
865     if(temp==0){
866         System.out.println("outof the quadrilateral");
867     }else{
868         System.out.println("in the quadrilateral");
869     }
870     }
871 }
View Code
复制代码

这里的判断点在四边形内与三角形内相同,应该全部提取出来写一个父类。

这次难度较难,在判断点在四边形内修改了多次。

题目集5(7-1)

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。

以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例1:

选项1,点重合。例如:

1:-1,-1 1,2 -1,1 1,0
 

输出样例:

在这里给出相应的输出。例如:

wrong number of points

SourceMonito报表

 类图:

 完整代码:

复制代码
   1 import java.text.DecimalFormat;
   2 import java.util.Scanner;
   3 
   4 public class Main {
   5     public static void main(String[] args) {
   6         Scanner sc = new Scanner(System.in);
   7         String s = sc.nextLine();
   8         String s2[] = null;
   9         String s1[] = null;
  10         String s3[] = null;
  11         String s4 = "";
  12         double[] d = new double[30];
  13 
  14         if (!s.matches("^[1-5]:(([+-]?(0|(0\\.\\d+)|[1-9]\\d*(\\.\\d+)?)),([+-]?(0|(0\\.\\d+)|[1-9]\\d*(\\.\\d+)?))\\s?)+$")) {
  15             System.out.println("Wrong Format");
  16             System.exit(0);
  17         }
  18         int index = 0;
  19         s3 = s.split(":");
  20         s1 = s3[1].split(" ");
  21         for (int i = 0; i < s1.length; i++) {
  22             s2 = s1[i].split(",");
  23             for (int j = 0; j < s2.length; j++) {
  24                 if (!check(s2[j])) {
  25                     System.out.println("Wrong Format");
  26                     System.exit(0);
  27                 }
  28                 d[index++] = Double.valueOf(s2[j]);
  29             }
  30         }
  31 
  32         int num = 0;
  33         Point[] parr = new Point[10];
  34         for (int i = 0; i < index; i++) {
  35             if (i % 2 == 0) {
  36                 parr[num++] = new Point(d[i], d[i + 1]);
  37             }
  38         }
  39 
  40         switch (s.charAt(0)) {
  41             case '1':
  42                 if (index == 10) {
  43                     Pentagon p = new Pentagon(parr, 5);
  44                     System.out.println(p.IsPolygon());
  45                 } else {
  46                     System.out.println("wrong number of points");
  47                 }
  48                 break;
  49             case '2':
  50                 if (index == 10) {
  51                     Pentagon p = new Pentagon(parr, 5);
  52                     if (p.IsPolygon()) {
  53                         if (p.isConvexPolygon()) {
  54                             System.out.println(true + " " + new DecimalFormat("0.0##").format(p.sideLength()) + " " + p.getArea());
  55                         } else {
  56                             System.out.println(false);
  57                         }
  58                     } else {
  59                         System.out.println("not a pentagon");
  60                     }
  61                 } else {
  62                     System.out.println("wrong number of points");
  63                 }
  64                 break;
  65             case '3':
  66                 if (index == 14) {
  67                     if (parr[0].equal(parr[1])) {
  68                         System.out.println("points coincide");
  69                         System.exit(0);
  70                     }
  71                     Point[] point = {parr[2], parr[3], parr[4], parr[5], parr[6]};
  72                     Pentagon p = new Pentagon(point, 5);
  73                     Line l = new Line(parr[0], parr[1]);
  74                     if (!Point_equal(point) && p.IsPolygon()) {
  75                         p.Intersection(l);
  76                         System.exit(0);
  77                     }
  78                     if (parr[2].equal(parr[3])) {
  79                         Point[] p5 = {parr[3], parr[4], parr[5], parr[6]};
  80                         Quadrilateral q = new Quadrilateral(p5, 4);
  81                         if (q.IsPolygon()) {
  82                             q.Intersection(l);
  83                             System.exit(0);
  84                         } else {
  85                             sj(q, l);
  86                         }
  87                     } else if (parr[3].equal(parr[4]) || parr[4].equal(parr[5])) {
  88                         Point[] p4 = {parr[2], parr[3], parr[5], parr[6]};
  89                         Quadrilateral q = new Quadrilateral(p4, 4);
  90                         if (q.IsPolygon()) {
  91                             q.Intersection(l);
  92                             System.exit(0);
  93 
  94                         }else{
  95                             sj(q, l);
  96                         }
  97                     } else if (parr[5].equal(parr[6]) || parr[6].equal(parr[2])) {
  98                         Point[] p1 = {parr[2], parr[3], parr[4], parr[5]};
  99                         Quadrilateral q = new Quadrilateral(p1, 4);
 100                         if (q.IsPolygon()) {
 101                             q.Intersection(l);
 102                             System.exit(0);
 103 
 104                         }else{
 105                             sj(q, l);
 106                         }
 107                     } else if (p.lines[0].isParallel(p.lines[1])) {
 108                         Line temp = new Line(point[0], point[2]);
 109                         if (temp.IsMid(point[1])) {
 110                             Point[] points = {point[0], point[2], point[3], point[4]};
 111                             Quadrilateral q = new Quadrilateral(points, 4);
 112                             q.Intersection(l);
 113                         } else {
 114                             System.out.println("not a polygon");
 115                         }
 116                     } else if (p.lines[1].isParallel(p.lines[2])) {
 117                         Line temp = new Line(point[1], point[3]);
 118                         if (temp.IsMid(point[2])) {
 119                             Point[] points = {point[0], point[1], point[3], point[4]};
 120                             Quadrilateral q = new Quadrilateral(points, 4);
 121                             q.Intersection(l);
 122                         } else {
 123                             System.out.println("not a polygon");
 124                         }
 125                     } else if (p.lines[2].isParallel(p.lines[3])) {
 126                         Line temp = new Line(point[2], point[4]);
 127                         if (temp.IsMid(point[3])) {
 128                             Point[] points = {point[0], point[1], point[2], point[4]};
 129                             Quadrilateral q = new Quadrilateral(points, 4);
 130                             q.Intersection(l);
 131                         } else {
 132                             System.out.println("not a polygon");
 133                         }
 134                     } else if (p.lines[3].isParallel(p.lines[4])) {
 135                         Line temp = new Line(point[3], point[0]);
 136                         if (temp.IsMid(point[4])) {
 137                             Point[] points = {point[0], point[1], point[2], point[3]};
 138                             Quadrilateral q = new Quadrilateral(points, 4);
 139                             q.Intersection(l);
 140                         } else {
 141                             System.out.println("not a polygon");
 142                         }
 143                     } else if (p.lines[4].isParallel(p.lines[0])) {
 144                         Line temp = new Line(point[1], point[4]);
 145                         if (temp.IsMid(point[0])) {
 146                             Point[] points = {point[1], point[2], point[3], point[4]};
 147                             Quadrilateral q = new Quadrilateral(points, 4);
 148                             q.Intersection(l);
 149                         } else {
 150                             System.out.println("not a polygon");
 151                         }
 152                     }
 153                 } else {
 154                     System.out.println("wrong number of points");
 155                 }
 156                 break;
 157         }
 158 
 159     }
 160 
 161     public static boolean check(String str) {
 162         return str.matches("^[+-]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$");
 163     }
 164 
 165     public static boolean Point_equal(Point[] points) {
 166         for (int i = 0; i < points.length; i++) {
 167             for (int j = i + 1; j < points.length; j++) {
 168                 if (points[i].equal(points[j])) {
 169                     return true;
 170                 }
 171             }
 172         }
 173         return false;
 174     }
 175 
 176     public static void judgeDelta(Triangle D) {
 177         if (!D.IsPolygon()) {
 178             System.out.println("not a polygon");
 179             System.exit(0);
 180         }
 181     }
 182 
 183     public static void sj(Quadrilateral q, Line l) {
 184         Point[] parr = new Point[4];
 185         for (int i = 0; i < 4; i++) {
 186             parr[i] = q.points[i];
 187         }
 188         if (parr[0].equal(parr[1])) {
 189             Point[] points = {parr[0], parr[2], parr[3]};
 190             Triangle D = new Triangle(points, 3);
 191             judgeDelta(D);
 192             D.Intersection(l);
 193         } else if (parr[0].equal(parr[2]) || parr[1].equal(parr[2])) {
 194             Point[] points = {parr[0], parr[1], parr[3]};
 195             Triangle D = new Triangle(points, 3);
 196             judgeDelta(D);
 197             D.Intersection(l);
 198         } else if (parr[0].equal(parr[3]) || parr[1].equal(parr[3]) || parr[2].equal(parr[3])) {
 199             Point[] points = {parr[0], parr[1], parr[2]};
 200             Triangle D = new Triangle(points, 3);
 201             judgeDelta(D);
 202             D.Intersection(l);
 203         } else if (q.lines[0].getSlope() == q.lines[1].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope()) {
 204             Line temp = new Line(parr[0], parr[2]);
 205             if (!temp.IsMid(parr[1])) {
 206                 System.out.println("not a polygon");
 207                 System.exit(0);
 208             }
 209             Point[] points = {parr[0], parr[2], parr[3]};
 210             Triangle D = new Triangle(points, 3);
 211             judgeDelta(D);
 212             D.Intersection(l);
 213         } else if (q.lines[0].getSlope() == q.lines[3].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope()) {
 214             Line temp = new Line(parr[1], parr[3]);
 215             if (!temp.IsMid(parr[0])) {
 216                 System.out.println("not a polygon");
 217                 System.exit(0);
 218             }
 219             Point[] points = {parr[1], parr[2], parr[3]};
 220             Triangle D = new Triangle(points, 3);
 221             judgeDelta(D);
 222             D.Intersection(l);
 223         } else if (q.lines[1].getSlope() == q.lines[2].getSlope() && q.lines[2].getSlope() != q.lines[3].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope()) {
 224             Line temp = new Line(parr[1], parr[3]);
 225             if (!temp.IsMid(parr[2])) {
 226                 System.out.println("not a polygon");
 227                 System.exit(0);
 228             }
 229             Point[] points = {parr[0], parr[1], parr[3]};
 230             Triangle D = new Triangle(points, 3);
 231             judgeDelta(D);
 232             D.Intersection(l);
 233         } else if (q.lines[2].getSlope() == q.lines[3].getSlope() && q.lines[0].getSlope() != q.lines[1].getSlope() && q.lines[0].getSlope() != q.lines[3].getSlope() && q.lines[1].getSlope() != q.lines[2].getSlope()) {
 234             Line temp = new Line(parr[0], parr[2]);
 235             if (!temp.IsMid(parr[3])) {
 236                 System.out.println("not a polygon");
 237                 System.exit(0);
 238             }
 239             Point[] points = {parr[0], parr[1], parr[2]};
 240             Triangle D = new Triangle(points, 3);
 241             judgeDelta(D);
 242             D.Intersection(l);
 243         } else if (q.lines[2].getSlope() == q.lines[3].getSlope() && q.lines[0].getSlope() == q.lines[1].getSlope() || q.lines[0].getSlope() == q.lines[3].getSlope() && q.lines[1].getSlope() == q.lines[2].getSlope()) {
 244             System.out.println("not a polygon");
 245             System.exit(0);
 246         } else {
 247             System.out.println("not a polygon");
 248             System.exit(0);
 249         }
 250     }
 251 }
 252 
 253 class Point {
 254     double x, y;
 255 
 256     Point() {
 257     }
 258 
 259     Point(double x, double y) {
 260         this.x = x;
 261         this.y = y;
 262     }
 263 
 264     double distance(Point p) {
 265         return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
 266     }
 267 
 268     boolean equal(Point p) {
 269         if (this.x == p.x && this.y == p.y) {
 270             return true;
 271         } else {
 272             return false;
 273         }
 274     }
 275 }
 276 
 277 class Line {
 278     Point p1, p2;
 279     double a, b, c;
 280     double length;
 281 
 282 
 283     Line() {
 284     }
 285 
 286     Line(Point p1, Point p2) {
 287         this.p1 = p1;
 288         this.p2 = p2;
 289         this.a = p2.y - p1.y;
 290         this.b = p1.x - p2.x;
 291         this.c = p2.x * p1.y - p1.x * p2.y;
 292         this.length = p1.distance(p2);
 293     }
 294 
 295 
 296     public double getSlope() {
 297         if (b == 0) {
 298             return 9999;
 299         } else {
 300             return -a / b;
 301         }
 302     }
 303 
 304     public double getDistance(Point p) {
 305         return Math.abs(this.a * p.x + this.b * p.y + c) / Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
 306     }
 307 
 308     public boolean isParallel(Line l) {
 309         if (this.getSlope() == l.getSlope())
 310             return true;
 311         else
 312             return false;
 313     }
 314 
 315     public boolean IsSame(Line l) {
 316         if (this.getSlope() == l.getSlope() && this.getDistance(l.p1) == 0)
 317             return true;
 318         else
 319             return false;
 320     }
 321 
 322     public boolean IsMid(Point p) {
 323         if (this.b != 0) {
 324             if ((this.p2.x >= p.x && this.p1.x <= p.x) || (this.p2.x <= p.x && this.p1.x >= p.x)) {
 325                 return true;
 326             } else {
 327                 return false;
 328             }
 329         } else {
 330             if ((this.p2.y >= p.y && this.p1.y <= p.y) || (this.p2.y <= p.y && this.p1.y >= p.y)) {
 331                 return true;
 332             } else {
 333                 return false;
 334             }
 335         }
 336     }
 337 
 338     public double getLength() {
 339         this.length = this.p1.distance(this.p2);
 340         return this.length;
 341     }
 342 
 343     public Point InterSection(Line l) {
 344         double temp = this.a * l.b - l.a * this.b;
 345         double x = (this.b * l.c - l.b * this.c) / temp;
 346         double y = (l.a * this.c - this.a * l.c) / temp;
 347         Point p = new Point(x, y);
 348         return p;
 349     }
 350 
 351     public double Calculate(Point p) {
 352         return this.a * p.x + this.b * p.y + c;
 353     }
 354 
 355     //点在线的同一侧
 356     public boolean The_same_side(Point p1, Point p2) {
 357         if ((Calculate(p1) > 0 && Calculate(p2) > 0) || (Calculate(p1) < 0 && Calculate(p2) < 0)) {
 358             return true;
 359         } else {
 360             return false;
 361         }
 362     }
 363 }
 364 
 365 class Triangle extends Polygon {
 366 
 367     Triangle() {
 368     }
 369 
 370     Triangle(Point[] points, int num) {
 371         super(points, num);
 372     }
 373 
 374     @Override
 375     public boolean IsPolygon() {
 376         if (this.points[0].equal(this.points[1]) || this.points[0].equal(this.points[2]) || this.points[1].equal(this.points[2])) {
 377             return false;
 378         }
 379         if (this.lines[0].getDistance(this.points[2]) < 0.00001 || this.lines[1].getDistance(this.points[0]) < 0.00001 || this.lines[2].getDistance(this.points[1]) < 0.00001) {
 380             return false;
 381         }
 382         return true;
 383     }
 384 
 385 
 386     public static double area(Point p1, Point p2, Point p3) {
 387         double[] length = new double[3];
 388         length[0] = p1.distance(p2);
 389         length[1] = p2.distance(p3);
 390         length[2] = p3.distance(p1);
 391         double p = (length[1] + length[0] + length[2]) / 2;
 392         return Math.sqrt(p * (p - length[0]) * (p - length[1]) * (p - length[2]));
 393     }
 394 
 395 
 396     @Override
 397     public void Intersection(Line l) {
 398         Point temp1 = new Point();
 399         Point temp2 = new Point();
 400         Point temp3 = new Point();
 401         temp1 = l.InterSection(this.lines[0]);
 402         temp2 = l.InterSection(this.lines[1]);
 403         temp3 = l.InterSection(this.lines[2]);
 404         int num1 = 0;
 405         int num2 = 0;
 406         int num3 = 0;
 407         if (l.IsSame(this.lines[2]) || l.IsSame(this.lines[1]) || l.IsSame(this.lines[0])) {
 408             System.out.println("The line is coincide with one of the lines");
 409             return;
 410         }
 411         if (this.lines[0].IsMid(temp1)) {
 412             num1++;
 413         }
 414         if (this.lines[1].IsMid(temp2)) {
 415             num2++;
 416         }
 417         if (this.lines[2].IsMid(temp3)) {
 418             num3++;
 419         }
 420         double areaAll = 0.5 * this.lines[0].length * this.lines[0].getDistance(this.points[2]);
 421         if (num1 + num2 + num3 == 2) {
 422             if (num1 == 1 && num2 == 1) {
 423                 if (temp1.equal(temp2)) {
 424                     System.out.println("1");
 425                 } else {
 426                     Line tempLine = new Line(temp1, temp2);
 427                     double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.points[1]);
 428                     Show_area(area, areaAll - area);
 429                 }
 430             } else if (num1 == 1 && num3 == 1) {
 431                 if (temp1.equal(temp3)) {
 432                     System.out.println("1");
 433                 } else {
 434                     Line tempLine = new Line(temp1, temp3);
 435                     double area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(this.points[0]);
 436                     Show_area(area, areaAll - area);
 437                 }
 438             } else if (num2 == 1 && num3 == 1) {
 439                 if (temp2.equal(temp3)) {
 440                     System.out.println("1");
 441                 } else {
 442                     Line tempLine = new Line(temp2, temp3);
 443                     double area = 0.5 * temp3.distance(temp2) * tempLine.getDistance(this.points[2]);
 444                     Show_area(area, areaAll - area);
 445                 }
 446             }
 447         } else if (num1 + num2 + num3 == 3) {
 448             if (temp1.equal(temp2)) {
 449                 Line tempLine = new Line(temp1, temp3);
 450                 double area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(this.points[0]);
 451                 Show_area(area, areaAll - area);
 452             } else if (temp1.equal(temp3)) {
 453                 Line tempLine = new Line(temp2, temp3);
 454                 double area = 0.5 * temp3.distance(temp2) * tempLine.getDistance(this.points[3]);
 455                 Show_area(area, areaAll - area);
 456             } else if (temp2.equal(temp3)) {
 457                 Line tempLine = new Line(temp1, temp2);
 458                 double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.points[2]);
 459                 Show_area(area, areaAll - area);
 460             }
 461         } else {
 462             System.out.println(num1 + num2 + num3);
 463         }
 464     }
 465 
 466 
 467     @Override
 468     public int IsInThePolygon(Point p) {
 469         Point temp1 = new Point();
 470         Point temp2 = new Point();
 471         Line l = new Line();
 472         l.p1 = p;
 473         l.a = this.lines[0].a;
 474         l.b = this.lines[0].b;
 475         l.c = -l.a * l.p1.x - l.b * l.p1.y;
 476         temp1 = l.InterSection(this.lines[1]);
 477         temp2 = l.InterSection(this.lines[2]);
 478         l.p1 = temp1;
 479         l.p2 = temp2;
 480         int temp = 0;
 481         if ((this.lines[0].getDistance(p) == 0 && lines[0].IsMid(p)) || (this.lines[2].getDistance(p) == 0 && lines[2].IsMid(p)) || (lines[2].IsMid(p) && this.lines[2].getDistance(p) == 0)) {
 482             //System.out.println("on the triangle");
 483             return 0;
 484         }
 485 //        if ((this.lines[0].getDistance(p) == 0 && !lines[0].IsMid(p)) || (this.l2.getDistance(p) == 0 && !l2.IsMid(p)) || (!l3.IsMid(p) && this.l3.getDistance(p) == 0)) {
 486 //            System.out.println("outof the triangle");
 487 //            return;
 488 //        }
 489 //        if (temp1.equal(temp2)) {
 490 //            System.out.println("outof the triangle");
 491 //            return;
 492 //        }
 493 //        if (!this.l2.IsMid(temp1) || !this.l3.IsMid(temp2)) {
 494 //            System.out.println("outof the triangle");
 495 //            return;
 496 //        }
 497         if (this.lines[1].IsMid(temp1) && this.lines[2].IsMid(temp2)) {
 498             if (l.IsMid(p)) {
 499                 temp++;
 500             }
 501         }
 502         if (temp == 0) {
 503 //            System.out.println("in the triangle");
 504             return 1;
 505         } else {
 506 //            System.out.println("outof the triangle");
 507             return -1;
 508         }
 509     }
 510 }
 511 
 512 class Quadrilateral extends Polygon {
 513 
 514     Quadrilateral() {
 515     }
 516 
 517     Quadrilateral(Point[] points, int num) {
 518         super(points, num);
 519     }
 520 
 521     @Override
 522     public boolean IsPolygon() {
 523         if (this.lines[0].isParallel(this.lines[1]) || this.lines[0].isParallel(this.lines[3]) || this.lines[2].isParallel(this.lines[1]) || this.lines[2].isParallel(this.lines[3])) {
 524             return false;
 525         }
 526 
 527         Point p0 = this.lines[0].InterSection(this.lines[2]);
 528         Point p5 = this.lines[1].InterSection(this.lines[3]);
 529         if (p0 != null && (this.lines[0].IsMid(p0) && this.lines[2].IsMid(p0)) || p5 != null && (this.lines[1].IsMid(p5) && this.lines[3].IsMid(p5))) {
 530             return false;
 531         }
 532         return true;
 533     }
 534 
 535     //是否为平行四边形
 536     public boolean isParallelQuadrilateral() {
 537         return this.lines[0].isParallel(this.lines[2]) && this.lines[1].isParallel(this.lines[3]);
 538     }
 539 
 540     //是否为菱形
 541     public boolean isDiamond() {
 542         boolean flag = this.lines[1].length == this.lines[0].length && this.lines[1].length == this.lines[2].length && this.lines[2].length == this.lines[3].length;
 543         return this.isParallelQuadrilateral() && flag;
 544     }
 545 
 546     //是否为矩形
 547     public boolean isRectangle() {
 548         return this.points[0].distance(this.points[2]) == this.points[1].distance(this.points[3]);
 549     }
 550 
 551     //是否为正方形
 552     public boolean isSquare() {
 553         return this.isDiamond() && this.isRectangle();
 554     }
 555 
 556     //是否为凸四边形
 557     public boolean isConvexQuadrilateral() {
 558         double[] s = new double[4];
 559         s[0] = Triangle.area(this.points[0], this.points[1], this.points[2]);
 560         s[1] = Triangle.area(this.points[1], this.points[2], this.points[3]);
 561         s[2] = Triangle.area(this.points[0], this.points[1], this.points[3]);
 562         s[3] = Triangle.area(this.points[0], this.points[2], this.points[3]);
 563         return Math.abs(s[0] + s[1] - s[2] - s[3]) < 0.0001;
 564     }
 565 
 566     //四边形周长
 567     public double sideLength() {
 568         return this.lines[0].length + this.lines[1].length + this.lines[2].length + this.lines[3].length;
 569     }
 570 
 571     //四边形面积
 572     public double area() {
 573         double[] s = new double[4];
 574         s[0] = Triangle.area(this.points[0], this.points[1], this.points[2]);
 575         s[1] = Triangle.area(this.points[0], this.points[2], this.points[3]);
 576 
 577         s[2] = Triangle.area(this.points[0], this.points[1], this.points[3]);
 578         s[3] = Triangle.area(this.points[1], this.points[2], this.points[3]);
 579 
 580         return Math.min(s[0] + s[1], s[2] + s[3]);
 581     }
 582 
 583     //线切割四边形
 584     @Override
 585     public void Intersection(Line l) {
 586         int num1 = 0;
 587         int num2 = 0;
 588         int num3 = 0;
 589         int num4 = 0;
 590         Point temp1;
 591         Point temp2;
 592         Point temp3;
 593         Point temp4;
 594         temp1 = l.InterSection(this.lines[0]);
 595         temp2 = l.InterSection(this.lines[1]);
 596         temp3 = l.InterSection(this.lines[2]);
 597         temp4 = l.InterSection(this.lines[3]);
 598         if (l.IsSame(this.lines[0]) || l.IsSame(this.lines[1]) || l.IsSame(this.lines[2]) || l.IsSame(this.lines[3])) {
 599             System.out.println("The line is coincide with one of the lines");
 600             return;
 601         }
 602         if (this.lines[0].IsMid(temp1)) {
 603             num1++;
 604         }
 605         if (this.lines[1].IsMid(temp2)) {
 606             num2++;
 607         }
 608         if (this.lines[2].IsMid(temp3)) {
 609             num3++;
 610         }
 611         if (this.lines[3].IsMid(temp4)) {
 612             num4++;
 613         }
 614         int sum = num1 + num2 + num3 + num4;
 615         double areaAll = area();
 616         if (sum == 2) {
 617             if (num1 == 1 && num2 == 1) {
 618                 if (temp1.equal(temp2)) {
 619                     System.out.println("1");
 620                 } else {
 621                     Line tempLine = new Line(temp1, temp2);
 622                     double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.points[1]);
 623                     Show_area(area, areaAll - area);
 624                 }
 625             } else if (num1 == 1 && num3 == 1) {
 626                 Line tempLine = new Line(temp1, temp3);
 627                 Point[] point = {temp1, temp3, this.points[2], this.points[1]};
 628                 Quadrilateral q = new Quadrilateral(point, 4);
 629                 double area = q.area();
 630                 Show_area(area, areaAll - area);
 631             } else if (num1 == 1 && num4 == 1) {
 632                 if (temp1.equal(temp4)) {
 633                     System.out.println("1");
 634                 } else {
 635                     Line tempLine = new Line(temp1, temp4);
 636                     //Quadrilateral q = new Quadrilateral(temp1, temp3, this.points[1], this.points[2]);
 637                     double area = 0.5 * temp1.distance(temp4) * tempLine.getDistance(this.points[0]);
 638                     Show_area(area, areaAll - area);
 639                 }
 640             } else if (num2 == 1 && num3 == 1) {
 641                 if (temp2.equal(temp3)) {
 642                     System.out.println("1");
 643                 } else {
 644                     Line tempLine = new Line(temp2, temp3);
 645                     //Quadrilateral q = new Quadrilateral(temp1, temp3, this.points[1], this.points[2]);
 646                     double area = 0.5 * temp2.distance(temp3) * tempLine.getDistance(this.points[2]);
 647                     Show_area(area, areaAll - area);
 648                 }
 649             } else if (num2 == 1 && num4 == 1) {
 650                 Line tempLine = new Line(temp2, temp4);
 651                 Point[] point = {temp2, temp4, this.points[0], this.points[1]};
 652                 Quadrilateral q = new Quadrilateral(point, 4);
 653                 double area = q.area();
 654                 Show_area(area, areaAll - area);
 655             } else if (num3 == 1 && num4 == 1) {
 656                 if (temp3.equal(temp4)) {
 657                     System.out.println("1");
 658                 } else {
 659                     Line tempLine = new Line(temp3, temp4);
 660                     //Quadrilateral q = new Quadrilateral(temp2, temp4, this.points[0], this.points[1]);
 661                     double area = 0.5 * temp3.distance(temp4) * tempLine.getDistance(this.points[3]);
 662                     Show_area(area, areaAll - area);
 663                 }
 664             }
 665         } else if (sum == 3) {
 666             if (num1 == 1 && num2 == 1 && num3 == 1) {
 667                 if (temp1.equal(temp2)) {
 668                     Line tempLine = new Line(temp1, temp3);
 669                     double area = 0.5 * tempLine.getDistance(this.points[2]) * tempLine.length;
 670                     Show_area(area, areaAll - area);
 671                 } else if (temp2.equal(temp3)) {
 672                     Line tempLine = new Line(temp1, temp2);
 673                     double area = 0.5 * tempLine.getDistance(this.points[1]) * tempLine.length;
 674                     Show_area(area, areaAll - area);
 675                 }
 676             } else if (num1 == 1 && num2 == 1 && num4 == 1) {
 677                 if (temp1.equal(temp2)) {
 678                     Line tempLine = new Line(temp1, temp4);
 679                     double area = 0.5 * tempLine.getDistance(this.points[0]) * tempLine.length;
 680                     Show_area(area, areaAll - area);
 681                 } else if (temp1.equal(temp4)) {
 682                     Line tempLine = new Line(temp1, temp2);
 683                     double area = 0.5 * tempLine.getDistance(this.points[1]) * tempLine.length;
 684                     Show_area(area, areaAll - area);
 685                 }
 686             } else if (num1 == 1 && num3 == 1 && num4 == 1) {
 687                 if (temp1.equal(temp4)) {
 688                     Line tempLine = new Line(temp1, temp2);
 689                     double area = 0.5 * tempLine.getDistance(this.points[1]) * tempLine.length;
 690                     Show_area(area, areaAll - area);
 691                 } else if (temp3.equal(temp4)) {
 692                     Line tempLine = new Line(temp1, temp4);
 693                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[0]);
 694                     Show_area(area, areaAll - area);
 695                 }
 696             } else if (num2 == 1 && num3 == 1 && num4 == 1) {
 697                 if (temp2.equal(temp3)) {
 698                     Line tempLine = new Line(temp2, temp4);
 699                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[3]);
 700                     Show_area(area, areaAll - area);
 701                 } else if (temp3.equal(temp4)) {
 702                     Line tempLine = new Line(temp2, temp3);
 703                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[2]);
 704                     Show_area(area, areaAll - area);
 705                 }
 706             }
 707         } else if (sum == 4) {
 708             if (this.isConvexQuadrilateral()) {
 709                 if (temp1.equal(temp2) && temp3.equal(temp4)) {
 710                     Line tempLine = new Line(temp1, temp3);
 711                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[0]);
 712                     Show_area(area, areaAll - area);
 713                 } else if (temp1.equal(temp4) && temp3.equal(temp2)) {
 714                     Line tempLine = new Line(temp1, temp2);
 715                     double area = 0.5 * tempLine.length * tempLine.getDistance(this.points[1]);
 716                     Show_area(area, areaAll - area);
 717                 }
 718             } else {
 719                 Line tempLine1 = new Line(this.points[0], this.points[2]);
 720                 Line tempLine2 = new Line(this.points[1], this.points[3]);
 721                 if (tempLine1.The_same_side(this.points[1], this.points[3])) {
 722 
 723                 } else if (tempLine2.The_same_side(this.points[0], this.points[3])) {
 724 
 725                 }
 726 
 727             }
 728         } else {
 729             System.out.println(sum);
 730         }
 731     }
 732 
 733     //点是否在四边形内
 734     @Override
 735     public int IsInThePolygon(Point p) {
 736         Point temp1 = null;
 737         Point temp2 = null;
 738         Point temp3 = null;
 739         Line l = new Line();
 740         l.p1 = p;
 741         l.a = this.lines[0].a;
 742         l.b = this.lines[0].b;
 743         l.c = -l.a * l.p1.x - l.b * l.p1.y;
 744         temp1 = l.InterSection(this.lines[1]);
 745         temp2 = l.InterSection(this.lines[2]);
 746         temp3 = l.InterSection(this.lines[3]);
 747         int temp = 0;
 748         if ((this.lines[0].getDistance(p) == 0 && this.lines[0].IsMid(p)) || (this.lines[1].getDistance(p) == 0 && this.lines[1].IsMid(p)) || (this.lines[2].getDistance(p) == 0 && this.lines[2].IsMid(p)) || (this.lines[3].getDistance(p) == 0 && this.lines[3].IsMid(p))) {
 749             System.out.println("on the quadrilateral");
 750             return 0;
 751         }
 752 //        if ((this.lines[0].getDistance(p) == 0 && !this.lines[0].IsMid(p)) || (this.lines[1].getDistance(p) == 0 && !this.lines[1].IsMid(p)) || (this.lines[2].getDistance(p) == 0 && !this.lines[2].IsMid(p)) || (this.lines[3].getDistance(p) == 0 && !this.lines[3].IsMid(p))) {
 753 //            System.out.println("outof the quadrilateral");
 754 //            return;
 755 //        }
 756 //        if (temp1.equal(temp2) || temp2.equal(temp3)) {
 757 //            System.out.println("outof the quadrilateral");
 758 //            return;
 759 //        }
 760         if ((this.lines[1].IsMid(temp1) && this.lines[2].IsMid(temp2))) {
 761             l.p2 = temp2;
 762             l.p1 = temp1;
 763             if (l.IsMid(p)) {
 764 //                System.out.println("in the quadrilateral");
 765 //                return 1;
 766                 temp++;
 767             }
 768         }
 769         if (this.lines[1].IsMid(temp1) && this.lines[3].IsMid(temp3)) {
 770             l.p2 = temp3;
 771             l.p1 = temp1;
 772             if (l.IsMid(p)) {
 773 //                System.out.println("in the quadrilateral");
 774 //                return;
 775                 temp++;
 776             }
 777         }
 778         if (this.lines[3].IsMid(temp3) && this.lines[2].IsMid(temp2)) {
 779             l.p2 = temp3;
 780             l.p1 = temp2;
 781             if (l.IsMid(p)) {
 782 //                System.out.println("in the quadrilateral");
 783 //                return;
 784                 temp++;
 785             }
 786         }
 787         if (temp == 0) {
 788             return -1;
 789         } else {
 790             return 1;
 791         }
 792     }
 793 }
 794 
 795 class Pentagon extends Polygon {
 796 
 797     Pentagon() {
 798     }
 799 
 800     Pentagon(Point[] parr, int num) {
 801         super(parr, num);
 802     }
 803 
 804     @Override
 805     public boolean IsPolygon() {
 806         if (lines[0].isParallel(lines[1]) || lines[1].isParallel(lines[2]) || lines[2].isParallel(lines[3]) || lines[3].isParallel(lines[4]) || lines[4].isParallel(lines[0])) {
 807             return false;
 808         }
 809 
 810         Point[] p = new Point[5];
 811         if (!lines[0].isParallel(lines[2])) {
 812             p[0] = lines[0].InterSection(lines[2]);
 813         }
 814         if (!lines[0].isParallel(lines[3])) {
 815             p[1] = lines[1].InterSection(lines[3]);
 816         }
 817         if (!lines[1].isParallel(lines[3])) {
 818             p[2] = lines[1].InterSection(lines[3]);
 819         }
 820         if (!lines[1].isParallel(lines[4])) {
 821             p[3] = lines[1].InterSection(lines[4]);
 822         }
 823         if (!lines[2].isParallel(lines[4])) {
 824             p[4] = lines[2].InterSection(lines[4]);
 825         }
 826 
 827         if ((p[0] != null && lines[0].IsMid(p[0]) && lines[2].IsMid(p[0]))
 828                 || (p[1] != null && lines[0].IsMid(p[1]) && lines[3].IsMid(p[1]))
 829                 || (p[2] != null && lines[1].IsMid(p[2]) && lines[3].IsMid(p[2]))
 830                 || (p[3] != null && lines[1].IsMid(p[3]) && lines[4].IsMid(p[3]))
 831                 || (p[4] != null && lines[2].IsMid(p[4]) && lines[4].IsMid(p[4]))) {
 832             return false;
 833         }
 834 
 835         return true;
 836     }
 837 
 838     @Override
 839     public int IsInThePolygon(Point p) {
 840         return 0;
 841     }
 842 
 843     @Override
 844     public void Intersection(Line l) {
 845         int num1 = 0;
 846         int num2 = 0;
 847         int num3 = 0;
 848         int num4 = 0;
 849         int num5 = 0;
 850         Point temp1 = null;
 851         Point temp2 = null;
 852         Point temp3 = null;
 853         Point temp4 = null;
 854         Point temp5 = null;
 855         if (l.IsSame(lines[0]) || l.IsSame(lines[1]) || l.IsSame(lines[2]) || l.IsSame(lines[3]) || l.IsSame(lines[4])) {
 856             System.out.println("The line is coincide with one of the lines");
 857             return;
 858         }
 859         if (!l.isParallel(lines[0])) {
 860             temp1 = l.InterSection(lines[0]);
 861         }
 862         if (!l.isParallel(lines[1])) {
 863             temp2 = l.InterSection(lines[1]);
 864         }
 865         if (!l.isParallel(lines[2])) {
 866             temp3 = l.InterSection(lines[2]);
 867         }
 868         if (!l.isParallel(lines[3])) {
 869             temp4 = l.InterSection(lines[3]);
 870         }
 871         if (!l.isParallel(lines[4])) {
 872             temp5 = l.InterSection(lines[4]);
 873         }
 874 
 875         if (temp1 != null && lines[0].IsMid(temp1)) {
 876             num1++;
 877         }
 878         if (temp2 != null && lines[1].IsMid(temp2)) {
 879             num2++;
 880         }
 881         if (temp3 != null && lines[2].IsMid(temp3)) {
 882             num3++;
 883         }
 884         if (temp4 != null && lines[3].IsMid(temp4)) {
 885             num4++;
 886         }
 887         if (temp5 != null && lines[4].IsMid(temp5)) {
 888             num5++;
 889         }
 890         int sum = num1 + num2 + num3 + num4 + num5;
 891         double areaAll = this.getArea();
 892         double area = 0;
 893         if (sum == 2) {
 894             if (num1 == 1 && num2 == 1) {
 895                 if (temp1.equal(temp2)) {
 896                     System.out.println("1");
 897                     System.exit(0);
 898                 }
 899                 Line tempLine = new Line(temp1, temp2);
 900                 area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(points[1]);
 901                 Show_area(area, areaAll - area);
 902             } else if (num1 == 1 && num3 == 1) {
 903                 Line tempLine = new Line(temp1, temp3);
 904                 Point[] point = {temp1, temp3, this.points[2], this.points[1]};
 905                 Quadrilateral q = new Quadrilateral(point, 4);
 906                 area = q.getArea();
 907                 Show_area(area, areaAll - area);
 908             } else if (num1 == 1 && num4 == 1) {
 909                 Line tempLine = new Line(temp1, temp4);
 910                 Point[] point = {temp1, temp4, this.points[4], this.points[0]};
 911                 Quadrilateral q = new Quadrilateral(point, 4);
 912                 area = q.getArea();
 913                 Show_area(area, areaAll - area);
 914             } else if (num1 == 1 && num5 == 1) {
 915                 if (temp1.equal(temp5)) {
 916                     System.out.println("1");
 917                     System.exit(0);
 918                 }
 919                 Line tempLine = new Line(temp1, temp5);
 920                 area = 0.5 * temp1.distance(temp5) * tempLine.getDistance(points[0]);
 921                 Show_area(area, areaAll - area);
 922             } else if (num2 == 1 && num3 == 1) {
 923                 if (temp2.equal(temp3)) {
 924                     System.out.println("1");
 925                     System.exit(0);
 926                 }
 927                 Line tempLine = new Line(temp2, temp3);
 928                 area = 0.5 * temp2.distance(temp3) * tempLine.getDistance(points[2]);
 929             } else if (num2 == 1 && num4 == 1) {
 930                 Point[] point = {temp2, temp4, points[3], points[2]};
 931                 Quadrilateral q = new Quadrilateral(point, 4);
 932                 area = q.getArea();
 933             } else if (num2 == 1 && num5 == 1) {
 934                 Point[] point = {temp2, temp5, points[0], points[1]};
 935                 Quadrilateral q = new Quadrilateral(point, 4);
 936                 area = q.getArea();
 937             } else if (num3 == 1 && num4 == 1) {
 938                 if (temp3.equal(temp4)) {
 939                     System.out.println("1");
 940                     System.exit(0);
 941                 }
 942                 Line tempLine = new Line(temp3, temp4);
 943                 area = 0.5 * temp3.distance(temp4) * tempLine.getDistance(points[3]);
 944             } else if (num3 == 1 && num5 == 1) {
 945                 Point[] point = {temp3, temp5, points[4], points[3]};
 946                 Quadrilateral q = new Quadrilateral(point, 4);
 947                 area = q.getArea();
 948             } else if (num4 == 1 && num5 == 1) {
 949                 if (temp5.equal(temp4)) {
 950                     System.out.println("1");
 951                     System.exit(0);
 952                 }
 953                 Line tempLine = new Line(temp4, temp5);
 954                 area = 0.5 * temp5.distance(temp4) * tempLine.getDistance(points[4]);
 955             }
 956         } else if (sum == 3) {
 957             if (num1 == 1 && num5 == 1) {
 958                 if (num2 == 1) {
 959                     Line tempLine = new Line(temp1, temp2);
 960                     area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(points[1]);
 961                 } else if (num3 == 1) {
 962                     Point[] point = {temp1, temp3, points[2], points[1]};
 963                     Quadrilateral q = new Quadrilateral(point, 4);
 964                     area = q.getArea();
 965                 } else if (num4 == 1) {
 966                     Line tempLine = new Line(temp1, temp4);
 967                     area = 0.5 * temp1.distance(temp4) * tempLine.getDistance(points[4]);
 968                 }
 969             } else if (num1 == 1 && num2 == 1) {
 970                 if (num3 == 1) {
 971                     Line tempLine = new Line(temp2, temp3);
 972                     area = 0.5 * temp2.distance(temp3) * tempLine.getDistance(points[2]);
 973                 } else if (num4 == 1) {
 974                     Point[] point = {temp2, temp4, points[3], points[2]};
 975                     Quadrilateral q = new Quadrilateral(point, 4);
 976                     area = q.getArea();
 977                 } else if (num5 == 1) {
 978                     Line tempLine = new Line(temp2, temp5);
 979                     area = 0.5 * temp2.distance(temp5) * tempLine.getDistance(points[0]);
 980                 }
 981             } else if (num2 == 1 && num3 == 1) {
 982                 if (num1 == 1) {
 983                     Line tempLine = new Line(temp1, temp3);
 984                     area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(points[1]);
 985                 } else if (num4 == 1) {
 986                     Line tempLine = new Line(temp4, temp3);
 987                     area = 0.5 * temp3.distance(temp4) * tempLine.getDistance(points[3]);
 988                 } else if (num5 == 1) {
 989                     Point[] point = {temp3, temp5, points[4], points[3]};
 990                 }
 991             } else if (num3 == 1 && num4 == 1) {
 992                 if (num1 == 1) {
 993                     Point[] point = {temp4, temp1, points[1], points[2]};
 994                     Quadrilateral q = new Quadrilateral(point, 4);
 995                     area = q.getArea();
 996                 } else if (num2 == 1) {
 997                     Line tempLine = new Line(temp4, temp2);
 998                     area = 0.5 * tempLine.length * tempLine.getDistance(points[2]);
 999                 } else if (num5 == 1) {
1000                     Line tempLine = new Line(temp4, temp5);
1001                     area = 0.5 * tempLine.length * tempLine.getDistance(points[4]);
1002                 }
1003             } else if (num4 == 1 && num5 == 1) {
1004                 if (num1 == 1) {
1005                     Line tempLine = new Line(temp5, temp1);
1006                     area = 0.5 * tempLine.length * tempLine.getDistance(points[0]);
1007                 } else if (num2 == 1) {
1008                     Point[] point = {temp5, temp2, points[1], points[0]};
1009                     Quadrilateral q = new Quadrilateral(point, 4);
1010                     area = q.getArea();
1011                 } else if (num3 == 1) {
1012                     Line tempLine = new Line(temp5, temp3);
1013                     area = 0.5 * tempLine.length * tempLine.getDistance(points[3]);
1014                 }
1015             }
1016         } else if (sum == 4) {
1017             if (num1 == 1 && num5 == 1) {
1018                 if (num2 == 1 && num3 == 1) {
1019                     Line tempLine = new Line(temp1, temp2);
1020                     area = 0.5 * tempLine.length * tempLine.getDistance(points[1]);
1021                 } else if (num3 == 1 && num4 == 1) {
1022                     Line tempLine = new Line(temp1, temp3);
1023                     area = 0.5 * tempLine.length * tempLine.getDistance(points[4]);
1024                 }
1025             } else if (num1 == 1 && num2 == 1) {
1026                 if (num3 == 1 && num4 == 1) {
1027                     Line tempLine = new Line(temp1, temp3);
1028                     area = 0.5 * tempLine.length * tempLine.getDistance(points[2]);
1029                 } else if (num4 == 1 && num5 == 1) {
1030                     Line tempLine = new Line(temp1, temp4);
1031                     area = 0.5 * tempLine.length * tempLine.getDistance(points[0]);
1032                 }
1033             } else if (num2 == 1 && num3 == 1) {
1034                 if (num4 == 1 && num5 == 1) {
1035                     Line tempLine = new Line(temp2,temp4);
1036                     area = 0.5*tempLine.length*tempLine.getDistance(points[3]);
1037                 }
1038             }
1039         } else {
1040             System.out.println(sum);
1041             return;
1042         }
1043         Show_area(area, areaAll - area);
1044     }
1045 }
1046 
1047 abstract class Polygon {
1048     public Point[] points;
1049     public Line[] lines;
1050     public int SideNumber;
1051 
1052     public Polygon() {
1053 
1054     }
1055 
1056     public Polygon(Point[] points, int num) {
1057         this.points = points;
1058         this.SideNumber = num;
1059         lines = new Line[num];
1060         for (int i = 0; i < SideNumber; i++) {
1061             lines[i] = new Line(points[i % SideNumber], points[(i + 1) % SideNumber]);
1062         }
1063     }
1064 
1065     public Point[] getPoints() {
1066         return points;
1067     }
1068 
1069     public void setPoints(Point[] points) {
1070         this.points = points;
1071     }
1072 
1073     public Line[] getLines() {
1074         return lines;
1075     }
1076 
1077     public void setLines(Line[] lines) {
1078         this.lines = lines;
1079     }
1080 
1081     public double getArea() {
1082         double s = 0;
1083         for (int i = 0; i < SideNumber; i++) {
1084             s += points[i % SideNumber].x * points[(i + 1) % SideNumber].y - points[i % SideNumber].y * points[(i + 1) % SideNumber].x;
1085         }
1086         return s * 0.5;
1087     }
1088 
1089     public double getArea(Point[] points) {
1090         double s = 0;
1091         for (int i = 0; i < points.length; i++) {
1092             s += points[i % points.length].x * points[(i + 1) % points.length].y - points[i % points.length].y * points[(i + 1) % points.length].x;
1093         }
1094         return s * 0.5;
1095     }
1096 
1097     public abstract boolean IsPolygon();
1098 
1099     //四边形周长
1100     public double sideLength() {
1101         double s = 0;
1102         for (int i = 0; i < SideNumber; i++) {
1103             s += lines[i].length;
1104         }
1105         return s;
1106     }
1107 
1108     public abstract int IsInThePolygon(Point p);
1109 
1110     public void Show(double num){
1111         System.out.println(new DecimalFormat("0.0#####").format(num));
1112     }
1113 
1114     public void Show_area(double area1, double area2) {
1115         if (area1 > area2) {
1116             System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1));
1117         } else {
1118             System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2));
1119         }
1120     }
1121 
1122     //线切割多边形
1123     public abstract void Intersection(Line l);
1124 
1125     public boolean isConvexPolygon() {
1126         while (true) {
1127             //凸多边形
1128             boolean tag = true;
1129             int j, k, t;
1130             for (int i = 0; i < SideNumber; i++) {
1131                 //k,t直接对n求余就行了
1132                 j = i;
1133                 k = i + 1;
1134                 t = i + 2;
1135                 //以三角形为例看看
1136                 if (k == SideNumber) {
1137                     k = 0;
1138                 }
1139                 if (t == SideNumber + 1) {
1140                     t = 1;
1141                 }
1142                 if (t == SideNumber) {
1143                     t = 0;
1144                 }
1145                 //注意是后面减去前面的
1146                 Point p1 = new Point(points[k].x - points[j].x, points[k].y - points[j].y);
1147                 Point p2 = new Point(points[t].x - points[k].x, points[t].y - points[k].y);
1148                 //叉积
1149                 double ans = p1.x * p2.y - p1.y * p2.x;
1150                 if (ans < 0) {
1151                     return false;
1152                 }
1153             }
1154             if (tag) {
1155                 return true;
1156             } else {
1157                 return false;
1158             }
1159         }
1160     }
1161 
1162 }
View Code
复制代码

这次使用了抽象类,但方法大多数是抽象方法,需要重写。

题目集5(7-2)

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

输入样例:

在这里给出一组输入。例如:

4:0,0 6,0 7,1 8,3 6,6 0,0 6,0 7,1 8,3 6,6
 

输出样例:

在这里给出相应的输出。例如:

the previous pentagon coincides with the following pentagon


SourceMonito报表

 类图:

完整代码:

复制代码
  1 import java.text.DecimalFormat;
  2 import java.util.Scanner;
  3 import java.util.Arrays;
  4 import java.util.Comparator;
  5 public class Main {
  6     public static void main(String[] args) {
  7         Scanner sc = new Scanner(System.in);
  8         String s = sc.nextLine();
  9         String s2[] = null;
 10         String s1[] = null;
 11         String s3[] = null;
 12         String s4 = "";
 13         double[] d = new double[30];
 14 
 15         if (!s.matches("^[1-6]:(([+-]?(0|(0\\.\\d+)|[1-9]\\d*(\\.\\d+)?)),([+-]?(0|(0\\.\\d+)|[1-9]\\d*(\\.\\d+)?))\\s?)+$")) {
 16             System.out.println("Wrong Format");
 17             System.exit(0);
 18         }
 19         int index = 0;
 20         s3 = s.split(":");
 21         s1 = s3[1].split(" ");
 22         for (int i = 0; i < s1.length; i++) {
 23             s2 = s1[i].split(",");
 24             for (int j = 0; j < s2.length; j++) {
 25                 if (!check(s2[j])) {
 26                     System.out.println("Wrong Format");
 27                     System.exit(0);
 28                 }
 29                 d[index++] = Double.valueOf(s2[j]);
 30             }
 31         }
 32 
 33         int num = 0;
 34         Point[] parr = new Point[10];
 35         for (int i = 0; i < index; i++) {
 36             if (i % 2 == 0) {
 37                 parr[num++] = new Point(d[i], d[i + 1]);
 38             }
 39         }
 40         pointsCntCheck(s.charAt(0) - '0', index);
 41         switch (s.charAt(0)) {
 42             case '4':
 43                 Polygon p1 = new Polygon(new Point[]{parr[0], parr[1], parr[2], parr[3], parr[4]});
 44                 Polygon p2 = new Polygon(new Point[]{parr[5], parr[6], parr[7], parr[8], parr[9]});
 45                 System.out.println(p1.relationshipWith(p2));
 46 //                 String[] name1 = new String[]{"triangle", "quadrilateral", "pentagon"};
 47 //                 System.out.println("no overlapping area between the previous " + name1[0] + " and the following " + name1[0]);
 48                 break;
 49             case '5':
 50                 Polygon p3 = new Polygon(new Point[]{parr[0], parr[1], parr[2], parr[3], parr[4]});
 51                 Polygon p4 = new Polygon(new Point[]{parr[5], parr[6], parr[7], parr[8], parr[9]});
 52                 System.out.println(new DecimalFormat("0.0##").format(p3.overlappingArea(p4)));
 53                 break;
 54             case '6':
 55                 Polygon p = new Polygon(new Point[]{parr[1], parr[2], parr[3], parr[4], parr[5]});
 56                 int res = p.IsInThePolygon(parr[0]);
 57                 String[] name = {"triangle", "quadrilateral", "pentagon"};
 58                 if (res == -1) {
 59                     System.out.println("outof the " + name[p.len - 3]);
 60                 } else if (res == 0) {
 61                     System.out.println("on the " + name[p.len - 3]);
 62                 } else if (res == 1) {
 63                     System.out.println("in the " + name[p.len - 3]);
 64                 }
 65                 break;
 66         }
 67     }
 68 
 69     public static boolean check(String str) {
 70         return str.matches("^[+-]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$");
 71     }
 72 
 73     public static void pointsCntCheck(int cmd, int cnt) {
 74         if (cmd == 4 || cmd == 5) {
 75             if (cnt != 20) {
 76                 System.out.println("wrong number of points");
 77                 System.exit(0);
 78             }
 79         }
 80         if (cmd == 6) {
 81             if (cnt != 12) {
 82                 System.out.println("wrong number of points");
 83                 System.exit(0);
 84             }
 85         }
 86     }
 87 }
 88 
 89 class Point {
 90     double x, y;
 91 
 92     Point() {
 93     }
 94 
 95     Point(double x, double y) {
 96         this.x = x;
 97         this.y = y;
 98     }
 99 
100     double distance(Point p) {
101         return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2));
102     }
103 
104     boolean equal(Point p) {
105         if (this.x == p.x && this.y == p.y) {
106             return true;
107         } else {
108             return false;
109         }
110     }
111 
112     public Point deepCopy(){
113         Point res = new Point();
114         res.x = this.x;
115         res.y = this.y;
116         return res;
117     }
118 
119     public Point add(Point another){
120         Point res = this.deepCopy();
121         res.x += another.x;
122         res.y += another.y;
123         return res;
124     }
125 
126     public Point sub(Point another){
127         Point res = this.deepCopy();
128         res.x -= another.x;
129         res.y -= another.y;
130         return res;
131     }
132 
133     //求点集重心
134     public static Point focusPoint(Point[] points ){
135         Point res = new Point(0,0);
136         for(Point item:points){
137             res = res.add(item);
138         }
139         res.x /= points.length;
140         res.y /= points.length;
141         return res;
142     }
143 }
144 
145 class Line {
146     Point p1, p2;
147     double a, b, c;
148     double length;
149     private Point vector;
150 
151     Line() {
152     }
153 
154     Line(Point p1, Point p2) {
155         this.p1 = p1;
156         this.p2 = p2;
157         this.a = p2.y - p1.y;
158         this.b = p1.x - p2.x;
159         this.c = p2.x * p1.y - p1.x * p2.y;
160         this.length = p1.distance(p2);
161         this.vector = p2.sub(p1);
162     }
163 
164     public double getSlope() {
165         if (b == 0) {
166             return 9999;
167         } else {
168             return -a / b;
169         }
170     }
171 
172     public double getDistance(Point p) {
173         return Math.abs(this.a * p.x + this.b * p.y + c) / Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2));
174     }
175 
176     public boolean isParallel(Line l) {
177         if (this.getSlope() == l.getSlope())
178             return true;
179         else
180             return false;
181     }
182 
183     public boolean IsSame(Line l) {
184         if (this.getSlope() == l.getSlope() && this.getDistance(l.p1) == 0)
185             return true;
186         else
187             return false;
188     }
189 
190     //判断是否在直线之上
191     public boolean inLine(Point p) {
192         return Math.abs(this.a * p.x + this.b * p.y + this.c) < 0.000001;
193     }
194 
195     //包含端点
196     public boolean IsMid(Point p) {
197         if (!this.inLine(p)) return false;
198         double res = p.distance(this.p1) + p.distance(this.p2) - this.getLength();
199         return Math.abs(res) < 0.000001;
200     }
201 
202     public double getLength() {
203         this.length = this.p1.distance(this.p2);
204         return this.length;
205     }
206 
207     public Point getIntersection(Line l) {
208         if (this.isParallel(l)) {
209             return null;
210         }
211         double temp = this.a * l.b - l.a * this.b;
212         double x = (this.b * l.c - l.b * this.c) / temp;
213         double y = (l.a * this.c - this.a * l.c) / temp;
214         Point p = new Point(x, y);
215         return p;
216     }
217 
218     //获取线段交点
219     public Point LineSegmentIntersection(Line another){
220         Point res = this.getIntersection(another);
221         if(res == null) return res;
222         boolean ok = (this.IsMid(res) && another.IsMid(res));
223         return ok ? res:null;
224     }
225 
226     //判断是否垂直
227     public boolean isVerticalTo(Line another) {
228         return this.a * another.a + this.b * another.b == 0;
229     }
230 
231     //求向量模
232     public double vectorLength() {
233         return Math.sqrt(Math.pow(this.vector.x, 2) + Math.pow(this.vector.y, 2));
234     }
235 
236     //求向量点积
237     public double vectorMul(Line another) {
238         return (this.vector.x * another.vector.x) + (this.vector.y * another.vector.y);
239     }
240 
241     //求向量叉积
242     public double vectorCrossMul(Line another) {
243         return (this.vector.x * another.vector.y) - (another.vector.x * this.vector.y);
244     }
245 
246     //求两向量夹角(非0向量)
247     public double vectorAngle(Line another) {
248         double cos_angle = this.vectorMul(another) / (this.vectorLength() * another.vectorLength());
249         return Math.acos(cos_angle);
250     }
251 }
252 
253 class Triangle extends Polygon {
254 
255     public Triangle() {
256     }
257 
258     public Triangle(Point[] points) {
259         super(points);
260         if (this.status == -1 || this.len != 3) {
261             System.out.println("not a triangle");
262             System.exit(0);
263         }
264     }
265 
266     //求三个点围成的图形面积(三点可能共线,面积为0)
267     public static double area(Point a, Point b, Point c) {
268         double len1 = a.distance(b);
269         double len2 = b.distance(c);
270         double len3 = c.distance(a);
271         double p = (len1 + len2 + len3) / 2;
272         return Math.sqrt(p * (p - len1) * (p - len2) * (p - len3));
273     }
274 }
275 
276 class Quadrilateral extends Polygon {
277 
278     Quadrilateral() {
279     }
280 
281     public Quadrilateral(Point[] points) {
282         super(points);
283         if (this.status == -1 || this.len != 4) {
284             System.out.println("not a quadrilateral");
285             System.exit(0);
286         }
287     }
288 
289     //是否为平行四边形
290     public boolean isParallelQuadrilateral() {
291         return this.lines[0].isParallel(this.lines[2]) && this.lines[1].isParallel(this.lines[3]);
292     }
293 
294     //是否为菱形
295     public boolean isDiamond() {
296         boolean flag = this.lines[1].length == this.lines[0].length && this.lines[1].length == this.lines[2].length && this.lines[2].length == this.lines[3].length;
297         return this.isParallelQuadrilateral() && flag;
298     }
299 
300     //是否为矩形
301     public boolean isRectangle() {
302         return this.points[0].distance(this.points[2]) == this.points[1].distance(this.points[3]);
303     }
304 
305     //是否为正方形
306     public boolean isSquare() {
307         return this.isDiamond() && this.isRectangle();
308     }
309 }
310 
311 class Pentagon extends Polygon {
312 
313     public Pentagon() {
314     }
315 
316     public Pentagon(Point[] parr) {
317         super(parr);
318         if (this.status == -1 || this.len != 5) {
319             System.out.println("not a pentagon");
320             System.exit(0);
321         }
322     }
323 }
324 
325 class Polygon {
326     public int len = 0, status = 1;
327     public Point[] points;
328     public Line[] lines;
329     public boolean isConvexGraphical = true;
330     public double sideLength = 0, area = 0;   //边长,面积
331 
332     public Polygon() {
333     }
334 
335     public Polygon(Point[] points) {
336         this.points = new Point[points.length];
337 
338         points = this.removeMulti(points);  //去除重复点
339         if (points.length <= 2) {
340             this.status = -1;
341             return;
342         }
343 
344         //相邻边夹角0则去除中间点,夹角180则status:-1
345         for (int i = 0; i < points.length; i++) {
346             int first = i, second = (i + 1) % points.length, third = (i + 2) % points.length;
347             Line l1 = new Line(points[first], points[second]);
348             Line l2 = new Line(points[second], points[third]);
349             if (Math.abs(l1.vectorAngle(l2) - Math.PI) < 0.000001) {    //夹角180
350                 this.status = -1;
351                 return;
352             } else if (Math.abs(l1.vectorAngle(l2)) > 0.000001) {   //夹角不为0
353                 this.points[this.len++] = points[second].deepCopy();
354             }
355         }
356         
357         this.points = Arrays.copyOf(this.points, this.len);
358         this.lines = new Line[this.len];
359         
360         for (int i = 0; i < this.len; i++) {
361             this.lines[i] = new Line(this.points[i], this.points[(i + 1) % this.len]);
362         }
363 
364 //        //判断任意不相邻边(线段交点)是否有交点
365         checkEdge();
366         Polygon.area(this);
367         Polygon.sideLength(this);
368         Polygon.checkConvex(this);
369     }
370 
371     //判断不相邻边是否有交点
372     private void checkEdge() {
373         for (int i = 0; i < this.len; i++) {
374             for (int j = i + 2; j < this.len; j++) {
375                 if (i == 0 && j == this.len - 1) continue;
376                 Point p = this.lines[i].getIntersection(this.lines[j]);
377                 if (p == null) continue;
378 
379                 if (this.lines[i].IsMid(p) && this.lines[j].IsMid(p)) {
380                     this.status = -1;
381                     return;
382                 }
383             }
384         }
385     }
386 
387     public static void getArea(Polygon e) {
388         double s = 0;
389         for (int i = 0; i < e.len; i++) {
390             s += e.points[i % e.len].x * e.points[(i + 1) % e.len].y - e.points[i % e.len].y * e.points[(i + 1) % e.len].x;
391         }
392         e.area = s;
393     }
394 
395     public double getArea(Point[] points) {
396         double s = 0;
397         for (int i = 0; i < points.length; i++) {
398             s += points[i % points.length].x * points[(i + 1) % points.length].y - points[i % points.length].y * points[(i + 1) % points.length].x;
399         }
400         return s * 0.5;
401     }
402 
403     //多边形周长
404     private static void sideLength(Polygon e) {
405         double res = 0;
406         for (int i = 0; i < e.len; i++) {
407             res += e.points[i].distance(e.points[(i + 1) % e.len]);
408         }
409         e.sideLength = res;
410     }
411 
412     //判断图形是否包含某个点返回值1,0,-1 (内部,边缘,外部)
413     //由于只考虑凸多边形,用面积法就行
414     public int IsInThePolygon(Point p) {
415         for (int i = 0; i < this.len; i++) {
416             if (Math.abs(lines[i].getDistance(p)) < 0.0000001 && lines[i].IsMid(p)) {
417                 return 0;
418             }
419         }
420         double s = 0;
421         for (int i = 0; i < this.len; i++) {
422             s += Triangle.area(p, this.points[i], this.points[(i + 1) % this.len]);
423         }
424         return Math.abs(s - this.area) < 0.0000001 ? 1 : -1;
425     }
426 
427     private static void area(Polygon e) {
428         double res = 0;
429         Point origin = new Point(0, 0);
430         for (int i = 0; i < e.len; i++) {
431             try {
432                 Line l1 = new Line(origin, e.points[i]);
433                 Line l2 = new Line(origin, e.points[(i + 1) % e.len]);
434                 res += 0.5 * l1.vectorCrossMul(l2);
435             } catch (Exception reason) {
436             }
437 
438         }
439         e.area = Math.abs(res);
440     }
441 
442     public void Show(double num) {
443         System.out.println(new DecimalFormat("0.0#####").format(num));
444     }
445 
446     public void Show_area(double area1, double area2) {
447         if (area1 > area2) {
448             System.out.println("2 " + new DecimalFormat("0.0##").format(area2) + " " + new DecimalFormat("0.0###").format(area1));
449         } else {
450             System.out.println("2 " + new DecimalFormat("0.0##").format(area1) + " " + new DecimalFormat("0.0###").format(area2));
451         }
452     }
453 
454     //计算两个图形的重叠面积(交点加内部顶点构成重叠多边形)
455     public double overlappingArea(Polygon p) {
456         Point[] intersection = new Point[100];
457         int intersection_len = 0;
458 
459         for (Line item1 : this.lines) {   //求出两多边形的交点
460             for (Line item2 : p.lines) {
461                 Point tmp = item1.LineSegmentIntersection(item2);
462                 if (tmp != null) {
463                     intersection[intersection_len++] = tmp.deepCopy();
464                 }
465             }
466         }
467         
468         if (intersection_len == 0) {
469             return 0;
470         }
471 
472         for (Point item : p.points) {   //顶点包含在内部
473             if (this.IsInThePolygon(item) == 1) intersection[intersection_len++] = item.deepCopy();
474         }
475         for (Point item : this.points) {   //顶点包含在内部
476             if (p.IsInThePolygon(item) == 1) intersection[intersection_len++] = item.deepCopy();
477         }
478 
479         /*排序交点数组*/
480         intersection = Arrays.copyOf(intersection, intersection_len);
481         intersection = this.removeMulti(intersection);
482         Point focus = Point.focusPoint(intersection);
483         Point sta = intersection[0].deepCopy();
484 
485         Arrays.sort(intersection, 1, intersection.length, new Comparator<Point>() {
486             @Override
487             public int compare(Point o1, Point o2) {
488                 Line origin = new Line(focus, sta);
489                 Line l1 = new Line(focus, o1);
490                 Line l2 = new Line(focus, o2);
491                 double angle1 = origin.vectorAngle(l1);
492                 double angle2 = origin.vectorAngle(l2);
493                 if (origin.vectorCrossMul(l1) < 0) angle1 = 2 * Math.PI - angle1;
494                 if (origin.vectorCrossMul(l2) < 0) angle2 = 2 * Math.PI - angle2;
495                 if (angle1 - angle2 > 0.000001) return 1;
496                 if (Math.abs(angle1 - angle2) < 0.000001) return 0;
497                 return -1;
498             }
499         });
500 
501         Polygon p1 = new Polygon(intersection);
502         return p1.area;
503     }
504 
505     //去除所有重复点
506     private Point[] removeMulti(Point[] points) {
507         Point[] tmp_points = new Point[points.length];
508         int tmp_len = 0;
509 
510         for (int i = 0; i < points.length; i++) {
511             boolean ok = true;
512             for (int j = 0; j < tmp_len; j++) {
513                 if (points[i].equal(tmp_points[j])) {
514                     ok = false;
515                     break;
516                 }
517             }
518             if (ok) tmp_points[tmp_len++] = points[i].deepCopy();
519         }
520         return Arrays.copyOf(tmp_points, tmp_len);
521     }
522 
523     public static void checkConvex(Polygon e) {
524         if (e.len == 3) return;
525         int v = 0;
526         for (int i = 0; i < e.len; i++) {
527             int first = i, second = (i + 1) % e.len, thrid = (i + 2) % e.len;
528             try {
529                 Line l1 = new Line(e.points[first], e.points[second]);
530                 Line l2 = new Line(e.points[first], e.points[thrid]);
531                 if (v == 0) {
532                     if (l1.vectorCrossMul(l2) > 0) v = 1;
533                     else v = -1;
534                 }
535                 if (v == 1 && l1.vectorCrossMul(l2) < 0) e.isConvexGraphical = false;
536                 if (v == -1 && l1.vectorCrossMul(l2) > 0) e.isConvexGraphical = false;
537             } catch (Exception reason) {
538             }
539         }
540     }
541 
542     //判断两个图形类之间的关系()
543     public String relationshipWith(Polygon p) {
544         String[] name = new String[]{"triangle", "quadrilateral", "pentagon"};
545         //分离
546         if (this.isSeparatedFrom(p)) {
547             return "no overlapping area between the previous " + name[this.len - 3] + " and the following " + name[p.len - 3];
548         }
549         //完全重合
550         if (this.isSameTo(p))
551             return "the previous " + name[this.len - 3] + " coincides with the following " + name[p.len - 3];
552         //包含
553         if (this.isContainPol(p)) {
554             return "the previous " + name[this.len - 3] + " contains the following " + name[p.len - 3];
555         }
556         //被包含
557         if (p.isContainPol(this)) {
558             return "the previous " + name[this.len - 3] + " is inside the following " + name[p.len - 3];
559         }
560 
561         //连接
562         if (this.overlappingArea(p) == 0) {
563             return "the previous " + name[this.len - 3] + " is connected to the following " + name[p.len - 3];
564         }else{
565             //交错
566             return "the previous " + name[this.len - 3] + " is interlaced with the following " + name[p.len - 3];
567         }
568     }
569 
570     //判断两个图形是否一模一样(点完全重合)
571     public boolean isSameTo(Polygon g) {
572         if (this.len != g.len) return false;
573         for (int i = 0; i < this.len; i++) {
574             boolean ok = false;
575             for (int j = 0; j < g.len; j++) {
576                 if (this.points[i].equal(g.points[j])) ok = true;
577             }
578             if (!ok) return false;
579         }
580         return true;
581     }
582 
583     //判断完全包含另一个图形(任意点都在this之内)
584     public boolean isContainPol(Polygon p) {
585         boolean ok = true;
586         int[] check2 = new int[p.len];
587         for (int i = 0; i < p.len; i++) {
588             check2[i] = this.IsInThePolygon(p.points[i]);
589         }
590         for (int item : check2) {
591             if (item == -1) ok = false;
592         }
593         return ok;
594     }
595 
596     //判断和另一个图形完全分离(重叠面积为0,并且任意点都在this之外)
597     public boolean isSeparatedFrom(Polygon p) {
598         boolean ok = true;
599         int[] check2 = new int[p.len];
600         for (int i = 0; i < p.len; i++) {
601             check2[i] = this.IsInThePolygon(p.points[i]);
602         }
603         for (int item : check2) {
604             if (item != -1) ok = false;
605         }
606         if (this.overlappingArea(p) != 0) ok = false;
607         return ok;
608     }
609 }
View Code
复制代码

这次使用了继承关系,并且将大多数方法都放到了父类中使用,大大减少了子类的内容。

期中考试:

(7-1)点与线

  • 设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

  • 设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:

      ```
          The line's color is:颜色值
          The line's begin point's Coordinate is:
          (x1,y1)
          The line's end point's Coordinate is:
          (x2,y2)
          The line's length is:长度值
      ```
    
     

    其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

      设计类图如下图所示。
    
     

1641304523(1).jpg

** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
 

输入样例1:

在这里给出一组输入。例如:

5
9.4
12.3
84
Red
 

输出样例1:

在这里给出相应的输出。例如:

The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
 

输入样例2:

在这里给出一组输入。例如:

80.2356
352.12
24.5
100
Black
 

输出样例2:

在这里给出相应的输出。例如:

Wrong Format


SourceMonito报表

 

类图: 
完整代码:
复制代码
  1 import java.util.Scanner;
  2 import java.text.DecimalFormat;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner sc = new Scanner(System.in);
  7         double x, y;
  8         x = sc.nextDouble();
  9         y = sc.nextDouble();
 10         if (x > 200 || x <= 0) {
 11             System.out.println("Wrong Format");
 12             System.exit(0);
 13         }
 14         if (y > 200 || y <= 0) {
 15             System.out.println("Wrong Format");
 16             System.exit(0);
 17         }
 18         Point Point1 = new Point(x, y);
 19         x = sc.nextDouble();
 20         y = sc.nextDouble();
 21         if (x > 200 || x <= 0) {
 22             System.out.println("Wrong Format");
 23             System.exit(0);
 24         }
 25         if (y > 200 || y <= 0) {
 26             System.out.println("Wrong Format");
 27             System.exit(0);
 28         }
 29         Point Point2 = new Point(x, y);
 30         String color = sc.next();
 31         Line line = new Line(Point1, Point2, color);
 32         line.display();
 33     }
 34 }
 35 
 36 class Point {
 37     private double x, y;
 38 
 39     public Point() {
 40 
 41     }
 42 
 43     public Point(double x, double y) {
 44         this.x = x;
 45         this.y = y;
 46     }
 47 
 48     public double getX() {
 49         return x;
 50     }
 51 
 52     public void setX(double x) {
 53         this.x = x;
 54     }
 55 
 56     public double getY() {
 57         return y;
 58     }
 59 
 60     public void setY(double y) {
 61         this.y = y;
 62     }
 63 
 64     public void display() {
 65         System.out.printf("(%.2f,%.2f)", x, y);
 66         System.out.println();
 67     }
 68 }
 69 
 70 class Line {
 71     Point Point1;
 72     Point Point2;
 73     String color;
 74 
 75     public Line() {
 76 
 77     }
 78 
 79     public Line(Point point1, Point point2, String color) {
 80         Point1 = point1;
 81         Point2 = point2;
 82         this.color = color;
 83     }
 84 
 85     public Point getPoint1() {
 86         return Point1;
 87     }
 88 
 89     public void setPoint1(Point point1) {
 90         Point1 = point1;
 91     }
 92 
 93     public Point getPoint2() {
 94         return Point2;
 95     }
 96 
 97     public void setPoint2(Point point2) {
 98         Point2 = point2;
 99     }
100 
101     public String getColor() {
102         return color;
103     }
104 
105     public void setColor(String color) {
106         this.color = color;
107     }
108 
109     public void display() {
110         System.out.println("The line's color is:" + color);
111         System.out.println("The line's begin point's Coordinate is:");
112         this.Point1.display();
113         System.out.println("The line's end point's Coordinate is:");
114         this.Point2.display();
115         double length = Math.sqrt(Math.pow(this.Point1.getX() - this.Point2.getX(), 2) + Math.pow(this.Point1.getY() - this.Point2.getY(), 2));
116         System.out.println("The line's length is:"+new DecimalFormat("0.0#").format(length));
117     }
118 }
View Code
复制代码

这次较为简单,只使用了点线类。

(7-2)点线面问题重构

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

  • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
  • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
  • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
          element.display();
          
          element = p2;//终点Point
          element.display();
          
          element = line;//线段
          element.display();
          
          element = plane;//面
          element.display();
    
     
    类结构如下图所示。

1641340607(1).jpg

其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。

输出格式:

(x1,y1)
(x2,y2)
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
The Plane's color is:颜色值
 

输入样例1:

在这里给出一组输入。例如:

5
9.4
12.3
84
Red
 

输出样例1:

在这里给出相应的输出。例如:

(5.00,9.40)
(12.30,84.00)
The line's color is:Red
The line's begin point's Coordinate is:
(5.00,9.40)
The line's end point's Coordinate is:
(12.30,84.00)
The line's length is:74.96
The Plane's color is:Red
 

输入样例2:

在这里给出一组输入。例如:

5
9.4
12.3
845
Black
 

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

SourceMonito报表

 

类图:
完整代码:
复制代码
  1 import java.util.Scanner;
  2 
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner sc = new Scanner(System.in);
  7         double x, y;
  8         x = sc.nextDouble();
  9         y = sc.nextDouble();
 10         if (x > 200 || x <= 0) {
 11             System.out.println("Wrong Format");
 12             System.exit(0);
 13         }
 14         if (y > 200 || y <= 0) {
 15             System.out.println("Wrong Format");
 16             System.exit(0);
 17         }
 18         Point P1 = new Point(x, y);
 19         x = sc.nextDouble();
 20         y = sc.nextDouble();
 21         if (x > 200 || x <= 0) {
 22             System.out.println("Wrong Format");
 23             System.exit(0);
 24         }
 25         if (y > 200 || y <= 0) {
 26             System.out.println("Wrong Format");
 27             System.exit(0);
 28         }
 29         Point P2 = new Point(x, y);
 30         String color = sc.next();
 31         Line line = new Line(P1, P2, color);
 32         Plane plane = new Plane(color);
 33         Element element;
 34         element = P1;
 35         element.display();
 36         element = P2;
 37         element.display();
 38         element = line;
 39         element.display();
 40         element = plane;
 41         element.display();
 42     }
 43 }
 44 
 45 class Point extends Element {
 46     private double x, y;
 47 
 48     public Point() {
 49 
 50     }
 51 
 52     public Point(double x, double y) {
 53         this.x = x;
 54         this.y = y;
 55     }
 56 
 57     public double getX() {
 58         return x;
 59     }
 60 
 61     public void setX(double x) {
 62         this.x = x;
 63     }
 64 
 65     public double getY() {
 66         return y;
 67     }
 68 
 69     public void setY(double y) {
 70         this.y = y;
 71     }
 72 
 73     public void display() {
 74        System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f", y) + ")");
 75     }
 76 }
 77 
 78 class Line extends Element {
 79     Point Point1;
 80     Point Point2;
 81     String color;
 82 
 83     public Line() {
 84 
 85     }
 86 
 87     public Line(Point point1, Point point2, String color) {
 88         Point1 = point1;
 89         Point2 = point2;
 90         this.color = color;
 91     }
 92 
 93     public Point getPoint1() {
 94         return Point1;
 95     }
 96 
 97     public void setPoint1(Point point1) {
 98         Point1 = point1;
 99     }
100 
101     public Point getPoint2() {
102         return Point2;
103     }
104 
105     public void setPoint2(Point point2) {
106         Point2 = point2;
107     }
108 
109     public String getColor() {
110         return color;
111     }
112 
113     public void setColor(String color) {
114         this.color = color;
115     }
116 
117     public void display() {
118         System.out.println("The line's color is:" + color);
119         System.out.println("The line's begin point's Coordinate is:");
120         this.Point1.display();
121         System.out.println("The line's end point's Coordinate is:");
122         this.Point2.display();
123         double length = Math.sqrt(Math.pow(this.Point1.getX() - this.Point2.getX(), 2) + Math.pow(this.Point1.getY() - this.Point2.getY(), 2));
124         System.out.println("The line's length is:" + String.format("%.2f", length));
125     }
126 }
127 
128 class Plane extends Element {
129     private String color;
130 
131     public Plane() {
132 
133     }
134 
135     public Plane(String color) {
136         this.color = color;
137     }
138 
139     public String getColor() {
140         return color;
141     }
142 
143     public void setColor(String color) {
144         this.color = color;
145     }
146 
147     public void display() {
148         System.out.println("The Plane's color is:" + color);
149     }
150 }
151 
152 abstract class Element {
153     public abstract void display();
154 }
View Code
复制代码

题目要求使用抽象类,继承关系。

在题目输出方式上试了较多次。

(7-3)点线面问题再重构(容器类)

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束
    示例代码如下:
       choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
              ...
                break;
            case 2://insert Line object into list
                ...
                break;
            case 3://insert Plane object into list
                ...
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                ...
            }
            choice = input.nextInt();
        }
    
     
    输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
    类图如下所示:

classdiagram.jpg

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

switch(choice) {
            case 1://insert Point object into list 
              输入“点”对象的x,y值
                break;
            case 2://insert Line object into list
                输入“线”对象两个端点的x,y值
                break;
            case 3://insert Plane object into list
                输入“面”对象的颜色值
                break;
            case 4://delete index - 1 object from list
                输入要删除的对象位置(从1开始)
                ...
            }
 

输出格式:

  • Point、Line、Plane的输出参考题目2
  • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作

输入样例:

在这里给出一组输入。例如:

1
3.4
5.6
2
4.4
8.0
0.98
23.888
Red
3
Black
1
9.8
7.5
3
Green
4
3
0
 

输出样例:

在这里给出相应的输出。例如:

(3.40,5.60)
The line's color is:Red
The line's begin point's Coordinate is:
(4.40,8.00)
The line's end point's Coordinate is:
(0.98,23.89)
The line's length is:16.25
(9.80,7.50)
The Plane's color is:Green


SourceMonito报表
类图:
完整代码:
复制代码
  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner input = new Scanner(System.in);
  7         GeometryObject go = new GeometryObject();
  8         int choice = input.nextInt();
  9         while (choice != 0) {
 10             switch (choice) {
 11                 case 1:
 12                     Point p = new Point(input.nextDouble(), input.nextDouble());
 13                     go.add(p);
 14                     break;
 15                 case 2:
 16                     Point p1 = new Point(input.nextDouble(), input.nextDouble());
 17                     Point p2 = new Point(input.nextDouble(), input.nextDouble());
 18                     String str1 = input.next();
 19                     Line line = new Line(p1, p2, str1);
 20                     go.add(line);
 21                     break;
 22                 case 3:
 23                     String str2 = input.next();
 24                     Plane plane = new Plane(str2);
 25                     go.add(plane);
 26                     break;
 27                 case 4:
 28                     int index = input.nextInt();
 29                     go.remove(index);
 30                     break;
 31             }
 32             choice = input.nextInt();
 33         }
 34         ArrayList<Element> arr = go.getArr();
 35         for (int i = 0; i < arr.size(); i++) {
 36             arr.get(i).display();
 37         }
 38     }
 39 }
 40 
 41 class GeometryObject {
 42     private ArrayList<Element> arr = new ArrayList<>();
 43 
 44     public GeometryObject() {
 45 
 46     }
 47 
 48     public void add(Element element) {
 49         arr.add(element);
 50     }
 51 
 52     public ArrayList<Element> getArr() {
 53         return arr;
 54     }
 55 
 56     public void remove(int index) {
 57         if (index > 0 && index <= arr.size()){
 58             arr.remove(index - 1);
 59         }
 60     }
 61 }
 62 
 63 class Point extends Element {
 64     private double x, y;
 65 
 66     public Point() {
 67 
 68     }
 69 
 70     public Point(double x, double y) {
 71         this.x = x;
 72         this.y = y;
 73     }
 74 
 75     public double getX() {
 76         return x;
 77     }
 78 
 79     public void setX(double x) {
 80         this.x = x;
 81     }
 82 
 83     public double getY() {
 84         return y;
 85     }
 86 
 87     public void setY(double y) {
 88         this.y = y;
 89     }
 90 
 91     public void display() {
 92         //System.out.println("(" + new DecimalFormat("0.0#").format(x) + "," + new DecimalFormat("0.0#").format(y) + ")");
 93 //        System.out.printf("(%.2f,%.2f)", x, y);
 94 //        System.out.println();
 95         System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f", y) + ")");
 96     }
 97 
 98 }
 99 
100 class Line extends Element {
101     Point Point1;
102     Point Point2;
103     String color;
104 
105     public Line() {
106 
107     }
108 
109     public Line(Point point1, Point point2, String color) {
110         Point1 = point1;
111         Point2 = point2;
112         this.color = color;
113     }
114 
115     public Point getPoint1() {
116         return Point1;
117     }
118 
119     public void setPoint1(Point point1) {
120         Point1 = point1;
121     }
122 
123     public Point getPoint2() {
124         return Point2;
125     }
126 
127     public void setPoint2(Point point2) {
128         Point2 = point2;
129     }
130 
131     public String getColor() {
132         return color;
133     }
134 
135     public void setColor(String color) {
136         this.color = color;
137     }
138 
139     public void display() {
140         System.out.println("The line's color is:" + color);
141         System.out.println("The line's begin point's Coordinate is:");
142         this.Point1.display();
143         System.out.println("The line's end point's Coordinate is:");
144         this.Point2.display();
145         double length = Math.sqrt(Math.pow(this.Point1.getX() - this.Point2.getX(), 2) + Math.pow(this.Point1.getY() - this.Point2.getY(), 2));
146         System.out.println("The line's length is:" + String.format("%.2f", length));
147     }
148 }
149 
150 class Plane extends Element {
151     private String color;
152 
153     public Plane() {
154 
155     }
156 
157     public Plane(String color) {
158         this.color = color;
159     }
160 
161     public String getColor() {
162         return color;
163     }
164 
165     public void setColor(String color) {
166         this.color = color;
167     }
168 
169     public void display() {
170         System.out.println("The Plane's color is:" + color);
171     }
172 }
173 
174 abstract class Element {
175     public abstract void display();
176 }
View Code
复制代码

在删除数据上,要删除第index-1个,之前的方法会删除第-1个,超出了范围。

踩坑心得:

题目集4(7-2)

这道题要注意凹凸四边形的判断和点在四边形内的判断方法。

题目集5(7-2)

这道题要注意凹凸四边形的判断和面积的计算以及五边形无法形成时的处理方法

期中考试(7-3)

在删除数据上,要删除第index-1个,之前的方法会删除第-1个,超出了范围。要注意index的数据范围。

总结:

  通过这几次大作业和考试,我对面向对象有了更深层次的了解,同时对如何设计框架,如何使用继承,多态了解了更多。

posted @   Micellae  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示