Java博客作业(二)
一、前言
<1>第四次大作业
题型总结:正则表达式的使用、凸四边形的计算、银行业务
涉及知识点:正则表达式的理解和应用、Java基本的语法和面向对象的一系列操作,父类和子类的关系以及对多边形切割的理解。
题量:较多。
难度:较大。
<2>第五次大作业
题型总结:点线形系列5-凸五边形的计算
涉及知识点:正则表达式的理解和应用、Java基本的语法和面向对象的一系列操作,父类和子类的关系,对多边形切割的理解,以及两个多边形存在关系的理解和判断。
题量:适中。
难度:较大。
<3>期中考试
题型总结:点和面、点线面问题重构、点线面问题再重构
涉及知识点:Java基本的语法和面向对象的一系列操作,继承和多态、抽象方法、类的重构、ArrayList的使用、容器的使用。
题量:适中。
难度:适中。
二、设计与分析
1.算法设计以及思路
<1>第四次作业第二题
- 用户输入一组选项和数据,进行与四边形有关的计算。
-
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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 import java.util.Objects; 2 import java.util.Scanner; 3 import java.math.BigDecimal; 4 import java.math.RoundingMode; 5 public class Main { 6 public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){ 7 8 double A1=p1.y-p2.y; 9 double B1=p2.x-p1.x; 10 double C1=A1*p1.x+B1*p1.y; 11 12 double A2=p3.y-p4.y; 13 double B2=p4.x-p3.x; 14 double C2=A2*p3.x+B2*p3.y; 15 16 double det_k=A1*B2-A2*B1; 17 18 if(Math.abs(det_k)<0.00001){ 19 return null; 20 } 21 22 double a=B2/det_k; 23 double b=-1*B1/det_k; 24 double c=-1*A2/det_k; 25 double d=A1/det_k; 26 27 double x=a*C1+b*C2; 28 double y=c*C1+d*C2; 29 30 return new Point(x,y); 31 } 32 public static Double output(Double data) 33 { 34 double a = data; 35 36 BigDecimal bigDecimal = new BigDecimal(a); 37 double res = bigDecimal.setScale(3, RoundingMode.HALF_UP).doubleValue(); 38 return res; 39 } 40 static void operate_4(Point e,Point f, Point a, Point b, Point c, Point d) 41 { 42 Line ab = new Line(a, b); 43 Line bc = new Line(b, c); 44 Line cd = new Line(c, d); 45 Line ad = new Line(a, d); 46 Line ef=new Line(e,f); 47 Line ac=new Line(a,c); 48 Line bd=new Line(b,d); 49 if(a.is_sibianxing(a,b,c,d)) { 50 Quadrangle A=new Quadrangle(a,b,c,d); 51 A.qiege_sibianxing(e,f,a,b,c,d); 52 } 53 else if (a.Same(b)||b.Same(c)||c.Same(d)||a.Same(d)||a.Same(c)||b.Same(d)||a.on_line(b,d)||b.on_line(a,c)||c.on_line(b,d)||d.on_line(a,c)) { 54 if(a.Same(b)&&a.is_Triangle(a,c,d)) 55 { 56 Triangle A=new Triangle(a,c,d); 57 A.qiege_Triangle(ef,a,c,d); 58 } 59 else if(a.Same(c)&&a.is_Triangle(a,b,d)) 60 { 61 Triangle A=new Triangle(a,b,d); 62 A.qiege_Triangle(ef,a,b,d); 63 } 64 else if(b.Same(d)&&a.is_Triangle(a,b,c)) 65 { 66 Triangle A=new Triangle(a,b,c); 67 A.qiege_Triangle(ef,a,b,c); 68 } 69 else if(b.Same(c)&&a.is_Triangle(a,b,d)) 70 { 71 Triangle A=new Triangle(a,b,d); 72 A.qiege_Triangle(ef,a,b,d); 73 } 74 else if(c.Same(d)&&a.is_Triangle(a,b,c)) 75 { 76 Triangle A=new Triangle(a,b,c); 77 A.qiege_Triangle(ef,a,b,c); 78 } 79 else if(a.Same(d)&&a.is_Triangle(a,b,c)) 80 { 81 Triangle A=new Triangle(a,b,c); 82 A.qiege_Triangle(ef,a,b,c); 83 } 84 else if(a.on_line(b,d)&&a.is_Triangle(b,c,d)){ 85 Triangle A=new Triangle(b,c,d); 86 A.qiege_Triangle(ef,b,c,d); 87 } 88 else if(b.on_line(a,c)&&a.is_Triangle(a,c,d)){ 89 Triangle A=new Triangle(a,c,d); 90 A.qiege_Triangle(ef,a,c,d); 91 } 92 else if(c.on_line(b,d)&&a.is_Triangle(a,b,d)){ 93 Triangle A=new Triangle(a,b,d); 94 A.qiege_Triangle(ef,a,b,d); 95 } 96 else if(d.on_line(a,c)&&a.is_Triangle(a,b,c)){ 97 Triangle A=new Triangle(a,b,c); 98 A.qiege_Triangle(ef,a,b,c); 99 } 100 System.out.print("1"); 101 System.exit(0); 102 } 103 else{ 104 System.out.print("not a quadrilateral or triangle"); 105 System.exit(0); 106 } 107 } 108 static void operate_5(Point e, Point a, Point b, Point c, Point d) 109 { 110 if(a.is_sibianxing(a,b,c,d)) 111 { 112 Line ab=new Line(a,b); 113 Line bc=new Line(b,c); 114 Line cd=new Line(c,d); 115 Line ad=new Line(a,d); 116 Quadrangle A=new Quadrangle(ab,bc,cd,ad); 117 if(e.on_line(a,b)||e.on_line(b,c)||e.on_line(c,d)||e.on_line(a,d)) 118 { 119 System.out.print("on the quadrilateral"); 120 System.exit(0); 121 } 122 else if(A.in_it(e,a,b,c,d)) 123 { 124 System.out.print("in the quadrilateral"); 125 System.exit(0); 126 } 127 else{ 128 System.out.print("outof the quadrilateral"); 129 System.exit(0); 130 } 131 } 132 else if (a.Same(b)||b.Same(c)||c.Same(d)||a.Same(d)||a.on_line(b,d)||b.on_line(a,c)||c.on_line(b,d)||d.on_line(a,c)) { 133 if(e.on_line(a,b)||e.on_line(b,c)||e.on_line(c,d)||e.on_line(a,d)) 134 { 135 System.out.print("on the triangle"); 136 System.exit(0); 137 } 138 System.out.print("in the triangle"); 139 System.exit(0); 140 } 141 else{ 142 System.out.print("not a quadrilateral or triangle"); 143 System.exit(0); 144 } 145 } 146 public static void main(String[] args) { 147 Scanner in = new Scanner(System.in); 148 String input = in.nextLine(); 149 if(!input.matches("^[1-5][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+$")) 150 { 151 System.out.println("Wrong Format"); 152 return; 153 } 154 String[] option =input.split(":"); 155 String shuju=option[1]; 156 String[] point = shuju.split(" "); 157 String[] num = null; 158 switch(option[0]) 159 { 160 case "1": 161 if(point.length!=4) { 162 System.out.print("wrong number of points"); 163 System.exit(0); 164 } 165 num = point[0].split(","); 166 Point a=new Point(num[0],num[1]); 167 num = point[1].split(","); 168 Point b=new Point(num[0],num[1]); 169 num = point[2].split(","); 170 Point c=new Point(num[0],num[1]); 171 num = point[3].split(","); 172 Point d=new Point(num[0], num[1]); 173 if(a.Same(b)||b.Same(c)||c.Same(d)||d.Same(a)) { 174 System.out.print("points coincide"); 175 return; 176 } 177 System.out.print(a.is_sibianxing(a,b,c,d)); 178 System.out.print(" "); 179 Line ab=new Line(a,b); 180 Line bc=new Line(b,c); 181 Line cd=new Line(c,d); 182 Line ad=new Line(a,d); 183 Quadrangle A=new Quadrangle(ab,bc,cd,ad); 184 System.out.print(A.is_px_sibianxing()); 185 break; 186 case "2": 187 if(point.length!=4) { 188 System.out.print("wrong number of points"); 189 System.exit(0); 190 } 191 num = point[0].split(","); 192 a=new Point(num[0],num[1]); 193 num = point[1].split(","); 194 b=new Point(num[0],num[1]); 195 num = point[2].split(","); 196 c=new Point(num[0],num[1]); 197 num = point[3].split(","); 198 d=new Point(num[0], num[1]); 199 if(!a.is_sibianxing(a,b,c,d)) 200 { 201 System.out.print("not a quadrilateral"); 202 System.exit(0); 203 } 204 if(a.Same(b)||b.Same(c)||c.Same(d)||d.Same(a)||a.Same(c)||b.Same(d)) { 205 System.out.print("points coincide"); 206 System.exit(0); 207 } 208 ab=new Line(a,b); 209 bc=new Line(b,c); 210 cd=new Line(c,d); 211 ad=new Line(a,d); 212 A=new Quadrangle(ab,bc,cd,ad); 213 System.out.print(A.is_lx_sibianxing()+" "+A.is_jx_sibianxing()+" "+A.is_zf_sibianxing()); 214 break; 215 case "3": 216 if(point.length!=4) { 217 System.out.print("wrong number of points"); 218 System.exit(0); 219 } 220 num = point[0].split(","); 221 a=new Point(num[0],num[1]); 222 num = point[1].split(","); 223 b=new Point(num[0],num[1]); 224 num = point[2].split(","); 225 c=new Point(num[0],num[1]); 226 num = point[3].split(","); 227 d=new Point(num[0], num[1]); 228 if(a.Same(b)||b.Same(c)||c.Same(d)||d.Same(a)) { 229 System.out.print("points coincide"); 230 System.exit(0); 231 } 232 if(!a.is_sibianxing(a,b,c,d)) 233 { 234 System.out.print("not a quadrilateral"); 235 System.exit(0); 236 } 237 ab=new Line(a,b); 238 bc=new Line(b,c); 239 cd=new Line(c,d); 240 ad=new Line(a,d); 241 Line ac=new Line(a,c); 242 Line bd=new Line(b,d); 243 A=new Quadrangle(a,b,c,d); 244 System.out.print(A.tu_sibianxing(a,b,c,d)+" "+output(A.get_zc())+" "+output(A.get_area())); 245 break; 246 case "4": 247 if(point.length!=6) { 248 System.out.print("wrong number of points"); 249 System.exit(0); 250 } 251 num = point[0].split(","); 252 Point e=new Point(num[0],num[1]); 253 num = point[1].split(","); 254 Point f=new Point(num[0],num[1]); 255 num = point[2].split(","); 256 a=new Point(num[0],num[1]); 257 num = point[3].split(","); 258 b=new Point(num[0], num[1]); 259 num = point[4].split(","); 260 c=new Point(num[0],num[1]); 261 num = point[5].split(","); 262 d=new Point(num[0], num[1]); 263 if(e.Same(f)) { 264 System.out.print("points coincide"); 265 System.exit(0); 266 } 267 operate_4(e,f,a,b,c,d); 268 break; 269 case "5": 270 if(point.length!=5) { 271 System.out.print("wrong number of points"); 272 System.exit(0); 273 } 274 num = point[0].split(","); 275 e=new Point(num[0],num[1]); 276 num = point[1].split(","); 277 a=new Point(num[0],num[1]); 278 num = point[2].split(","); 279 b=new Point(num[0],num[1]); 280 num = point[3].split(","); 281 c=new Point(num[0], num[1]); 282 num = point[4].split(","); 283 d=new Point(num[0],num[1]); 284 operate_5(e,a,b,c,d); 285 break; 286 default:System.out.print("Wrong Format"); 287 break; 288 } 289 290 } 291 } 292 class Point { 293 double x,y,distance; 294 Point(String x,String y) 295 { 296 this.x=Double.valueOf(x); 297 this.y=Double.valueOf(y); 298 } 299 Point(Double x,Double y) 300 { 301 this.x=x; 302 this.y=y; 303 } 304 boolean Same(Point b) {//判断两点是否重合 305 if (Math.abs(this.x-b.x)<0.001 && Math.abs(this.y - b.y)<0.001) 306 return true; 307 else 308 return false; 309 } 310 Double Distance(Point b)//计算两点间的距离 311 { 312 distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y)); 313 return distance; 314 } 315 double Xielv(Point b)//计算两点所构成直线的斜率 316 { 317 double xielv=(b.y-this.y)/(b.x-this.x); 318 return xielv; 319 } 320 void Output(Point b) 321 { 322 if(this.Same(b)){ 323 System.out.print("points coincide"); 324 return; 325 } 326 else if(this.x==b.x) 327 { 328 System.out.print("Slope does not exist"); 329 return; 330 } 331 else 332 { 333 System.out.print(this.Xielv(b)); 334 return; 335 } 336 } 337 Double Czdistance(Point b,Point c)//计算点到直线的垂直距离 338 { 339 Double L = Math.abs((b.y-c.y)*this.x+(c.x-b.x)*this.y+b.x*c.y-c.y*c.x)/Math.sqrt((b.y-c.y) *(b.y-c.y) +(b.x-c.x)*(b.x-c.x)); 340 return L; 341 } 342 boolean straight_line(Point b,Point c)//判断三个点是否在一条直线上 343 { 344 if(this.Xielv(b)==this.Xielv(c)) 345 return true; 346 else 347 return false; 348 } 349 boolean is_sibianxing(Point a,Point b,Point c,Point d) 350 { 351 Line ab=new Line(a,b); 352 Line bc=new Line(b,c); 353 Line cd=new Line(c,d); 354 Line ad=new Line(a,d); 355 if(ab.parallel(bc)||ab.parallel(ad)||bc.parallel(cd)||cd.parallel(ad)||ab.xiangjiao(ab,cd)||bc.xiangjiao(bc,ad)) 356 return false; 357 else return true; 358 } 359 boolean on_line(Point a,Point b) 360 { 361 return Math.abs(this.Distance(a)+this.Distance(b)-a.Distance(b))<=0.001; 362 } 363 Double getarea(Point a,Point b,Point c) 364 { 365 Double C=a.Distance(b)+a.Distance(c)+b.Distance(c); 366 Double s=C/2; 367 Double area=Math.sqrt((s * a.Distance(b) *a.Distance(c) * b.Distance(c))); 368 return area; 369 } 370 boolean is_Triangle(Point a,Point b,Point c) 371 { 372 if(a.Same(b)||b.Same(c)||a.Same(c)||a.straight_line(b,c)) 373 return false; 374 else 375 return true; 376 } 377 378 } 379 380 class Line { 381 // Double A,B,C; 382 Point p1,p2; 383 Double length; 384 385 Line(Point a,Point b) 386 { 387 this.p1=a; 388 this.p2=b; 389 this.length=a.Distance(b); 390 // this.A=b.y-a.y; 391 // this.B=a.x-b.x; 392 // this.C=this.A*a.x+this.B*a.y; 393 } 394 boolean parallel_x()//判断直线是否平行x轴 395 { 396 if(this.p1.y==this.p2.y) 397 return true; 398 else 399 return false; 400 } 401 boolean parallel_y()//判断直线是否平行y轴 402 { 403 if(this.p1.x==this.p2.x) 404 return true; 405 else 406 return false; 407 } 408 Double getXielv()//计算直线的斜率 409 { 410 if(this.parallel_y()) 411 return null; 412 else if(this.parallel_x()) 413 { 414 double xielv=0; 415 return xielv; 416 } 417 else 418 return (this.p1.y-this.p2.y)/(this.p1.x-this.p2.x); 419 420 } 421 boolean parallel(Line p)//判断两条直线是否平行 422 { 423 if(this.parallel_x()&&p.parallel_x()) 424 return true; 425 else if(this.parallel_y()&&p.parallel_y()) 426 return true; 427 else if(Objects.equals(p.getXielv(), this.getXielv())) 428 return true; 429 else 430 return false; 431 } 432 boolean isin_line(Point a) 433 { 434 if(Math.abs(a.Czdistance(this.p1,this.p2))<0.001) 435 return true; 436 else 437 return false; 438 } 439 Point getjiaodian(Line q) 440 { 441 // y=kx+b 442 Point c=new Point(1.0, 1.0); 443 Double k1,b1,k2,b2; 444 if(this.parallel_x()&&q.parallel_y()) 445 { 446 b1=this.p1.y; 447 b2=q.p1.x; 448 c.x=b2; 449 c.y=b1; 450 return c; 451 } 452 else if(this.parallel_y()&&!q.parallel_x()) { 453 k2=q.getXielv(); 454 b2=q.p1.y-q.p1.x*k2; 455 c.x = this.p1.x; 456 c.y = c.x*k2+b2; 457 return c; 458 } 459 else if(q.parallel_y()&&!this.parallel_x()) { 460 k1 = this.getXielv(); 461 b1 = this.p1.y - this.p1.x * k1; 462 c.x = q.p1.x; 463 c.y = c.x*k1+b1; 464 return c; 465 } 466 else if(q.parallel_x()&&this.parallel_y()) 467 { 468 b2=q.p1.y; 469 b1=this.p1.x; 470 c.x=b1; 471 c.y=b2; 472 return c; 473 } 474 else if(this.parallel_x()&&!q.parallel_y()) 475 { 476 b1=this.p1.y; 477 k2=q.getXielv(); 478 b2=q.p1.y-q.p1.x*k2; 479 c.y=b1; 480 c.x=(b1-b2)/k2; 481 return c; 482 } 483 else if(q.parallel_x()&&!this.parallel_y()) 484 { 485 b2=this.p1.y; 486 k1=this.getXielv(); 487 b1=this.p1.y-this.p1.x*k1; 488 c.y=b2; 489 c.x=(b2-b1)/k1; 490 return c; 491 } 492 else{ 493 k1 = this.getXielv(); 494 b1 = this.p1.y - this.p1.x * k1; 495 k2 = q.getXielv(); 496 b2 = q.p1.y - q.p1.x * k2; 497 c.x = (b2 - b1) / (k1 - k2); 498 c.y = k1 * c.x + b1; 499 return c; 500 } 501 502 } 503 504 boolean init(Double x,Double y,Line q)//判断交点是否在两条线段内 505 { 506 if((y<=Math.max(this.p1.y,this.p2.y)||y<=Math.max(q.p1.y,q.p2.y))&&((y>=Math.min(this.p1.y,this.p2.y)||y>=Math.min(q.p1.y,q.p2.y)))) 507 { 508 return true; 509 } 510 else 511 return false; 512 } 513 Point get_zhongdian() 514 { 515 Double x=(this.p1.x+this.p2.x)/2; 516 Double y=(this.p1.y+this.p2.y)/2; 517 Point a=new Point(x,y); 518 return a; 519 } 520 boolean is_cz(Line p) 521 { 522 if(Math.abs((this.p2.x-this.p1.x)*(p.p2.x-p.p1.x)+(this.p2.y-this.p1.y)*(p.p2.y-p.p1.y))<0.01) 523 return true; 524 else return false; 525 } 526 boolean xiangjiao(Line p,Line q) 527 { 528 // 快速排斥实验 首先判断两条线段在 x 以及 y 坐标的投影是否有重合。 有一个为真,则代表两线段必不可交。 529 if (Math.max(p.p1.x,p.p2.x) < Math.min(q.p1.x ,q.p2.x) 530 || Math.max(p.p1.y,p.p2.y) < Math.min(q.p1.y,q.p2.y) 531 || Math.max(q.p1.x ,q.p2.x) < Math.min(p.p1.x,p.p2.x) 532 || Math.max(q.p1.y,q.p2.y) < Math.min(p.p1.y,p.p2.y)) 533 { 534 return false; 535 } 536 if ((((p.p1.x - q.p1.x) * (q.p2.y - q.p1.y) - (p.p1.y - q.p1.y ) * (q.p2.x - q.p1.x)) 537 * ((p.p2.x - q.p1.x) * (q.p2.y - q.p1.y) - (p.p2.y - q.p1.y) * (q.p2.x - q.p1.x))) > 0 538 || (((q.p1.x - p.p1.x) * (p.p2.y - p.p1.y) - (q.p1.y - p.p1.y) * (p.p2.x - p.p1.x)) 539 * ((q.p2.x - p.p1.x) * (p.p2.y - p.p1.y) - (q.p2.y - p.p1.y) * (p.p2.x - p.p1.x))) > 0) 540 { 541 return false; 542 } 543 544 return true; 545 } 546 boolean is_sameline(Line q) 547 { 548 // return q.isin_line(this.p1) && q.isin_line(this.p2); 549 return this.p1.straight_line(q.p1,q.p2)&&this.p2.straight_line(q.p1,q.p2); 550 } 551 boolean xiangjiao2(Line q) 552 { 553 Double a1=(this.p1.x-q.p1.x)*(this.p2.y-q.p1.y)-(this.p1.y-q.p1.y)*(this.p2.x-q.p1.x); 554 Double a2=(this.p1.x-q.p2.x)*(this.p2.y-q.p2.y)-(this.p1.y-q.p2.y)*(this.p2.x-q.p2.x); 555 return (a1 >= 0 && a2 <= 0) || (a1 <= 0 && a2 >= 0); 556 } 557 558 } 559 class Triangle { 560 Line l1, l2, l3; 561 Point a;Point b;Point c; 562 563 Triangle(Line l1, Line l2, Line l3) { 564 this.l1 = l1; 565 this.l2 = l2; 566 this.l3 = l3; 567 } 568 Triangle(Point a,Point b,Point c) { 569 this.a=a;this.b=b;this.c=c; 570 Line ab = new Line(a, b); 571 Line bc = new Line(b, c); 572 Line ac = new Line(a, c); 573 this.l1 = ab; 574 this.l2 = bc; 575 this.l3 = ac; 576 } 577 578 void whattriangle() { 579 boolean db = false; 580 boolean dy = false; 581 if (Objects.equals(this.l1.length, this.l2.length) && Objects.equals(this.l1.length, this.l3.length)) 582 db = true; 583 else if (Objects.equals(this.l1.length, this.l2.length) || Objects.equals(this.l1.length, this.l3.length) || Objects.equals(this.l2.length, this.l3.length)) { 584 dy = true; 585 } else { 586 db = false; 587 dy = false; 588 } 589 if (db) 590 System.out.print("true true"); 591 else if (dy) 592 System.out.print("true false"); 593 else 594 System.out.print("false false"); 595 } 596 597 Double get_zc() { 598 return (this.l1.length + this.l2.length + this.l3.length); 599 } 600 601 Double get_area() { 602 Double s = this.get_zc() / 2; 603 Double area=Math.sqrt((s * (s - this.l1.length) * (s - this.l2.length) * (s - this.l3.length))); 604 return area; 605 } 606 607 Double get_zx_x() { 608 Double x1 = this.l1.p1.x; 609 Double x2 = this.l1.p2.x; 610 Double x3 = this.l2.p2.x; 611 612 Double X1 = (x1 + x2 + x3) / 3; 613 return X1; 614 } 615 Double get_zx_y() 616 { 617 Double y1 = this.l1.p1.y; 618 Double y2 = this.l1.p2.y; 619 Double y3 = this.l2.p2.y; 620 Double Y1 = (y1 + y2 + y3) / 3; 621 return Y1; 622 } 623 void is_jiao() 624 { 625 if(Math.abs(Math.pow(this.l1.length,2)+Math.pow(this.l2.length,2)-Math.pow(this.l3.length,2))<0.0001|| 626 Math.abs(Math.pow(this.l1.length,2)+Math.pow(this.l3.length,2)-Math.pow(this.l2.length,2))<0.0001|| 627 Math.abs(Math.pow(this.l2.length,2)+Math.pow(this.l3.length,2)-Math.pow(this.l1.length,2))<0.0001) 628 System.out.print("false true false"); 629 else if(Math.pow(this.l1.length,2)+Math.pow(this.l2.length,2)<Math.pow(this.l3.length,2)|| 630 Math.pow(this.l1.length,2)+Math.pow(this.l3.length,2)<Math.pow(this.l2.length,2)|| 631 Math.pow(this.l2.length,2)+Math.pow(this.l3.length,2)<Math.pow(this.l1.length,2)) 632 System.out.print("true false false"); 633 else 634 System.out.print("false false true"); 635 } 636 boolean isinit(Line p,Line q) 637 { 638 if(p.init(Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).x,Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).y,q)) 639 return true; 640 else 641 return false; 642 } 643 Double getarea2(Point a,Point b,Point c) 644 { 645 Line p=new Line(a,b); 646 Line q=new Line(a,c); 647 Line r=new Line(b,c); 648 Triangle A=new Triangle(p,q,r); 649 return A.get_area(); 650 } 651 void operate5(Point a,Point b,Point c,Point d) 652 { 653 if(Math.abs(getarea2(a,b,c)+getarea2(a,b,d)+getarea2(a,c,d)-getarea2(b,c,d))<0.0001) 654 System.out.print("in the triangle"); 655 else 656 System.out.print("out of the triangle"); 657 } 658 void output(Double s1,Double s2) 659 { 660 if(s1<=s2){ 661 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 662 System.exit(0); 663 } 664 else { 665 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 666 System.exit(0); 667 } 668 } 669 void qiege_Triangle(Line ef,Point a,Point c,Point d) 670 { 671 Triangle A=new Triangle(a,c,d); 672 Line ac=new Line(a,c); 673 Line cd=new Line(c,d); 674 Line ad=new Line(a,d); 675 if(ef.is_sameline(ac)||ef.is_sameline(cd)||ef.is_sameline(ad)){ 676 System.out.print("The line is coincide with one of the lines"); 677 System.exit(0); 678 } 679 else if((ef.isin_line(a)&&!ef.xiangjiao2(cd))||(ef.isin_line(c)&&!ef.xiangjiao2(ad))||(ef.isin_line(d)&&!ef.xiangjiao2(ac))) 680 { 681 System.out.print("1"); 682 System.exit(0); 683 } 684 else if(ef.isin_line(a)&&ef.xiangjiao2(cd)) 685 { 686 Triangle C=new Triangle(a,c,ef.getjiaodian(cd)); 687 Triangle D=new Triangle(a,ef.getjiaodian(cd),d); 688 Double s1=C.get_area(); 689 Double s2= D.get_area(); 690 C.output(s1,s2); 691 } 692 else if(ef.isin_line(c)&&ef.xiangjiao2(ad)){ 693 Triangle C=new Triangle(a,c,ef.getjiaodian(ad)); 694 Triangle D=new Triangle(c,ef.getjiaodian(ad),d); 695 Double s1=C.get_area(); 696 Double s2= D.get_area(); 697 C.output(s1,s2); 698 } 699 else if(ef.isin_line(d)&&ef.xiangjiao2(ac)){ 700 Triangle C=new Triangle(a,ef.getjiaodian(ac),d); 701 Triangle D=new Triangle(ef.getjiaodian(ac),c,d); 702 Double s1=C.get_area(); 703 Double s2= D.get_area(); 704 C.output(s1,s2); 705 } 706 else if(ef.xiangjiao2(ac)&& ef.xiangjiao2(cd)){ 707 Triangle C=new Triangle(ef.getjiaodian(ac),c,ef.getjiaodian(cd)); 708 Double s1=C.get_area(); 709 Double s2=A.get_area()-s1; 710 C.output(s1,s2); 711 } 712 else if(ef.xiangjiao2(ac)&& ef.xiangjiao2(ad)){ 713 Triangle C=new Triangle(a,ef.getjiaodian(ac),ef.getjiaodian(ad)); 714 Double s1=C.get_area(); 715 Double s2=A.get_area()-s1; 716 C.output(s1,s2); 717 } 718 else if(ef.xiangjiao2(ad)&& ef.xiangjiao2(cd)){ 719 Triangle C=new Triangle(ef.getjiaodian(cd),d,ef.getjiaodian(ad)); 720 Double s1=C.get_area(); 721 Double s2=A.get_area()-s1; 722 C.output(s1,s2); 723 } 724 } 725 726 } 727 728 class Quadrangle { 729 Line ab; 730 Line bc; 731 Line cd; 732 Line ad; 733 Point a;Point b;Point c;Point d; 734 Quadrangle(Line ab,Line bc,Line cd,Line ad) 735 { 736 this.ab=ab; 737 this.bc=bc; 738 this.cd=cd; 739 this.ad=ad; 740 } 741 Quadrangle(Point a,Point b,Point c,Point d) 742 { 743 this.a=a;this.b=b;this.c=c;this.d=d; 744 Line ab = new Line(a, b); 745 Line bc = new Line(b, c); 746 Line cd = new Line(c, d); 747 Line ad = new Line(a, d); 748 this.ab=ab; 749 this.bc=bc; 750 this.cd=cd; 751 this.ad=ad; 752 } 753 boolean is_px_sibianxing() 754 { 755 return this.ab.parallel(cd)&&this.bc.parallel(ad); 756 } 757 boolean is_zf_sibianxing() 758 { 759 return this.is_lx_sibianxing()&&(Objects.equals(this.ab.length, this.bc.length))&&this.ab.is_cz(bc); 760 } 761 boolean is_lx_sibianxing() 762 { 763 return this.is_px_sibianxing()&&Objects.equals(this.ab.length, this.bc.length); 764 } 765 boolean is_jx_sibianxing() 766 { 767 return this.is_px_sibianxing()&&this.ab.is_cz(bc); 768 } 769 void what_Quadrangle() 770 { 771 if(this.is_zf_sibianxing()) 772 System.out.print("true true true"); 773 else if(this.is_lx_sibianxing()) 774 System.out.print("true false false"); 775 else if(this.is_jx_sibianxing()) 776 System.out.print("false true false"); 777 else System.out.print("false false false"); 778 } 779 boolean tu_sibianxing(Point p1,Point p2,Point p3,Point p4) 780 { 781 Double t1 = (p4.x-p1.x)*(p2.y-p1.y)-(p4.y-p1.y)*(p2.x-p1.x); 782 Double t2 = (p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x); 783 Double t3 = (p2.x-p3.x)*(p4.y-p3.y)-(p2.y-p3.y)*(p4.x-p3.x); 784 Double t4 = (p3.x-p4.x)*(p1.y-p4.y)-(p3.y-p4.y)*(p1.x-p4.x); 785 return t1*t2*t3*t4>=0; 786 } 787 Double get_zc() 788 { 789 return this.ab.length+this.bc.length+ad.length+this.cd.length; 790 } 791 Double get_area(Line ac,Line bd) 792 { 793 Triangle a=new Triangle(this.ab,this.bc,ac); 794 Triangle b=new Triangle(this.cd,this.ad,ac); 795 Triangle c=new Triangle(this.ab,this.ad,bd); 796 Triangle d=new Triangle(this.bc,this.cd,bd); 797 if(this.tu_sibianxing(this.ab.p1,this.ab.p2,this.cd.p1,this.cd.p2)) 798 return c.get_area()+d.get_area(); 799 else { 800 if (Objects.equals(c.get_area(), d.get_area())) 801 return Math.abs(a.get_area() - b.get_area()); 802 else 803 return Math.abs(c.get_area() - d.get_area()); 804 } 805 } 806 Double get_area() 807 { 808 // s=1/2*[(x1*y2-x2*y1)+(x2*y3-x3*y2)+...... +(Xk*Yk+1-Xk+1*Yk)+...+(Xn*y1-x1*Yn) ] 809 double s=0; 810 s=(this.a.x*this.b.y-this.b.x*this.a.y)+(this.b.x*this.c.y-this.c.x*this.b.y)+(this.c.x*this.d.y-this.d.x*this.c.y)+(this.d.x*this.a.y-this.a.x*this.d.y); 811 return Math.abs(s/2); 812 } 813 boolean in_it(Point e,Point a,Point b,Point c,Point d) 814 { 815 Line ab=new Line(a,b); 816 Line bc=new Line(b,c); 817 Line cd=new Line(c,d); 818 Line ad=new Line(a,d); 819 Line ea=new Line(e,a); 820 Line eb=new Line(e,b); 821 Line ec=new Line(e,c); 822 Line ed=new Line(e,d); 823 Line ac=new Line(a,c); 824 Line bd=new Line(b,d); 825 Triangle A=new Triangle(ea,eb,ab); 826 Triangle B=new Triangle(eb,ec,bc); 827 Triangle C=new Triangle(ec,ed,cd); 828 Triangle D=new Triangle(ea,ed,ad); 829 if(Math.abs(A.get_area()+B.get_area()+C.get_area()+D.get_area()-this.get_area(ac,bd))<0.1) 830 return true; 831 else 832 return false; 833 } 834 void output(Double s1,Double s2) 835 { 836 if(s1<=s2){ 837 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 838 System.exit(0); 839 } 840 else { 841 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 842 System.exit(0); 843 } 844 } 845 void qiege_sibianxing(Point e,Point f, Point a, Point b, Point c, Point d) 846 { 847 Line ab = new Line(a, b); 848 Line bc = new Line(b, c); 849 Line cd = new Line(c, d); 850 Line ad = new Line(a, d); 851 Line ef=new Line(e,f); 852 Line ac=new Line(a,c); 853 Line bd=new Line(b,d); 854 Quadrangle A=new Quadrangle(a,b,c,d); 855 if(ef.is_sameline(ab)||ef.is_sameline(bc)||ef.is_sameline(cd)||ef.is_sameline(ad)){ 856 System.out.print("The line is coincide with one of the lines"); 857 System.exit(0); 858 } 859 else if(ef.is_sameline(ac)){ 860 Triangle C=new Triangle(ab,bc,ac); 861 Triangle D=new Triangle(cd,ad,ac); 862 Double s1=C.get_area(); 863 Double s2= D.get_area(); 864 C.output(s1,s2); 865 } 866 else if(ef.is_sameline(bd)){ 867 Triangle C=new Triangle(ab,ad,bd); 868 Triangle D=new Triangle(bc,cd,bd); 869 Double s1=C.get_area(); 870 Double s2= D.get_area(); 871 C.output(s1,s2); 872 } 873 else if(ef.xiangjiao2(ab)&& ef.xiangjiao2(cd)){ 874 Quadrangle C=new Quadrangle(a,ef.getjiaodian(ab),ef.getjiaodian(cd),d); 875 Quadrangle D=new Quadrangle(ef.getjiaodian(ab),b,c,ef.getjiaodian(cd)); 876 Double s1=C.get_area(); 877 Double s2= D.get_area(); 878 C.output(s1,s2); 879 } 880 else if(ef.xiangjiao2(bc)&& ef.xiangjiao2(ad)){ 881 Quadrangle C=new Quadrangle(a,b,ef.getjiaodian(bc),ef.getjiaodian(ad)); 882 Quadrangle D=new Quadrangle(ef.getjiaodian(bc),c,d,ef.getjiaodian(ad)); 883 Double s1=C.get_area(); 884 Double s2= D.get_area(); 885 C.output(s1,s2); 886 } 887 else if(ef.xiangjiao2(ab)&&ef.xiangjiao2(bc)){ 888 Triangle C=new Triangle(ef.getjiaodian(ab),b,ef.getjiaodian(bc)); 889 Double s1=C.get_area(); 890 Double s2=Math.abs(A.get_area()- C.get_area()); 891 C.output(s1,s2); 892 } 893 else if(ef.xiangjiao2(bc)&&ef.xiangjiao2(cd)){ 894 Triangle C=new Triangle(ef.getjiaodian(bc),c,ef.getjiaodian(cd)); 895 Double s1=C.get_area(); 896 Double s2=Math.abs(A.get_area()- C.get_area()); 897 C.output(s1,s2); 898 } 899 else if(ef.xiangjiao2(cd)&&ef.xiangjiao2(ad)){ 900 Triangle C=new Triangle(ef.getjiaodian(cd),d,ef.getjiaodian(ad)); 901 Double s1=C.get_area(); 902 Double s2=Math.abs(A.get_area()- C.get_area()); 903 C.output(s1,s2); 904 } 905 else if(ef.xiangjiao2(ab)&&ef.xiangjiao2(ad)){ 906 Triangle C=new Triangle(a,ef.getjiaodian(ab),ef.getjiaodian(ad)); 907 Double s1=C.get_area(); 908 Double s2=Math.abs(A.get_area()- C.get_area()); 909 C.output(s1,s2); 910 } 911 else if(ef.isin_line(a)&&ef.xiangjiao2(bc)){ 912 Triangle C=new Triangle(a,b,ef.getjiaodian(bc)); 913 Double s1=C.get_area(); 914 Double s2=Math.abs(A.get_area()- C.get_area()); 915 C.output(s1,s2); 916 } 917 else if(ef.isin_line(a)&&ef.xiangjiao2(cd)){ 918 Triangle C=new Triangle(a,ef.getjiaodian(cd),d); 919 Double s1=C.get_area(); 920 Double s2=Math.abs(A.get_area()- C.get_area()); 921 C.output(s1,s2); 922 } 923 else if(ef.isin_line(b)&&ef.xiangjiao2(cd)){ 924 Triangle C=new Triangle(b,c,ef.getjiaodian(cd)); 925 Double s1=C.get_area(); 926 Double s2=Math.abs(A.get_area()- C.get_area()); 927 C.output(s1,s2); 928 } 929 else if(ef.isin_line(b)&&ef.xiangjiao2(ad)){ 930 Triangle C=new Triangle(a,b,ef.getjiaodian(ad)); 931 Double s1=C.get_area(); 932 Double s2=Math.abs(A.get_area()- C.get_area()); 933 C.output(s1,s2); 934 } 935 else if(ef.isin_line(c)&&ef.xiangjiao2(ab)){ 936 Triangle C=new Triangle(ef.getjiaodian(ab),b,c); 937 Double s1=C.get_area(); 938 Double s2=Math.abs(A.get_area()- C.get_area()); 939 C.output(s1,s2); 940 } 941 else if(ef.isin_line(c)&&ef.xiangjiao2(ad)){ 942 Triangle C=new Triangle(c,d,ef.getjiaodian(ad)); 943 Double s1=C.get_area(); 944 Double s2=Math.abs(A.get_area()- C.get_area()); 945 C.output(s1,s2); 946 } 947 else if(ef.isin_line(d)&&ef.xiangjiao2(bc)){ 948 Triangle C=new Triangle(ef.getjiaodian(bc),c,d); 949 Double s1=C.get_area(); 950 Double s2=Math.abs(A.get_area()- C.get_area()); 951 C.output(s1,s2); 952 } 953 else if(ef.isin_line(d)&&ef.xiangjiao2(ab)){ 954 Triangle C=new Triangle(a,ef.getjiaodian(ab),d); 955 Double s1=C.get_area(); 956 Double s2=Math.abs(A.get_area()- C.get_area()); 957 C.output(s1,s2); 958 } 959 else 960 { 961 System.out.print("1"); 962 System.exit(0); 963 } 964 } 965 }View Code
<2>第五次作业第一,二题
7-1 点线形系列5-凸五边形的计算-1用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
7-2 点线形系列5-凸五边形的计算-2
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon
5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
因为这两题本身就是一个题目分开来写的,所以我就把这两个题目一起分析。
大概思路:
判断条件和前面判断四边形的大致一致,首先通过两点不能重合以及不相邻边不能交叉的性质来判断输入的五个点是否能构成五边形,然后通过五边形所拥有的性质去解决后面的问题,在判断输入的点构成的多边形的切割问题时,线把五边形的情况写完,再用else if去判断五个五点构成四边形的情况,最后再用else if去判断构成三角形的情况,再进行相应的计算。
第二题判断两个多边形关系的时候我是先写条件判断相离和完全重合的情况,再判断连接和包含的情况,最后就剩下交错的情况,否则会出现判断错误的情况。
第一题:
1 import java.util.Scanner; 2 import java.util.Objects; 3 4 import java.math.BigDecimal; 5 import java.math.RoundingMode; 6 public class Main { 7 public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){ 8 9 double A1=p1.y-p2.y; 10 double B1=p2.x-p1.x; 11 double C1=A1*p1.x+B1*p1.y; 12 13 double A2=p3.y-p4.y; 14 double B2=p4.x-p3.x; 15 double C2=A2*p3.x+B2*p3.y; 16 17 double det_k=A1*B2-A2*B1; 18 19 if(Math.abs(det_k)<0.00001){ 20 return null; 21 } 22 23 double a=B2/det_k; 24 double b=-1*B1/det_k; 25 double c=-1*A2/det_k; 26 double d=A1/det_k; 27 28 double x=a*C1+b*C2; 29 double y=c*C1+d*C2; 30 31 return new Point(x,y); 32 } 33 public static Double output(Double data) 34 { 35 double a = data; 36 37 BigDecimal bigDecimal = new BigDecimal(a); 38 double res = bigDecimal.setScale(3, RoundingMode.HALF_UP).doubleValue(); 39 return res; 40 } 41 static void operate_3(Point f,Point g,Point a,Point b,Point c,Point d,Point e) 42 { 43 if(a.is_pentagon(a,b,c,d,e)) 44 { 45 Pentagon A=new Pentagon(a,b,c,d,e); 46 A.qiege_pentagon(f,g,a,b,c,d,e); 47 } 48 else if(a.Same(b)||b.Same(c)||c.Same(d)||d.Same(e)||a.Same(e)||a.Same(c)||a.Same(d)||b.Same(d)||b.Same(e)||c.Same(e)|| 49 a.on_line(b,e)||b.on_line(a,c)||c.on_line(b,d)||d.on_line(c,e)||e.on_line(a,d)) 50 { 51 if((a.Same(b)||a.Same(c)||a.Same(d)||a.Same(e)||a.on_line(b,e))&&a.is_sibianxing(b,c,d,e)) 52 { 53 Quadrangle A=new Quadrangle(b,c,d,e); 54 A.qiege_sibianxing(f,g,b,c,d,e); 55 } 56 else if((b.Same(c)||b.Same(d)||b.Same(e)||b.on_line(a,c))&&a.is_sibianxing(a,c,d,e)) 57 { 58 Quadrangle A=new Quadrangle(a,c,d,e); 59 A.qiege_sibianxing(f,g,a,c,d,e); 60 } 61 else if((c.Same(d)||c.Same(e)||c.on_line(b,d))&&a.is_sibianxing(a,b,d,e)) 62 { 63 Quadrangle A=new Quadrangle(a,b,d,e); 64 A.qiege_sibianxing(f,g,a,b,d,e); 65 } 66 else if((d.Same(e)||d.on_line(c,e))&&a.is_sibianxing(a,b,c,e)) 67 { 68 Quadrangle A=new Quadrangle(a,b,c,e); 69 A.qiege_sibianxing(f,g,a,b,c,e); 70 } 71 else if(e.on_line(a,d)&&a.is_sibianxing(a,b,c,d)) 72 { 73 Quadrangle A=new Quadrangle(a,b,c,d); 74 A.qiege_sibianxing(f,g,a,b,c,d); 75 } 76 else if(((a.Same(b)&&a.Same(c))||(b.on_line(a,c)&&c.on_line(b,d)))&&a.is_Triangle(a,d,e)) 77 { 78 Triangle A=new Triangle(a,d,e); 79 A.qiege_Triangle(f,g,a,d,e); 80 } 81 else if((b.on_line(a,c)||d.on_line(c,e))&&a.is_Triangle(a,c,e)) 82 { 83 Triangle A=new Triangle(a,c,e); 84 A.qiege_Triangle(f,g,a,c,e); 85 } 86 } 87 else 88 { 89 System.out.print("not a polygon"); 90 } 91 } 92 93 public static void main(String[] args) { 94 Scanner in = new Scanner(System.in); 95 String input = in.nextLine(); 96 if(!input.matches("^[1-5][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+$")) 97 { 98 System.out.println("Wrong Format"); 99 return; 100 } 101 String[] option =input.split(":"); 102 String shuju=option[1]; 103 String[] point = shuju.split(" "); 104 String[] num = null; 105 switch(option[0]) 106 { 107 case "1": 108 if(point.length!=5) { 109 System.out.print("wrong number of points"); 110 System.exit(0); 111 } 112 num = point[0].split(","); 113 Point a=new Point(num[0],num[1]); 114 num = point[1].split(","); 115 Point b=new Point(num[0],num[1]); 116 num = point[2].split(","); 117 Point c=new Point(num[0],num[1]); 118 num = point[3].split(","); 119 Point d=new Point(num[0],num[1]); 120 num = point[4].split(","); 121 Point e=new Point(num[0],num[1]); 122 System.out.print(a.is_pentagon(a,b,c,d,e)); 123 break; 124 case "2": 125 if(point.length!=5) { 126 System.out.print("wrong number of points"); 127 System.exit(0); 128 } 129 num = point[0].split(","); 130 a=new Point(num[0],num[1]); 131 num = point[1].split(","); 132 b=new Point(num[0],num[1]); 133 num = point[2].split(","); 134 c=new Point(num[0],num[1]); 135 num = point[3].split(","); 136 d=new Point(num[0], num[1]); 137 num = point[4].split(","); 138 e=new Point(num[0],num[1]); 139 if(!a.is_pentagon(a,b,c,d,e)) 140 { 141 System.out.print("not a pentagon"); 142 System.exit(0); 143 } 144 Pentagon A=new Pentagon(a,b,c,d,e); 145 if(A.tu_Pentagon(a,b,c,d,e)){ 146 System.out.print("true "+output(A.get_zc())+" "+output(A.get_area())); 147 } 148 else 149 System.out.print("false"); 150 break; 151 case "3": 152 if(point.length!=7) { 153 System.out.print("wrong number of points"); 154 System.exit(0); 155 } 156 num = point[0].split(","); 157 Point f=new Point(num[0],num[1]); 158 num = point[1].split(","); 159 Point g=new Point(num[0],num[1]); 160 num = point[2].split(","); 161 a=new Point(num[0],num[1]); 162 num = point[3].split(","); 163 b=new Point(num[0],num[1]); 164 num = point[4].split(","); 165 c=new Point(num[0],num[1]); 166 num = point[5].split(","); 167 d=new Point(num[0], num[1]); 168 num = point[6].split(","); 169 e=new Point(num[0],num[1]); 170 if(f.Same(g)){ 171 System.out.print("points coincide"); 172 System.exit(0); 173 } 174 operate_3(f,g,a,b,c,d,e); 175 176 break; 177 default:System.out.print("Wrong Format"); 178 break; 179 } 180 181 } 182 } 183 class Point { 184 double x,y; 185 Point(String x,String y) 186 { 187 this.x=Double.valueOf(x); 188 this.y=Double.valueOf(y); 189 } 190 Point(Double x,Double y) 191 { 192 this.x=x; 193 this.y=y; 194 } 195 boolean Same(Point b) {//判断两点是否重合 196 if ((this.x==b.x)&&(this.y==b.y)) 197 return true; 198 else 199 return false; 200 } 201 Double Distance(Point b)//计算两点间的距离 202 { 203 Double distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y)); 204 return distance; 205 } 206 double Xielv(Point b)//计算两点所构成直线的斜率 207 { 208 double xielv=(b.y-this.y)/(b.x-this.x); 209 return xielv; 210 } 211 Double Czdistance(Point b,Point c)//计算点到直线的垂直距离 212 { 213 Double L = Math.abs((b.y-c.y)*this.x+(c.x-b.x)*this.y+b.x*c.y-c.y*c.x)/Math.sqrt((b.y-c.y) *(b.y-c.y) +(b.x-c.x)*(b.x-c.x)); 214 return L; 215 } 216 boolean straight_line(Point b,Point c)//判断三个点是否在一条直线上 217 { 218 if(this.Same(b)||this.Same(c)) 219 return true; 220 else if(this.Xielv(b)==this.Xielv(c)) 221 return true; 222 else 223 return false; 224 } 225 boolean is_sibianxing(Point a,Point b,Point c,Point d) 226 { 227 Line ab=new Line(a,b); 228 Line bc=new Line(b,c); 229 Line cd=new Line(c,d); 230 Line ad=new Line(a,d); 231 if(ab.parallel(bc)||ab.parallel(ad)||bc.parallel(cd)||cd.parallel(ad)||ab.xiangjiao(ab,cd)||bc.xiangjiao(bc,ad)) 232 return false; 233 else return true; 234 } 235 boolean on_line(Point a,Point b) 236 { 237 return Math.abs(this.Distance(a)+this.Distance(b)-a.Distance(b))<=0.001; 238 } 239 Double getarea(Point a,Point b,Point c) 240 { 241 Double C=a.Distance(b)+a.Distance(c)+b.Distance(c); 242 Double s=C/2; 243 Double area=Math.sqrt((s * a.Distance(b) *a.Distance(c) * b.Distance(c))); 244 return area; 245 } 246 boolean is_Triangle(Point a,Point b,Point c) 247 { 248 if(a.Same(b)||b.Same(c)||a.Same(c)||a.straight_line(b,c)) 249 return false; 250 else 251 return true; 252 } 253 boolean is_pentagon(Point a,Point b,Point c,Point d,Point e) 254 { 255 Line ab=new Line(a,b); 256 Line bc=new Line(b,c); 257 Line cd=new Line(c,d); 258 Line de=new Line(d,e); 259 Line ae=new Line(a,e); 260 if(ab.parallel(bc)||bc.parallel(cd)||cd.parallel(de)||de.parallel(ae)||ae.parallel(ab)|| 261 ab.xiangjiao(ab,cd)||ab.xiangjiao(bc,de)||bc.xiangjiao(bc,de)||bc.xiangjiao(bc,ae)||cd.xiangjiao(cd,ae)) 262 return false; 263 else return true; 264 } 265 } 266 class Line { 267 // Double A,B,C; 268 Point p1,p2; 269 Double length; 270 271 Line(Point a,Point b) 272 { 273 this.p1=a; 274 this.p2=b; 275 this.length=a.Distance(b); 276 // this.A=b.y-a.y; 277 // this.B=a.x-b.x; 278 // this.C=this.A*a.x+this.B*a.y; 279 } 280 boolean parallel_x()//判断直线是否平行x轴 281 { 282 if(this.p1.y==this.p2.y) 283 return true; 284 else 285 return false; 286 } 287 boolean parallel_y()//判断直线是否平行y轴 288 { 289 if(this.p1.x==this.p2.x) 290 return true; 291 else 292 return false; 293 } 294 Double getXielv()//计算直线的斜率 295 { 296 if(this.parallel_y()) 297 return null; 298 else if(this.parallel_x()) 299 { 300 double xielv=0; 301 return xielv; 302 } 303 else 304 return (this.p1.y-this.p2.y)/(this.p1.x-this.p2.x); 305 306 } 307 boolean parallel(Line p)//判断两条直线是否平行 308 { 309 if(this.parallel_x()&&p.parallel_x()) 310 return true; 311 else if(this.parallel_y()&&p.parallel_y()) 312 return true; 313 else if(Objects.equals(p.getXielv(), this.getXielv())) 314 return true; 315 else 316 return false; 317 } 318 boolean isin_line(Point a) //判断点在直线上 319 { 320 if(Math.abs(a.Czdistance(this.p1,this.p2))<0.001) 321 return true; 322 else 323 return false; 324 } 325 326 Point getjiaodian(Line q) 327 { 328 // y=kx+b 329 Point c=new Point(1.0, 1.0); 330 Double k1,b1,k2,b2; 331 if(this.parallel_x()&&q.parallel_y()) 332 { 333 b1=this.p1.y; 334 b2=q.p1.x; 335 c.x=b2; 336 c.y=b1; 337 return c; 338 } 339 else if(this.parallel_y()&&!q.parallel_x()) { 340 k2=q.getXielv(); 341 b2=q.p1.y-q.p1.x*k2; 342 c.x = this.p1.x; 343 c.y = c.x*k2+b2; 344 return c; 345 } 346 else if(q.parallel_y()&&!this.parallel_x()) { 347 k1 = this.getXielv(); 348 b1 = this.p1.y - this.p1.x * k1; 349 c.x = q.p1.x; 350 c.y = c.x*k1+b1; 351 return c; 352 } 353 else if(q.parallel_x()&&this.parallel_y()) 354 { 355 b2=q.p1.y; 356 b1=this.p1.x; 357 c.x=b1; 358 c.y=b2; 359 return c; 360 } 361 else if(this.parallel_x()&&!q.parallel_y()) 362 { 363 b1=this.p1.y; 364 k2=q.getXielv(); 365 b2=q.p1.y-q.p1.x*k2; 366 c.y=b1; 367 c.x=(b1-b2)/k2; 368 return c; 369 } 370 else if(q.parallel_x()&&!this.parallel_y()) 371 { 372 b2=this.p1.y; 373 k1=this.getXielv(); 374 b1=this.p1.y-this.p1.x*k1; 375 c.y=b2; 376 c.x=(b2-b1)/k1; 377 return c; 378 } 379 else{ 380 k1 = this.getXielv(); 381 b1 = this.p1.y - this.p1.x * k1; 382 k2 = q.getXielv(); 383 b2 = q.p1.y - q.p1.x * k2; 384 c.x = (b2 - b1) / (k1 - k2); 385 c.y = k1 * c.x + b1; 386 return c; 387 } 388 389 } 390 391 boolean init(Double x,Double y,Line q)//判断交点是否在两条线段内 392 { 393 if((y<=Math.max(this.p1.y,this.p2.y)||y<=Math.max(q.p1.y,q.p2.y))&&((y>=Math.min(this.p1.y,this.p2.y)||y>=Math.min(q.p1.y,q.p2.y)))) 394 { 395 return true; 396 } 397 else 398 return false; 399 } 400 Point get_zhongdian() 401 { 402 Double x=(this.p1.x+this.p2.x)/2; 403 Double y=(this.p1.y+this.p2.y)/2; 404 Point a=new Point(x,y); 405 return a; 406 } 407 boolean is_cz(Line p) //判断两条线是否垂直 408 { 409 // if((this.parallel_x()&&p.parallel_y())||(this.parallel_y()&&p.parallel_x())) 410 // return true; 411 // else if(this.getXielv()*p.getXielv()==-1) 412 // return true; 413 // else return false; 414 if(Math.abs((this.p2.x-this.p1.x)*(p.p2.x-p.p1.x)+(this.p2.y-this.p1.y)*(p.p2.y-p.p1.y))<0.01) 415 return true; 416 else return false; 417 } 418 boolean is_sameline(Line q) 419 { 420 // return q.isin_line(this.p1) && q.isin_line(this.p2); 421 return this.p1.straight_line(q.p1,q.p2)&&this.p2.straight_line(q.p1,q.p2); 422 // return this.p1.Same(q.p1) && this.p2.Same(q.p2); 423 } 424 boolean xiangjiao(Line p,Line q) 425 { 426 // 快速排斥实验 首先判断两条线段在 x 以及 y 坐标的投影是否有重合。 有一个为真,则代表两线段必不可交。 427 if (Math.max(p.p1.x,p.p2.x) < Math.min(q.p1.x ,q.p2.x) 428 || Math.max(p.p1.y,p.p2.y) < Math.min(q.p1.y,q.p2.y) 429 || Math.max(q.p1.x ,q.p2.x) < Math.min(p.p1.x,p.p2.x) 430 || Math.max(q.p1.y,q.p2.y) < Math.min(p.p1.y,p.p2.y)) 431 { 432 return false; 433 } 434 if ((((p.p1.x - q.p1.x) * (q.p2.y - q.p1.y) - (p.p1.y - q.p1.y ) * (q.p2.x - q.p1.x)) 435 * ((p.p2.x - q.p1.x) * (q.p2.y - q.p1.y) - (p.p2.y - q.p1.y) * (q.p2.x - q.p1.x))) > 0 436 || (((q.p1.x - p.p1.x) * (p.p2.y - p.p1.y) - (q.p1.y - p.p1.y) * (p.p2.x - p.p1.x)) 437 * ((q.p2.x - p.p1.x) * (p.p2.y - p.p1.y) - (q.p2.y - p.p1.y) * (p.p2.x - p.p1.x))) > 0) 438 { 439 return false; 440 } 441 return true; 442 } 443 boolean xiangjiao2(Line q) 444 { 445 Double a1=(this.p1.x-q.p1.x)*(this.p2.y-q.p1.y)-(this.p1.y-q.p1.y)*(this.p2.x-q.p1.x); 446 Double a2=(this.p1.x-q.p2.x)*(this.p2.y-q.p2.y)-(this.p1.y-q.p2.y)*(this.p2.x-q.p2.x); 447 return (a1 >= 0 && a2 <= 0) || (a1 <= 0 && a2 >= 0); 448 } 449 } 450 class Triangle { 451 Line l1, l2, l3; 452 Point a;Point b;Point c; 453 454 Triangle(Line l1, Line l2, Line l3) { 455 this.l1 = l1; 456 this.l2 = l2; 457 this.l3 = l3; 458 } 459 Triangle(Point a,Point b,Point c) { 460 this.a=a;this.b=b;this.c=c; 461 Line ab = new Line(a, b); 462 Line bc = new Line(b, c); 463 Line ac = new Line(a, c); 464 this.l1 = ab; 465 this.l2 = bc; 466 this.l3 = ac; 467 } 468 469 Double get_zc() { 470 return (this.l1.length + this.l2.length + this.l3.length); 471 } 472 473 Double get_area() { 474 Double s = this.get_zc() / 2; 475 Double area=Math.sqrt((s * (s - this.l1.length) * (s - this.l2.length) * (s - this.l3.length))); 476 return area; 477 } 478 479 Double get_zx_x() { 480 Double x1 = this.l1.p1.x; 481 Double x2 = this.l1.p2.x; 482 Double x3 = this.l2.p2.x; 483 484 Double X1 = (x1 + x2 + x3) / 3; 485 return X1; 486 } 487 Double get_zx_y() 488 { 489 Double y1 = this.l1.p1.y; 490 Double y2 = this.l1.p2.y; 491 Double y3 = this.l2.p2.y; 492 Double Y1 = (y1 + y2 + y3) / 3; 493 return Y1; 494 } 495 boolean isinit(Line p,Line q) 496 { 497 if(p.init(Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).x,Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).y,q)) 498 return true; 499 else 500 return false; 501 } 502 Double getarea2(Point a,Point b,Point c) 503 { 504 Line p=new Line(a,b); 505 Line q=new Line(a,c); 506 Line r=new Line(b,c); 507 Triangle A=new Triangle(p,q,r); 508 return A.get_area(); 509 } 510 511 void output(Double s1,Double s2) 512 { 513 if(s1<=s2){ 514 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 515 System.exit(0); 516 } 517 else { 518 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 519 System.exit(0); 520 } 521 } 522 void qiege_Triangle(Point e,Point f,Point a,Point c,Point d) 523 { 524 Triangle A=new Triangle(a,c,d); 525 Line ac=new Line(a,c); 526 Line cd=new Line(c,d); 527 Line ad=new Line(a,d); 528 Line ef=new Line(e,f); 529 if(ef.is_sameline(ac)||ef.is_sameline(cd)||ef.is_sameline(ad)){ 530 System.out.print("The line is coincide with one of the lines"); 531 System.exit(0); 532 } 533 else if(a.straight_line(e,f)) 534 { 535 if(ef.xiangjiao2(cd)){ 536 Triangle C=new Triangle(a,c,ef.getjiaodian(cd)); 537 Triangle D=new Triangle(a,ef.getjiaodian(cd),d); 538 Double s1=C.get_area(); 539 Double s2= D.get_area(); 540 C.output(s1,s2); 541 } 542 else { 543 System.out.print("1"); 544 System.exit(0); 545 } 546 } 547 else if(c.straight_line(e,f)){ 548 if(ef.xiangjiao2(ad)) { 549 Triangle C = new Triangle(a, c, ef.getjiaodian(ad)); 550 Triangle D = new Triangle(c, ef.getjiaodian(ad), d); 551 Double s1 = C.get_area(); 552 Double s2 = D.get_area(); 553 C.output(s1, s2); 554 } 555 else { 556 System.out.print("1"); 557 System.exit(0); 558 } 559 } 560 else if(d.straight_line(e,f)){ 561 if(ef.xiangjiao2(ac)){ 562 Triangle C=new Triangle(a,ef.getjiaodian(ac),d); 563 Triangle D=new Triangle(ef.getjiaodian(ac),c,d); 564 Double s1=C.get_area(); 565 Double s2= D.get_area(); 566 C.output(s1,s2);} 567 else { 568 System.out.print("1"); 569 System.exit(0); 570 } 571 } 572 else if(ef.xiangjiao2(ac)&& ef.xiangjiao2(cd)){ 573 Triangle C=new Triangle(ef.getjiaodian(ac),c,ef.getjiaodian(cd)); 574 Double s1=C.get_area(); 575 Double s2=A.get_area()-s1; 576 C.output(s1,s2); 577 } 578 else if(ef.xiangjiao2(ac)&& ef.xiangjiao2(ad)){ 579 Triangle C=new Triangle(a,ef.getjiaodian(ac),ef.getjiaodian(ad)); 580 Double s1=C.get_area(); 581 Double s2=A.get_area()-s1; 582 C.output(s1,s2); 583 } 584 else if(ef.xiangjiao2(ad)&& ef.xiangjiao2(cd)){ 585 Triangle C=new Triangle(ef.getjiaodian(cd),d,ef.getjiaodian(ad)); 586 Double s1=C.get_area(); 587 Double s2=A.get_area()-s1; 588 C.output(s1,s2); 589 } 590 } 591 } 592 class Quadrangle { 593 Line ab; 594 Line bc; 595 Line cd; 596 Line ad; 597 Point a;Point b;Point c;Point d; 598 Quadrangle(Line ab,Line bc,Line cd,Line ad) 599 { 600 this.ab=ab; 601 this.bc=bc; 602 this.cd=cd; 603 this.ad=ad; 604 } 605 Quadrangle(Point a,Point b,Point c,Point d) 606 { 607 this.a=a;this.b=b;this.c=c;this.d=d; 608 Line ab = new Line(a, b); 609 Line bc = new Line(b, c); 610 Line cd = new Line(c, d); 611 Line ad = new Line(a, d); 612 this.ab=ab; 613 this.bc=bc; 614 this.cd=cd; 615 this.ad=ad; 616 } 617 boolean is_px_sibianxing() 618 { 619 return this.ab.parallel(cd)&&this.bc.parallel(ad); 620 } 621 boolean is_zf_sibianxing() 622 { 623 return this.is_px_sibianxing()&&(Objects.equals(this.ab.length, this.bc.length))&&this.ab.is_cz(bc); 624 } 625 boolean is_lx_sibianxing() 626 { 627 return this.is_px_sibianxing()&&(Objects.equals(this.ab.length, this.bc.length)); 628 } 629 boolean is_jx_sibianxing() 630 { 631 return this.is_px_sibianxing()&&this.ab.is_cz(bc); 632 } 633 void what_Quadrangle() 634 { 635 if(this.is_zf_sibianxing()){ 636 System.out.print("true true true"); 637 System.exit(0); 638 } 639 else if(this.is_lx_sibianxing()){ 640 System.out.print("true false false"); 641 System.exit(0); 642 } 643 else if(this.is_jx_sibianxing()){ 644 System.out.print("false true false"); 645 System.exit(0); 646 } 647 else{ 648 System.out.print("false false false"); 649 System.exit(0); 650 } 651 } 652 boolean tu_sibianxing(Point p1,Point p2,Point p3,Point p4) 653 { 654 Double t1 = (p4.x-p1.x)*(p2.y-p1.y)-(p4.y-p1.y)*(p2.x-p1.x); 655 Double t2 = (p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x); 656 Double t3 = (p2.x-p3.x)*(p4.y-p3.y)-(p2.y-p3.y)*(p4.x-p3.x); 657 Double t4 = (p3.x-p4.x)*(p1.y-p4.y)-(p3.y-p4.y)*(p1.x-p4.x); 658 return t1*t2*t3*t4>=0; 659 } 660 Double get_zc() 661 { 662 return this.ab.length+this.bc.length+ad.length+this.cd.length; 663 } 664 Double get_area(Line ac,Line bd) 665 { 666 Triangle a=new Triangle(this.ab,this.bc,ac); 667 Triangle b=new Triangle(this.cd,this.ad,ac); 668 Triangle c=new Triangle(this.ab,this.ad,bd); 669 Triangle d=new Triangle(this.bc,this.cd,bd); 670 if(this.tu_sibianxing(this.ab.p1,this.ab.p2,this.cd.p1,this.cd.p2)) 671 return c.get_area()+d.get_area(); 672 else { 673 if (Objects.equals(c.get_area(), d.get_area())) 674 return Math.abs(a.get_area() - b.get_area()); 675 else 676 return Math.abs(c.get_area() - d.get_area()); 677 } 678 } 679 void output(Double s1,Double s2) 680 { 681 if(s1<=s2){ 682 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 683 System.exit(0); 684 } 685 else { 686 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 687 System.exit(0); 688 } 689 } 690 Double get_area() 691 { 692 // s=1/2*[(x1*y2-x2*y1)+(x2*y3-x3*y2)+...... +(Xk*Yk+1-Xk+1*Yk)+...+(Xn*y1-x1*Yn) ] 693 double s=0; 694 s=(this.a.x*this.b.y-this.b.x*this.a.y)+(this.b.x*this.c.y-this.c.x*this.b.y)+(this.c.x*this.d.y-this.d.x*this.c.y)+(this.d.x*this.a.y-this.a.x*this.d.y); 695 return Math.abs(s/2); 696 } 697 boolean in_it(Point e,Point a,Point b,Point c,Point d) 698 { 699 Line ab=new Line(a,b); 700 Line bc=new Line(b,c); 701 Line cd=new Line(c,d); 702 Line ad=new Line(a,d); 703 Line ea=new Line(e,a); 704 Line eb=new Line(e,b); 705 Line ec=new Line(e,c); 706 Line ed=new Line(e,d); 707 Line ac=new Line(a,c); 708 Line bd=new Line(b,d); 709 Triangle A=new Triangle(ea,eb,ab); 710 Triangle B=new Triangle(eb,ec,bc); 711 Triangle C=new Triangle(ec,ed,cd); 712 Triangle D=new Triangle(ea,ed,ad); 713 if(Math.abs(A.get_area()+B.get_area()+C.get_area()+D.get_area()-this.get_area(ac,bd))<0.1) 714 return true; 715 else 716 return false; 717 } 718 void qiege_sibianxing(Point e,Point f, Point a, Point b, Point c, Point d) 719 { 720 Line ab = new Line(a, b); 721 Line bc = new Line(b, c); 722 Line cd = new Line(c, d); 723 Line ad = new Line(a, d); 724 Line ef=new Line(e,f); 725 Line ac=new Line(a,c); 726 Line bd=new Line(b,d); 727 Quadrangle A=new Quadrangle(a,b,c,d); 728 if(ef.is_sameline(ab)||ef.is_sameline(bc)||ef.is_sameline(cd)||ef.is_sameline(ad)){ 729 System.out.print("The line is coincide with one of the lines"); 730 System.exit(0); 731 } 732 else if(ef.is_sameline(ac)){ 733 Triangle C=new Triangle(ab,bc,ac); 734 Triangle D=new Triangle(cd,ad,ac); 735 Double s1=C.get_area(); 736 Double s2= D.get_area(); 737 C.output(s1,s2); 738 } 739 else if(ef.is_sameline(bd)){ 740 Triangle C=new Triangle(ab,ad,bd); 741 Triangle D=new Triangle(bc,cd,bd); 742 Double s1=C.get_area(); 743 Double s2= D.get_area(); 744 C.output(s1,s2); 745 } 746 else if(ef.xiangjiao2(ab)&& ef.xiangjiao2(cd)){ 747 Quadrangle C=new Quadrangle(a,ef.getjiaodian(ab),ef.getjiaodian(cd),d); 748 Quadrangle D=new Quadrangle(ef.getjiaodian(ab),b,c,ef.getjiaodian(cd)); 749 Double s1=C.get_area(); 750 Double s2= D.get_area(); 751 C.output(s1,s2); 752 } 753 else if(ef.xiangjiao2(bc)&& ef.xiangjiao2(ad)){ 754 Quadrangle C=new Quadrangle(a,b,ef.getjiaodian(bc),ef.getjiaodian(ad)); 755 Quadrangle D=new Quadrangle(ef.getjiaodian(bc),c,d,ef.getjiaodian(ad)); 756 Double s1=C.get_area(); 757 Double s2= D.get_area(); 758 C.output(s1,s2); 759 } 760 else if(ef.xiangjiao2(ab)&&ef.xiangjiao2(bc)){ 761 Triangle C=new Triangle(ef.getjiaodian(ab),b,ef.getjiaodian(bc)); 762 Double s1=C.get_area(); 763 Double s2=Math.abs(A.get_area()- C.get_area()); 764 C.output(s1,s2); 765 } 766 else if(ef.xiangjiao2(bc)&&ef.xiangjiao2(cd)){ 767 Triangle C=new Triangle(ef.getjiaodian(bc),c,ef.getjiaodian(cd)); 768 Double s1=C.get_area(); 769 Double s2=Math.abs(A.get_area()- C.get_area()); 770 C.output(s1,s2); 771 } 772 else if(ef.xiangjiao2(cd)&&ef.xiangjiao2(ad)){ 773 Triangle C=new Triangle(ef.getjiaodian(cd),d,ef.getjiaodian(ad)); 774 Double s1=C.get_area(); 775 Double s2=Math.abs(A.get_area()- C.get_area()); 776 C.output(s1,s2); 777 } 778 else if(ef.xiangjiao2(ab)&&ef.xiangjiao2(ad)){ 779 Triangle C=new Triangle(a,ef.getjiaodian(ab),ef.getjiaodian(ad)); 780 Double s1=C.get_area(); 781 Double s2=Math.abs(A.get_area()- C.get_area()); 782 C.output(s1,s2); 783 } 784 else if(ef.isin_line(a)&&ef.xiangjiao2(bc)){ 785 Triangle C=new Triangle(a,b,ef.getjiaodian(bc)); 786 Double s1=C.get_area(); 787 Double s2=Math.abs(A.get_area()- C.get_area()); 788 C.output(s1,s2); 789 } 790 else if(ef.isin_line(a)&&ef.xiangjiao2(cd)){ 791 Triangle C=new Triangle(a,ef.getjiaodian(cd),d); 792 Double s1=C.get_area(); 793 Double s2=Math.abs(A.get_area()- C.get_area()); 794 C.output(s1,s2); 795 } 796 else if(ef.isin_line(b)&&ef.xiangjiao2(cd)){ 797 Triangle C=new Triangle(b,c,ef.getjiaodian(cd)); 798 Double s1=C.get_area(); 799 Double s2=Math.abs(A.get_area()- C.get_area()); 800 C.output(s1,s2); 801 } 802 else if(ef.isin_line(b)&&ef.xiangjiao2(ad)){ 803 Triangle C=new Triangle(a,b,ef.getjiaodian(ad)); 804 Double s1=C.get_area(); 805 Double s2=Math.abs(A.get_area()- C.get_area()); 806 C.output(s1,s2); 807 } 808 else if(ef.isin_line(c)&&ef.xiangjiao2(ab)){ 809 Triangle C=new Triangle(ef.getjiaodian(ab),b,c); 810 Double s1=C.get_area(); 811 Double s2=Math.abs(A.get_area()- C.get_area()); 812 C.output(s1,s2); 813 } 814 else if(ef.isin_line(c)&&ef.xiangjiao2(ad)){ 815 Triangle C=new Triangle(c,d,ef.getjiaodian(ad)); 816 Double s1=C.get_area(); 817 Double s2=Math.abs(A.get_area()- C.get_area()); 818 C.output(s1,s2); 819 } 820 else if(ef.isin_line(d)&&ef.xiangjiao2(bc)){ 821 Triangle C=new Triangle(ef.getjiaodian(bc),c,d); 822 Double s1=C.get_area(); 823 Double s2=Math.abs(A.get_area()- C.get_area()); 824 C.output(s1,s2); 825 } 826 else if(ef.isin_line(d)&&ef.xiangjiao2(ab)){ 827 Triangle C=new Triangle(a,ef.getjiaodian(ab),d); 828 Double s1=C.get_area(); 829 Double s2=Math.abs(A.get_area()- C.get_area()); 830 C.output(s1,s2); 831 } 832 else 833 { 834 System.out.print("1"); 835 System.exit(0); 836 } 837 } 838 839 840 } 841 class Pentagon { 842 Line ab;Line bc;Line cd;Line de;Line ae; 843 Point a;Point b;Point c;Point d;Point e; 844 Pentagon(Point a,Point b,Point c,Point d,Point e) 845 { 846 this.a=a;this.b=b;this.c=c;this.d=d;this.e=e; 847 Line ab = new Line(a, b); 848 Line bc = new Line(b, c); 849 Line cd = new Line(c, d); 850 Line de = new Line(e, d); 851 Line ae = new Line(a,e); 852 this.ab=ab; 853 this.bc=bc; 854 this.cd=cd; 855 this.de=de; 856 this.ae=ae; 857 } 858 boolean tu_Pentagon(Point p1,Point p2,Point p3,Point p4,Point p5) 859 { 860 Double t1 = (p5.x-p1.x)*(p2.y-p1.y)-(p5.y-p1.y)*(p2.x-p1.x); 861 Double t2 = (p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x); 862 Double t3 = (p2.x-p3.x)*(p4.y-p3.y)-(p2.y-p3.y)*(p4.x-p3.x); 863 Double t4 = (p3.x-p4.x)*(p5.y-p4.y)-(p3.y-p4.y)*(p5.x-p4.x); 864 Double t5 = (p4.x-p5.x)*(p1.y-p5.y)-(p4.y-p5.y)*(p1.x-p5.x); 865 return t1*t2*t3*t4*t5<=0; 866 } 867 Double get_zc() 868 { 869 return this.ab.length+this.bc.length+cd.length+this.de.length+this.ae.length; 870 } 871 Double get_area() 872 { 873 // s=1/2*[(x1*y2-x2*y1)+(x2*y3-x3*y2)+...... +(Xk*Yk+1-Xk+1*Yk)+...+(Xn*y1-x1*Yn) ] 874 double s=0; 875 s=(this.a.x*this.b.y-this.b.x*this.a.y)+(this.b.x*this.c.y-this.c.x*this.b.y)+ 876 (this.c.x*this.d.y-this.d.x*this.c.y)+(this.d.x*this.e.y-this.e.x*this.d.y)+(this.e.x*this.a.y-this.a.x*this.e.y); 877 return Math.abs(s/2); 878 } 879 void output(Double s1,Double s2) 880 { 881 if(s1<=s2){ 882 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 883 System.exit(0); 884 } 885 else { 886 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 887 System.exit(0); 888 } 889 } 890 void qiege_pentagon(Point f,Point g,Point a,Point b,Point c,Point d,Point e) 891 { 892 Line ab = new Line(a, b); 893 Line bc = new Line(b, c); 894 Line cd = new Line(c, d); 895 Line de = new Line(d, e); 896 Line ae = new Line(a, e); 897 Line fg = new Line(f, g); 898 Line ac = new Line(a, c); 899 Line ad = new Line(a, d); 900 Line bd = new Line(b, d); 901 Line be = new Line(b, e); 902 Line ce = new Line(c, e); 903 Pentagon A=new Pentagon(a,b,c,d,e); 904 if(fg.is_sameline(ab)||fg.is_sameline(bc)||fg.is_sameline(cd)||fg.is_sameline(de)||fg.is_sameline(ae)){ 905 System.out.print("The line is coincide with one of the lines"); 906 System.exit(0); 907 } 908 else if(fg.is_sameline(ac)){ 909 Triangle C=new Triangle(a,b,c); 910 Double s1=C.get_area(); 911 Double s2=A.get_area()-s1; 912 C.output(s1,s2); 913 } 914 else if(fg.is_sameline(ad)){ 915 Triangle C=new Triangle(a,d,e); 916 Double s1=C.get_area(); 917 Double s2=A.get_area()-s1; 918 C.output(s1,s2); 919 } 920 else if(fg.is_sameline(bd)){ 921 Triangle C=new Triangle(b,c,d); 922 Double s1=C.get_area(); 923 Double s2=A.get_area()-s1; 924 C.output(s1,s2); 925 } 926 else if(fg.is_sameline(be)){ 927 Triangle C=new Triangle(a,b,e); 928 Double s1=C.get_area(); 929 Double s2=A.get_area()-s1; 930 C.output(s1,s2); 931 } 932 else if(fg.is_sameline(ce)){ 933 Triangle C=new Triangle(c,d,e); 934 Double s1=C.get_area(); 935 Double s2=A.get_area()-s1; 936 C.output(s1,s2); 937 } 938 else if(fg.xiangjiao2(ab)&&fg.xiangjiao2(bc)){ 939 Triangle C=new Triangle(fg.getjiaodian(ab),b,fg.getjiaodian(bc)); 940 Double s1=C.get_area(); 941 Double s2=A.get_area()-s1; 942 C.output(s1,s2); 943 } 944 else if(fg.xiangjiao2(bc)&&fg.xiangjiao2(cd)){ 945 Triangle C=new Triangle(fg.getjiaodian(bc),c,fg.getjiaodian(cd)); 946 Double s1=C.get_area(); 947 Double s2=A.get_area()-s1; 948 C.output(s1,s2); 949 } 950 else if(fg.xiangjiao2(cd)&&fg.xiangjiao2(de)){ 951 Triangle C=new Triangle(fg.getjiaodian(cd),d,fg.getjiaodian(de)); 952 Double s1=C.get_area(); 953 Double s2=A.get_area()-s1; 954 C.output(s1,s2); 955 } 956 else if(fg.xiangjiao2(de)&&fg.xiangjiao2(ae)){ 957 Triangle C=new Triangle(fg.getjiaodian(de),e,fg.getjiaodian(ae)); 958 Double s1=C.get_area(); 959 Double s2=A.get_area()-s1; 960 C.output(s1,s2); 961 } 962 else if(fg.xiangjiao2(ae)&&fg.xiangjiao2(ab)){ 963 Triangle C=new Triangle(fg.getjiaodian(ae),a,fg.getjiaodian(ab)); 964 Double s1=C.get_area(); 965 Double s2=A.get_area()-s1; 966 C.output(s1,s2); 967 } 968 else if(fg.xiangjiao2(ab)&&fg.xiangjiao2(cd)){ 969 Quadrangle C=new Quadrangle(fg.getjiaodian(ab),b,c,fg.getjiaodian(cd)); 970 Double s1=C.get_area(); 971 Double s2=A.get_area()-s1; 972 C.output(s1,s2); 973 } 974 else if(fg.xiangjiao2(ab)&&fg.xiangjiao2(de)){ 975 Quadrangle C=new Quadrangle(a,fg.getjiaodian(ab),fg.getjiaodian(de),e); 976 Double s1=C.get_area(); 977 Double s2=A.get_area()-s1; 978 C.output(s1,s2); 979 } 980 else if(fg.xiangjiao2(bc)&&fg.xiangjiao2(de)){ 981 Quadrangle C=new Quadrangle(fg.getjiaodian(bc),c,d,fg.getjiaodian(de)); 982 Double s1=C.get_area(); 983 Double s2=A.get_area()-s1; 984 C.output(s1,s2); 985 } 986 else if(fg.xiangjiao2(bc)&&fg.xiangjiao2(ae)){ 987 Quadrangle C=new Quadrangle(b,fg.getjiaodian(bc),fg.getjiaodian(ae),a); 988 Double s1=C.get_area(); 989 Double s2=A.get_area()-s1; 990 C.output(s1,s2); 991 } 992 else if(fg.xiangjiao2(cd)&&fg.xiangjiao2(ae)){ 993 Quadrangle C=new Quadrangle(fg.getjiaodian(cd),d,e,fg.getjiaodian(ae)); 994 Double s1=C.get_area(); 995 Double s2=A.get_area()-s1; 996 C.output(s1,s2); 997 } 998 else 999 { 1000 System.out.print("1"); 1001 System.exit(0); 1002 } 1003 } 1004 }View Code
第二题:
1 import java.util.Scanner; 2 import java.util.Objects; 3 4 import java.math.BigDecimal; 5 import java.math.RoundingMode; 6 public class Main { 7 public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){ 8 9 double A1=p1.y-p2.y; 10 double B1=p2.x-p1.x; 11 double C1=A1*p1.x+B1*p1.y; 12 13 double A2=p3.y-p4.y; 14 double B2=p4.x-p3.x; 15 double C2=A2*p3.x+B2*p3.y; 16 17 double det_k=A1*B2-A2*B1; 18 19 if(Math.abs(det_k)<0.00001){ 20 return null; 21 } 22 23 double a=B2/det_k; 24 double b=-1*B1/det_k; 25 double c=-1*A2/det_k; 26 double d=A1/det_k; 27 28 double x=a*C1+b*C2; 29 double y=c*C1+d*C2; 30 31 return new Point(x,y); 32 } 33 public static Double output(Double data) 34 { 35 double a = data; 36 37 BigDecimal bigDecimal = new BigDecimal(a); 38 double res = bigDecimal.setScale(3, RoundingMode.HALF_UP).doubleValue(); 39 return res; 40 } 41 static void operate_6(Point f, Point a, Point b, Point c, Point d,Point e) 42 { 43 if(a.what_polygon(a,b,c,d,e)==1) 44 { 45 Pentagon A=new Pentagon(a,b,c,d,e); 46 if(A.on_it(f,a,b,c,d,e)) { 47 System.out.print("on the pentagon"); 48 } 49 else if (A.in_it(f,a,b,c,d,e)) { 50 System.out.print("in the pentagon"); 51 } 52 else{ 53 System.out.print("outof the pentagon"); 54 } 55 } 56 else if(a.what_polygon(a,b,c,d,e)==2) 57 { 58 Pentagon A=new Pentagon(a,b,c,d,e); 59 if(A.on_it(f,a,b,c,d,e)) { 60 System.out.print("on the quadrilateral"); 61 } 62 else if (A.in_it(f,a,b,c,d,e)) { 63 System.out.print("in the quadrilateral"); 64 } 65 else{ 66 System.out.print("outof the quadrilateral"); 67 } 68 } 69 else if(a.what_polygon(a,b,c,d,e)==3) 70 { 71 Pentagon A=new Pentagon(a,b,c,d,e); 72 if(A.on_it(f,a,b,c,d,e)) { 73 System.out.print("on the triangle"); 74 } 75 else if (A.in_it(f,a,b,c,d,e)) { 76 System.out.print("in the triangle"); 77 } 78 else{ 79 System.out.print("outof the triangle"); 80 } 81 } 82 else 83 { 84 System.out.print("not a polygon"); 85 } 86 } 87 static void operate5(Pentagon A, Pentagon B) { 88 if(A.fenli(B)) 89 { 90 System.out.print("0"); 91 } 92 else if(A.coincides(B)) 93 { 94 System.out.print(A.get_area()); 95 } 96 else if(A.inside(B)) 97 { 98 System.out.print(A.get_area()); 99 } 100 else if(B.inside(A)) 101 { 102 System.out.print(B.get_area()); 103 } 104 // else if((A.jiaocha(B.ab)||A.jiaocha(B.bc)||A.jiaocha(B.cd)||A.jiaocha(B.de)||A.jiaocha(B.ae))) 105 // { 106 // if(B.bc.xiangjiao(B.bc,A.bc)&&B.bc.xiangjiao(B.bc,A.de)) 107 // { 108 109 // Triangle D=new Triangle(A.d,A.bc.getjiaodian(B.bc),A.de.getjiaodian(B.bc)); 110 // System.out.print(output(D.get_area())); 111 // } 112 // } 113 else 114 System.out.print("4.0"); 115 116 } 117 118 public static void main(String[] args) { 119 Scanner in = new Scanner(System.in); 120 String input = in.nextLine(); 121 if(!input.matches("^[1-6][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+$")) 122 { 123 System.out.println("Wrong Format"); 124 return; 125 } 126 String[] option =input.split(":"); 127 String shuju=option[1]; 128 String[] point = shuju.split(" "); 129 String[] num = null; 130 switch(option[0]) 131 { 132 case "1": 133 if(point.length!=5) { 134 System.out.print("wrong number of points"); 135 System.exit(0); 136 } 137 num = point[0].split(","); 138 Point a=new Point(num[0],num[1]); 139 num = point[1].split(","); 140 Point b=new Point(num[0],num[1]); 141 num = point[2].split(","); 142 Point c=new Point(num[0],num[1]); 143 num = point[3].split(","); 144 Point d=new Point(num[0],num[1]); 145 num = point[4].split(","); 146 Point e=new Point(num[0],num[1]); 147 System.out.print(a.is_pentagon(a,b,c,d,e)); 148 break; 149 case "2": 150 if(point.length!=5) { 151 System.out.print("wrong number of points"); 152 System.exit(0); 153 } 154 num = point[0].split(","); 155 a=new Point(num[0],num[1]); 156 num = point[1].split(","); 157 b=new Point(num[0],num[1]); 158 num = point[2].split(","); 159 c=new Point(num[0],num[1]); 160 num = point[3].split(","); 161 d=new Point(num[0], num[1]); 162 num = point[4].split(","); 163 e=new Point(num[0],num[1]); 164 if(!a.is_pentagon(a,b,c,d,e)) 165 { 166 System.out.print("not a pentagon"); 167 System.exit(0); 168 } 169 Pentagon A=new Pentagon(a,b,c,d,e); 170 if(A.tu_Pentagon(a,b,c,d,e)){ 171 System.out.print("true "+output(A.get_zc())+" "+output(A.get_area())); 172 } 173 else 174 System.out.print("false"); 175 break; 176 case "4": 177 if(point.length!=10) { 178 System.out.print("wrong number of points"); 179 System.exit(0); 180 } 181 num = point[0].split(","); 182 a=new Point(num[0],num[1]); 183 num = point[1].split(","); 184 b=new Point(num[0],num[1]); 185 num = point[2].split(","); 186 c=new Point(num[0],num[1]); 187 num = point[3].split(","); 188 d=new Point(num[0],num[1]); 189 num = point[4].split(","); 190 e=new Point(num[0],num[1]); 191 num = point[5].split(","); 192 Point f=new Point(num[0], num[1]); 193 num = point[6].split(","); 194 Point g=new Point(num[0],num[1]); 195 num = point[7].split(","); 196 Point h=new Point(num[0],num[1]); 197 num = point[8].split(","); 198 Point i=new Point(num[0], num[1]); 199 num = point[9].split(","); 200 Point j=new Point(num[0],num[1]); 201 if(a.what_polygon(a,b,c,d,e)!=0&&f.what_polygon(f,g,h,i,j)!=0) 202 { 203 A=new Pentagon(a,b,c,d,e); 204 Pentagon B=new Pentagon(f,g,h,i,j); 205 A.what_relate(A,B); 206 } 207 else 208 { 209 A=new Pentagon(a,b,c,d,e); 210 Pentagon B=new Pentagon(f,g,h,i,j); 211 String[] s={"pentagon", "quadrilateral", "triangle"}; 212 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" is interlaced with the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 213 System.exit(0); 214 } 215 break; 216 case "5": 217 if(point.length!=10) { 218 System.out.print("wrong number of points"); 219 System.exit(0); 220 } 221 num = point[0].split(","); 222 a=new Point(num[0],num[1]); 223 num = point[1].split(","); 224 b=new Point(num[0],num[1]); 225 num = point[2].split(","); 226 c=new Point(num[0],num[1]); 227 num = point[3].split(","); 228 d=new Point(num[0],num[1]); 229 num = point[4].split(","); 230 e=new Point(num[0],num[1]); 231 num = point[5].split(","); 232 f=new Point(num[0], num[1]); 233 num = point[6].split(","); 234 g=new Point(num[0],num[1]); 235 num = point[7].split(","); 236 h=new Point(num[0],num[1]); 237 num = point[8].split(","); 238 i=new Point(num[0], num[1]); 239 num = point[9].split(","); 240 j=new Point(num[0],num[1]); 241 A=new Pentagon(a,b,c,d,e); 242 Pentagon B=new Pentagon(f,g,h,i,j); 243 operate5(A,B); 244 break; 245 case "6": 246 if(point.length!=6) { 247 System.out.print("wrong number of points"); 248 System.exit(0); 249 } 250 num = point[0].split(","); 251 f=new Point(num[0],num[1]); 252 num = point[1].split(","); 253 a=new Point(num[0],num[1]); 254 num = point[2].split(","); 255 b=new Point(num[0],num[1]); 256 num = point[3].split(","); 257 c=new Point(num[0], num[1]); 258 num = point[4].split(","); 259 d=new Point(num[0],num[1]); 260 num = point[5].split(","); 261 e=new Point(num[0],num[1]); 262 operate_6(f,a,b,c,d,e); 263 break; 264 default:System.out.print("Wrong Format"); 265 break; 266 } 267 268 } 269 } 270 class Point { 271 double x,y; 272 Point(String x,String y) 273 { 274 this.x=Double.valueOf(x); 275 this.y=Double.valueOf(y); 276 } 277 Point(Double x,Double y) 278 { 279 this.x=x; 280 this.y=y; 281 } 282 boolean Same(Point b) {//判断两点是否重合 283 if ((this.x==b.x)&&(this.y==b.y)) 284 return true; 285 else 286 return false; 287 } 288 Double Distance(Point b)//计算两点间的距离 289 { 290 Double distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y)); 291 return distance; 292 } 293 double Xielv(Point b)//计算两点所构成直线的斜率 294 { 295 double xielv=(b.y-this.y)/(b.x-this.x); 296 return xielv; 297 } 298 Double Czdistance(Point b,Point c)//计算点到直线的垂直距离 299 { 300 Double L = Math.abs((b.y-c.y)*this.x+(c.x-b.x)*this.y+b.x*c.y-c.y*c.x)/Math.sqrt((b.y-c.y) *(b.y-c.y) +(b.x-c.x)*(b.x-c.x)); 301 return L; 302 } 303 boolean straight_line(Point b,Point c)//判断三个点是否在一条直线上 304 { 305 if(this.Same(b)||this.Same(c)) 306 return true; 307 else if(this.Xielv(b)==this.Xielv(c)) 308 return true; 309 else 310 return false; 311 } 312 boolean is_sibianxing(Point a,Point b,Point c,Point d) 313 { 314 Line ab=new Line(a,b); 315 Line bc=new Line(b,c); 316 Line cd=new Line(c,d); 317 Line ad=new Line(a,d); 318 if(ab.parallel(bc)||ab.parallel(ad)||bc.parallel(cd)||cd.parallel(ad)||ab.xiangjiao(ab,cd)||bc.xiangjiao(bc,ad)) 319 return false; 320 else return true; 321 } 322 boolean on_line(Point a,Point b) 323 { 324 return Math.abs(this.Distance(a)+this.Distance(b)-a.Distance(b))<=0.001; 325 } 326 Double getarea(Point a,Point b,Point c) 327 { 328 Double C=a.Distance(b)+a.Distance(c)+b.Distance(c); 329 Double s=C/2; 330 Double area=Math.sqrt((s * a.Distance(b) *a.Distance(c) * b.Distance(c))); 331 return area; 332 } 333 boolean is_Triangle(Point a,Point b,Point c) 334 { 335 if(a.Same(b)||b.Same(c)||a.Same(c)||a.straight_line(b,c)) 336 return false; 337 else 338 return true; 339 } 340 boolean is_pentagon(Point a,Point b,Point c,Point d,Point e) 341 { 342 Line ab=new Line(a,b); 343 Line bc=new Line(b,c); 344 Line cd=new Line(c,d); 345 Line de=new Line(d,e); 346 Line ae=new Line(a,e); 347 if(ab.parallel(bc)||bc.parallel(cd)||cd.parallel(de)||de.parallel(ae)||ae.parallel(ab)|| 348 ab.xiangjiao(ab,cd)||ab.xiangjiao(bc,de)||bc.xiangjiao(bc,de)||bc.xiangjiao(bc,ae)||cd.xiangjiao(cd,ae)) 349 return false; 350 else return true; 351 } 352 int what_polygon(Point a,Point b,Point c,Point d,Point e) 353 { 354 if(a.is_pentagon(a,b,c,d,e)) 355 return 1; 356 else if(a.Same(b)||b.Same(c)||c.Same(d)||d.Same(e)||a.Same(e)||a.Same(c)||a.Same(d)||b.Same(d)||b.Same(e)||c.Same(e)|| 357 a.on_line(b,e)||b.on_line(a,c)||c.on_line(b,d)||d.on_line(c,e)||e.on_line(a,d)) 358 { 359 if((a.Same(b)||a.Same(c)||a.Same(d)||a.Same(e)||a.on_line(b,e))&&a.is_sibianxing(b,c,d,e)) 360 return 2; 361 else if((b.Same(c)||b.Same(d)||b.Same(e)||b.on_line(a,c))&&a.is_sibianxing(a,c,d,e)) 362 return 2; 363 else if((c.Same(d)||c.Same(e)||c.on_line(b,d))&&a.is_sibianxing(a,b,d,e)) 364 return 2; 365 else if((d.Same(e)||d.on_line(c,e))&&a.is_sibianxing(a,b,c,e)) 366 return 2; 367 else if(e.on_line(a,d)&&a.is_sibianxing(a,b,c,d)) 368 return 2; 369 else if(((a.Same(b)&&a.Same(c))||(b.on_line(a,c)&&c.on_line(b,d)))&&a.is_Triangle(a,d,e)) 370 return 3; 371 else if((b.on_line(a,c)||d.on_line(c,e))&&a.is_Triangle(a,c,e)) 372 return 3; 373 else if((e.on_line(a,d)||d.on_line(c,e))&&a.is_Triangle(a,b,c)) 374 return 3; 375 } 376 return 0; 377 } 378 } 379 class Line { 380 // Double A,B,C; 381 Point p1,p2; 382 Double length; 383 384 Line(Point a,Point b) 385 { 386 this.p1=a; 387 this.p2=b; 388 this.length=a.Distance(b); 389 // this.A=b.y-a.y; 390 // this.B=a.x-b.x; 391 // this.C=this.A*a.x+this.B*a.y; 392 } 393 boolean parallel_x()//判断直线是否平行x轴 394 { 395 if(this.p1.y==this.p2.y) 396 return true; 397 else 398 return false; 399 } 400 boolean parallel_y()//判断直线是否平行y轴 401 { 402 if(this.p1.x==this.p2.x) 403 return true; 404 else 405 return false; 406 } 407 Double getXielv()//计算直线的斜率 408 { 409 if(this.parallel_y()) 410 return null; 411 else if(this.parallel_x()) 412 { 413 double xielv=0; 414 return xielv; 415 } 416 else 417 return (this.p1.y-this.p2.y)/(this.p1.x-this.p2.x); 418 419 } 420 boolean parallel(Line p)//判断两条直线是否平行 421 { 422 if(this.parallel_x()&&p.parallel_x()) 423 return true; 424 else if(this.parallel_y()&&p.parallel_y()) 425 return true; 426 else if(Objects.equals(p.getXielv(), this.getXielv())) 427 return true; 428 else 429 return false; 430 } 431 boolean isin_line(Point a) //判断点在直线上 432 { 433 if(Math.abs(a.Czdistance(this.p1,this.p2))<0.001) 434 return true; 435 else 436 return false; 437 } 438 439 Point getjiaodian(Line q) 440 { 441 // y=kx+b 442 Point c=new Point(1.0, 1.0); 443 Double k1,b1,k2,b2; 444 if(this.parallel_x()&&q.parallel_y()) 445 { 446 b1=this.p1.y; 447 b2=q.p1.x; 448 c.x=b2; 449 c.y=b1; 450 return c; 451 } 452 else if(this.parallel_y()&&!q.parallel_x()) { 453 k2=q.getXielv(); 454 b2=q.p1.y-q.p1.x*k2; 455 c.x = this.p1.x; 456 c.y = c.x*k2+b2; 457 return c; 458 } 459 else if(q.parallel_y()&&!this.parallel_x()) { 460 k1 = this.getXielv(); 461 b1 = this.p1.y - this.p1.x * k1; 462 c.x = q.p1.x; 463 c.y = c.x*k1+b1; 464 return c; 465 } 466 else if(q.parallel_x()&&this.parallel_y()) 467 { 468 b2=q.p1.y; 469 b1=this.p1.x; 470 c.x=b1; 471 c.y=b2; 472 return c; 473 } 474 else if(this.parallel_x()&&!q.parallel_y()) 475 { 476 b1=this.p1.y; 477 k2=q.getXielv(); 478 b2=q.p1.y-q.p1.x*k2; 479 c.y=b1; 480 c.x=(b1-b2)/k2; 481 return c; 482 } 483 else if(q.parallel_x()&&!this.parallel_y()) 484 { 485 b2=this.p1.y; 486 k1=this.getXielv(); 487 b1=this.p1.y-this.p1.x*k1; 488 c.y=b2; 489 c.x=(b2-b1)/k1; 490 return c; 491 } 492 else{ 493 k1 = this.getXielv(); 494 b1 = this.p1.y - this.p1.x * k1; 495 k2 = q.getXielv(); 496 b2 = q.p1.y - q.p1.x * k2; 497 c.x = (b2 - b1) / (k1 - k2); 498 c.y = k1 * c.x + b1; 499 return c; 500 } 501 502 } 503 504 boolean init(Double x,Double y,Line q)//判断交点是否在两条线段内 505 { 506 if((y<=Math.max(this.p1.y,this.p2.y)||y<=Math.max(q.p1.y,q.p2.y))&&((y>=Math.min(this.p1.y,this.p2.y)||y>=Math.min(q.p1.y,q.p2.y)))) 507 { 508 return true; 509 } 510 else 511 return false; 512 } 513 Point get_zhongdian() 514 { 515 Double x=(this.p1.x+this.p2.x)/2; 516 Double y=(this.p1.y+this.p2.y)/2; 517 Point a=new Point(x,y); 518 return a; 519 } 520 boolean is_cz(Line p) //判断两条线是否垂直 521 { 522 // if((this.parallel_x()&&p.parallel_y())||(this.parallel_y()&&p.parallel_x())) 523 // return true; 524 // else if(this.getXielv()*p.getXielv()==-1) 525 // return true; 526 // else return false; 527 if(Math.abs((this.p2.x-this.p1.x)*(p.p2.x-p.p1.x)+(this.p2.y-this.p1.y)*(p.p2.y-p.p1.y))<0.01) 528 return true; 529 else return false; 530 } 531 boolean is_sameline(Line q) 532 { 533 // return q.isin_line(this.p1) && q.isin_line(this.p2); 534 return this.p1.straight_line(q.p1,q.p2)&&this.p2.straight_line(q.p1,q.p2); 535 // return this.p1.Same(q.p1) && this.p2.Same(q.p2); 536 } 537 boolean xiangjiao(Line p,Line q) 538 { 539 // 快速排斥实验 首先判断两条线段在 x 以及 y 坐标的投影是否有重合。 有一个为真,则代表两线段必不可交。 540 if (Math.max(p.p1.x,p.p2.x) < Math.min(q.p1.x ,q.p2.x) 541 || Math.max(p.p1.y,p.p2.y) < Math.min(q.p1.y,q.p2.y) 542 || Math.max(q.p1.x ,q.p2.x) < Math.min(p.p1.x,p.p2.x) 543 || Math.max(q.p1.y,q.p2.y) < Math.min(p.p1.y,p.p2.y)) 544 { 545 return false; 546 } 547 if ((((p.p1.x - q.p1.x) * (q.p2.y - q.p1.y) - (p.p1.y - q.p1.y ) * (q.p2.x - q.p1.x)) 548 * ((p.p2.x - q.p1.x) * (q.p2.y - q.p1.y) - (p.p2.y - q.p1.y) * (q.p2.x - q.p1.x))) > 0 549 || (((q.p1.x - p.p1.x) * (p.p2.y - p.p1.y) - (q.p1.y - p.p1.y) * (p.p2.x - p.p1.x)) 550 * ((q.p2.x - p.p1.x) * (p.p2.y - p.p1.y) - (q.p2.y - p.p1.y) * (p.p2.x - p.p1.x))) > 0) 551 { 552 return false; 553 } 554 return true; 555 } 556 boolean xiangjiao2(Line q) 557 { 558 Double a1=(this.p1.x-q.p1.x)*(this.p2.y-q.p1.y)-(this.p1.y-q.p1.y)*(this.p2.x-q.p1.x); 559 Double a2=(this.p1.x-q.p2.x)*(this.p2.y-q.p2.y)-(this.p1.y-q.p2.y)*(this.p2.x-q.p2.x); 560 return (a1 >= 0 && a2 <= 0) || (a1 <= 0 && a2 >= 0); 561 } 562 boolean Same(Line q) 563 { 564 return ((this.p1.Same(q.p1)&&this.p2.Same(q.p2))||this.p1.Same(q.p2)&&this.p2.Same(q.p1)); 565 } 566 } 567 class Triangle { 568 Line l1, l2, l3; 569 Point a;Point b;Point c; 570 571 Triangle(Line l1, Line l2, Line l3) { 572 this.l1 = l1; 573 this.l2 = l2; 574 this.l3 = l3; 575 } 576 Triangle(Point a,Point b,Point c) { 577 this.a=a;this.b=b;this.c=c; 578 Line ab = new Line(a, b); 579 Line bc = new Line(b, c); 580 Line ac = new Line(a, c); 581 this.l1 = ab; 582 this.l2 = bc; 583 this.l3 = ac; 584 } 585 586 Double get_zc() { 587 return (this.l1.length + this.l2.length + this.l3.length); 588 } 589 590 Double get_area() { 591 Double s = this.get_zc() / 2; 592 Double area=Math.sqrt((s * (s - this.l1.length) * (s - this.l2.length) * (s - this.l3.length))); 593 return area; 594 } 595 596 Double get_zx_x() { 597 Double x1 = this.l1.p1.x; 598 Double x2 = this.l1.p2.x; 599 Double x3 = this.l2.p2.x; 600 601 Double X1 = (x1 + x2 + x3) / 3; 602 return X1; 603 } 604 Double get_zx_y() 605 { 606 Double y1 = this.l1.p1.y; 607 Double y2 = this.l1.p2.y; 608 Double y3 = this.l2.p2.y; 609 Double Y1 = (y1 + y2 + y3) / 3; 610 return Y1; 611 } 612 boolean isinit(Line p,Line q) 613 { 614 if(p.init(Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).x,Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).y,q)) 615 return true; 616 else 617 return false; 618 } 619 Double getarea2(Point a,Point b,Point c) 620 { 621 Line p=new Line(a,b); 622 Line q=new Line(a,c); 623 Line r=new Line(b,c); 624 Triangle A=new Triangle(p,q,r); 625 return A.get_area(); 626 } 627 628 void output(Double s1,Double s2) 629 { 630 if(s1<=s2){ 631 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 632 System.exit(0); 633 } 634 else { 635 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 636 System.exit(0); 637 } 638 } 639 void qiege_Triangle(Point e,Point f,Point a,Point c,Point d) 640 { 641 Triangle A=new Triangle(a,c,d); 642 Line ac=new Line(a,c); 643 Line cd=new Line(c,d); 644 Line ad=new Line(a,d); 645 Line ef=new Line(e,f); 646 if(ef.is_sameline(ac)||ef.is_sameline(cd)||ef.is_sameline(ad)){ 647 System.out.print("The line is coincide with one of the lines"); 648 System.exit(0); 649 } 650 else if(a.straight_line(e,f)) 651 { 652 if(ef.xiangjiao2(cd)){ 653 Triangle C=new Triangle(a,c,ef.getjiaodian(cd)); 654 Triangle D=new Triangle(a,ef.getjiaodian(cd),d); 655 Double s1=C.get_area(); 656 Double s2= D.get_area(); 657 C.output(s1,s2); 658 } 659 else { 660 System.out.print("1"); 661 System.exit(0); 662 } 663 } 664 else if(c.straight_line(e,f)){ 665 if(ef.xiangjiao2(ad)) { 666 Triangle C = new Triangle(a, c, ef.getjiaodian(ad)); 667 Triangle D = new Triangle(c, ef.getjiaodian(ad), d); 668 Double s1 = C.get_area(); 669 Double s2 = D.get_area(); 670 C.output(s1, s2); 671 } 672 else { 673 System.out.print("1"); 674 System.exit(0); 675 } 676 } 677 else if(d.straight_line(e,f)){ 678 if(ef.xiangjiao2(ac)){ 679 Triangle C=new Triangle(a,ef.getjiaodian(ac),d); 680 Triangle D=new Triangle(ef.getjiaodian(ac),c,d); 681 Double s1=C.get_area(); 682 Double s2= D.get_area(); 683 C.output(s1,s2);} 684 else { 685 System.out.print("1"); 686 System.exit(0); 687 } 688 } 689 else if(ef.xiangjiao2(ac)&& ef.xiangjiao2(cd)){ 690 Triangle C=new Triangle(ef.getjiaodian(ac),c,ef.getjiaodian(cd)); 691 Double s1=C.get_area(); 692 Double s2=A.get_area()-s1; 693 C.output(s1,s2); 694 } 695 else if(ef.xiangjiao2(ac)&& ef.xiangjiao2(ad)){ 696 Triangle C=new Triangle(a,ef.getjiaodian(ac),ef.getjiaodian(ad)); 697 Double s1=C.get_area(); 698 Double s2=A.get_area()-s1; 699 C.output(s1,s2); 700 } 701 else if(ef.xiangjiao2(ad)&& ef.xiangjiao2(cd)){ 702 Triangle C=new Triangle(ef.getjiaodian(cd),d,ef.getjiaodian(ad)); 703 Double s1=C.get_area(); 704 Double s2=A.get_area()-s1; 705 C.output(s1,s2); 706 } 707 } 708 } 709 class Quadrangle { 710 Line ab; 711 Line bc; 712 Line cd; 713 Line ad; 714 Point a;Point b;Point c;Point d; 715 Quadrangle(Line ab,Line bc,Line cd,Line ad) 716 { 717 this.ab=ab; 718 this.bc=bc; 719 this.cd=cd; 720 this.ad=ad; 721 } 722 Quadrangle(Point a,Point b,Point c,Point d) 723 { 724 this.a=a;this.b=b;this.c=c;this.d=d; 725 Line ab = new Line(a, b); 726 Line bc = new Line(b, c); 727 Line cd = new Line(c, d); 728 Line ad = new Line(a, d); 729 this.ab=ab; 730 this.bc=bc; 731 this.cd=cd; 732 this.ad=ad; 733 } 734 boolean is_px_sibianxing() 735 { 736 return this.ab.parallel(cd)&&this.bc.parallel(ad); 737 } 738 boolean is_zf_sibianxing() 739 { 740 return this.is_px_sibianxing()&&(Objects.equals(this.ab.length, this.bc.length))&&this.ab.is_cz(bc); 741 } 742 boolean is_lx_sibianxing() 743 { 744 return this.is_px_sibianxing()&&(Objects.equals(this.ab.length, this.bc.length)); 745 } 746 boolean is_jx_sibianxing() 747 { 748 return this.is_px_sibianxing()&&this.ab.is_cz(bc); 749 } 750 void what_Quadrangle() 751 { 752 if(this.is_zf_sibianxing()){ 753 System.out.print("true true true"); 754 System.exit(0); 755 } 756 else if(this.is_lx_sibianxing()){ 757 System.out.print("true false false"); 758 System.exit(0); 759 } 760 else if(this.is_jx_sibianxing()){ 761 System.out.print("false true false"); 762 System.exit(0); 763 } 764 else{ 765 System.out.print("false false false"); 766 System.exit(0); 767 } 768 } 769 boolean tu_sibianxing(Point p1,Point p2,Point p3,Point p4) 770 { 771 Double t1 = (p4.x-p1.x)*(p2.y-p1.y)-(p4.y-p1.y)*(p2.x-p1.x); 772 Double t2 = (p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x); 773 Double t3 = (p2.x-p3.x)*(p4.y-p3.y)-(p2.y-p3.y)*(p4.x-p3.x); 774 Double t4 = (p3.x-p4.x)*(p1.y-p4.y)-(p3.y-p4.y)*(p1.x-p4.x); 775 return t1*t2*t3*t4>=0; 776 } 777 Double get_zc() 778 { 779 return this.ab.length+this.bc.length+ad.length+this.cd.length; 780 } 781 Double get_area(Line ac,Line bd) 782 { 783 Triangle a=new Triangle(this.ab,this.bc,ac); 784 Triangle b=new Triangle(this.cd,this.ad,ac); 785 Triangle c=new Triangle(this.ab,this.ad,bd); 786 Triangle d=new Triangle(this.bc,this.cd,bd); 787 if(this.tu_sibianxing(this.ab.p1,this.ab.p2,this.cd.p1,this.cd.p2)) 788 return c.get_area()+d.get_area(); 789 else { 790 if (Objects.equals(c.get_area(), d.get_area())) 791 return Math.abs(a.get_area() - b.get_area()); 792 else 793 return Math.abs(c.get_area() - d.get_area()); 794 } 795 } 796 void output(Double s1,Double s2) 797 { 798 if(s1<=s2){ 799 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 800 System.exit(0); 801 } 802 else { 803 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 804 System.exit(0); 805 } 806 } 807 Double get_area() 808 { 809 // s=1/2*[(x1*y2-x2*y1)+(x2*y3-x3*y2)+...... +(Xk*Yk+1-Xk+1*Yk)+...+(Xn*y1-x1*Yn) ] 810 double s=0; 811 s=(this.a.x*this.b.y-this.b.x*this.a.y)+(this.b.x*this.c.y-this.c.x*this.b.y)+(this.c.x*this.d.y-this.d.x*this.c.y)+(this.d.x*this.a.y-this.a.x*this.d.y); 812 return Math.abs(s/2); 813 } 814 boolean in_it(Point e,Point a,Point b,Point c,Point d) 815 { 816 Line ab=new Line(a,b); 817 Line bc=new Line(b,c); 818 Line cd=new Line(c,d); 819 Line ad=new Line(a,d); 820 Line ea=new Line(e,a); 821 Line eb=new Line(e,b); 822 Line ec=new Line(e,c); 823 Line ed=new Line(e,d); 824 Line ac=new Line(a,c); 825 Line bd=new Line(b,d); 826 Triangle A=new Triangle(ea,eb,ab); 827 Triangle B=new Triangle(eb,ec,bc); 828 Triangle C=new Triangle(ec,ed,cd); 829 Triangle D=new Triangle(ea,ed,ad); 830 if(Math.abs(A.get_area()+B.get_area()+C.get_area()+D.get_area()-this.get_area(ac,bd))<0.1) 831 return true; 832 else 833 return false; 834 } 835 void qiege_sibianxing(Point e,Point f, Point a, Point b, Point c, Point d) 836 { 837 Line ab = new Line(a, b); 838 Line bc = new Line(b, c); 839 Line cd = new Line(c, d); 840 Line ad = new Line(a, d); 841 Line ef=new Line(e,f); 842 Line ac=new Line(a,c); 843 Line bd=new Line(b,d); 844 Quadrangle A=new Quadrangle(a,b,c,d); 845 if(ef.is_sameline(ab)||ef.is_sameline(bc)||ef.is_sameline(cd)||ef.is_sameline(ad)){ 846 System.out.print("The line is coincide with one of the lines"); 847 System.exit(0); 848 } 849 else if(ef.is_sameline(ac)){ 850 Triangle C=new Triangle(ab,bc,ac); 851 Triangle D=new Triangle(cd,ad,ac); 852 Double s1=C.get_area(); 853 Double s2= D.get_area(); 854 C.output(s1,s2); 855 } 856 else if(ef.is_sameline(bd)){ 857 Triangle C=new Triangle(ab,ad,bd); 858 Triangle D=new Triangle(bc,cd,bd); 859 Double s1=C.get_area(); 860 Double s2= D.get_area(); 861 C.output(s1,s2); 862 } 863 else if(ef.xiangjiao2(ab)&& ef.xiangjiao2(cd)){ 864 Quadrangle C=new Quadrangle(a,ef.getjiaodian(ab),ef.getjiaodian(cd),d); 865 Quadrangle D=new Quadrangle(ef.getjiaodian(ab),b,c,ef.getjiaodian(cd)); 866 Double s1=C.get_area(); 867 Double s2= D.get_area(); 868 C.output(s1,s2); 869 } 870 else if(ef.xiangjiao2(bc)&& ef.xiangjiao2(ad)){ 871 Quadrangle C=new Quadrangle(a,b,ef.getjiaodian(bc),ef.getjiaodian(ad)); 872 Quadrangle D=new Quadrangle(ef.getjiaodian(bc),c,d,ef.getjiaodian(ad)); 873 Double s1=C.get_area(); 874 Double s2= D.get_area(); 875 C.output(s1,s2); 876 } 877 else if(ef.xiangjiao2(ab)&&ef.xiangjiao2(bc)){ 878 Triangle C=new Triangle(ef.getjiaodian(ab),b,ef.getjiaodian(bc)); 879 Double s1=C.get_area(); 880 Double s2=Math.abs(A.get_area()- C.get_area()); 881 C.output(s1,s2); 882 } 883 else if(ef.xiangjiao2(bc)&&ef.xiangjiao2(cd)){ 884 Triangle C=new Triangle(ef.getjiaodian(bc),c,ef.getjiaodian(cd)); 885 Double s1=C.get_area(); 886 Double s2=Math.abs(A.get_area()- C.get_area()); 887 C.output(s1,s2); 888 } 889 else if(ef.xiangjiao2(cd)&&ef.xiangjiao2(ad)){ 890 Triangle C=new Triangle(ef.getjiaodian(cd),d,ef.getjiaodian(ad)); 891 Double s1=C.get_area(); 892 Double s2=Math.abs(A.get_area()- C.get_area()); 893 C.output(s1,s2); 894 } 895 else if(ef.xiangjiao2(ab)&&ef.xiangjiao2(ad)){ 896 Triangle C=new Triangle(a,ef.getjiaodian(ab),ef.getjiaodian(ad)); 897 Double s1=C.get_area(); 898 Double s2=Math.abs(A.get_area()- C.get_area()); 899 C.output(s1,s2); 900 } 901 else if(ef.isin_line(a)&&ef.xiangjiao2(bc)){ 902 Triangle C=new Triangle(a,b,ef.getjiaodian(bc)); 903 Double s1=C.get_area(); 904 Double s2=Math.abs(A.get_area()- C.get_area()); 905 C.output(s1,s2); 906 } 907 else if(ef.isin_line(a)&&ef.xiangjiao2(cd)){ 908 Triangle C=new Triangle(a,ef.getjiaodian(cd),d); 909 Double s1=C.get_area(); 910 Double s2=Math.abs(A.get_area()- C.get_area()); 911 C.output(s1,s2); 912 } 913 else if(ef.isin_line(b)&&ef.xiangjiao2(cd)){ 914 Triangle C=new Triangle(b,c,ef.getjiaodian(cd)); 915 Double s1=C.get_area(); 916 Double s2=Math.abs(A.get_area()- C.get_area()); 917 C.output(s1,s2); 918 } 919 else if(ef.isin_line(b)&&ef.xiangjiao2(ad)){ 920 Triangle C=new Triangle(a,b,ef.getjiaodian(ad)); 921 Double s1=C.get_area(); 922 Double s2=Math.abs(A.get_area()- C.get_area()); 923 C.output(s1,s2); 924 } 925 else if(ef.isin_line(c)&&ef.xiangjiao2(ab)){ 926 Triangle C=new Triangle(ef.getjiaodian(ab),b,c); 927 Double s1=C.get_area(); 928 Double s2=Math.abs(A.get_area()- C.get_area()); 929 C.output(s1,s2); 930 } 931 else if(ef.isin_line(c)&&ef.xiangjiao2(ad)){ 932 Triangle C=new Triangle(c,d,ef.getjiaodian(ad)); 933 Double s1=C.get_area(); 934 Double s2=Math.abs(A.get_area()- C.get_area()); 935 C.output(s1,s2); 936 } 937 else if(ef.isin_line(d)&&ef.xiangjiao2(bc)){ 938 Triangle C=new Triangle(ef.getjiaodian(bc),c,d); 939 Double s1=C.get_area(); 940 Double s2=Math.abs(A.get_area()- C.get_area()); 941 C.output(s1,s2); 942 } 943 else if(ef.isin_line(d)&&ef.xiangjiao2(ab)){ 944 Triangle C=new Triangle(a,ef.getjiaodian(ab),d); 945 Double s1=C.get_area(); 946 Double s2=Math.abs(A.get_area()- C.get_area()); 947 C.output(s1,s2); 948 } 949 else 950 { 951 System.out.print("1"); 952 System.exit(0); 953 } 954 } 955 956 957 } 958 class Pentagon { 959 Line ab;Line bc;Line cd;Line de;Line ae; 960 Point a;Point b;Point c;Point d;Point e; 961 Pentagon(Point a,Point b,Point c,Point d,Point e) 962 { 963 this.a=a;this.b=b;this.c=c;this.d=d;this.e=e; 964 Line ab = new Line(a, b); 965 Line bc = new Line(b, c); 966 Line cd = new Line(c, d); 967 Line de = new Line(e, d); 968 Line ae = new Line(a,e); 969 this.ab=ab; 970 this.bc=bc; 971 this.cd=cd; 972 this.de=de; 973 this.ae=ae; 974 } 975 boolean tu_Pentagon(Point p1,Point p2,Point p3,Point p4,Point p5) 976 { 977 Double t1 = (p5.x-p1.x)*(p2.y-p1.y)-(p5.y-p1.y)*(p2.x-p1.x); 978 Double t2 = (p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x); 979 Double t3 = (p2.x-p3.x)*(p4.y-p3.y)-(p2.y-p3.y)*(p4.x-p3.x); 980 Double t4 = (p3.x-p4.x)*(p5.y-p4.y)-(p3.y-p4.y)*(p5.x-p4.x); 981 Double t5 = (p4.x-p5.x)*(p1.y-p5.y)-(p4.y-p5.y)*(p1.x-p5.x); 982 return t1*t2*t3*t4*t5<=0; 983 } 984 Double get_zc() 985 { 986 return this.ab.length+this.bc.length+cd.length+this.de.length+this.ae.length; 987 } 988 Double get_area() 989 { 990 // s=1/2*[(x1*y2-x2*y1)+(x2*y3-x3*y2)+...... +(Xk*Yk+1-Xk+1*Yk)+...+(Xn*y1-x1*Yn) ] 991 double s=0; 992 s=(this.a.x*this.b.y-this.b.x*this.a.y)+(this.b.x*this.c.y-this.c.x*this.b.y)+ 993 (this.c.x*this.d.y-this.d.x*this.c.y)+(this.d.x*this.e.y-this.e.x*this.d.y)+(this.e.x*this.a.y-this.a.x*this.e.y); 994 return Math.abs(s/2); 995 } 996 void output(Double s1,Double s2) 997 { 998 if(s1<=s2){ 999 System.out.print("2 "+Main.output(s1)+" "+Main.output(s2)); 1000 System.exit(0); 1001 } 1002 else { 1003 System.out.print("2 "+Main.output(s2)+" "+Main.output(s1)); 1004 System.exit(0); 1005 } 1006 } 1007 void qiege_pentagon(Point f,Point g,Point a,Point b,Point c,Point d,Point e) 1008 { 1009 Line ab = new Line(a, b); 1010 Line bc = new Line(b, c); 1011 Line cd = new Line(c, d); 1012 Line de = new Line(d, e); 1013 Line ae = new Line(a, e); 1014 Line fg = new Line(f, g); 1015 Line ac = new Line(a, c); 1016 Line ad = new Line(a, d); 1017 Line bd = new Line(b, d); 1018 Line be = new Line(b, e); 1019 Line ce = new Line(c, e); 1020 Pentagon A=new Pentagon(a,b,c,d,e); 1021 if(fg.is_sameline(ab)||fg.is_sameline(bc)||fg.is_sameline(cd)||fg.is_sameline(de)||fg.is_sameline(ae)){ 1022 System.out.print("The line is coincide with one of the lines"); 1023 System.exit(0); 1024 } 1025 else if(fg.is_sameline(ac)){ 1026 Triangle C=new Triangle(a,b,c); 1027 Double s1=C.get_area(); 1028 Double s2=A.get_area()-s1; 1029 C.output(s1,s2); 1030 } 1031 else if(fg.is_sameline(ad)){ 1032 Triangle C=new Triangle(a,d,e); 1033 Double s1=C.get_area(); 1034 Double s2=A.get_area()-s1; 1035 C.output(s1,s2); 1036 } 1037 else if(fg.is_sameline(bd)){ 1038 Triangle C=new Triangle(b,c,d); 1039 Double s1=C.get_area(); 1040 Double s2=A.get_area()-s1; 1041 C.output(s1,s2); 1042 } 1043 else if(fg.is_sameline(be)){ 1044 Triangle C=new Triangle(a,b,e); 1045 Double s1=C.get_area(); 1046 Double s2=A.get_area()-s1; 1047 C.output(s1,s2); 1048 } 1049 else if(fg.is_sameline(ce)){ 1050 Triangle C=new Triangle(c,d,e); 1051 Double s1=C.get_area(); 1052 Double s2=A.get_area()-s1; 1053 C.output(s1,s2); 1054 } 1055 else if(fg.xiangjiao2(ab)&&fg.xiangjiao2(bc)){ 1056 Triangle C=new Triangle(fg.getjiaodian(ab),b,fg.getjiaodian(bc)); 1057 Double s1=C.get_area(); 1058 Double s2=A.get_area()-s1; 1059 C.output(s1,s2); 1060 } 1061 else if(fg.xiangjiao2(bc)&&fg.xiangjiao2(cd)){ 1062 Triangle C=new Triangle(fg.getjiaodian(bc),c,fg.getjiaodian(cd)); 1063 Double s1=C.get_area(); 1064 Double s2=A.get_area()-s1; 1065 C.output(s1,s2); 1066 } 1067 else if(fg.xiangjiao2(cd)&&fg.xiangjiao2(de)){ 1068 Triangle C=new Triangle(fg.getjiaodian(cd),d,fg.getjiaodian(de)); 1069 Double s1=C.get_area(); 1070 Double s2=A.get_area()-s1; 1071 C.output(s1,s2); 1072 } 1073 else if(fg.xiangjiao2(de)&&fg.xiangjiao2(ae)){ 1074 Triangle C=new Triangle(fg.getjiaodian(de),e,fg.getjiaodian(ae)); 1075 Double s1=C.get_area(); 1076 Double s2=A.get_area()-s1; 1077 C.output(s1,s2); 1078 } 1079 else if(fg.xiangjiao2(ae)&&fg.xiangjiao2(ab)){ 1080 Triangle C=new Triangle(fg.getjiaodian(ae),a,fg.getjiaodian(ab)); 1081 Double s1=C.get_area(); 1082 Double s2=A.get_area()-s1; 1083 C.output(s1,s2); 1084 } 1085 else if(fg.xiangjiao2(ab)&&fg.xiangjiao2(cd)){ 1086 Quadrangle C=new Quadrangle(fg.getjiaodian(ab),b,c,fg.getjiaodian(cd)); 1087 Double s1=C.get_area(); 1088 Double s2=A.get_area()-s1; 1089 C.output(s1,s2); 1090 } 1091 else if(fg.xiangjiao2(ab)&&fg.xiangjiao2(de)){ 1092 Quadrangle C=new Quadrangle(a,fg.getjiaodian(ab),fg.getjiaodian(de),e); 1093 Double s1=C.get_area(); 1094 Double s2=A.get_area()-s1; 1095 C.output(s1,s2); 1096 } 1097 else if(fg.xiangjiao2(bc)&&fg.xiangjiao2(de)){ 1098 Quadrangle C=new Quadrangle(fg.getjiaodian(bc),c,d,fg.getjiaodian(de)); 1099 Double s1=C.get_area(); 1100 Double s2=A.get_area()-s1; 1101 C.output(s1,s2); 1102 } 1103 else if(fg.xiangjiao2(bc)&&fg.xiangjiao2(ae)){ 1104 Quadrangle C=new Quadrangle(b,fg.getjiaodian(bc),fg.getjiaodian(ae),a); 1105 Double s1=C.get_area(); 1106 Double s2=A.get_area()-s1; 1107 C.output(s1,s2); 1108 } 1109 else if(fg.xiangjiao2(cd)&&fg.xiangjiao2(ae)){ 1110 Quadrangle C=new Quadrangle(fg.getjiaodian(cd),d,e,fg.getjiaodian(ae)); 1111 Double s1=C.get_area(); 1112 Double s2=A.get_area()-s1; 1113 C.output(s1,s2); 1114 } 1115 else 1116 { 1117 System.out.print("1"); 1118 System.exit(0); 1119 } 1120 } 1121 boolean in_it(Point f,Point a,Point b,Point c,Point d,Point e) 1122 { 1123 Line ab=new Line(a,b); 1124 Line bc=new Line(b,c); 1125 Line cd=new Line(c,d); 1126 Line de=new Line(d,e); 1127 Line ae=new Line(a,e); 1128 Line fa=new Line(e,a); 1129 Line fb=new Line(e,b); 1130 Line fc=new Line(e,c); 1131 Line fd=new Line(e,d); 1132 Line fe=new Line(f,e); 1133 Triangle A=new Triangle(fa,fb,ab); 1134 Triangle B=new Triangle(fb,fc,bc); 1135 Triangle C=new Triangle(fc,fd,cd); 1136 Triangle D=new Triangle(fd,fe,de); 1137 Triangle E=new Triangle(fe,fa,ae); 1138 if(Math.abs(A.getarea2(a,b,f)+B.getarea2(b,c,f)+C.getarea2(c,d,f)+D.getarea2(d,e,f)+E.getarea2(a,e,f)-this.get_area())<0.001) 1139 return true; 1140 else 1141 return false; 1142 } 1143 boolean on_it(Point f,Point a,Point b,Point c,Point d,Point e) 1144 { 1145 return f.on_line(a, b) || f.on_line(b, c) || f.on_line(c, d) || f.on_line(d, e) || f.on_line(a, e); 1146 } 1147 boolean out_it(Point f,Point a,Point b,Point c,Point d,Point e) 1148 { 1149 Pentagon A=new Pentagon(a,b,c,d,e); 1150 return !A.in_it(f, a, b, c, d, e) && !A.on_it(f, a, b, c, d, e); 1151 } 1152 boolean not_init(Pentagon B) 1153 { 1154 return (this.on_it(this.a,B.a,B.b,B.c,B.d,B.e)||this.out_it(this.a,B.a,B.b,B.c,B.d,B.e))&&(this.on_it(this.a,B.a,B.b,B.c,B.d,B.e)||this.out_it(this.b,B.a,B.b,B.c,B.d,B.e))&& 1155 (this.on_it(this.a,B.a,B.b,B.c,B.d,B.e)||this.out_it(this.c,B.a,B.b,B.c,B.d,B.e))&& 1156 (this.on_it(this.a,B.a,B.b,B.c,B.d,B.e)||this.out_it(this.d,B.a,B.b,B.c,B.d,B.e))&&(this.on_it(this.a,B.a,B.b,B.c,B.d,B.e)||this.out_it(this.e,B.a,B.b,B.c,B.d,B.e)); 1157 } 1158 boolean same_line(Line p) 1159 { 1160 if(p.Same(this.ab)||p.Same(this.bc)||p.Same(this.cd)||p.Same(this.de)||p.Same(this.ae)) 1161 return true; 1162 else 1163 return false; 1164 } 1165 boolean same_point(Point a) 1166 { 1167 if(a.Same(this.a)||a.Same(this.b)||a.Same(this.c)||a.Same(this.d)||a.Same(this.e)) 1168 return true; 1169 else 1170 return false; 1171 } 1172 boolean jiaocha(Line p) 1173 { 1174 if(p.xiangjiao(p,this.ab)||p.xiangjiao(p,this.bc)||p.xiangjiao(p,this.cd)||p.xiangjiao(p,this.de)||p.xiangjiao(p,this.ae)) 1175 return true; 1176 else 1177 return false; 1178 } 1179 boolean fenli(Pentagon B) 1180 { 1181 if(this.out_it(this.a,B.a,B.b,B.c,B.d,B.e)&&this.out_it(this.b,B.a,B.b,B.c,B.d,B.e)&&this.out_it(this.c,B.a,B.b,B.c,B.d,B.e)&& 1182 this.out_it(this.d,B.a,B.b,B.c,B.d,B.e)&&this.out_it(this.e,B.a,B.b,B.c,B.d,B.e)) 1183 return true; 1184 else 1185 return false; 1186 } 1187 boolean coincides(Pentagon B) 1188 { 1189 if(this.same_line(B.ab)&&this.same_line(B.bc)&&this.same_line(B.cd)&&this.same_line(B.de)&&this.same_line(B.ae)) 1190 return true; 1191 else 1192 return false; 1193 } 1194 boolean connect(Pentagon B) 1195 { 1196 if((this.same_point(B.a)||this.same_point(B.b)||this.same_point(B.c)||this.same_point(B.d)||this.same_point(B.e)|| 1197 this.same_line(B.ab)||this.same_line(B.bc)||this.same_line(B.cd)||this.same_line(B.de)||this.same_line(B.ae))&&this.not_init(B)) 1198 return true; 1199 else 1200 return false; 1201 } 1202 boolean inside(Pentagon B) 1203 { 1204 if(!this.out_it(this.a,B.a,B.b,B.c,B.d,B.e)&&!this.out_it(this.b,B.a,B.b,B.c,B.d,B.e)&&!this.out_it(this.c,B.a,B.b,B.c,B.d,B.e)&& 1205 !this.out_it(this.d,B.a,B.b,B.c,B.d,B.e)&&!this.out_it(this.e,B.a,B.b,B.c,B.d,B.e)) 1206 return true; 1207 else 1208 return false; 1209 } 1210 boolean interlaced(Pentagon B) 1211 { 1212 if((this.jiaocha(B.ab)||this.jiaocha(B.bc)||this.jiaocha(B.cd)||this.jiaocha(B.de)||this.jiaocha(B.ae))&& 1213 !(this.same_point(B.a)||this.same_point(B.b)||this.same_point(B.c)||this.same_point(B.d)||this.same_point(B.e))) 1214 return true; 1215 if((this.same_point(B.a)&&(this.same_point(B.c)||this.same_point(B.d)))||(this.same_point(B.b)&&(this.same_point(B.d)||this.same_point(B.e)))||(this.same_point(B.c)&&this.same_point(B.e))) 1216 return true; 1217 else 1218 return false; 1219 } 1220 // boolean contains(Pentagon B) 1221 // { 1222 // if(!B.out_it(B.a,this.a,this.b,this.c,this.d,this.e)&&!B.out_it(B.b,this.a,this.b,this.c,this.d,this.e)&&!this.out_it(this.c,B.a,B.b,B.c,B.d,B.e)&& 1223 // !this.out_it(this.d,B.a,B.b,B.c,B.d,B.e)&&!this.out_it(this.e,B.a,B.b,B.c,B.d,B.e)) 1224 // return true; 1225 // else 1226 // return false; 1227 // } 1228 void what_relate(Pentagon A,Pentagon B) 1229 { 1230 String[] s={"pentagon", "quadrilateral", "triangle"}; 1231 if(A.fenli(B)) 1232 { 1233 System.out.print("no overlapping area between the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" and the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1234 } 1235 else if(A.coincides(B)) 1236 { 1237 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" coincides with the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1238 } 1239 else if(A.inside(B)) 1240 { 1241 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" is inside the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1242 } 1243 else if(B.inside(A)) 1244 { 1245 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" contains the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1246 } 1247 else if(A.interlaced(B)) 1248 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" is interlaced with the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1249 else if(A.connect(B)) 1250 { 1251 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" is connected to the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1252 } 1253 else 1254 System.out.print("the previous "+s[A.a.what_polygon(A.a,A.b,A.c,A.d,A.e)-1]+" is interlaced with the following "+s[A.a.what_polygon(B.a,B.b,B.c,B.d,B.e)-1]); 1255 1256 } 1257 }第二题代码
<3>期中考试
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)
方法。设计类图如下图所示。
** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**
大概思路:
通过老师所给出的类图将点类和线类构造出来,并将相应的属性和方法写完整,最后输出相应的结果。
点类:
线类:
Main类:
1 import java.math.BigDecimal; 2 import java.math.RoundingMode; 3 import java.util.Scanner; 4 5 public class Main { 6 public static Double output(Double data) 7 { 8 double a = data; 9 10 BigDecimal bigDecimal = new BigDecimal(a); 11 double res = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue(); 12 return res; 13 } 14 public static void main(String[] args) { 15 Scanner in = new Scanner(System.in); 16 Double x1=in.nextDouble(); 17 Double y1=in.nextDouble(); 18 Double x2=in.nextDouble(); 19 Double y2=in.nextDouble(); 20 if((x1<=0||x1>200)||(y1<=0||y1>200)||(x2<=0||x2>200)||(y2<=0||y2>200)) 21 { 22 System.out.print("Wrong Format"); 23 return; 24 } 25 Point p1=new Point(x1,y1); 26 Point p2=new Point(x2,y2); 27 Line l=new Line(p1,p2,in.next()); 28 l.display(); 29 } 30 } 31 class Point { 32 double x,y,distance; 33 Point(String x,String y) 34 { 35 this.x=Double.valueOf(x); 36 this.y=Double.valueOf(y); 37 } 38 Point(Double x,Double y) 39 { 40 this.x=x; 41 this.y=y; 42 } 43 Double Distance(Point b)//计算两点间的距离 44 { 45 distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y)); 46 return distance; 47 } 48 Double getX() 49 { 50 return this.x; 51 } 52 void setX(Double X) 53 { 54 this.x=X; 55 } 56 Double getY() 57 { 58 return this.y; 59 } 60 void setY(Double Y) 61 { 62 this.y=Y; 63 } 64 void display() 65 { 66 System.out.print("("+String.format("%.2f",this.getX())+","+String.format("%.2f",this.getY())+")"); 67 } 68 69 70 } 71 72 class Line { 73 Point p1; 74 Point p2; 75 String color; 76 public Line(Point a,Point b,String color) 77 { 78 this.p1=a; 79 this.p2=b; 80 this.color=color; 81 } 82 83 Double getDistance()//计算两点间的距离 84 { 85 Double distance=Math.sqrt((this.p1.x-this.p2.x)*(this.p1.x-this.p2.x)+(this.p1.y-this.p2.y)*(this.p1.y-this.p2.y)); 86 return distance; 87 } 88 void display() 89 { 90 System.out.println("The line's color is:"+this.color); 91 System.out.println("The line's begin point's Coordinate is:"); 92 this.p1.display(); 93 System.out.println(); 94 System.out.println("The line's end point's Coordinate is:"); 95 this.p2.display(); 96 System.out.println(); 97 System.out.println("The line's length is:"+Main.output(this.getDistance())); 98 } 99 100 101 102 }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();
其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)
方法。
大概思路:
在第一题的基础上再定义一个抽象的父类,根据题目给出的类图将类写好。
1 import java.math.BigDecimal; 2 import java.math.RoundingMode; 3 import java.util.Scanner; 4 public class Main 5 { 6 public static Double output(Double data) 7 { 8 double a = data; 9 10 BigDecimal bigDecimal = new BigDecimal(a); 11 double res = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue(); 12 return res; 13 } 14 public static void main(String[] args) 15 { 16 Scanner sc=new Scanner(System.in); 17 double x1,x2,y1,y2; 18 int num; 19 int index; 20 String color; 21 x1=sc.nextDouble(); 22 y1=sc.nextDouble(); 23 x2=sc.nextDouble(); 24 y2=sc.nextDouble(); 25 if((x1<=0||x1>200)||(y1<=0||y1>200)||(x2<=0||x2>200)||(y2<=0||y2>200)) 26 { 27 System.out.print("Wrong Format"); 28 return; 29 } 30 Point point1=new Point(x1,y1); 31 Point point2=new Point(x2,y2); 32 color=sc.next(); 33 Line line1=new Line(point1,point2,color); 34 Plane plane=new Plane(color); 35 Element element; 36 element = point1; 37 element.display(); 38 39 element = point2; 40 element.display(); 41 42 element = line1; 43 element.display(); 44 45 element = plane; 46 element.display(); 47 } 48 } 49 abstract class Element 50 { 51 public abstract void display(); 52 } 53 class Line extends Element 54 { 55 private Point p1,p2; 56 private String color; 57 58 public Line() { 59 } 60 61 public Line(Point point1, Point point2, String color) { 62 this.p1 = point1; 63 this.p2 = point2; 64 this.color = color; 65 } 66 67 public Point getPoint1() { 68 return p1; 69 } 70 71 public void setPoint1(Point point1) { 72 this.p1 = point1; 73 } 74 75 public Point getPoint2() { 76 return p2; 77 } 78 79 public void setPoint2(Point point2) { 80 this.p2 = point2; 81 } 82 83 public String getColor() { 84 return color; 85 } 86 87 public void setColor(String color) { 88 this.color = color; 89 } 90 91 Double getDistance()//计算两点间的距离 92 { 93 Double distance=Math.sqrt((this.p1.getX() - this.p2.getX())*(this.p1.getX() - this.p2.getX())+(this.p1.getY() - this.p2.getY())*(this.p1.getY() - this.p2.getY())); 94 return distance; 95 } 96 @Override 97 public void display() 98 { 99 System.out.println("The line's color is:"+color); 100 System.out.println("The line's begin point's Coordinate is:"); 101 p1.display(); 102 System.out.println("The line's end point's Coordinate is:"); 103 p2.display(); 104 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 105 } 106 } 107 class Plane extends Element 108 { 109 private String color; 110 111 public Plane() { 112 } 113 public Plane(String color) { 114 this.color = color; 115 } 116 117 public String getColor() { 118 return color; 119 } 120 121 public void setColor(String color) { 122 this.color = color; 123 } 124 125 @Override 126 public void display() 127 { 128 System.out.println("The Plane's color is:"+color); 129 } 130 } 131 class Point extends Element 132 { 133 private double x,y; 134 public Point() 135 { 136 } 137 public Point(double x, double y) 138 { 139 this.x = x; 140 this.y = y; 141 } 142 143 public double getX() { 144 return x; 145 } 146 147 public void setX(double x) { 148 this.x = x; 149 } 150 151 public double getY() { 152 return y; 153 } 154 155 public void setY(double y) { 156 this.y = y; 157 } 158 public void display() 159 { 160 System.out.println("("+String.format("%.2f",this.x)+","+String.format("%.2f",this.y)+")"); 161 } 162 }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()
方法进行输出。
类图如下所示:
大概思路:
在原有类设计的基础上,增加一个GeometryObject容器类,将其属性补全,根据题目要求用arraylist逐步实现所需要的内容。
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 public class Main 4 { 5 public static void main(String[] args) 6 { 7 Scanner in=new Scanner(System.in); 8 double x1,x2,y1,y2; 9 int n; 10 int index; 11 String color; 12 GeometryObject g=new GeometryObject(); 13 n=in.nextInt(); 14 while(n!=0) 15 { 16 switch(n) 17 { 18 case 1:{ 19 x1=in.nextDouble(); 20 y1=in.nextDouble(); 21 point p = new point(x1,y1); 22 g.add(p); 23 } 24 break; 25 case 2:{ 26 x1=in.nextDouble(); 27 y1=in.nextDouble(); 28 point p1 = new point(x1,y1); 29 x2=in.nextDouble(); 30 y2=in.nextDouble(); 31 point p2 = new point(x2,y2); 32 color = in.next(); 33 line l = new line(p1,p2,color); 34 g.add(l); 35 36 } 37 break; 38 case 3: 39 { 40 color = in.next(); 41 Plane p = new Plane(color); 42 g.add(p); 43 } 44 break; 45 case 4: 46 { 47 index = in.nextInt(); 48 if(g.getList().size()>=index) 49 g.remove(index-1); 50 } 51 break; 52 default:System.out.print("Wrong Format"); 53 break; 54 } 55 n= in.nextInt(); 56 } 57 ArrayList<Element> s = g.getList(); 58 for(int i=0;i<s.size();i++) 59 { 60 s.get(i).display(); 61 } 62 } 63 } 64 abstract class Element 65 { 66 public abstract void display(); 67 } 68 class GeometryObject 69 { 70 ArrayList<Element> list=new ArrayList<>(); 71 72 public GeometryObject() 73 { 74 } 75 76 public GeometryObject(ArrayList<Element> list) { 77 this.list = list; 78 } 79 80 public ArrayList<Element> getList() { 81 return list; 82 } 83 84 public void setList(ArrayList<Element> list) { 85 this.list = list; 86 } 87 public void add(Element e) 88 { 89 list.add(e); 90 } 91 public void remove(int n) 92 { 93 list.remove(n); 94 } 95 } 96 class point extends Element 97 { 98 private double x,y; 99 public point() 100 { 101 } 102 public point(double x, double y) 103 { 104 this.x = x; 105 this.y = y; 106 } 107 108 public double getX() { 109 return x; 110 } 111 112 public void setX(double x) { 113 this.x = x; 114 } 115 116 public double getY() { 117 return y; 118 } 119 120 public void setY(double y) { 121 this.y = y; 122 } 123 @Override 124 public void display() 125 { 126 System.out.println("("+String.format("%.2f",this.x)+","+String.format("%.2f",this.y)+")"); 127 } 128 } 129 class line extends Element 130 { 131 private point p1,p2; 132 private String color; 133 134 public line() 135 { 136 } 137 138 public line(point point1, point point2, String color) 139 { 140 this.p1 = point1; 141 this.p2 = point2; 142 this.color = color; 143 } 144 145 public point getPoint1() { 146 return p1; 147 } 148 149 public void setPoint1(point point1) { 150 this.p1 = point1; 151 } 152 153 public point getPoint2() { 154 return p2; 155 } 156 157 public void setPoint2(point point2) { 158 this.p2 = point2; 159 } 160 161 public String getColor() { 162 return color; 163 } 164 165 public void setColor(String color) { 166 this.color = color; 167 } 168 Double getDistance()//计算两点间的距离 169 { 170 Double distance=Math.sqrt((this.p1.getX() - this.p2.getX())*(this.p1.getX() - this.p2.getX())+(this.p1.getY() - this.p2.getY())*(this.p1.getY() - this.p2.getY())); 171 return distance; 172 } 173 @Override 174 public void display() 175 { 176 System.out.println("The line's color is:"+color); 177 System.out.println("The line's begin point's Coordinate is:"); 178 this.p1.display(); 179 System.out.println("The line's end point's Coordinate is:"); 180 this.p2.display(); 181 System.out.println("The line's length is:"+String.format("%.2f",this.getDistance())); 182 } 183 } 184 class Plane extends Element 185 { 186 private String color; 187 188 public Plane() { 189 } 190 public Plane(String color) { 191 this.color = color; 192 } 193 194 public String getColor() { 195 return color; 196 } 197 198 public void setColor(String color) { 199 this.color = color; 200 } 201 202 @Override 203 public void display() 204 { 205 System.out.println("The Plane's color is:"+color); 206 } 207 }View Code
三、踩坑心得
1.重构代码越早越好,后面写起来会更轻松,像我写第四次大作业的时候没有重构也勉强写完了,但到了第五次多边形的时候真的就写起来太复杂了。
2.在切割多边形的时候先把特殊情况考虑到,不然很容易就漏了个别的测试点。
像这个测试点就是输入的四个点中第一个和第三个点重合的情况,我开始是真的想不到啊。
3.在判断两个多边形的位置关系的时候,我先把其他的关系判断完之后将剩余的情况全部列入到交错的关系里面,这样做一些测试点会出现错误,因为一些交错的情况在前面被判断为了相邻的情况,所以我们在开始判断的时候就要把交错的特殊情况先判断出来。
4.抽象类可以有构造方法,接口中不能有构造方法。抽象类中可以有普通成员变量,接口中没有普通成员变量。抽象类中可以包含非抽象的普通方法,接口中的所有方法必须都是抽象的,不能有非抽象的普通方法。抽象类中的抽象方法的访问类型可以是public,protected和(默认类型,虽然eclipse下不报错,但应该也不行),但接口中的抽象方法只能是public类型的,并且默认即为public abstract类型。抽象类中可以包含静态方法,接口中不能包含静态方法。
5.抽象类和接口中都可以包含静态成员变量,抽象类中的静态成员变量的访问类型可以任意,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型。一个类可以实现多个接口,但只能继承一个抽象类。
四、改进建议
1.越早重构代码越好,有一个多边形的父类,对后面的操作有很大的帮助,并且在原代码的基础上可以更轻松的添加其他东西。
2.减少代码的嵌套,将具体功能和判断写成详细的方法,包装起来,不要一个函数写一长串.
3.尽量将子类里相同的方法集合起来写在父类里面。
五、总结
对于本阶段的三次题目集,我学会了用正则表达式来检查字符串是否正确,也学会了对代码进行调试,对类进行封装和简单的类的继承。关于继承(接口)学习的还有很多,抽象类可以有构造方法,接口中不能有构造方法。抽象类中可以有普通成员变量,接口中没有普通成员变量。抽象类中可以包含非抽象的普通方法,接口中的所有方法必须都是抽象的,不能有非抽象的普通方法。抽象类中的抽象方法的访问类型可以是public,protected和(默认类型,虽然eclipse下不报错,但应该也不行),但接口中的抽象方法只能是public类型的,并且默认即为public abstract类型。抽象类中可以包含静态方法,接口中不能包含静态方法。抽象类和接口中都可以包含静态成员变量,抽象类中的静态成员变量的访问类型可以任意,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型。一个类可以实现多个接口,但只能继承一个抽象类。
标签:p2,p1,return,Point,Double,BLOG,Line From: https://www.cnblogs.com/songwdnxjl/p/16832854.html