1.前言:
在这一阶段Java的学习过程中,我们学习了继承与多态,抽象类与接口,异常处理方式以及圈复杂度的判定还有其他的常用方法等,进行完成了更进一步的相关大作业(点线形系列在原有的基础上进一步的提高和突破),我认为这一阶段这些作业均是具有一定的难度的,但我认为这样的几次作业很好的将编程与数学结合起来(果然计算机的尽头是数学。。。( ̄ー ̄)),在解决这些问题的同时,合理的运用数学思维同时选择合适的数学方法来寻找解决方法,从而再需要通过合理的编程结构来设计实现,往往会起到事半功倍的效果。 考查难度由易到难,层层递进,前面的题目即为后面的代码做了很大的设计铺垫。对于期中考试,层层递进,很好的考查了半个学期所学知识。在此阶段的学习过程中,我也意识到了我的很多问题,紧密完成我的作业同时,也要学会花时间停下来去思考代码结构问题(不要一味盲目莽撞地去写),花更多时间去完成作业和掌握知识点,要细心一点去思考。
2.设计与分析:
7-2 点线形系列4-凸四边形的计算
1 import java.text.DecimalFormat; 2 import java.util.Arrays; 3 import java.util.Scanner; 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner sc = new Scanner(System.in); 7 int count = 0, cnt = 0; 8 String point = sc.nextLine(); 9 String data[] = null; 10 if (point.charAt(0) >= '1' && point.charAt(0) <= '5' && point.charAt(1) == ':') { 11 for (int i = 2; i < point.length(); i++) { 12 if(point.charAt(i) == ' '){ 13 cnt++; 14 } 15 if (point.charAt(i) == ',') { 16 count++; 17 } 18 } 19 if(point.charAt(point.length()-1)==' '){ 20 cnt--; 21 } 22 if (count - 1 != cnt) { 23 System.out.print("Wrong Format"); 24 return; 25 } 26 data = point.split(":|,| "); 27 for (int i = 1; i < data.length; i++) { 28 if (!data[i].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) { 29 System.out.print("Wrong Format"); 30 return; 31 } 32 } 33 } else { 34 System.out.print("Wrong Format"); 35 return; 36 } 37 switch (data[0]) { 38 case "1": 39 ISquadrilateral_parallelogram(data); 40 break; 41 case "2": 42 Quadrilateraltype(data); 43 break; 44 case "3": 45 ISConcaveQuadrilateral(data); 46 break; 47 case "4": 48 Piontnumber(data); 49 break; 50 default: 51 Isoutside(data); 52 break; 53 } 54 } 55 56 public static boolean ISQuadrilateral(Point a, Point b, Point c, Point d) {//是否为四边形 57 Line l1=new Line(a,b); 58 Line l2=new Line(b,c); 59 Line l3=new Line(d,c); 60 Line l4=new Line(a,d); 61 if((l1.getSlope()==l2.getSlope())||(l3.getSlope()==l4.getSlope())||(l1.getSlope()==l4.getSlope())||(l2.getSlope()==l3.getSlope())){ 62 return false; 63 } 64 if(!l1.isParallel(l3)){ 65 if(l1.Intersection(l3)){ 66 return false; 67 } 68 } 69 if(!l2.isParallel(l4)){ 70 if(l2.Intersection(l4)){ 71 return false; 72 } 73 } 74 return true; 75 } 76 77 public static Point[] IStriangle(Point a, Point b, Point c, Point d){ 78 Line l1=new Line(a,b); 79 Line l2=new Line(b,c); 80 Line l3=new Line(c,d); 81 Line l4=new Line(a,d); 82 Line l5=new Line(a,c); 83 Line l6=new Line(b,d); 84 Point[] num =new Point[3]; 85 Point p=new Point(65536.0,65536.0); 86 num[0]=p; 87 if(b.pointsCoincideError(a)||c.pointsCoincideError(a)||d.pointsCoincideError(a)){ 88 trianglepro x=new trianglepro(l2,l6,l3); 89 if(x.IStriangle()){ 90 num[0]=b; 91 num[1]=c; 92 num[2]=d; 93 return num; 94 } 95 } 96 if(a.pointsCoincideError(b)||c.pointsCoincideError(b)||d.pointsCoincideError(b)){ 97 trianglepro x=new trianglepro(l4,l5,l3); 98 if(x.IStriangle()){ 99 num[0]=a; 100 num[1]=c; 101 num[2]=d; 102 return num; 103 } 104 }if(a.pointsCoincideError(c)||b.pointsCoincideError(c)||d.pointsCoincideError(c)){ 105 trianglepro x=new trianglepro(l1,l6,l4); 106 if(x.IStriangle()){ 107 num[0]=a; 108 num[1]=b; 109 num[2]=d; 110 return num; 111 } 112 }if(a.pointsCoincideError(d)||b.pointsCoincideError(d)||c.pointsCoincideError(d)){ 113 trianglepro x=new trianglepro(l1,l5,l2); 114 if(x.IStriangle()){ 115 num[0]=b; 116 num[1]=c; 117 num[2]=d; 118 return num; 119 } 120 } 121 if(l1.isParallel(l4)&&l6.isOnline(a)){ 122 trianglepro x=new trianglepro(l2,l6,l3); 123 if(x.IStriangle()){ 124 num[0]=b; 125 num[1]=c; 126 num[2]=d; 127 return num; 128 } 129 } 130 if(l1.isParallel(l2)&&l5.isOnline(b)){ 131 trianglepro x=new trianglepro(l3,l5,l4); 132 if(x.IStriangle()){ 133 num[0]=a; 134 num[1]=c; 135 num[2]=d; 136 return num; 137 } 138 } 139 if(l2.isParallel(l3)&&l6.isOnline(c)){ 140 trianglepro x=new trianglepro(l1,l6,l4); 141 if(x.IStriangle()){ 142 num[0]=a; 143 num[1]=b; 144 num[2]=d; 145 return num; 146 } 147 } 148 if(l3.isParallel(l4)&&l5.isOnline(d)){ 149 trianglepro x=new trianglepro(l1,l5,l2); 150 if(x.IStriangle()){ 151 num[0]=a; 152 num[1]=b; 153 num[2]=c; 154 return num; 155 } 156 } 157 return num; 158 } 159 160 161 public static boolean pointsCoincide(Point a, Point b, Point c, Point d){ 162 if(a.pointsCoincideError(b)||b.pointsCoincideError(c)||c.pointsCoincideError(d)||b.pointsCoincideError(d)||a.pointsCoincideError(d)||a.pointsCoincideError(c)){ 163 return true; 164 } 165 else{ 166 return false; 167 } 168 } 169 170 public static void ISquadrilateral_parallelogram(String[] arr) { 171 if (arr.length!= 9) { 172 System.out.print("wrong number of points"); 173 return; 174 } 175 int k = 0; 176 Point[] num = new Point[4]; 177 for (int i = 1; i <arr.length; i =i+2){ 178 Point p=new Point(Double.valueOf(arr[i]),Double.valueOf(arr[i+1])); 179 num[k]=p; 180 k++; 181 } 182 if(pointsCoincide(num[0],num[1],num[2],num[3])){ 183 System.out.print("points coincide"); 184 return; 185 } 186 if (ISQuadrilateral(num[0],num[1], num[2], num[3])) { 187 System.out.print("true"); 188 } else { 189 System.out.print("false false"); 190 return; 191 } 192 if (ISparallelogram(num[0],num[1],num[2],num[3])){ 193 System.out.print(" true"); 194 } else { 195 System.out.print(" false"); 196 } 197 return; 198 } 199 200 public static void Quadrilateraltype(String[] arr){ 201 if (arr.length!= 9) { 202 System.out.print("wrong number of points"); 203 return; 204 } 205 Point[] num = new Point[4]; 206 int k=0; 207 for (int i = 1; i <arr.length; i =i+2){ 208 Point p=new Point(Double.valueOf(arr[i]),Double.valueOf(arr[i+1])); 209 num[k]=p; 210 k++; 211 } 212 if(!ISQuadrilateral(num[0],num[1], num[2], num[3])) { 213 System.out.print("not a quadrilateral"); 214 return; 215 } 216 if(pointsCoincide(num[0],num[1],num[2],num[3])){ 217 System.out.print("points coincide"); 218 return; 219 } 220 if(ISdiamond(num[0],num[1],num[2],num[3])){ 221 System.out.print("true"); 222 } 223 else{ 224 System.out.print("false"); 225 } 226 if(ISrectangle(num[0],num[1],num[2],num[3])){ 227 System.out.print(" true"); 228 } 229 else{ 230 System.out.print(" false"); 231 } 232 if(ISsquare(num[0],num[1],num[2],num[3])){ 233 System.out.print(" true"); 234 } 235 else{ 236 System.out.print(" false"); 237 } 238 return; 239 } 240 public static boolean ISparallelogram(Point a, Point b, Point c, Point d){//平行四边形 241 Line l1 = new Line(a, b); 242 Line l2 = new Line(d,c); 243 if(l1.isParallel(l2)&&Math.abs(l1.segment()-l2.segment())<1e-6){ 244 return true; 245 } 246 return false; 247 } 248 249 public static boolean ISdiamond(Point a, Point b, Point c, Point d){//菱形 250 if(ISparallelogram(a,b,c,d)){ 251 if(Math.abs(a.getDistance(b)-a.getDistance(d))<1e-6){ 252 return true; 253 } 254 } 255 return false; 256 } 257 258 public static boolean ISrectangle(Point a, Point b, Point c, Point d){//长方形 259 Line l1 = new Line(a,b); 260 Line l2 = new Line(a,d); 261 if(ISparallelogram(a,b,c,d)){ 262 if(l1.isVertical(l2)){ 263 return true; 264 } 265 } 266 return false; 267 } 268 269 public static boolean ISsquare(Point a, Point b, Point c, Point d){//正方形 270 if(ISrectangle(a,b,c,d)){ 271 if(Math.abs(a.getDistance(b)-a.getDistance(d))<1e-6){ 272 return true; 273 } 274 } 275 return false; 276 } 277 278 public static void ISConcaveQuadrilateral(String[] arr){ 279 if (arr.length!= 9){ 280 System.out.print("wrong number of points"); 281 return; 282 } 283 Point[] num = new Point[4]; 284 int k=0; 285 for(int i = 1; i <arr.length; i =i+2){ 286 Point p=new Point(Double.valueOf(arr[i]),Double.valueOf(arr[i+1])); 287 num[k]=p; 288 k++; 289 } 290 if(!ISQuadrilateral(num[0],num[1], num[2], num[3])) { 291 System.out.print("not a quadrilateral"); 292 return; 293 } 294 if(pointsCoincide(num[0],num[1],num[2],num[3])){ 295 System.out.print("points coincide"); 296 return; 297 } 298 Line l1=new Line(num[0],num[1]); 299 Line l2=new Line(num[0],num[2]); 300 Line l3=new Line(num[0],num[3]); 301 Line l4=new Line(num[1],num[2]); 302 Line l5=new Line(num[2],num[3]); 303 Line l6=new Line(num[1],num[3]); 304 double circumference=l1.segment()+l4.segment()+l5.segment()+l3.segment(); 305 trianglepro a=new trianglepro(l1,l2,l4); 306 trianglepro b=new trianglepro(l3,l2,l5); 307 trianglepro c=new trianglepro(l4,l6,l5); 308 trianglepro d=new trianglepro(l1,l6,l3); 309 if(Math.abs(a.area()+b.area()-c.area()-d.area())<1e-6){ 310 System.out.print("true "); 311 System.out.print(new DecimalFormat("0.0##").format(circumference)+" "); 312 System.out.print(new DecimalFormat("0.0##").format(a.area()+b.area())); 313 return; 314 } 315 else{ 316 System.out.print("false "); 317 System.out.print(new DecimalFormat("0.0##").format(circumference)+" "); 318 if(a.area()+b.area()>c.area()+d.area()){ 319 System.out.print(new DecimalFormat("0.0##").format(c.area()+d.area())); 320 } 321 else{ 322 System.out.print(new DecimalFormat("0.0##").format(a.area()+b.area())); 323 } 324 return; 325 } 326 } 327 328 public static void Piontnumber(String[] arr) { 329 if (arr.length != 13) { 330 System.out.print("wrong number of points"); 331 return; 332 } 333 Point[] num = new Point[6]; 334 int k = 0; 335 for (int i = 1; i < arr.length; i = i + 2) { 336 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 337 num[k] = p; 338 k++; 339 } 340 if (num[0].pointsCoincideError(num[1])) { 341 System.out.print("points coincide"); 342 return; 343 } 344 Point p=new Point(65536.0,65536.0); 345 if (!ISQuadrilateral(num[2], num[3], num[4], num[5])){ 346 Point[] length = IStriangle(num[2], num[3], num[4], num[5]); 347 if (length[0].x == 65536.0) { 348 System.out.print("not a quadrilateral or triangle"); 349 return; 350 } else { 351 Line l1 = new Line(length[0], length[1]); 352 Line l2 = new Line(length[1], length[2]); 353 Line l3 = new Line(length[0], length[2]); 354 Line l4 = new Line(num[0], num[1]); 355 if ((l1.iscoincideline(num[0]) && l1.iscoincideline(num[1])) || (l2.iscoincideline(num[0]) && l2.iscoincideline(num[1])) || (l3.iscoincideline(num[0]) && l3.iscoincideline(num[1]))) { 356 System.out.print("The line is coincide with one of the lines"); 357 return; 358 } 359 else if(l1.getIntersectionpoint(l4).equals(p)&&l2.getIntersectionpoint(l4).equals(p)&&l3.getIntersectionpoint(l4).equals(p)){ 360 System.out.print("0"); 361 return; 362 } 363 else if((l1.getIntersectionpoint(l4).equals(l2.getIntersectionpoint(l4))&&l3.getIntersectionpoint(l4).equals(p))||(l2.getIntersectionpoint(l4).equals(l3.getIntersectionpoint(l4))&&l1.getIntersectionpoint(l4).equals(p))||(l1.getIntersectionpoint(l4).equals(l3.getIntersectionpoint(l4))&&l2.getIntersectionpoint(l4).equals(p))){ 364 System.out.print("1"); 365 return; 366 } 367 else{ 368 trianglepro tri=new trianglepro(l1,l2,l3); 369 Line ll1=new Line(); 370 Line ll2=new Line(); 371 Line ll3=new Line(); 372 if(l3.getIntersectionpoint(l4).equals(p)){ 373 ll1.getpoints(l1.getIntersectionpoint(l2),l1.getIntersectionpoint(l4)); 374 ll2.getpoints(l1.getIntersectionpoint(l2),l2.getIntersectionpoint(l4)); 375 ll3.getpoints(l1.getIntersectionpoint(l4),l2.getIntersectionpoint(l4)); 376 } 377 else if(l2.getIntersectionpoint(l4).equals(p)){ 378 ll1.getpoints(l1.getIntersectionpoint(l3),l1.getIntersectionpoint(l4)); 379 ll2.getpoints(l1.getIntersectionpoint(l3),l3.getIntersectionpoint(l4)); 380 ll3.getpoints(l1.getIntersectionpoint(l4),l3.getIntersectionpoint(l4)); 381 } 382 else if(l1.getIntersectionpoint(l4).equals(p)){ 383 ll1.getpoints(l2.getIntersectionpoint(l3),l2.getIntersectionpoint(l4)); 384 ll2.getpoints(l2.getIntersectionpoint(l3),l3.getIntersectionpoint(l4)); 385 ll3.getpoints(l2.getIntersectionpoint(l4),l3.getIntersectionpoint(l4)); 386 } 387 else if(l1.getIntersectionpoint(l4).equals(l2.getIntersectionpoint(l4))){ 388 ll1.getpoints(l1.p1,l1.p2); 389 ll2.getpoints(l1.getIntersectionpoint(l3),l3.getIntersectionpoint(l4)); 390 ll3.getpoints(l1.getIntersectionpoint(l2),l3.getIntersectionpoint(l4)); 391 } 392 else if(l1.getIntersectionpoint(l4).equals(l3.getIntersectionpoint(l4))){ 393 ll1.getpoints(l1.p1,l1.p2); 394 ll2.getpoints(l1.getIntersectionpoint(l3),l2.getIntersectionpoint(l4)); 395 ll3.getpoints(l1.getIntersectionpoint(l2),l2.getIntersectionpoint(l4)); 396 } 397 else if(l2.getIntersectionpoint(l4).equals(l3.getIntersectionpoint(l4))){ 398 ll1.getpoints(l2.p1,l2.p2); 399 ll2.getpoints(l2.getIntersectionpoint(l3),l1.getIntersectionpoint(l4)); 400 ll3.getpoints(l1.getIntersectionpoint(l4),l1.getIntersectionpoint(l2)); 401 } 402 trianglepro ntri=new trianglepro(ll1,ll2,ll3); 403 double area1=ntri.area(); 404 double area2=tri.area()-ntri.area(); 405 if(area2<area1){ 406 double temp = area1; 407 area1 = area2; 408 area2 = temp; 409 } 410 System.out.print("2 "+new DecimalFormat("0.0##").format(area1) +" "+new DecimalFormat("0.0##").format(area2)); 411 return; 412 } 413 } 414 } else { 415 Line l1 = new Line(num[2], num[3]); 416 Line l2 = new Line(num[3], num[4]); 417 Line l3 = new Line(num[4], num[5]); 418 Line l4 = new Line(num[2], num[5]); 419 Line l5 = new Line(num[0], num[1]); 420 if((l1.iscoincideline(num[0]) && l1.iscoincideline(num[1])) || (l2.iscoincideline(num[0]) && l2.iscoincideline(num[1])) || (l3.iscoincideline(num[0]) && l3.iscoincideline(num[1])) || (l4.iscoincideline(num[0]) && l4.iscoincideline(num[1]))) { 421 System.out.print("The line is coincide with one of the lines"); 422 return; 423 } 424 else if(l1.getIntersectionpoint(l5).equals(p)&&l2.getIntersectionpoint(l5).equals(p)&&l3.getIntersectionpoint(l5).equals(p)&&l4.getIntersectionpoint(l5).equals(p)){ 425 System.out.print("0"); 426 return; 427 } 428 else if((l1.getIntersectionpoint(l5).equals(l2.getIntersectionpoint(l5))&&l3.getIntersectionpoint(l5).equals(p)&&l4.getIntersectionpoint(l5).equals(p)) 429 ||(l2.getIntersectionpoint(l5).equals(l3.getIntersectionpoint(l5))&&l1.getIntersectionpoint(l5).equals(p)&&l4.getIntersectionpoint(l5).equals(p)) 430 ||(l3.getIntersectionpoint(l5).equals(l4.getIntersectionpoint(l5))&&l2.getIntersectionpoint(l5).equals(p)&&l4.getIntersectionpoint(l5).equals(p)) 431 ||(l1.getIntersectionpoint(l5).equals(l4.getIntersectionpoint(l5))&&l2.getIntersectionpoint(l5).equals(p)&&l3.getIntersectionpoint(l5).equals(p))){ 432 System.out.print("1"); 433 return; 434 } 435 else{ 436 Line l6=new Line(num[2],num[4]); 437 Line ll1=new Line(); 438 Line ll2=new Line(); 439 Line ll3=new Line(); 440 Line ll4=new Line(); 441 trianglepro a=new trianglepro(l1,l6,l2); 442 trianglepro b=new trianglepro(l3,l6,l4); 443 double area1=0; 444 double area=a.area()+b.area(); 445 if((l5.isOnline(num[2])&&l5.isOnline(num[4]))||(l5.isOnline(num[3])&&l5.isOnline(num[5]))){ 446 area1=0.5*area; 447 } 448 else if(!l1.getIntersectionpoint(l5).equals(p)&&!l2.getIntersectionpoint(l5).equals(p)){ 449 ll1.getpoints(l1.getIntersectionpoint(l5),num[3]); 450 ll2.getpoints(l2.getIntersectionpoint(l5),num[3]); 451 ll3.getpoints(l1.getIntersectionpoint(l5),l2.getIntersectionpoint(l5)); 452 trianglepro c=new trianglepro(ll1,ll2,ll3); 453 area1=c.area(); 454 } 455 else if(!l1.getIntersectionpoint(l5).equals(p)&&!l3.getIntersectionpoint(l5).equals(p)){ 456 ll1.getpoints(l1.getIntersectionpoint(l5),num[3]); 457 ll2.getpoints(l3.getIntersectionpoint(l5),num[3]); 458 ll3.getpoints(l3.getIntersectionpoint(l5),num[4]); 459 ll4.getpoints(l1.getIntersectionpoint(l5),l3.getIntersectionpoint(l5)); 460 trianglepro c=new trianglepro(ll1,ll2,ll4); 461 trianglepro d=new trianglepro(l2,ll2,ll3); 462 area1=c.area()+d.area(); 463 } 464 else if(!l1.getIntersectionpoint(l5).equals(p)&&!l4.getIntersectionpoint(l5).equals(p)){ 465 ll1.getpoints(l1.getIntersectionpoint(l5),num[2]); 466 ll2.getpoints(l4.getIntersectionpoint(l5),num[2]); 467 ll3.getpoints(l1.getIntersectionpoint(l5),l4.getIntersectionpoint(l5)); 468 trianglepro c=new trianglepro(ll1,ll2,ll3); 469 area1=c.area(); 470 } 471 else if(!l2.getIntersectionpoint(l5).equals(p)&&!l3.getIntersectionpoint(l5).equals(p)){ 472 ll1.getpoints(l2.getIntersectionpoint(l5),num[4]); 473 ll2.getpoints(l3.getIntersectionpoint(l5),num[4]); 474 ll3.getpoints(l2.getIntersectionpoint(l5),l3.getIntersectionpoint(l5)); 475 trianglepro c=new trianglepro(ll1,ll2,ll3); 476 area1=c.area(); 477 } 478 else if(!l2.getIntersectionpoint(l5).equals(p)&&!l4.getIntersectionpoint(l5).equals(p)){ 479 ll1.getpoints(l2.getIntersectionpoint(l5),num[4]); 480 ll2.getpoints(l4.getIntersectionpoint(l5),num[4]); 481 ll3.getpoints(l4.getIntersectionpoint(l5),num[5]); 482 ll4.getpoints(l2.getIntersectionpoint(l5),l4.getIntersectionpoint(l5)); 483 trianglepro c=new trianglepro(ll1,ll2,ll4); 484 trianglepro d=new trianglepro(l3,ll2,ll3); 485 area1=c.area()+d.area(); 486 } 487 else if(!l3.getIntersectionpoint(l5).equals(p)&&!l4.getIntersectionpoint(l5).equals(p)){ 488 ll1.getpoints(l3.getIntersectionpoint(l5),num[5]); 489 ll2.getpoints(l4.getIntersectionpoint(l5),num[5]); 490 ll3.getpoints(l3.getIntersectionpoint(l5),l4.getIntersectionpoint(l5)); 491 trianglepro c=new trianglepro(ll1,ll2,ll3); 492 area1=c.area(); 493 } 494 double area2=area-area1; 495 if(area2<area1){ 496 double temp = area1; 497 area1 = area2; 498 area2 = temp; 499 } 500 System.out.print("2 "+new DecimalFormat("0.0##").format(area1) +" "+new DecimalFormat("0.0##").format(area2)); 501 return; 502 } 503 } 504 } 505 506 public static void Isoutside(String[] arr) { 507 if(arr.length!= 11){ 508 System.out.print("wrong number of points"); 509 return; 510 } 511 Point[] num = new Point[5]; 512 int k=0; 513 for(int i = 1; i <arr.length; i =i+2){ 514 Point p=new Point(Double.valueOf(arr[i]),Double.valueOf(arr[i+1])); 515 num[k]=p; 516 k++; 517 } 518 if(!ISQuadrilateral(num[1],num[2],num[3],num[4])){ 519 Point[] length=IStriangle(num[1],num[2],num[3],num[4]); 520 if(length[0].x==65536.0){ 521 System.out.print("not a quadrilateral or triangle"); 522 return; 523 } 524 else{ 525 Line l1=new Line(length[0],length[1]); 526 Line l2=new Line(length[1],length[2]); 527 Line l3=new Line(length[0],length[2]); 528 int count=0; 529 if(l1.isOnline(num[0])||l2.isOnline(num[0])||l3.isOnline(num[0])){ 530 System.out.print("on the triangle"); 531 return; 532 } 533 count+=l1.numpoint(num[0]); 534 count+=l2.numpoint(num[0]); 535 count+=l3.numpoint(num[0]); 536 if(count%2==0){ 537 System.out.print("outof the triangle"); 538 } 539 else{ 540 System.out.print("in the triangle"); 541 } 542 return; 543 } 544 } 545 else{ 546 Line l1=new Line(num[1],num[2]); 547 Line l2=new Line(num[2],num[3]); 548 Line l3=new Line(num[3],num[4]); 549 Line l4=new Line(num[1],num[4]); 550 if(l1.isOnline(num[0])||l2.isOnline(num[0])||l3.isOnline(num[0])||l4.isOnline(num[0])){ 551 System.out.print("on the quadrilateral"); 552 return; 553 } 554 int count=0; 555 count+=l1.numpoint(num[0]); 556 count+=l2.numpoint(num[0]); 557 count+=l3.numpoint(num[0]); 558 count+=l4.numpoint(num[0]); 559 if(count%2==0){ 560 System.out.print("outof the quadrilateral"); 561 } 562 else{ 563 System.out.print("in the quadrilateral"); 564 } 565 return; 566 } 567 } 568 } 569 570 571 class Point{ 572 public double x; 573 public double y; 574 public Point(){}//构造函数 575 public Point(double x, double y){ 576 this.x = x; 577 this.y = y; 578 } 579 public void getPoint(double x, double y){ 580 this.x = x; 581 this.y = y; 582 } 583 public boolean pointsCoincideError(Point p) { 584 if (this.x== p.x && this.y == p.y) { 585 return true; 586 } 587 else{ 588 return false; 589 } 590 } 591 public boolean equals(Point p){ 592 boolean flag = false; 593 if (this.x == p.x && this.y == p.y){ 594 flag = true; 595 } 596 return flag; 597 } 598 public double getDistance(Point p){ 599 return Math.sqrt((Math.pow((p.x-this.x),2)+Math.pow((p.y-this.y),2))); 600 } 601 } 602 603 class Line { 604 public Point p1; 605 public Point p2; 606 607 public Line() { 608 } 609 610 public Line(Point p1, Point p2) { 611 this.p1 = p1; 612 this.p2 = p2; 613 } 614 615 public double segment() { 616 return p1.getDistance(p2); 617 } 618 619 public void getpoints(Point p1, Point p2) { 620 this.p1 = p1; 621 this.p2 = p2; 622 } 623 624 public double getSlope() { 625 if (p1.x == p2.x) { 626 return Double.POSITIVE_INFINITY; 627 } else { 628 return (p2.y - p1.y) / (p2.x - p1.x); 629 } 630 } 631 632 public int numpoint(Point p){ 633 int count=0; 634 double a= this.p2.y - this.p1.y; 635 double b= this.p1.x - this.p2.x; 636 double c= this.p2.x*this.p1.y-this.p1.x *this.p2.y; 637 if(a==0){ 638 return count; 639 } 640 if(b==0){ 641 if(p.x>this.p1.x){ 642 count++; 643 } 644 } 645 else{ 646 double px=(b*p.y+c)/(-a); 647 if((px<this.p1.x&&px>this.p2.x&&px<p.x)||(px>this.p1.x&&px<this.p2.x&&px<p.x)){ 648 count++; 649 } 650 } 651 return count; 652 } 653 654 public boolean iscoincideline(Point p){ 655 double a=this.p2.y-this.p1.y; 656 double b=this.p1.x-this.p2.x; 657 double c=this.p2.x*this.p1.y-this.p1.x*this.p2.y; 658 if(a*p.x+b*p.y+c==0){ 659 return true; 660 } 661 return false; 662 } 663 664 public boolean isOnline(Point p){ 665 double a=this.p2.y-this.p1.y; 666 double b=this.p1.x-this.p2.x; 667 double c=this.p2.x*this.p1.y-this.p1.x*this.p2.y; 668 double x1=Math.max(this.p1.x, this.p2.x); 669 double x2=Math.min(this.p1.x, this.p2.x); 670 double y1=Math.max(this.p1.y, this.p2.y); 671 double y2=Math.min(this.p1.y, this.p2.y); 672 if((a*p.x+b*p.y+c==0)&&(p.x <=x1&&p.x>=x2)&&(p.y<=y1&&p.y>=y2)){ 673 return true; 674 } 675 return false; 676 } 677 678 public double getDistance(Point x) { 679 // 求得点到直线的距离 680 // 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0 681 double distY = this.p2.y - this.p1.y; 682 double distX = this.p2.x - this.p1.x; 683 return Math.abs(x.x * distY - x.y * distX - p1.x * distY + p1.y * distX) / this.p1.getDistance(this.p2); 684 } 685 686 public boolean isVertical(Line l) { 687 double ax = l.p2.x - l.p1.x; 688 double ay = l.p2.y - l.p1.y; 689 double bx = this.p2.x - this.p1.x; 690 double by = this.p2.y - this.p1.y; 691 if (ax * bx + ay * by == 0) { 692 return true; 693 } else { 694 return false; 695 } 696 } 697 698 public boolean isParallel(Line l) { 699 double ax = l.p2.x - l.p1.x; 700 double ay = l.p2.y - l.p1.y; 701 double bx = this.p2.x - this.p1.x; 702 double by = this.p2.y - this.p1.y; 703 if (ax * by - bx * ay == 0) { 704 return true; 705 } else { 706 return false; 707 } 708 } 709 710 public Point getIntersectionpoint(Line l){ 711 Point p=new Point(65536.0,65536.0); 712 double A= this.p2.y - this.p1.y; 713 double B= this.p1.x - this.p2.x; 714 double C= this.p2.x*this.p1.y-this.p1.x *this.p2.y; 715 double a = l.p2.y - l.p1.y; 716 double b = l.p1.x - l.p2.x; 717 double c = l.p2.x * l.p1.y - l.p1.x * l.p2.y; 718 if(this.isParallel(l)){ 719 return p; 720 } 721 if (A == 0) { 722 if (a == 0) { 723 return p; 724 } else if (b == 0) { 725 if ((l.p1.x <= this.p1.x && l.p1.x >= this.p2.x) || (l.p1.x >= this.p1.x && l.p1.x <= this.p2.x)) { 726 p.getPoint(l.p1.x,this.p1.y); 727 //System.out.println(p.x+","+p.y); 728 return p; 729 } 730 } else { 731 double px = (b * this.p1.y + c) / (-a); 732 if ((px <= this.p1.x && px >= this.p2.x) || (px >= this.p1.x && px <= this.p2.x)) { 733 p.getPoint(px,this.p1.y); 734 //System.out.println(p.x+","+p.y); 735 return p; 736 } 737 } 738 } 739 else if (B == 0) { 740 if (a == 0) { 741 if ((l.p1.y <= this.p1.y && l.p1.y>=this.p2.y) || (l.p1.y>=this.p1.y &&l.p1.y<=this.p2.y)) { 742 p.getPoint(this.p1.x,l.p1.y); 743 //System.out.println(p.x+","+p.y); 744 return p; 745 } 746 } else if (b == 0) { 747 return p; 748 } else { 749 double py = (a * this.p1.x + c) / (-b); 750 if ((py <= this.p1.y && py >= this.p2.y) || (py >= this.p1.y && py <= this.p2.y)) { 751 p.getPoint(this.p1.x,py); 752 // System.out.println(p.x+","+p.y); 753 return p; 754 } 755 } 756 } else { 757 if (a == 0) { 758 double px = (B * l.p1.y + C) / (-A); 759 if((px <= this.p1.x && px >=this.p2.x) || (px >= this.p1.x && px <=this.p2.x)) { 760 p.getPoint(px,l.p1.y); 761 //System.out.println(p.x+","+p.y); 762 return p; 763 } 764 } else if (b == 0) { 765 double py = (A *l.p1.x+ C) / (-B); 766 if ((py <= this.p1.y && py >= this.p2.y) || (py >= this.p1.y && py <= this.p2.y)) { 767 p.getPoint(l.p1.x,py); 768 // System.out.println(p.x+","+p.y); 769 return p; 770 } 771 } else { 772 double px = (b * C - B * c) / (a * B - A * b); 773 if ((px <= this.p1.x && px >=this.p2.x) || (px >= this.p1.x && px <= this.p2.x)) { 774 double py=(A * px + C) / (-B); 775 p.getPoint(px,py); 776 //System.out.println(p.x+","+p.y); 777 return p; 778 } 779 } 780 } 781 return p; 782 } 783 784 public Point getIntersection(Line l) { 785 if (this.isParallel(l)) { 786 return null; 787 } 788 if (this.p1.equals(l.p1) || this.p1.equals(l.p2)) { 789 return p1; 790 } 791 if (this.p2.equals(l.p1) || this.p2.equals(l.p2)) { 792 return p2; 793 } 794 Point cross_point = new Point(); 795 double x, y; 796 double a1 = this.p2.y - this.p1.y; 797 double b1 = this.p1.x - this.p2.x; 798 double a2 = l.p2.y - l.p1.y; 799 double b2 = l.p1.x - l.p2.x; 800 double c1 = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 801 double c2 = l.p2.x * l.p1.y - l.p1.x * l.p2.y; 802 cross_point.x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2); 803 cross_point.y = (a1 * c2 - a2 * c1) / (a2 * b1 - a1 * b2); 804 return cross_point; 805 } 806 807 public boolean Intersection(Line l) { 808 Point p = this.getIntersection(l); 809 double x=p.x; 810 double y=p.y; 811 double x1 = Math.max(this.p1.x,this.p2.x); 812 double x2 = Math.max(l.p1.x,l.p2.x); 813 double x3 = Math.min(this.p1.x,this.p2.x); 814 double x4 = Math.min(l.p1.x,l.p2.x); 815 if ((x >=x3 && x<= x1) && ( x >=x4 &&x <= x2)) { 816 return true; 817 } else { 818 return false; 819 } 820 } 821 } 822 823 class trianglepro{ 824 public Line l1; 825 public Line l2; 826 public Line l3; 827 public trianglepro(){} 828 public trianglepro(Line l1, Line l2, Line l3){ 829 this.l1 =l1; 830 this.l2 =l2; 831 this.l3=l3; 832 } 833 834 public boolean IStriangle(){ 835 double[] lines={this.l1.segment(),this.l2.segment(),this.l3.segment()}; 836 Arrays.sort(lines); 837 if (lines[0]+ lines[1] > lines[2]) { 838 return true; 839 } else { 840 return false; 841 } 842 } 843 844 public double circumference(){ 845 double circumference=this.l1.segment()+this.l2.segment()+this.l3.segment(); 846 return circumference; 847 } 848 849 public double area(){ 850 double p = 0.5 * this.circumference();; 851 double area = Math.sqrt(p * (p - this.l1.segment()) * (p - this.l2.segment()) * (p -this.l3.segment())); 852 return area; 853 } 854 }View Code
代码分析与处理:
1.输入的判断,输入中有空格和字符","的出现,对输入判断不方便。
2.输入数据第一位必须是数字且为1-5,第二位必须为":"(可作为先行判断的条件)
3.输入的点的坐标必须和输入的第一位数字的对应情况对应。
4.两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。
5.每个数字前"+","-"只能有一个,必须符合基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
6.定义一个字符串,将输入数据存入其中,同时对字符串中的","和空格数量进行对比,对所给数据进行字符串进行切割。
7.之后可对所给数据进行是否符合正则表达式判断,看其是否符合基本格式。
8.再运用switch语句,根据所读入的数字确定为何种情况,根据不同的情况判断输入数据的点数以及坐标是否重合,判断是否点的数量符合要求,输入的数据是否能够构成正常四边形(三角形)等等。
9.若为情况1则断是否是四边形、平行四边形,运用判断邻边不能平行,对边不能相交(或交点不能在两对边线段范围内)。
10.若为情况2则判断是否是菱形(平行四边形的基础上判断邻边是否相等)、矩形(平行四边形的基础上判断是否有直角)、正方形(长方形的基础上判断领边是否相等)。
11.若为情况3则将原四边形按不同的对角线连接得到的不同的两个三角形的面积和进行计算,若结果相差无几为凸四边形,反之为凹四边形,然后计算输出四边形周长、面积。
12.为情况4或5时,需要对输入的四个点进行判断判断(点是否重合以及点是否在其相邻领边上,去除冗余点后),判断构成一个四边形或三角形,
13.若为情况4则计算两个点所在的直线与所构成的四边形(三角形)相交的交点数量,如果交点有两个,则按面积大小依次输出四边形(三角形)被直线分割成两部分的面积,否则直接输出交点的个数。若直线与四边形(三角形)一条线重合,输出"The line is coincide with one of the lines"。
14.若为情况5则使用射线法,水平做一条射线计算出与与四边形(三角形)交点数量,在其左端的个数,判断是否奇数以此判断是否在后三个点所构成的四边形(三角形)的内部。
类图:
分析报告:
7-1 点线形系列5-凸五边形的计算-1
1 import java.text.DecimalFormat; 2 import java.util.*; 3 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner sc = new Scanner(System.in); 7 int count = 0, cnt = 0; 8 String point = sc.nextLine(); 9 String[] data = null; 10 if (point.charAt(0) >= '1' && point.charAt(0) <= '6' && point.charAt(1) == ':') { 11 for (int i = 2; i < point.length(); i++) { 12 if (point.charAt(i) == ' ') { 13 cnt++; 14 } 15 if (point.charAt(i) == ',') { 16 count++; 17 } 18 } 19 if (point.charAt(point.length() - 1) == ' ') { 20 cnt--; 21 } 22 if (count - 1 != cnt) { 23 System.out.print("Wrong Format"); 24 return; 25 } 26 data = point.split(":|,| "); 27 for (int i = 1; i < data.length; i++) { 28 if (!data[i].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) { 29 System.out.print("Wrong Format"); 30 return; 31 } 32 } 33 } else { 34 System.out.print("Wrong Format"); 35 return; 36 } 37 switch (data[0]) { 38 case "1": 39 Ifpentagon(data); 40 break; 41 case "2": 42 ISConcavepentagon(data); 43 break; 44 case "3": 45 Piontnumber(data); 46 break; 47 } 48 } 49 public static void Ifpentagon(String[] arr) { 50 if (arr.length != 11) { 51 System.out.print("wrong number of points"); 52 return; 53 } 54 int k = 0; 55 Point[] num = new Point[5]; 56 for (int i = 1; i < arr.length; i = i + 2) { 57 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 58 num[k] = p; 59 k++; 60 } 61 pentagon pe=new pentagon(num[0], num[1], num[2], num[3], num[4]); 62 if (pe.ISpentagon()){ 63 System.out.print("true"); 64 } else { 65 System.out.print("false"); 66 } 67 } 68 public static boolean onside(Line l, Point a, Point b, Point c) { 69 if ((l.side(a) && l.side(b) && l.side(c)) || (!l.side(a) && !l.side(b) && !l.side(c))) { 70 return true; 71 } else { 72 return false; 73 } 74 } 75 public static boolean IfConcavepentagon(Point a,Point b,Point c,Point d,Point e) { 76 pentagon pe=new pentagon(a,b,c,d,e); 77 if (onside(pe.l1, c, d, e) && onside(pe.l2, a, d, e) && onside(pe.l3, a, b, e)&& 78 onside(pe.l4, a, b, c) && onside(pe.l5, b, c, d)) { 79 return true; 80 } else { 81 return false; 82 } 83 } 84 public static int ISpolygon(Point[] num){ 85 if(num.length==5){ 86 pentagon pe=new pentagon(num[0],num[1],num[2],num[3],num[4]); 87 if(pe.ISpentagon()){ 88 return 5; 89 } 90 }else if(num.length==4){ 91 quadrilateral qu=new quadrilateral(num[0],num[1],num[2],num[3]); 92 if(qu.ISQuadrilateral()){ 93 return 4; 94 } 95 }else if(num.length==3){ 96 triangle tr=new triangle(num[0],num[1],num[2]); 97 if(tr.IStriangle()){ 98 return 3; 99 } 100 } 101 return 0; 102 } 103 public static Point[] caculatepoint(Point[] res1, Point[] res2){ 104 ArrayList<Point> num=new ArrayList(); 105 if(res1.length==5&&res2.length==5){ 106 pentagon a=new pentagon(res1[0],res1[1],res1[2],res1[3],res1[4]); 107 pentagon b=new pentagon(res2[0],res2[1],res2[2],res2[3],res2[4]); 108 for(int i=0;i<res1.length;i++){ 109 if(!b.outpentagon(res1[i])){ 110 num.add(res1[i]); 111 } 112 if(!a.outpentagon(res2[i])){ 113 num.add(res2[i]); 114 } 115 for(int j=0;j<res2.length;j++){ 116 if(a.ls[i].Intersection(b.ls[j])){ 117 num.add(a.ls[i].getIntersection(b.ls[j])); 118 } 119 } 120 } 121 } 122 else if(res1.length==5&&res2.length==4||res1.length==4&&res2.length==5){ 123 if(res1.length<res2.length) { 124 Point[] temp = res1.clone(); 125 res1=res2; 126 res2=temp; 127 } 128 pentagon a=new pentagon(res1[0],res1[1],res1[2],res1[3],res1[4]); 129 quadrilateral b=new quadrilateral(res2[0],res2[1],res2[2],res2[3]); 130 for(int i=0;i<res1.length;i++){ 131 if(!b.outquadrilateral(res1[i])){ 132 num.add(res1[i]); 133 } 134 } 135 for(int i=0;i<res2.length;i++) { 136 if (!a.outpentagon(res2[i])) { 137 num.add(res2[i]); 138 } 139 } 140 for(int i=0;i<res1.length;i++){ 141 for(int j=0;j<res2.length;j++) { 142 if (a.ls[i].Intersection(b.ls[j])) { 143 num.add(a.ls[i].getIntersection(b.ls[j])); 144 } 145 } 146 } 147 } 148 else if(res1.length==5&&res2.length==3||res1.length==3&&res2.length==5){ 149 if(res1.length<res2.length){ 150 Point[] temp = res1.clone(); 151 res1=res2; 152 res2=temp; 153 } 154 pentagon a=new pentagon(res1[0],res1[1],res1[2],res1[3],res1[4]); 155 triangle b=new triangle(res2[0],res2[1],res2[2]); 156 for(int i=0;i<res1.length;i++){ 157 if(!b.outtriangle(res1[i])){ 158 num.add(res1[i]); 159 } 160 } 161 for(int i=0;i<res2.length;i++) { 162 if (!a.outpentagon(res2[i])) { 163 num.add(res2[i]); 164 } 165 } 166 for(int i=0;i<res1.length;i++){ 167 for(int j=0;j<res2.length;j++) { 168 if (a.ls[i].Intersection(b.ls[j])) { 169 num.add(a.ls[i].getIntersection(b.ls[j])); 170 } 171 } 172 } 173 } 174 else if(res1.length==4&&res2.length==4){ 175 quadrilateral a=new quadrilateral(res1[0],res1[1],res1[2],res1[3]); 176 quadrilateral b=new quadrilateral(res2[0],res2[1],res2[2],res2[3]); 177 for(int i=0;i<res1.length;i++){ 178 if(!b.outquadrilateral(res1[i])){ 179 num.add(res1[i]); 180 } 181 if(!a.outquadrilateral(res2[i])){ 182 num.add(res2[i]); 183 } 184 for(int j=0;j<res2.length;j++){ 185 if(a.ls[i].Intersection(b.ls[j])){ 186 num.add(a.ls[i].getIntersection(b.ls[j])); 187 } 188 } 189 } 190 } 191 else if(res1.length==4&&res2.length==3||res1.length==3&&res2.length==4){ 192 if(res1.length<res2.length){ 193 Point[] temp = res1.clone(); 194 res1=res2; 195 res2=temp; 196 } 197 quadrilateral a=new quadrilateral(res1[0],res1[1],res1[2],res1[3]); 198 triangle b=new triangle(res2[0],res2[1],res2[2]); 199 for(int i=0;i<res1.length;i++){ 200 if(!b.outtriangle(res1[i])){ 201 num.add(res1[i]); 202 } 203 } 204 for(int i=0;i<res2.length;) { 205 if (!a.outquadrilateral(res2[i])) { 206 num.add(res2[i]); 207 } 208 } 209 for(int i=0;i<res1.length;i++){ 210 for(int j=0;j<res2.length;j++) { 211 if (a.ls[i].Intersection(b.ls[j])) { 212 num.add(a.ls[i].getIntersection(b.ls[j])); 213 } 214 } 215 } 216 } 217 else if(res1.length==3&&res2.length==3){ 218 triangle a=new triangle(res1[0],res1[1],res1[2]); 219 triangle b=new triangle(res2[0],res2[1],res2[2]); 220 for(int i=0;i<res1.length;i++){ 221 if(!b.outtriangle(res1[i])){ 222 num.add(res1[i]); 223 } 224 if(!a.outtriangle(res2[i])){ 225 num.add(res2[i]); 226 } 227 for(int j=0;j<res2.length;j++){ 228 if(a.ls[i].Intersection(b.ls[j])){ 229 num.add(a.ls[i].getIntersection(b.ls[j])); 230 } 231 } 232 } 233 } 234 return num.toArray(new Point[num.size()]); 235 } 236 public static void ISConcavepentagon(String[] arr) { 237 if (arr.length != 11) { 238 System.out.print("wrong number of points"); 239 return; 240 } 241 int k = 0; 242 Point[] num = new Point[5]; 243 for (int i = 1; i < arr.length; i = i + 2) { 244 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 245 num[k] = p; 246 k++; 247 } 248 pentagon pe=new pentagon(num[0], num[1], num[2], num[3], num[4]); 249 if (!pe.ISpentagon()) { 250 System.out.print("not a pentagon"); 251 return; 252 } 253 if (IfConcavepentagon(num[0], num[1], num[2], num[3], num[4])) { 254 System.out.print("true" + " "); 255 System.out.print(new DecimalFormat("0.0##").format(pe.circumference())+" "); 256 System.out.print(new DecimalFormat("0.0##").format(pe.area(pe.ps))); 257 } else { 258 System.out.print("false"); 259 } 260 } 261 public static void Piontnumber(String[] arr) { 262 if (arr.length != 15) { 263 System.out.print("wrong number of points"); 264 return; 265 } 266 Point[] num = new Point[8]; 267 int k = 0; 268 for (int i = 1; i < arr.length; i = i + 2) { 269 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 270 num[k] = p; 271 k++; 272 } 273 if (num[0].equals(num[1])) { 274 System.out.print("points coincide"); 275 return; 276 } 277 Point[] nums=new Point[]{num[2], num[3], num[4], num[5], num[6]}; 278 polygon po=new polygon(nums); 279 Point[] res= po.simplypolygon(po.simplypoints()); 280 switch(ISpolygon(res)){ 281 case 3: 282 triangle tr = new triangle(res[0],res[1],res[2]); 283 tr.pointnumber(num[0],num[1]); 284 break; 285 case 4: 286 quadrilateral qu=new quadrilateral(res[0],res[1],res[2],res[3]); 287 qu.pointnumber(num[0],num[1]); 288 break; 289 case 5: 290 pentagon pe=new pentagon(res[0], res[1], res[2], res[3], res[4]); 291 pe.pointnumber(num[0],num[1]); 292 break; 293 default: 294 System.out.print("not a polygon"); 295 break; 296 } 297 } 298 } 299 300 class Point { 301 public double x; 302 public double y; 303 public Point() { 304 }//构造函数 305 public Point(double x, double y) { 306 this.x = x; 307 this.y = y; 308 } 309 public void getPoint(double x, double y) { 310 this.x = x; 311 this.y = y; 312 } 313 public boolean equals(Point p){ 314 boolean flag = false; 315 if (Math.abs(this.x-p.x)<1e-6&&Math.abs(this.y-p.y)<1e-6){ 316 flag = true; 317 } 318 return flag; 319 } 320 public double getDistance(Point p) { 321 return Math.sqrt((Math.pow((Math.abs(p.x - this.x)), 2) + Math.pow((Math.abs(p.y - this.y)), 2))); 322 } 323 } 324 325 class Line { 326 public Point p1; 327 public Point p2; 328 public Line() { 329 } 330 public Line(Point p1, Point p2) { 331 this.p1 = p1; 332 this.p2 = p2; 333 } 334 public double segment() { 335 return p1.getDistance(p2); 336 } 337 public void getpoints(Point p1, Point p2) { 338 this.p1 = p1; 339 this.p2 = p2; 340 } 341 public boolean side(Point p) { 342 double a = this.p2.y - this.p1.y; 343 double b = this.p1.x - this.p2.x; 344 double c = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 345 if (a * p.x + b * p.y + c > 0) { 346 /*在下方*/ 347 return true; 348 } else { 349 /*在上方*/ 350 return false; 351 } 352 } 353 public double getSlope() { 354 if (p1.x == p2.x) { 355 return Double.POSITIVE_INFINITY; 356 } else { 357 return (p2.y - p1.y) / (p2.x - p1.x); 358 } 359 } 360 public boolean iscoincideline(Point p) { 361 double a = this.p2.y - this.p1.y; 362 double b = this.p1.x - this.p2.x; 363 double c = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 364 if (a * p.x + b * p.y + c == 0) { 365 return true; 366 } 367 return false; 368 } 369 public boolean isOnline(Point p) { 370 double a = this.p2.y - this.p1.y; 371 double b = this.p1.x - this.p2.x; 372 double c = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 373 double x1 = Math.max(this.p1.x, this.p2.x); 374 double x2 = Math.min(this.p1.x, this.p2.x); 375 double y1 = Math.max(this.p1.y, this.p2.y); 376 double y2 = Math.min(this.p1.y, this.p2.y); 377 if ((a * p.x + b * p.y + c == 0) && (p.x <= x1 && p.x >= x2) && (p.y <= y1 && p.y >= y2)) { 378 return true; 379 } 380 return false; 381 } 382 public boolean isParallel(Line l) { 383 double ax = l.p2.x - l.p1.x; 384 double ay = l.p2.y - l.p1.y; 385 double bx = this.p2.x - this.p1.x; 386 double by = this.p2.y - this.p1.y; 387 if (ax * by - bx * ay == 0) { 388 return true; 389 } else { 390 return false; 391 } 392 } 393 public Point getIntersectionpoint(Line l) { 394 Point p = new Point(65536.0, 65536.0); 395 double A = this.p2.y - this.p1.y; 396 double B = this.p1.x - this.p2.x; 397 double C = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 398 double a = l.p2.y - l.p1.y; 399 double b = l.p1.x - l.p2.x; 400 double c = l.p2.x * l.p1.y - l.p1.x * l.p2.y; 401 if (this.isParallel(l)) { 402 return p; 403 } 404 if (A == 0) { 405 if (a == 0) { 406 return p; 407 } else if (b == 0) { 408 if ((l.p1.x <this.p1.x && l.p1.x >this.p2.x) || (l.p1.x > this.p1.x && l.p1.x <this.p2.x)||(Math.abs(l.p1.x-this.p1.x)<1e-6)||(Math.abs(l.p1.x-this.p2.x)<1e-6)) { 409 p.getPoint(l.p1.x, this.p1.y); 410 return p; 411 } 412 } else { 413 double px = (b * this.p1.y + c) / (-a); 414 if ((px < this.p1.x && px > this.p2.x) || (px > this.p1.x && px < this.p2.x)||(Math.abs(px-this.p1.x)<1e-6)||(Math.abs(px-this.p2.x)<1e-6)) { 415 p.getPoint(px, this.p1.y); 416 return p; 417 } 418 } 419 } else if (B == 0) { 420 if (a == 0) { 421 if ((l.p1.y < this.p1.y && l.p1.y > this.p2.y) || (l.p1.y > this.p1.y && l.p1.y < this.p2.y)||(Math.abs(l.p1.y-this.p1.y)<1e-6)||(Math.abs(l.p1.y-this.p2.y)<1e-6)) { 422 p.getPoint(this.p1.x, l.p1.y); 423 return p; 424 } 425 } else if (b == 0) { 426 return p; 427 } else { 428 double py = (a * this.p1.x + c) / (-b); 429 if ((py < this.p1.y && py > this.p2.y) || (py > this.p1.y && py < this.p2.y)||(Math.abs(py-this.p1.y)<1e-6)||(Math.abs(py-this.p2.y)<1e-6)) { 430 p.getPoint(this.p1.x, py); 431 return p; 432 } 433 } 434 } else { 435 if (a == 0) { 436 double px = (B * l.p1.y + C) / (-A); 437 if ((px <this.p1.x && px > this.p2.x) || (px > this.p1.x && px < this.p2.x)||(Math.abs(px-this.p1.x)<1e-6)||(Math.abs(px-this.p2.x)<1e-6)) { 438 p.getPoint(px, l.p1.y); 439 return p; 440 } 441 } else if (b == 0) { 442 double py = (A * l.p1.x + C) / (-B); 443 if ((py < this.p1.y && py > this.p2.y) || (py > this.p1.y && py < this.p2.y)||(Math.abs(py-this.p1.y)<1e-6)||(Math.abs(py-this.p2.y)<1e-6)) { 444 p.getPoint(l.p1.x, py); 445 return p; 446 } 447 } else { 448 double px = (b * C - B * c) / (a * B - A * b); 449 if ((px < this.p1.x && px > this.p2.x) || (px > this.p1.x && px < this.p2.x)||(Math.abs(px-this.p1.x)<1e-6)||(Math.abs(px-this.p2.x)<1e-6)) { 450 double py = (A * px + C) / (-B); 451 p.getPoint(px, py); 452 return p; 453 } 454 } 455 } 456 return p; 457 } 458 459 public Point getIntersection(Line l) { 460 Point p = new Point(65536.0, 65536.0); 461 if (this.isParallel(l)) { 462 return p; 463 } 464 if (this.p1.equals(l.p1) || this.p1.equals(l.p2)) { 465 return p1; 466 } 467 if (this.p2.equals(l.p1) || this.p2.equals(l.p2)) { 468 return p2; 469 } 470 Point cross_point = new Point(); 471 double x, y; 472 double a1 = this.p2.y - this.p1.y; 473 double b1 = this.p1.x - this.p2.x; 474 double a2 = l.p2.y - l.p1.y; 475 double b2 = l.p1.x - l.p2.x; 476 double c1 = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 477 double c2 = l.p2.x * l.p1.y - l.p1.x * l.p2.y; 478 cross_point.x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2); 479 cross_point.y = (a1 * c2 - a2 * c1) / (a2 * b1 - a1 * b2); 480 return cross_point; 481 } 482 483 public boolean Intersection(Line l) { 484 Point p = this.getIntersection(l); 485 double x = p.x; 486 double y = p.y; 487 double x1 = Math.max(this.p1.x, this.p2.x); 488 double x2 = Math.max(l.p1.x, l.p2.x); 489 double x3 = Math.min(this.p1.x, this.p2.x); 490 double x4 = Math.min(l.p1.x, l.p2.x); 491 if ((x >= x3 && x <= x1) && (x >= x4 && x <= x2)) { 492 return true; 493 } else { 494 return false; 495 } 496 } 497 } 498 499 class polygon { 500 public Point[] ps; 501 public polygon() { 502 } 503 public polygon(Point[] num) { 504 this.ps = num; 505 } 506 public ArrayList<Point> simplypoints() { 507 ArrayList<Point> nums = new ArrayList(); 508 nums.add(this.ps[0]); 509 for (int i = 0; i < this.ps.length; i++) { 510 boolean flag = true; 511 for (int j = 0; j < nums.size(); j++) { 512 if (this.ps[i].equals(nums.get(j))) { 513 flag = false; 514 break; 515 } 516 } 517 if (flag) { 518 nums.add(this.ps[i]); 519 } 520 } 521 return nums; 522 } 523 524 public Point[] sortpoints(ArrayList<Point> num){ 525 double x=0,y=0; 526 Point temp=new Point(); 527 for (int i=0;i< num.size();i++){ 528 x+=num.get(i).x; 529 y+=num.get(i).y; 530 } 531 Point p=new Point(x/ num.size(),y/ num.size()); 532 for (int i = 0; i < num.size()-1; i++) { //排序的趟数 n-1趟 533 for (int j = 0; j < num.size() - 1 - i; j++) { //两两排序 走完一遍最大的放后面 534 if (Math.atan2(num.get(j).y - p.y, num.get(j).x - p.x) > Math.atan2(num.get(j + 1).y - p.y, num.get(j + 1).x - p.x)) { 535 Collections.swap(num,j,j+1); 536 } 537 } 538 } 539 return num.toArray(new Point[num.size()]); 540 } 541 public Point[] simplypolygon(ArrayList<Point> nums){ 542 for(int i=0;i<nums.size();){ 543 Line ll=new Line(); 544 if(i==nums.size()-1){ 545 ll.getpoints(nums.get(i-1),nums.get(0)); 546 } 547 else if(i==0){ 548 ll.getpoints(nums.get(i+1),nums.get(nums.size()-1)); 549 }else{ 550 ll.getpoints(nums.get(i - 1), nums.get(i + 1)); 551 } 552 if(ll.isOnline(nums.get(i))){ 553 nums.remove(i); 554 }else{ 555 i++; 556 } 557 } 558 return nums.toArray(new Point[nums.size()]); 559 } 560 public double area(Point[] num){ 561 double area = 0; 562 for (int i = 0; i < num.length; i++) { 563 if (i != num.length - 1) { 564 area += num[i].x * num[i + 1].y - num[i].y * num[i + 1].x; 565 } else { 566 area += num[i].x * num[0].y - num[i].y * num[0].x; 567 } 568 } 569 return Math.abs(area) / 2; 570 } 571 } 572 573 class triangle extends polygon{ 574 public Point a;public Point b;public Point c; 575 public Line l1;public Line l2;public Line l3; 576 public Point[] ps; 577 public Line[] ls; 578 579 public triangle() { 580 } 581 582 public triangle(Point a, Point b, Point c) { 583 this.a = a; 584 this.b = b; 585 this.c = c; 586 this.ps=new Point[]{a,b,c}; 587 this.length(); 588 } 589 590 public void length() { 591 this.l1 = new Line(this.a, this.b); 592 this.l2 = new Line(this.b, this.c); 593 this.l3 = new Line(this.c, this.a); 594 this.ls=new Line[]{this.l1,this.l2,this.l3}; 595 } 596 597 public boolean IStriangle() { 598 double[] lines = {l1.segment(), l2.segment(), l3.segment()}; 599 Arrays.sort(lines); 600 if (lines[0] + lines[1] > lines[2]) { 601 return true; 602 } else { 603 return false; 604 } 605 } 606 607 public double circumference() { 608 return l1.segment() + l2.segment() + l3.segment(); 609 } 610 611 public void pointnumber(Point a,Point b){ 612 Line ll=new Line(a,b); 613 Point p=new Point(65536.0,65536.0); 614 if((l1.iscoincideline(a) && l1.iscoincideline(b)) || (l2.iscoincideline(a) && l2.iscoincideline(b)) || (l3.iscoincideline(a) && l3.iscoincideline(b))) { 615 System.out.print("The line is coincide with one of the lines"); 616 return; 617 } else if (l1.getIntersectionpoint(ll).equals(p) && l2.getIntersectionpoint(ll).equals(p) && l3.getIntersectionpoint(ll).equals(p)) { 618 System.out.print("0"); 619 return; 620 } else if ((l1.getIntersectionpoint(ll).equals(l2.getIntersectionpoint(ll)) && l3.getIntersectionpoint(ll).equals(p)) 621 || (l2.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll)) && l1.getIntersectionpoint(ll).equals(p)) 622 || (l1.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll)) && l2.getIntersectionpoint(ll).equals(p))) { 623 System.out.print("1"); 624 return; 625 } else { 626 double area1 = 0, area2 = 0; 627 if (!l1.getIntersectionpoint(ll).equals(p) && !l2.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l2.getIntersectionpoint(ll))) { 628 triangle small=new triangle(l1.getIntersectionpoint(ll),this.b,l2.getIntersectionpoint(ll)); 629 area1 =small.area(small.ps); 630 } else if (!l1.getIntersectionpoint(ll).equals(p) && !l3.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))) { 631 triangle small=new triangle(l1.getIntersectionpoint(ll),l3.getIntersectionpoint(ll),this.a); 632 area1 =small.area(small.ps); 633 } else if (!l2.getIntersectionpoint(ll).equals(p) && !l3.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))) { 634 triangle small=new triangle(l2.getIntersectionpoint(ll),this.c,l3.getIntersectionpoint(ll)); 635 area1 =small.area(small.ps); 636 } 637 area2 = this.area(this.ps) - area1; 638 if (area2 < area1) { 639 double temp = area1; 640 area1 = area2; 641 area2 = temp; 642 } 643 System.out.print("2 " + new DecimalFormat("0.0##").format(area1) + " " + new DecimalFormat("0.0##").format(area2)); 644 return; 645 } 646 } 647 648 public boolean outtriangle(Point p){ 649 double sum=0; 650 triangle x=new triangle(this.a,this.b,p); 651 triangle y=new triangle(this.b,this.c,p); 652 triangle z=new triangle(this.a,p,this.c); 653 sum=x.area(x.ps)+y.area(y.ps)+z.area(z.ps); 654 if(Math.abs(this.area(this.ps)-sum)<1e-6){ 655 return false; 656 } 657 else { 658 return true; 659 } 660 } 661 } 662 663 class quadrilateral extends polygon { 664 public Point a;public Point b; 665 public Point c;public Point d; 666 public Line l1;public Line l2; 667 public Line l3;public Line l4; 668 public Line ll1;public Line ll2; 669 public Point[] ps; 670 public Line[] ls; 671 672 public quadrilateral(){ 673 } 674 public quadrilateral(Point a, Point b, Point c, Point d) { 675 this.a=a;this.b=b; 676 this.c=c;this.d=d; 677 this.ps=new Point[]{a,b,c,d}; 678 this.length(); 679 } 680 681 public void length(){ 682 this.l1=new Line(this.a,this.b); 683 this.l2=new Line(this.b,this.c); 684 this.l3=new Line(this.c,this.d); 685 this.l4=new Line(this.d,this.a); 686 this.ls=new Line[]{this.l1,this.l2,this.l3,this.l4}; 687 this.ll1=new Line(this.a,this.c); 688 this.ll2=new Line(this.b,this.d); 689 } 690 691 public boolean ISQuadrilateral() {//是否为四边形 692 if ((l1.getSlope() == l2.getSlope()) || (l2.getSlope() == l3.getSlope())||(l3.getSlope() == l4.getSlope()) || (l1.getSlope() == l4.getSlope()) ) { 693 return false; 694 } 695 if (!l1.isParallel(l3)) { 696 if (l1.Intersection(l3)) { 697 return false; 698 } 699 } 700 if (!l2.isParallel(l4)) { 701 if (l2.Intersection(l4)) { 702 return false; 703 } 704 } 705 return true; 706 } 707 708 public double circumference() { 709 return l1.segment() + l2.segment() + l3.segment() + l4.segment(); 710 } 711 712 public void soutarea(double area1){ 713 double area2=this.area(this.ps)-area1; 714 if(area2<area1){ 715 double temp = area1; 716 area1 = area2; 717 area2 = temp; 718 } 719 System.out.print("2 "+new DecimalFormat("0.0##").format(area1) +" "+new DecimalFormat("0.0##").format(area2)); 720 } 721 722 public void pointnumber(Point a,Point b){ 723 Line ll=new Line(a,b); 724 Point p=new Point(65536.0,65536.0); 725 if ((l1.iscoincideline(a) && l1.iscoincideline(b))|| (l2.iscoincideline(a) && l2.iscoincideline(b)) 726 || (l3.iscoincideline(a) && l3.iscoincideline(b))||(l4.iscoincideline(a) && l4.iscoincideline(b))) { 727 System.out.print("The line is coincide with one of the lines"); 728 return; 729 } 730 else if(l1.getIntersectionpoint(ll).equals(p)&&l2.getIntersectionpoint(ll).equals(p)&&l3.getIntersectionpoint(ll).equals(p)&&l4.getIntersectionpoint(ll).equals(p)){ 731 System.out.print("0"); 732 return; 733 } 734 else if((l1.getIntersectionpoint(ll).equals(l2.getIntersectionpoint(ll))&&l3.getIntersectionpoint(ll).equals(p)&&l4.getIntersectionpoint(ll).equals(p)) 735 ||(l2.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))&&l1.getIntersectionpoint(ll).equals(p)&&l4.getIntersectionpoint(ll).equals(p)) 736 ||(l3.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))&&l1.getIntersectionpoint(ll).equals(p)&&l2.getIntersectionpoint(ll).equals(p)) 737 ||(l1.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))&&l2.getIntersectionpoint(ll).equals(p)&&l3.getIntersectionpoint(ll).equals(p))){ 738 System.out.print("1"); 739 return; 740 } 741 else{ 742 if(ll.isOnline(this.a)&&ll.isOnline(this.c)){ 743 triangle small=new triangle(this.a,this.b,this.c); 744 this.soutarea(small.area(small.ps)); 745 return; 746 } 747 else if(ll.isOnline(this.b)&&ll.isOnline(this.d)){ 748 triangle small=new triangle(this.a,this.b,this.d); 749 this.soutarea(small.area(small.ps)); 750 return; 751 } 752 else if(!l1.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l2.getIntersectionpoint(ll))){ 753 triangle small=new triangle(l1.getIntersectionpoint(ll),this.b,l2.getIntersectionpoint(ll)); 754 this.soutarea(small.area(small.ps)); 755 return; 756 } 757 else if(!l1.getIntersectionpoint(ll).equals(p)&&!l3.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))){ 758 if(ll.isOnline(this.b)||ll.isOnline(this.c)){ 759 quadrilateral small=new quadrilateral(l1.getIntersectionpoint(ll),l3.getIntersectionpoint(ll),this.d,this.a); 760 this.soutarea(small.area(small.ps)); 761 return; 762 } 763 else{ 764 quadrilateral small=new quadrilateral(l1.getIntersectionpoint(ll),this.b,this.c,l3.getIntersectionpoint(ll)); 765 this.soutarea(small.area(small.ps)); 766 return; 767 } 768 } 769 else if(!l1.getIntersectionpoint(ll).equals(p)&&!l4.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))){ 770 triangle small=new triangle(l1.getIntersectionpoint(ll),l4.getIntersectionpoint(ll),this.a); 771 this.soutarea(small.area(small.ps)); 772 return; 773 } 774 else if(!l2.getIntersectionpoint(ll).equals(p)&&!l3.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))){ 775 triangle small=new triangle(l2.getIntersectionpoint(ll),this.c,l3.getIntersectionpoint(ll)); 776 this.soutarea(small.area(small.ps)); 777 return; 778 } 779 else if(!l2.getIntersectionpoint(ll).equals(p)&&!l4.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))){ 780 if(ll.isOnline(this.d)||ll.isOnline(this.c)){ 781 quadrilateral small=new quadrilateral(l2.getIntersectionpoint(ll),l4.getIntersectionpoint(ll),this.a,this.b); 782 this.soutarea(small.area(small.ps)); 783 return; 784 } 785 else{ 786 quadrilateral small=new quadrilateral(l2.getIntersectionpoint(ll),this.c,this.d,l4.getIntersectionpoint(ll)); 787 this.soutarea(small.area(small.ps)); 788 return; 789 } 790 } 791 else if(!l3.getIntersectionpoint(ll).equals(p)&&!l4.getIntersectionpoint(ll).equals(p)&&!l3.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))){ 792 triangle small=new triangle(l3.getIntersectionpoint(ll),this.d,l4.getIntersectionpoint(ll)); 793 this.soutarea(small.area(small.ps)); 794 return; 795 } 796 } 797 } 798 799 public boolean outquadrilateral(Point p){ 800 double sum=0; 801 triangle x=new triangle(this.a,this.b,p); 802 triangle y=new triangle(this.b,this.c,p); 803 triangle z=new triangle(this.c,this.d,p); 804 triangle a=new triangle(this.d,this.a,p); 805 sum=x.area(x.ps)+y.area(y.ps)+z.area(z.ps)+a.area(a.ps); 806 if(Math.abs(this.area(this.ps)-sum)<1e-6){ 807 return false; 808 } 809 else { 810 return true; 811 } 812 } 813 } 814 815 class pentagon extends polygon{ 816 public Point a;public Point b; 817 public Point c;public Point d; 818 public Point e;public Line l1; 819 public Line l2;public Line l3; 820 public Line l4;public Line l5; 821 public Line ll1;public Line ll2; 822 public Line ll3;public Line ll4; 823 public Line ll5;public Point[] ps; 824 public Line[] ls; 825 public pentagon(){} 826 public pentagon(Point a,Point b,Point c,Point d,Point e){ 827 this.a=a;this.b=b; 828 this.c=c;this.d=d; 829 this.e=e; 830 this.length(); 831 this.ps=new Point[]{a,b,c,d,e}; 832 } 833 834 public void length(){ 835 this.l1=new Line(this.a,this.b); 836 this.l2=new Line(this.b,this.c); 837 this.l3=new Line(this.c,this.d); 838 this.l4=new Line(this.d,this.e); 839 this.l5=new Line(this.e,this.a); 840 this.ls=new Line[]{this.l1,this.l2,this.l3,this.l4,this.l5}; 841 this.ll1=new Line(this.a,this.c); 842 this.ll2=new Line(this.a,this.d); 843 this.ll3=new Line(this.b,this.d); 844 this.ll4=new Line(this.b,this.e); 845 this.ll5=new Line(this.c,this.e); 846 } 847 public boolean ISpentagon() { 848 if (l1.isParallel(l2) || l1.isParallel(l5) || l2.isParallel(l3) || l3.isParallel(l4) || l4.isParallel(l5)) { 849 return false; 850 } else if (l1.Intersection(l3) || l1.Intersection(l4) || l2.Intersection(l4) || l2.Intersection(l5) || l3.Intersection(l5)) { 851 return false; 852 } else { 853 return true; 854 } 855 } 856 public double circumference() { 857 return l1.segment() + l2.segment() + l3.segment() + l4.segment()+l5.segment(); 858 } 859 860 public void soutarea(double area1){ 861 double area2=this.area(this.ps)-area1; 862 if(area2<area1){ 863 double temp = area1; 864 area1 = area2; 865 area2 = temp; 866 } 867 System.out.print("2 "+new DecimalFormat("0.0##").format(area1) +" "+new DecimalFormat("0.0##").format(area2)); 868 } 869 public void pointnumber(Point a,Point b){ 870 Line ll=new Line(a,b); 871 Point p=new Point(65536.0,65536.0); 872 if ((l1.iscoincideline(a) && l1.iscoincideline(b)) || (l2.iscoincideline(a) && l2.iscoincideline(b)) 873 || (l3.iscoincideline(a) && l3.iscoincideline(b)) || (l4.iscoincideline(a) && l4.iscoincideline(b)) 874 || (l5.iscoincideline(a) && l5.iscoincideline(b))) { 875 System.out.print("The line is coincide with one of the lines"); 876 return; 877 } else if (l1.getIntersectionpoint(ll).equals(p) && l2.getIntersectionpoint(ll).equals(p) && l3.getIntersectionpoint(ll).equals(p) 878 && l4.getIntersectionpoint(ll).equals(p) && l5.getIntersectionpoint(ll).equals(p)) { 879 System.out.print("0"); 880 return; 881 } else if ((l1.getIntersectionpoint(ll).equals(l2.getIntersectionpoint(ll)) && l3.getIntersectionpoint(ll).equals(p) && l4.getIntersectionpoint(ll).equals(p) && l5.getIntersectionpoint(ll).equals(p)) 882 || (l2.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll)) && l1.getIntersectionpoint(ll).equals(p) && l4.getIntersectionpoint(ll).equals(p) && l5.getIntersectionpoint(ll).equals(p)) 883 || (l3.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll)) && l1.getIntersectionpoint(ll).equals(p) && l2.getIntersectionpoint(ll).equals(p) && l5.getIntersectionpoint(ll).equals(p)) 884 || (l4.getIntersectionpoint(ll).equals(l5.getIntersectionpoint(ll)) && l1.getIntersectionpoint(ll).equals(p) && l2.getIntersectionpoint(ll).equals(p) && l3.getIntersectionpoint(ll).equals(p)) 885 || (l1.getIntersectionpoint(ll).equals(l5.getIntersectionpoint(ll)) && l2.getIntersectionpoint(ll).equals(p) && l3.getIntersectionpoint(ll).equals(p) && l4.getIntersectionpoint(ll).equals(p))) { 886 System.out.print("1"); 887 return; 888 } else { 889 double area1 = 0; 890 if(ll.isOnline(this.a)&&ll.isOnline(this.c)){ 891 triangle small = new triangle(this.a,this.b,this.c); 892 this.soutarea(small.area(small.ps)); 893 return; 894 } 895 else if(ll.isOnline(this.a)&&ll.isOnline(this.d)){ 896 triangle small = new triangle(this.a,this.d,this.e); 897 this.soutarea(small.area(small.ps)); 898 return; 899 } 900 else if(ll.isOnline(this.b)&&ll.isOnline(this.d)){ 901 triangle small = new triangle(this.b,this.c,this.d); 902 this.soutarea(small.area(small.ps)); 903 return; 904 } 905 else if(ll.isOnline(this.b)&&ll.isOnline(this.e)){ 906 triangle small = new triangle(this.b,this.e,this.a); 907 this.soutarea(small.area(small.ps)); 908 return; 909 } 910 else if(ll.isOnline(this.c)&&ll.isOnline(this.e)){ 911 triangle small = new triangle(this.c,this.d,this.e); 912 this.soutarea(small.area(small.ps)); 913 return; 914 } 915 else if (!l1.getIntersectionpoint(ll).equals(p) && !l2.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l2.getIntersectionpoint(ll))) { 916 triangle small = new triangle(l1.getIntersectionpoint(ll),this.b,l2.getIntersectionpoint(ll)); 917 this.soutarea(small.area(small.ps)); 918 return; 919 } else if (!l1.getIntersectionpoint(ll).equals(p) && !l3.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))) { 920 if(ll.isOnline(this.b)||ll.isOnline(this.c)){ 921 pentagon small=new pentagon(l1.getIntersectionpoint(ll),l3.getIntersectionpoint(ll),this.d,this.e,this.a); 922 this.soutarea(small.area(small.ps)); 923 return; 924 } 925 else{ 926 quadrilateral small=new quadrilateral(l1.getIntersectionpoint(ll),this.b,this.c,l3.getIntersectionpoint(ll)); 927 this.soutarea(small.area(small.ps)); 928 return; 929 } 930 } else if (!l1.getIntersectionpoint(ll).equals(p) && !l4.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))) { 931 if(ll.isOnline(this.a)||ll.isOnline(this.e)){ 932 pentagon small=new pentagon(l1.getIntersectionpoint(ll),this.b,this.c,this.d,l4.getIntersectionpoint(ll)); 933 this.soutarea(small.area(small.ps)); 934 return; 935 } 936 else{ 937 quadrilateral small=new quadrilateral(l1.getIntersectionpoint(ll),l3.getIntersectionpoint(ll),this.e,this.a); 938 this.soutarea(small.area(small.ps)); 939 return; 940 } 941 } else if (!l1.getIntersectionpoint(ll).equals(p) && !l5.getIntersectionpoint(ll).equals(p)&&!l1.getIntersectionpoint(ll).equals(l5.getIntersectionpoint(ll))) { 942 triangle small = new triangle(l1.getIntersectionpoint(ll),l5.getIntersectionpoint(ll),this.a); 943 this.soutarea(small.area(small.ps)); 944 return; 945 } else if (!l2.getIntersectionpoint(ll).equals(p) && !l3.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(l3.getIntersectionpoint(ll))) { 946 triangle small = new triangle(l2.getIntersectionpoint(ll),this.c,l3.getIntersectionpoint(ll)); 947 this.soutarea(small.area(small.ps)); 948 return; 949 } else if (!l2.getIntersectionpoint(ll).equals(p) && !l4.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))) { 950 if(ll.isOnline(this.c)||ll.isOnline(this.d)){ 951 pentagon small=new pentagon(l2.getIntersectionpoint(ll),l4.getIntersectionpoint(ll),this.e,this.a,this.b); 952 this.soutarea(small.area(small.ps)); 953 return; 954 } 955 else{ 956 quadrilateral small=new quadrilateral(l2.getIntersectionpoint(ll),this.c,this.d,l4.getIntersectionpoint(ll)); 957 this.soutarea(small.area(small.ps)); 958 return; 959 } 960 } else if (!l2.getIntersectionpoint(ll).equals(p) && !l5.getIntersectionpoint(ll).equals(p)&&!l2.getIntersectionpoint(ll).equals(l5.getIntersectionpoint(ll))) { 961 if(ll.isOnline(this.a)||ll.isOnline(this.b)){ 962 pentagon small=new pentagon(l2.getIntersectionpoint(ll),this.c,this.d,this.e,l5.getIntersectionpoint(ll)); 963 this.soutarea(small.area(small.ps)); 964 return; 965 } 966 else{ 967 quadrilateral small=new quadrilateral(l2.getIntersectionpoint(ll),l5.getIntersectionpoint(ll),this.a,this.b); 968 this.soutarea(small.area(small.ps)); 969 return; 970 } 971 } else if (!l3.getIntersectionpoint(ll).equals(p) && !l4.getIntersectionpoint(ll).equals(p)&&!l3.getIntersectionpoint(ll).equals(l4.getIntersectionpoint(ll))) { 972 triangle small = new triangle(l3.getIntersectionpoint(ll),this.d,l4.getIntersectionpoint(ll)); 973 this.soutarea(small.area(small.ps)); 974 return; 975 } else if (!l3.getIntersectionpoint(ll).equals(p) && !l5.getIntersectionpoint(ll).equals(p)&&!l3.getIntersectionpoint(ll).equals(l5.getIntersectionpoint(ll))) { 976 if(ll.isOnline(this.e)||ll.isOnline(this.d)){ 977 pentagon small=new pentagon(l3.getIntersectionpoint(ll),l5.getIntersectionpoint(ll),this.a,this.b,this.c); 978 this.soutarea(small.area(small.ps)); 979 return; 980 } 981 else{ 982 quadrilateral small=new quadrilateral(l3.getIntersectionpoint(ll),this.d,this.e,l5.getIntersectionpoint(ll)); 983 this.soutarea(small.area(small.ps)); 984 return; 985 } 986 } else if (!l4.getIntersectionpoint(ll).equals(p) && !l5.getIntersectionpoint(ll).equals(p)&&!l4.getIntersectionpoint(ll).equals(l5.getIntersectionpoint(ll))) { 987 triangle small = new triangle(l4.getIntersectionpoint(ll),this.e,l5.getIntersectionpoint(ll)); 988 this.soutarea(small.area(small.ps)); 989 return; 990 } 991 } 992 } 993 994 public boolean outpentagon(Point p){ 995 double sum=0; 996 triangle x=new triangle(this.a,this.b,p); 997 triangle y=new triangle(this.b,this.c,p); 998 triangle z=new triangle(this.c,this.d,p); 999 triangle a=new triangle(this.d,this.e,p); 1000 triangle b=new triangle(this.e,this.a,p); 1001 sum=x.area(x.ps)+y.area(y.ps)+z.area(z.ps)+a.area(a.ps)+b.area(b.ps); 1002 if(Math.abs(this.area(this.ps)-sum)<1e-6){ 1003 return false; 1004 } 1005 else { 1006 return true; 1007 } 1008 } 1009 }View Code
7-2 点线形系列5-凸五边形的计算-2
1 import java.text.DecimalFormat; 2 import java.util.*; 3 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner sc = new Scanner(System.in); 7 int count = 0, cnt = 0; 8 String point = sc.nextLine(); 9 String[] data = null; 10 if (point.charAt(0) >= '1' && point.charAt(0) <= '6' && point.charAt(1) == ':') { 11 for (int i = 2; i < point.length(); i++) { 12 if (point.charAt(i) == ' ') { 13 cnt++; 14 } 15 if (point.charAt(i) == ',') { 16 count++; 17 } 18 } 19 if (point.charAt(point.length() - 1) == ' ') { 20 cnt--; 21 } 22 if (count - 1 != cnt) { 23 System.out.print("Wrong Format"); 24 return; 25 } 26 data = point.split(":|,| "); 27 for (int i = 1; i < data.length; i++) { 28 if (!data[i].matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) { 29 System.out.print("Wrong Format"); 30 return; 31 } 32 } 33 } else { 34 System.out.print("Wrong Format"); 35 return; 36 } 37 switch (data[0]) { 38 case "4": 39 relate(data); 40 break; 41 case "5": 42 Concavearea(data); 43 break; 44 default: 45 outpolygon(data); 46 break; 47 } 48 } 49 public static void Ifpentagon(String[] arr) { 50 if (arr.length != 11) { 51 System.out.print("wrong number of points"); 52 return; 53 } 54 int k = 0; 55 Point[] num = new Point[5]; 56 for (int i = 1; i < arr.length; i = i + 2) { 57 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 58 num[k] = p; 59 k++; 60 } 61 pentagon pe=new pentagon(num[0], num[1], num[2], num[3], num[4]); 62 if (pe.ISpentagon()){ 63 System.out.print("true"); 64 } else { 65 System.out.print("false"); 66 } 67 } 68 public static boolean onside(Line l, Point a, Point b, Point c) { 69 if ((l.side(a) && l.side(b) && l.side(c)) || (!l.side(a) && !l.side(b) && !l.side(c))) { 70 return true; 71 } else { 72 return false; 73 } 74 } 75 public static boolean IfConcavepentagon(Point a,Point b,Point c,Point d,Point e) { 76 pentagon pe=new pentagon(a,b,c,d,e); 77 if (onside(pe.l1, c, d, e) && onside(pe.l2, a, d, e) && onside(pe.l3, a, b, e)&& 78 onside(pe.l4, a, b, c) && onside(pe.l5, b, c, d)) { 79 return true; 80 } else { 81 return false; 82 } 83 } 84 public static int ISpolygon(Point[] num){ 85 if(num.length==5){ 86 pentagon pe=new pentagon(num[0],num[1],num[2],num[3],num[4]); 87 if(pe.ISpentagon()){ 88 return 5; 89 } 90 }else if(num.length==4){ 91 quadrilateral qu=new quadrilateral(num[0],num[1],num[2],num[3]); 92 if(qu.ISQuadrilateral()){ 93 return 4; 94 } 95 }else if(num.length==3){ 96 triangle tr=new triangle(num[0],num[1],num[2]); 97 if(tr.IStriangle()){ 98 return 3; 99 } 100 } 101 return 0; 102 } 103 public static Point[] caculatepoint(Point[] res1, Point[] res2){ 104 ArrayList<Point> num=new ArrayList(); 105 if(res1.length==5&&res2.length==5){ 106 pentagon a=new pentagon(res1[0],res1[1],res1[2],res1[3],res1[4]); 107 pentagon b=new pentagon(res2[0],res2[1],res2[2],res2[3],res2[4]); 108 for(int i=0;i<res1.length;i++){ 109 if(!b.outpentagon(res1[i])){ 110 num.add(res1[i]); 111 } 112 if(!a.outpentagon(res2[i])){ 113 num.add(res2[i]); 114 } 115 for(int j=0;j<res2.length;j++){ 116 if(a.ls[i].Intersection(b.ls[j])){ 117 num.add(a.ls[i].getIntersection(b.ls[j])); 118 } 119 } 120 } 121 } 122 else if(res1.length==5&&res2.length==4||res1.length==4&&res2.length==5){ 123 if(res1.length<res2.length) { 124 Point[] temp = res1.clone(); 125 res1=res2; 126 res2=temp; 127 } 128 pentagon a=new pentagon(res1[0],res1[1],res1[2],res1[3],res1[4]); 129 quadrilateral b=new quadrilateral(res2[0],res2[1],res2[2],res2[3]); 130 for(int i=0;i<res1.length;i++){ 131 if(!b.outquadrilateral(res1[i])){ 132 num.add(res1[i]); 133 } 134 } 135 for(int i=0;i<res2.length;i++) { 136 if (!a.outpentagon(res2[i])) { 137 num.add(res2[i]); 138 } 139 } 140 for(int i=0;i<res1.length;i++){ 141 for(int j=0;j<res2.length;j++) { 142 if (a.ls[i].Intersection(b.ls[j])) { 143 num.add(a.ls[i].getIntersection(b.ls[j])); 144 } 145 } 146 } 147 } 148 else if(res1.length==5&&res2.length==3||res1.length==3&&res2.length==5){ 149 if(res1.length<res2.length){ 150 Point[] temp = res1.clone(); 151 res1=res2; 152 res2=temp; 153 } 154 pentagon a=new pentagon(res1[0],res1[1],res1[2],res1[3],res1[4]); 155 triangle b=new triangle(res2[0],res2[1],res2[2]); 156 for(int i=0;i<res1.length;i++){ 157 if(!b.outtriangle(res1[i])){ 158 num.add(res1[i]); 159 } 160 } 161 for(int i=0;i<res2.length;i++) { 162 if (!a.outpentagon(res2[i])) { 163 num.add(res2[i]); 164 } 165 } 166 for(int i=0;i<res1.length;i++){ 167 for(int j=0;j<res2.length;j++) { 168 if (a.ls[i].Intersection(b.ls[j])) { 169 num.add(a.ls[i].getIntersection(b.ls[j])); 170 } 171 } 172 } 173 } 174 else if(res1.length==4&&res2.length==4){ 175 quadrilateral a=new quadrilateral(res1[0],res1[1],res1[2],res1[3]); 176 quadrilateral b=new quadrilateral(res2[0],res2[1],res2[2],res2[3]); 177 for(int i=0;i<res1.length;i++){ 178 if(!b.outquadrilateral(res1[i])){ 179 num.add(res1[i]); 180 } 181 if(!a.outquadrilateral(res2[i])){ 182 num.add(res2[i]); 183 } 184 for(int j=0;j<res2.length;j++){ 185 if(a.ls[i].Intersection(b.ls[j])){ 186 num.add(a.ls[i].getIntersection(b.ls[j])); 187 } 188 } 189 } 190 } 191 else if(res1.length==4&&res2.length==3||res1.length==3&&res2.length==4){ 192 if(res1.length<res2.length){ 193 Point[] temp = res1.clone(); 194 res1=res2; 195 res2=temp; 196 } 197 quadrilateral a=new quadrilateral(res1[0],res1[1],res1[2],res1[3]); 198 triangle b=new triangle(res2[0],res2[1],res2[2]); 199 for(int i=0;i<res1.length;i++){ 200 if(!b.outtriangle(res1[i])){ 201 num.add(res1[i]); 202 } 203 } 204 for(int i=0;i<res2.length;) { 205 if (!a.outquadrilateral(res2[i])) { 206 num.add(res2[i]); 207 } 208 } 209 for(int i=0;i<res1.length;i++){ 210 for(int j=0;j<res2.length;j++) { 211 if (a.ls[i].Intersection(b.ls[j])) { 212 num.add(a.ls[i].getIntersection(b.ls[j])); 213 } 214 } 215 } 216 } 217 else if(res1.length==3&&res2.length==3){ 218 triangle a=new triangle(res1[0],res1[1],res1[2]); 219 triangle b=new triangle(res2[0],res2[1],res2[2]); 220 for(int i=0;i<res1.length;i++){ 221 if(!b.outtriangle(res1[i])){ 222 num.add(res1[i]); 223 } 224 if(!a.outtriangle(res2[i])){ 225 num.add(res2[i]); 226 } 227 for(int j=0;j<res2.length;j++){ 228 if(a.ls[i].Intersection(b.ls[j])){ 229 num.add(a.ls[i].getIntersection(b.ls[j])); 230 } 231 } 232 } 233 } 234 return num.toArray(new Point[num.size()]); 235 } 236 public static void ISConcavepentagon(String[] arr) { 237 if (arr.length != 11) { 238 System.out.print("wrong number of points"); 239 return; 240 } 241 int k = 0; 242 Point[] num = new Point[5]; 243 for (int i = 1; i < arr.length; i = i + 2) { 244 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 245 num[k] = p; 246 k++; 247 } 248 pentagon pe=new pentagon(num[0], num[1], num[2], num[3], num[4]); 249 if (!pe.ISpentagon()) { 250 System.out.print("not a pentagon"); 251 return; 252 } 253 if (IfConcavepentagon(num[0], num[1], num[2], num[3], num[4])) { 254 System.out.print("true" + " "); 255 System.out.print(new DecimalFormat("0.0##").format(pe.circumference())+" "); 256 System.out.print(new DecimalFormat("0.0##").format(pe.area(pe.ps))); 257 } else { 258 System.out.print("false"); 259 } 260 } 261 262 public static void relate(String[] arr) { 263 if (arr.length != 21) { 264 System.out.print("wrong number of points"); 265 return; 266 } 267 Point[] num = new Point[10]; 268 int k = 0; 269 for (int i = 1; i < arr.length; i = i + 2) { 270 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 271 num[k] = p; 272 k++; 273 } 274 Point[] nums1 = new Point[]{num[0], num[1], num[2], num[3], num[4]}; 275 polygon po1 = new polygon(nums1); 276 Point[] res1 = po1.simplypolygon(po1.simplypoints()); 277 Point[] nums2 = new Point[]{num[5], num[6], num[7], num[8], num[9]}; 278 polygon po2 = new polygon(nums2); 279 Point[] res2 = po2.simplypolygon(po2.simplypoints()); 280 if (ISpolygon(res1) == 0 || ISpolygon(res2) == 0) { 281 System.out.print("not a polygon"); 282 } else { 283 polygon po = new polygon(caculatepoint(res1, res2)); 284 if (res1.length == 5 && res2.length == 5) { 285 pentagon a = new pentagon(res1[0], res1[1], res1[2], res1[3], res1[4]); 286 pentagon b = new pentagon(res2[0], res2[1], res2[2], res2[3], res2[4]); 287 if (!a.outpentagon(res2[0])&&!a.outpentagon(res2[1])&&!a.outpentagon(res2[2])&&!a.outpentagon(res2[3])&&!a.outpentagon(res2[4])){ 288 if(Math.abs(a.area(res1) - b.area(res2)) < 1e-6) { 289 System.out.print("the previous pentagon coincides with the following pentagon"); 290 }else if(b.outpentagon(res1[0])&&b.outpentagon(res1[1])&&b.outpentagon(res1[2])&&b.outpentagon(res1[3])&&b.outpentagon(res1[4])){ 291 System.out.print("the previous pentagon contains the following pentagon"); 292 } 293 } else if(!b.outpentagon(res1[0])&&!b.outpentagon(res1[1])&&!b.outpentagon(res1[2])&&!b.outpentagon(res1[3])&&!b.outpentagon(res1[4])) { 294 if(a.outpentagon(res2[0])&&a.outpentagon(res2[1])&&a.outpentagon(res2[2])&&a.outpentagon(res2[3])&&a.outpentagon(res2[4])) { 295 System.out.print("the previous pentagon is inside the following pentagon"); 296 } 297 } else if(a.outpentagon(res2[0])&&a.outpentagon(res2[1])&&a.outpentagon(res2[2])&&a.outpentagon(res2[3])&&a.outpentagon(res2[4])){ 298 if(b.outpentagon(res1[0])&&b.outpentagon(res1[1])&&b.outpentagon(res1[2])&&b.outpentagon(res1[3])&&b.outpentagon(res1[4])) { 299 System.out.print("no overlapping area between the previous pentagon and the following pentagon"); 300 } 301 } else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 302 System.out.print("the previous pentagon is connected to the following pentagon"); 303 } else{ 304 System.out.print("the previous pentagon is interlaced with the following pentagon"); 305 } 306 } else if (res1.length == 5 && res2.length == 4) { 307 pentagon a = new pentagon(res1[0], res1[1], res1[2], res1[3], res1[4]); 308 quadrilateral b = new quadrilateral(res2[0], res2[1], res2[2], res2[3]); 309 if (!a.outpentagon(res2[0])&&!a.outpentagon(res2[1])&&!a.outpentagon(res2[2])&&!a.outpentagon(res2[3])){ 310 if(b.outquadrilateral(res1[0])&&b.outquadrilateral(res1[1])&&b.outquadrilateral(res1[2])&&b.outquadrilateral(res1[3])&&b.outquadrilateral(res1[4])) { 311 System.out.print("the previous pentagon contains the following quadrilateral"); 312 } 313 } else if (!b.outquadrilateral(res1[0])&&!b.outquadrilateral(res1[1])&&!b.outquadrilateral(res1[2])&&!b.outquadrilateral(res1[3])&&!b.outquadrilateral(res1[4])) { 314 if(a.outpentagon(res2[0])&&a.outpentagon(res2[1])&&a.outpentagon(res2[2])&&a.outpentagon(res2[3])) { 315 System.out.print("the previous pentagon is inside the following quadrilateral"); 316 } 317 } else if(a.outpentagon(res2[0])&&a.outpentagon(res2[1])&&a.outpentagon(res2[2])&&a.outpentagon(res2[3])){ 318 if(b.outquadrilateral(res1[0])&&b.outquadrilateral(res1[1])&&b.outquadrilateral(res1[2])&&b.outquadrilateral(res1[3])&&b.outquadrilateral(res1[4])) { 319 System.out.print("no overlapping area between the previous pentagon and the following quadrilateral"); 320 } 321 } else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 322 System.out.print("the previous pentagon is connected to the following quadrilateral"); 323 } else { 324 System.out.print("the previous pentagon is interlaced with the following quadrilateral"); 325 } 326 } else if (res1.length == 5 && res2.length == 3) { 327 pentagon a = new pentagon(res1[0], res1[1], res1[2], res1[3], res1[4]); 328 triangle b = new triangle(res2[0], res2[1], res2[2]); 329 if (!a.outpentagon(res2[0])&&!a.outpentagon(res2[1])&&!a.outpentagon(res2[2])) { 330 if(b.outtriangle(res1[0])&&b.outtriangle(res1[1])&&b.outtriangle(res1[2])&&b.outtriangle(res1[3])&&b.outtriangle(res1[4])) { 331 System.out.print("the previous pentagon contains the following triangle"); 332 } 333 } else if (!b.outtriangle(res1[0])&&!b.outtriangle(res1[1])&&!b.outtriangle(res1[2])&&!b.outtriangle(res1[3])&&!b.outtriangle(res1[4])){ 334 if(a.outpentagon(res2[0])&&a.outpentagon(res2[1])&&a.outpentagon(res2[2])) { 335 System.out.print("the previous pentagon is inside the following triangle"); 336 } 337 } else if(a.outpentagon(res2[0])&&a.outpentagon(res2[1])&&a.outpentagon(res2[2])){ 338 if(b.outtriangle(res1[0])&&b.outtriangle(res1[1])&&b.outtriangle(res1[2])&&b.outtriangle(res1[3])&&b.outtriangle(res1[4])) { 339 System.out.print("no overlapping area between the previous pentagon and the following triangle"); 340 } 341 }else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 342 System.out.print("the previous pentagon is connected to the following triangle"); 343 } else { 344 System.out.print("the previous pentagon is interlaced with the following triangle"); 345 } 346 } else if (res1.length == 4 && res2.length == 5) { 347 quadrilateral a = new quadrilateral(res1[0], res1[1], res1[2], res1[3]); 348 pentagon b = new pentagon(res2[0], res2[1], res2[2], res2[3], res2[4]); 349 if(!a.outquadrilateral(res2[0])&&!a.outquadrilateral(res2[1])&&!a.outquadrilateral(res2[2])&&!a.outquadrilateral(res2[3])&&!a.outquadrilateral(res2[4])) { 350 if(b.outpentagon(res1[0])&&b.outpentagon(res1[1])&&b.outpentagon(res1[2])&&b.outpentagon(res1[3])) { 351 System.out.print("the previous quadrilateral contains the following pentagon"); 352 } 353 } else if (!b.outpentagon(res1[0])&&!b.outpentagon(res1[1])&&!b.outpentagon(res1[2])&&!b.outpentagon(res1[3])) { 354 if(a.outquadrilateral(res2[0])&&a.outquadrilateral(res2[1])&&a.outquadrilateral(res2[2])&&a.outquadrilateral(res2[3])&&a.outquadrilateral(res2[4])) { 355 System.out.print("the previous quadrilateral is inside the following pentagon"); 356 } 357 } else if(a.outquadrilateral(res2[0])&&a.outquadrilateral(res2[1])&&a.outquadrilateral(res2[2])&&a.outquadrilateral(res2[3])&&a.outquadrilateral(res2[4])){ 358 if(b.outpentagon(res1[0])&&b.outpentagon(res1[1])&&b.outpentagon(res1[2])&&b.outpentagon(res1[3])&&po.simplypoints().isEmpty()) { 359 System.out.print("no overlapping area between the previous quadrilateral and the following pentagon"); 360 } 361 } else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 362 System.out.print("the previous quadrilateral is connected to the following pentagon"); 363 } else { 364 System.out.print("the previous quadrilateral is interlaced with the following pentagon"); 365 } 366 } else if (res1.length == 4 && res2.length == 4) { 367 quadrilateral a = new quadrilateral(res1[0], res1[1], res1[2], res1[3]); 368 quadrilateral b = new quadrilateral(res2[0], res2[1], res2[2], res2[3]); 369 if(!a.outquadrilateral(res2[0])&&!a.outquadrilateral(res2[1])&&!a.outquadrilateral(res1[2])&&!a.outquadrilateral(res2[3])) { 370 if(Math.abs(a.area(res1) - b.area(res2)) < 1e-6){ 371 System.out.print("the previous quadrilateral coincides with the following quadrilateral"); 372 } else if(b.outquadrilateral(res1[0])&&b.outquadrilateral(res1[1])&&b.outquadrilateral(res1[2])&&b.outquadrilateral(res1[3])){ 373 System.out.print("the previous quadrilateral contains the following quadrilateral"); 374 } 375 } else if (!b.outquadrilateral(res1[0])&&!b.outquadrilateral(res1[1])&&!b.outquadrilateral(res1[2])&&!b.outquadrilateral(res1[3])) { 376 if(a.outquadrilateral(res2[0])&&a.outquadrilateral(res2[1])&&a.outquadrilateral(res2[2])&&a.outquadrilateral(res2[3])) { 377 System.out.print("the previous quadrilateral is inside the following quadrilateral"); 378 } 379 } else if(a.outquadrilateral(res2[0])&&a.outquadrilateral(res2[1])&&a.outquadrilateral(res2[2])&&a.outquadrilateral(res2[3])){ 380 if(b.outquadrilateral(res1[0])&&b.outquadrilateral(res1[1])&&b.outquadrilateral(res1[2])&&b.outquadrilateral(res1[3])&&po.simplypoints().isEmpty()) { 381 System.out.print("no overlapping area between the previous quadrilateral and the following quadrilateral"); 382 } 383 } else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 384 System.out.print("the previous quadrilateral is connected to the following quadrilateral"); 385 } else { 386 System.out.print("the previous quadrilateral is interlaced with the following quadrilateral"); 387 } 388 } else if (res1.length == 4 && res2.length == 3) { 389 quadrilateral a = new quadrilateral(res1[0], res1[1], res1[2], res1[3]); 390 triangle b = new triangle(res2[0], res2[1], res2[2]); 391 if (!a.outquadrilateral(res2[0])&&!a.outquadrilateral(res2[1])&&!a.outquadrilateral(res2[2])) { 392 if(b.outtriangle(res1[0])&&b.outtriangle(res1[1])&&b.outtriangle(res1[2])&&b.outtriangle(res1[3])) { 393 System.out.print("the previous quadrilateral contains the following triangle"); 394 } 395 } else if (!b.outtriangle(res1[0])&&!b.outtriangle(res1[1])&&!b.outtriangle(res1[2])&&!b.outtriangle(res1[3])) { 396 if(a.outquadrilateral(res2[0])&&a.outquadrilateral(res2[1])&&a.outquadrilateral(res2[2])) { 397 System.out.print("the previous quadrilateral is inside the following triangle"); 398 } 399 } else if(a.outquadrilateral(res2[0])&&a.outquadrilateral(res2[1])&&a.outquadrilateral(res2[2])){ 400 if(b.outtriangle(res1[0])&&b.outtriangle(res1[1])&&b.outtriangle(res1[2])&&b.outtriangle(res1[3])&&po.simplypoints().isEmpty()) { 401 System.out.print("no overlapping area between the previous quadrilateral and the following triangle"); 402 } 403 }else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 404 System.out.print("the previous quadrilateral is connected to the following triangle"); 405 }else{ 406 System.out.print("the previous quadrilateral is interlaced with the following triangle"); 407 } 408 }else if (res1.length == 3 && res2.length == 5) { 409 triangle a = new triangle(res1[0], res1[1], res1[2]); 410 pentagon b = new pentagon(res2[0], res2[1], res2[2],res2[3],res2[4]); 411 if (!a.outtriangle(res2[0])&&!a.outtriangle(res2[1])&&!a.outtriangle(res2[2])&&!a.outtriangle(res2[3])&&!a.outtriangle(res2[4])){ 412 if(b.outpentagon(res1[0])&&b.outpentagon(res1[1])&&b.outpentagon(res1[2])) { 413 System.out.print("the previous triangle contains the following pentagon"); 414 } 415 } else if (!b.outpentagon(res1[0])&&!b.outpentagon(res1[1])&&!b.outpentagon(res1[2])) { 416 if(a.outtriangle(res2[0])&&a.outtriangle(res2[1])&&a.outtriangle(res2[2])&&a.outtriangle(res2[3])&&a.outtriangle(res2[4])) { 417 System.out.print("the previous triangle is inside the following pentagon"); 418 } 419 } else if(a.outtriangle(res2[0])&&a.outtriangle(res2[1])&&a.outtriangle(res2[2])&&a.outtriangle(res2[3])&&a.outtriangle(res2[4])){ 420 if(b.outpentagon(res1[0])&&b.outpentagon(res1[1])&&b.outpentagon(res1[2])&&po.simplypoints().isEmpty()) { 421 System.out.print("no overlapping area between the previous triangle and the following pentagon"); 422 } 423 }else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 424 System.out.print("the previous triangle is connected to the following pentagon"); 425 } else { 426 System.out.print("the previous triangle is interlaced with the following pentagon"); 427 } 428 }else if (res1.length == 3 && res2.length == 4) { 429 triangle a = new triangle(res1[0], res1[1], res1[2]); 430 quadrilateral b = new quadrilateral(res2[0], res2[1], res2[2],res2[3]); 431 if(!a.outtriangle(res2[0])&&!a.outtriangle(res2[1])&&!a.outtriangle(res2[2])&&!a.outtriangle(res2[3])) { 432 if(b.outquadrilateral(res1[0])&&b.outquadrilateral(res1[1])&&b.outquadrilateral(res1[2])) { 433 System.out.print("the previous triangle contains the following quadrilateral"); 434 } 435 }else if (!b.outquadrilateral(res1[0])&&!b.outquadrilateral(res1[1])&&!b.outquadrilateral(res1[2])) { 436 if(a.outtriangle(res2[0])&&a.outtriangle(res2[1])&&a.outtriangle(res2[2])&&a.outtriangle(res2[3])) { 437 System.out.print("the previous triangle is inside the following quadrilateral"); 438 } 439 }else if(b.outquadrilateral(res1[0])&&b.outquadrilateral(res1[1])&&b.outquadrilateral(res1[2])){ 440 if(a.outtriangle(res2[0])&&a.outtriangle(res2[1])&&a.outtriangle(res2[2])&&a.outtriangle(res2[3])&&po.simplypoints().isEmpty()) { 441 System.out.print("no overlapping area between the previous triangle and the following quadrilateral"); 442 } 443 }else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 444 System.out.print("the previous triangle is connected to the following quadrilateral"); 445 } else { 446 System.out.print("the previous triangle is interlaced with the following quadrilateral"); 447 } 448 } else if (res1.length == 3 && res2.length == 3) { 449 triangle a = new triangle(res1[0], res1[1], res1[2]); 450 triangle b = new triangle(res2[0], res2[1], res2[2]); 451 if (!a.outtriangle(res2[0])&&!a.outtriangle(res2[1])&&!a.outtriangle(res2[2])) { 452 if(Math.abs(a.area(res1) - b.area(res2)) < 1e-6){ 453 System.out.print("the previous triangle coincides with the following triangle"); 454 } else if(b.outtriangle(res1[0])&&b.outtriangle(res1[1])&&b.outtriangle(res1[2])){ 455 System.out.print("the previous triangle contains the following triangle"); 456 } 457 } else if (!b.outtriangle(res1[0])&&!b.outtriangle(res1[1])&&!b.outtriangle(res1[2])) { 458 if(a.outtriangle(res2[0])&&a.outtriangle(res2[1])&&a.outtriangle(res2[2])) { 459 System.out.print("the previous triangle is inside the following triangle"); 460 } 461 } else if(a.outtriangle(res2[0])&&a.outtriangle(res2[1])&&a.outtriangle(res2[2])){ 462 if(b.outtriangle(res1[0])&&b.outtriangle(res1[1])&&b.outtriangle(res1[2])&&po.simplypoints().isEmpty()) { 463 System.out.print("no overlapping area between the previous triangle and the following triangle"); 464 } 465 } else if (po.simplypoints().size() == 1 || po.simplypoints().size() == 2) { 466 System.out.print("the previous triangle is connected to the following triangle"); 467 } else{ 468 System.out.print("the previous triangle is interlaced with the following triangle"); 469 } 470 } 471 } 472 } 473 474 public static void Concavearea(String[] arr) { 475 if (arr.length != 21) { 476 System.out.print("wrong number of points"); 477 return; 478 } 479 Point[] num = new Point[10]; 480 int k = 0; 481 for (int i = 1; i < arr.length; i = i + 2) { 482 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 483 num[k] = p; 484 k++; 485 } 486 Point[] nums1 = new Point[]{num[0], num[1], num[2], num[3], num[4]}; 487 polygon po1 = new polygon(nums1); 488 Point[] res1 = po1.simplypolygon(po1.simplypoints()); 489 Point[] nums2 = new Point[]{num[5], num[6], num[7], num[8], num[9]}; 490 polygon po2 = new polygon(nums2); 491 Point[] res2 = po2.simplypolygon(po2.simplypoints()); 492 if (ISpolygon(res1) == 0 || ISpolygon(res2) == 0) { 493 System.out.print("not a polygon"); 494 } else{ 495 polygon po = new polygon(caculatepoint(res1, res2)); 496 double area=po.area(po.sortpoints(po.simplypoints())); 497 System.out.print(new DecimalFormat("0.0##").format(area)); 498 } 499 } 500 501 public static void outpolygon(String[] arr) { 502 if (arr.length != 13) { 503 System.out.print("wrong number of points"); 504 return; 505 } 506 Point[] num = new Point[6]; 507 int k = 0; 508 for (int i = 1; i < arr.length; i = i + 2) { 509 Point p = new Point(Double.valueOf(arr[i]), Double.valueOf(arr[i + 1])); 510 num[k] = p; 511 k++; 512 } 513 Point[] nums = new Point[]{num[1], num[2], num[3], num[4], num[5]}; 514 polygon po = new polygon(nums); 515 Point[] res = po.simplypolygon(po.simplypoints()); 516 switch (ISpolygon(res)) { 517 case 3: 518 triangle tr = new triangle(res[0], res[1], res[2]); 519 if (tr.l1.isOnline(num[0]) || tr.l2.isOnline(num[0]) || tr.l3.isOnline(num[0])) { 520 System.out.print("on the triangle"); 521 } else if (tr.outtriangle(num[0])) { 522 System.out.print("outof the triangle"); 523 } else { 524 System.out.print("in the triangle"); 525 } 526 break; 527 case 4: 528 quadrilateral qu = new quadrilateral(res[0], res[1], res[2], res[3]); 529 if (qu.l1.isOnline(num[0]) || qu.l2.isOnline(num[0]) || qu.l3.isOnline(num[0]) || qu.l4.isOnline(num[0])) { 530 System.out.print("on the quadrilateral"); 531 return; 532 } else if (qu.outquadrilateral(num[0])) { 533 System.out.print("outof the quadrilateral"); 534 } else { 535 System.out.print("in the quadrilateral"); 536 } 537 break; 538 case 5: 539 pentagon pe = new pentagon(res[0], res[1], res[2], res[3], res[4]); 540 if (pe.l1.isOnline(num[0]) || pe.l2.isOnline(num[0]) || pe.l3.isOnline(num[0]) || pe.l4.isOnline(num[0]) || pe.l5.isOnline(num[0])) { 541 System.out.print("on the pentagon"); 542 return; 543 } else if (pe.outpentagon(num[0])) { 544 System.out.print("outof the pentagon"); 545 } else { 546 System.out.print("in the pentagon"); 547 } 548 break; 549 default: 550 System.out.print("not a polygon"); 551 break; 552 } 553 } 554 } 555 556 class Point { 557 public double x; 558 public double y; 559 public Point() { 560 }//构造函数 561 public Point(double x, double y) { 562 this.x = x; 563 this.y = y; 564 } 565 public void getPoint(double x, double y) { 566 this.x = x; 567 this.y = y; 568 } 569 public boolean equals(Point p){ 570 boolean flag = false; 571 if (Math.abs(this.x-p.x)<1e-6&&Math.abs(this.y-p.y)<1e-6){ 572 flag = true; 573 } 574 return flag; 575 } 576 public double getDistance(Point p) { 577 return Math.sqrt((Math.pow((Math.abs(p.x - this.x)), 2) + Math.pow((Math.abs(p.y - this.y)), 2))); 578 } 579 } 580 581 class Line { 582 public Point p1; 583 public Point p2; 584 public Line() { 585 } 586 public Line(Point p1, Point p2) { 587 this.p1 = p1; 588 this.p2 = p2; 589 } 590 public double segment() { 591 return p1.getDistance(p2); 592 } 593 public void getpoints(Point p1, Point p2) { 594 this.p1 = p1; 595 this.p2 = p2; 596 } 597 public boolean side(Point p) { 598 double a = this.p2.y - this.p1.y; 599 double b = this.p1.x - this.p2.x; 600 double c = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 601 if (a * p.x + b * p.y + c > 0) { 602 /*在下方*/ 603 return true; 604 } else { 605 /*在上方*/ 606 return false; 607 } 608 } 609 public double getSlope() { 610 if (p1.x == p2.x) { 611 return Double.POSITIVE_INFINITY; 612 } else { 613 return (p2.y - p1.y) / (p2.x - p1.x); 614 } 615 } 616 public boolean iscoincideline(Point p) { 617 double a = this.p2.y - this.p1.y; 618 double b = this.p1.x - this.p2.x; 619 double c = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 620 if (a * p.x + b * p.y + c == 0) { 621 return true; 622 } 623 return false; 624 } 625 public boolean isOnline(Point p) { 626 double a = this.p2.y - this.p1.y; 627 double b = this.p1.x - this.p2.x; 628 double c = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 629 double x1 = Math.max(this.p1.x, this.p2.x); 630 double x2 = Math.min(this.p1.x, this.p2.x); 631 double y1 = Math.max(this.p1.y, this.p2.y); 632 double y2 = Math.min(this.p1.y, this.p2.y); 633 if(p.equals(this.p1)||p.equals(this.p2)){ 634 return true; 635 }else if ((a * p.x + b * p.y + c == 0) && (p.x <= x1 && p.x >= x2) && (p.y <= y1 && p.y >= y2)) { 636 return true; 637 } 638 return false; 639 } 640 public boolean isParallel(Line l) { 641 double ax = l.p2.x - l.p1.x; 642 double ay = l.p2.y - l.p1.y; 643 double bx = this.p2.x - this.p1.x; 644 double by = this.p2.y - this.p1.y; 645 if (ax * by - bx * ay == 0) { 646 return true; 647 } else { 648 return false; 649 } 650 } 651 public Point getIntersectionpoint(Line l) { 652 Point p = new Point(65536.0, 65536.0); 653 double A = this.p2.y - this.p1.y; 654 double B = this.p1.x - this.p2.x; 655 double C = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 656 double a = l.p2.y - l.p1.y; 657 double b = l.p1.x - l.p2.x; 658 double c = l.p2.x * l.p1.y - l.p1.x * l.p2.y; 659 if (this.isParallel(l)) { 660 return p; 661 } 662 if (A == 0) { 663 if (a == 0) { 664 return p; 665 } else if (b == 0) { 666 if ((l.p1.x <this.p1.x && l.p1.x >this.p2.x) || (l.p1.x > this.p1.x && l.p1.x <this.p2.x)||(Math.abs(l.p1.x-this.p1.x)<1e-6)||(Math.abs(l.p1.x-this.p2.x)<1e-6)) { 667 p.getPoint(l.p1.x, this.p1.y); 668 return p; 669 } 670 } else { 671 double px = (b * this.p1.y + c) / (-a); 672 if ((px < this.p1.x && px > this.p2.x) || (px > this.p1.x && px < this.p2.x)||(Math.abs(px-this.p1.x)<1e-6)||(Math.abs(px-this.p2.x)<1e-6)) { 673 p.getPoint(px, this.p1.y); 674 return p; 675 } 676 } 677 } else if (B == 0) { 678 if (a == 0) { 679 if ((l.p1.y < this.p1.y && l.p1.y > this.p2.y) || (l.p1.y > this.p1.y && l.p1.y < this.p2.y)||(Math.abs(l.p1.y-this.p1.y)<1e-6)||(Math.abs(l.p1.y-this.p2.y)<1e-6)) { 680 p.getPoint(this.p1.x, l.p1.y); 681 return p; 682 } 683 } else if (b == 0) { 684 return p; 685 } else { 686 double py = (a * this.p1.x + c) / (-b); 687 if ((py < this.p1.y && py > this.p2.y) || (py > this.p1.y && py < this.p2.y)||(Math.abs(py-this.p1.y)<1e-6)||(Math.abs(py-this.p2.y)<1e-6)) { 688 p.getPoint(this.p1.x, py); 689 return p; 690 } 691 } 692 } else { 693 if (a == 0) { 694 double px = (B * l.p1.y + C) / (-A); 695 if ((px <this.p1.x && px > this.p2.x) || (px > this.p1.x && px < this.p2.x)||(Math.abs(px-this.p1.x)<1e-6)||(Math.abs(px-this.p2.x)<1e-6)) { 696 p.getPoint(px, l.p1.y); 697 return p; 698 } 699 } else if (b == 0) { 700 double py = (A * l.p1.x + C) / (-B); 701 if ((py < this.p1.y && py > this.p2.y) || (py > this.p1.y && py < this.p2.y)||(Math.abs(py-this.p1.y)<1e-6)||(Math.abs(py-this.p2.y)<1e-6)) { 702 p.getPoint(l.p1.x, py); 703 return p; 704 } 705 } else { 706 double px = (b * C - B * c) / (a * B - A * b); 707 if ((px < this.p1.x && px > this.p2.x) || (px > this.p1.x && px < this.p2.x)||(Math.abs(px-this.p1.x)<1e-6)||(Math.abs(px-this.p2.x)<1e-6)) { 708 double py = (A * px + C) / (-B); 709 p.getPoint(px, py); 710 return p; 711 } 712 } 713 } 714 return p; 715 } 716 717 public Point getIntersection(Line l) { 718 Point p = new Point(65536.0, 65536.0); 719 if (this.isParallel(l)) { 720 return p; 721 } 722 if (this.p1.equals(l.p1) || this.p1.equals(l.p2)) { 723 return p1; 724 } 725 if (this.p2.equals(l.p1) || this.p2.equals(l.p2)) { 726 return p2; 727 } 728 Point cross_point = new Point(); 729 double x, y; 730 double a1 = this.p2.y - this.p1.y; 731 double b1 = this.p1.x - this.p2.x; 732 double a2 = l.p2.y - l.p1.y; 733 double b2 = l.p1.x - l.p2.x; 734 double c1 = this.p2.x * this.p1.y - this.p1.x * this.p2.y; 735 double c2 = l.p2.x * l.p1.y - l.p1.x * l.p2.y; 736 cross_point.x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2); 737 cross_point.y = (a1 * c2 - a2 * c1) / (a2 * b1 - a1 * b2); 738 return cross_point; 739 } 740 741 public boolean Intersection(Line l) { 742 Point p = this.getIntersection(l); 743 double x = p.x; 744 double y = p.y; 745 double x1 = Math.max(this.p1.x, this.p2.x); 746 double x2 = Math.max(l.p1.x, l.p2.x); 747 double x3 = Math.min(this.p1.x, this.p2.x); 748 double x4 = Math.min(l.p1.x, l.p2.x); 749 if ((x >= x3 && x <= x1) && (x >= x4 && x <= x2)) { 750 return true; 751 } else { 752 return false; 753 } 754 } 755 } 756 757 class polygon { 758 public Point[] ps; 759 public polygon() { 760 } 761 public polygon(Point[] num) { 762 this.ps = num; 763 } 764 public ArrayList<Point> simplypoints() { 765 ArrayList<Point> nums = new ArrayList(); 766 nums.add(this.ps[0]); 767 for (int i = 0; i < this.ps.length; i++) { 768 boolean flag = true; 769 for (int j = 0; j < nums.size(); j++) { 770 if (this.ps[i].equals(nums.get(j))) { 771 flag = false; 772 break; 773 } 774 } 775 if (flag) { 776 nums.add(this.ps[i]); 777 } 778 } 779 return nums; 780 } 781 782 public Point[] sortpoints(ArrayList<Point> num){ 783 double x=0,y=0; 784 Point temp=new Point(); 785 for (int i=0;i< num.size();i++){ 786 x+=num.get(i).x; 787 y+=num.get(i).y; 788 } 789 Point p=new Point(x/ num.size(),y/ num.size()); 790 for (int i = 0; i < num.size()-1; i++) { //排序的趟数 n-1趟 791 for (int j = 0; j < num.size() - 1 - i; j++) { //两两排序 走完一遍最大的放后面 792 if (Math.atan2(num.get(j).y - p.y, num.get(j).x - p.x) > Math.atan2(num.get(j + 1).y - p.y, num.get(j + 1).x - p.x)) { 793 Collections.swap(num,j,j+1); 794 } 795 } 796 } 797 return num.toArray(new Point[num.size()]); 798 } 799 public Point[] simplypolygon(ArrayList<Point> nums){ 800 for(int i=0;i<nums.size();){ 801 Line ll=new Line(); 802 if(i==nums.size()-1){ 803 ll.getpoints(nums.get(i-1),nums.get(0)); 804 } 805 else if(i==0){ 806 ll.getpoints(nums.get(i+1),nums.get(nums.size()-1)); 807 }else{ 808 ll.getpoints(nums.get(i - 1), nums.get(i + 1)); 809 } 810 if(ll.isOnline(nums.get(i))){ 811 nums.remove(i); 812 }else{ 813 i++; 814 } 815 } 816 return nums.toArray(new Point[nums.size()]); 817 } 818 public double area(Point[] num){ 819 double area = 0; 820 for (int i = 0; i < num.length; i++) { 821 if (i != num.length - 1) { 822 area += num[i].x * num[i + 1].y - num[i].y * num[i + 1].x; 823 } else { 824 area += num[i].x * num[0].y - num[i].y * num[0].x; 825 } 826 } 827 return Math.abs(area) / 2; 828 } 829 } 830 831 class triangle extends polygon{ 832 public Point a;public Point b;public Point c; 833 public Line l1;public Line l2;public Line l3; 834 public Point[] ps; 835 public Line[] ls; 836 837 public triangle() { 838 } 839 840 public triangle(Point a, Point b, Point c) { 841 this.a = a; 842 this.b = b; 843 this.c = c; 844 this.ps=new Point[]{a,b,c}; 845 this.length(); 846 } 847 848 public void length() { 849 this.l1 = new Line(this.a, this.b); 850 this.l2 = new Line(this.b, this.c); 851 this.l3 = new Line(this.c, this.a); 852 this.ls=new Line[]{this.l1,this.l2,this.l3}; 853 } 854 855 public boolean IStriangle() { 856 double[] lines = {l1.segment(), l2.segment(), l3.segment()}; 857 Arrays.sort(lines); 858 if (lines[0] + lines[1] > lines[2]) { 859 return true; 860 } else { 861 return false; 862 } 863 } 864 865 public double circumference() { 866 return l1.segment() + l2.segment() + l3.segment(); 867 } 868 869 public boolean outtriangle(Point p){ 870 double sum=0; 871 if(this.l1.isOnline(p)||this.l2.isOnline(p)||this.l3.isOnline(p)){ 872 return false; 873 }else{ 874 triangle x=new triangle(this.a,this.b,p); 875 triangle y=new triangle(this.b,this.c,p); 876 triangle z=new triangle(this.a,p,this.c); 877 sum=x.area(x.ps)+y.area(y.ps)+z.area(z.ps); 878 if(Math.abs(this.area(this.ps)-sum)<1e-6){ 879 return false; 880 } else { 881 return true; 882 } 883 } 884 } 885 } 886 887 class quadrilateral extends polygon { 888 public Point a;public Point b; 889 public Point c;public Point d; 890 public Line l1;public Line l2; 891 public Line l3;public Line l4; 892 public Line ll1;public Line ll2; 893 public Point[] ps; 894 public Line[] ls; 895 896 public quadrilateral(){ 897 } 898 public quadrilateral(Point a, Point b, Point c, Point d) { 899 this.a=a;this.b=b; 900 this.c=c;this.d=d; 901 this.ps=new Point[]{a,b,c,d}; 902 this.length(); 903 } 904 905 public void length(){ 906 this.l1=new Line(this.a,this.b); 907 this.l2=new Line(this.b,this.c); 908 this.l3=new Line(this.c,this.d); 909 this.l4=new Line(this.d,this.a); 910 this.ls=new Line[]{this.l1,this.l2,this.l3,this.l4}; 911 this.ll1=new Line(this.a,this.c); 912 this.ll2=new Line(this.b,this.d); 913 } 914 915 public boolean ISQuadrilateral() {//是否为四边形 916 if ((l1.getSlope() == l2.getSlope()) || (l2.getSlope() == l3.getSlope())||(l3.getSlope() == l4.getSlope()) || (l1.getSlope() == l4.getSlope()) ) { 917 return false; 918 } 919 if (!l1.isParallel(l3)) { 920 if (l1.Intersection(l3)) { 921 return false; 922 } 923 } 924 if (!l2.isParallel(l4)) { 925 if (l2.Intersection(l4)) { 926 return false; 927 } 928 } 929 return true; 930 } 931 932 public double circumference() { 933 return l1.segment() + l2.segment() + l3.segment() + l4.segment(); 934 } 935 936 public void soutarea(double area1){ 937 double area2=this.area(this.ps)-area1; 938 if(area2<area1){ 939 double temp = area1; 940 area1 = area2; 941 area2 = temp; 942 } 943 System.out.print("2 "+new DecimalFormat("0.0##").format(area1) +" "+new DecimalFormat("0.0##").format(area2)); 944 } 945 946 public boolean outquadrilateral(Point p){ 947 double sum=0; 948 if(this.l1.isOnline(p)||this.l2.isOnline(p)||this.l3.isOnline(p)||this.l4.isOnline(p)){ 949 return false; 950 } else{ 951 triangle x=new triangle(this.a,this.b,p); 952 triangle y=new triangle(this.b,this.c,p); 953 triangle z=new triangle(this.c,this.d,p); 954 triangle a=new triangle(this.d,this.a,p); 955 sum=x.area(x.ps)+y.area(y.ps)+z.area(z.ps)+a.area(a.ps); 956 if(Math.abs(this.area(this.ps)-sum)<1e-6){ 957 return false; 958 } 959 else { 960 return true; 961 } 962 } 963 } 964 } 965 966 class pentagon extends polygon{ 967 public Point a;public Point b; 968 public Point c;public Point d; 969 public Point e;public Line l1; 970 public Line l2;public Line l3; 971 public Line l4;public Line l5; 972 public Line ll1;public Line ll2; 973 public Line ll3;public Line ll4; 974 public Line ll5;public Point[] ps; 975 public Line[] ls; 976 public pentagon(){} 977 public pentagon(Point a,Point b,Point c,Point d,Point e){ 978 this.a=a;this.b=b; 979 this.c=c;this.d=d; 980 this.e=e; 981 this.length(); 982 this.ps=new Point[]{a,b,c,d,e}; 983 } 984 985 public void length(){ 986 this.l1=new Line(this.a,this.b); 987 this.l2=new Line(this.b,this.c); 988 this.l3=new Line(this.c,this.d); 989 this.l4=new Line(this.d,this.e); 990 this.l5=new Line(this.e,this.a); 991 this.ls=new Line[]{this.l1,this.l2,this.l3,this.l4,this.l5}; 992 this.ll1=new Line(this.a,this.c); 993 this.ll2=new Line(this.a,this.d); 994 this.ll3=new Line(this.b,this.d); 995 this.ll4=new Line(this.b,this.e); 996 this.ll5=new Line(this.c,this.e); 997 } 998 public boolean ISpentagon() { 999 if (l1.isParallel(l2) || l1.isParallel(l5) || l2.isParallel(l3) || l3.isParallel(l4) || l4.isParallel(l5)) { 1000 return false; 1001 } else if (l1.Intersection(l3) || l1.Intersection(l4) || l2.Intersection(l4) || l2.Intersection(l5) || l3.Intersection(l5)) { 1002 return false; 1003 } else { 1004 return true; 1005 } 1006 } 1007 public double circumference() { 1008 return l1.segment() + l2.segment() + l3.segment() + l4.segment()+l5.segment(); 1009 } 1010 1011 public void soutarea(double area1){ 1012 double area2=this.area(this.ps)-area1; 1013 if(area2<area1){ 1014 double temp = area1; 1015 area1 = area2; 1016 area2 = temp; 1017 } 1018 System.out.print("2 "+new DecimalFormat("0.0##").format(area1) +" "+new DecimalFormat("0.0##").format(area2)); 1019 } 1020 1021 public boolean outpentagon(Point p){ 1022 double sum=0; 1023 if(this.l1.isOnline(p)||this.l2.isOnline(p)||this.l3.isOnline(p)||this.l4.isOnline(p)||this.l5.isOnline(p)){ 1024 return false; 1025 }else { 1026 triangle x = new triangle(this.a, this.b, p); 1027 triangle y = new triangle(this.b, this.c, p); 1028 triangle z = new triangle(this.c, this.d, p); 1029 triangle a = new triangle(this.d, this.e, p); 1030 triangle b = new triangle(this.e, this.a, p); 1031 sum = x.area(x.ps) + y.area(y.ps) + z.area(z.ps) + a.area(a.ps) + b.area(b.ps); 1032 if (Math.abs(this.area(this.ps) - sum) < 1e-6) { 1033 return false; 1034 } else { 1035 return true; 1036 } 1037 } 1038 } 1039 }View Code
类图:
分析报告:
代码分析与处理:
1.有关输入的判断与题目集四边形基本无差(详情可参考上面)。
2.运用switch语句,根据所读入的数字确定为何种情况,根据不同的情况判断输入数据的点数以及坐标是否重合,判断是否点的数量符合要求,输入的数据是否能够构成正常的多边形等等。
3.若为情况1则断是否是五边形,方法与判断四边形类似,运用判断邻边不能平行,对边不能相交(或交点不能在两对边线段范围内)。
4.若为情况2则判断是凹五边形(false)还是凸五边形(true),可通过判断五边形的任意一条边,其余所有点是否都在同一侧,若均满足为凸五边形,反之为凹五边形,通过多边形类的面积计算方法计算。
5.为情况3或4或5或6时,需要对输入的点进行判断判断(点是否重合以及点是否在其相邻领边上,去除冗余点后),判断是否构成多边形以及具体为(五边形或四边形或三角形),以便于后续做题。
6.若为情况3则计算两个点所在的直线与所构成的多边形(五边形或四边形或三角形)相交的交点数量,如果交点有两个,则按面积大小依次输出多边形(五边形或四边形或三角形)被直线分割成两部分的面积,否则直接输出交点的个数。若直线与多边形(五边形或四边形或三角形)一条线重合,输出"the line is coincide with one of the lines",注意细心考虑,两对边交点重合的情况。
7.若为情况4则输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间的关系,根据得出的不同关系情况输出不同的结果语句。若为:
1.分离(完全无重合点)通过判断两多边形双方所有点均不在对方内部。
2.连接(只有一个点或一条边重合)通过判断若只有俩交点且只有一线相交或只有一交点只有一顶点重合。
3.完全重合,通过判断两多边形的所有点是否重合(在此基础上其实也可加上判断它们面积是否相等,不过加不加好像没啥影响。。。)。
4.被包含(前一个多边形在后一个多边形的内部)判断前一多边形的所有点是否在后一多边形内部。
5.交错,情况太多多多了。。。直接else吧。
6.包含(后一个多边形在前一个多边形的内部)判断后一多边形的所有点是否在前一多边形内部。
8.若为情况5则,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。统计两多边形所有点还有线与线的交点,计算所构成的多边形的面积即为公共区域面积。
9.若为情况6则使用面积法,将该点连接多边形各个顶点,计算出所构成的所有三角形的面积和与原多边形的面积关系,判断是否在多边形内部。
期中考试
7-1 点与线(类设计)& 7-2 点线面问题重构(继承与多态)&7-3 点线面问题再重构(容器类)
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner sc=new Scanner(System.in); 7 GeometryObject ge=new GeometryObject(); 8 int choice=sc.nextInt(); 9 while (choice!=0){ 10 switch (choice){ 11 case 1: 12 double x=sc.nextDouble();double y=sc.nextDouble(); 13 if(!judge(x)&&!judge(y)){ 14 System.out.println("Wrong Format"); 15 return; 16 } 17 Point p=new Point(x,y); 18 ge.add(p); 19 break; 20 case 2: 21 double a=sc.nextDouble();double b=sc.nextDouble(); 22 double c=sc.nextDouble();double d=sc.nextDouble(); 23 String color = sc.next(); 24 if(!judge(a)&&!judge(b)&&!judge(c)&&!judge(d)){ 25 System.out.println("Wrong Format"); 26 return; 27 } 28 Point p1 = new Point(a, b); 29 Point p2 = new Point(c, d); 30 Line l=new Line(p1,p2,color); 31 ge.add(l); 32 break; 33 case 3: 34 String colour = sc.next(); 35 Plane pl = new Plane(colour); 36 ge.add(pl); 37 break; 38 case 4: 39 int index = sc.nextInt(); 40 ge.remove(index); 41 break; 42 } 43 //System.out.println(ge.getList().size()); 44 choice=sc.nextInt(); 45 } 46 for(int i=0;i<ge.getList().size();i++){ 47 ge.getList().get(i).display(); 48 } 49 } 50 public static boolean judge(double x){ 51 if(x>0&&x<=200){ 52 return true; 53 }else{ 54 return false; 55 } 56 } 57 } 58 59 abstract class Element{ 60 public abstract void display(); 61 } 62 63 class Point extends Element { 64 private double x; 65 private double y; 66 public Point() {} 67 public Point(double x , double y) { 68 this.x = x; 69 this.y = y; 70 } 71 72 public double getX() { 73 return x; 74 } 75 76 public void setX(double x) { 77 this.x = x; 78 } 79 80 public double getY() { 81 return y; 82 } 83 84 public void setY(double y) { 85 this.y = y; 86 } 87 88 public void display(){ 89 System.out.printf("(%.2f,%.2f)\n",this.x,this.y); 90 } 91 } 92 93 class Line extends Element { 94 private Point p1; 95 private Point p2; 96 private String color; 97 public Line() {} 98 public Line(Point p1, Point p2, String color) { 99 this.p1 = p1; 100 this.p2 = p2; 101 this.color=color; 102 } 103 104 public Point getP1() { 105 return p1; 106 } 107 108 public String getColor() { 109 return color; 110 } 111 112 public void setColor(String color) { 113 this.color = color; 114 } 115 116 public void setP1(Point p1) { 117 this.p1 = p1; 118 } 119 120 public Point getP2() { 121 return p2; 122 } 123 124 public void setP2(Point p2) { 125 this.p2 = p2; 126 } 127 128 public double getDistance() { 129 return Math.sqrt((Math.pow((Math.abs(p2.getX() - p1.getX())), 2) + Math.pow((Math.abs(p2.getY() - p1.getY())), 2))); 130 } 131 public void display(){ 132 System.out.println("The line's color is:"+this.color); 133 System.out.println("The line's begin point's Coordinate is:"); 134 this.p1.display(); 135 System.out.println("The line's end point's Coordinate is:"); 136 this.p2.display(); 137 System.out.printf("The line's length is:%.2f\n",this.getDistance()); 138 } 139 } 140 141 class Plane extends Element{ 142 private String color; 143 public Plane(){}; 144 public Plane(String color) { 145 this.color = color; 146 } 147 148 public String getColor() { 149 return color; 150 } 151 152 public void setColor(String color) { 153 this.color = color; 154 } 155 156 public void display() { 157 System.out.println("The Plane's color is:"+this.color); 158 } 159 } 160 161 class GeometryObject{ 162 private ArrayList<Element> list = new ArrayList<Element>(); 163 public GeometryObject() {} 164 public void add(Element element) { 165 list.add(element); 166 } 167 public void remove(int index) { 168 if(index>=1 && index<=list.size()) { 169 list.remove(index - 1); 170 } 171 } 172 public ArrayList<Element> getList() {return list;} 173 }View Code
类图:
代码分析与处理:
1.本题输入采用while循环输入,循环条件为输入不为0时,执行循环,否则进程结束即可。对于容器类的添加以及删除方法,只需要遍历容器,索引到相应位置,调用Array List的add()和remove()方法即可。前面两题的基础上层层递进,一步步完善,考察了类,方法的设计,继承,多态方面,arraylist以及容器方面的知识。 2.注意在设置index的范围判断须在【1 ,list.size()】之间,以及直接为空等等,防止出现非零返回的情况。 采坑心得: 1.在7-2 点线形系列4-凸四边形的计算过程中,注意选择合适是否为四边形的判断方法,以及在输出数据方面可以使用DecimalFormat()方法来实现符合要求的数据格式,对冗余点判断可以封装为一个方法,来判断四边形(三边形)。 2.7-1 点线形系列5-凸五边形的计算-1&7-2 点线形系列5-凸五边形的计算-2中,完完全全体会到合适的代码结构的好处,之前一直将所有类方法的应用还是写入在主函数内,并没有对其做细致的类的划分,导致后面的解题寸步难行,不得不花费大把的时间进行代码的重构工作,基本算是又重写了一遍(一开始选定构造合适的类与类间的关系真的太重要了。。。)然后是在对求直线与多边形的交点的问题上,疏忽考虑对边与直线交点重合的状况,导致一直有几个点过不去。。。。而在后面的解题过程中,选择合适的结构引入了点集和线集对问题解决提供了很大的帮助,但是同样有部分地方的方法判断出现问题,导致卡了部分点。在对冗杂点去重和删除时,尽量考虑可能出现的情况以及对公共区域面积求解时,对取得的点可先进行排序,然后再利用鞋带定理计算求得所需面积,多边形关系方面多利用点与点、点与线、点与多边形间的关系。 3.关于期中考试的相关题目主要考察了这目前以来类,方法的设计,继承,多态方面,arraylist以及容器方面的知识。 改进建议: 关于点线形系列的题目确切的来说,一步步递进在一开始就要尽量设计合适的结构,然后灵活运用继承与多态类的关系,例如关于求直线与多边形的交点数量可基于三角形基础上,利用将四边形简化为两个三角形,将五边形简化为四边形和三角形,乃至运用到多边形,依次来简化问题的解决方式,不至于考虑的不清不楚把自己给绕晕,清晰合适的结构,再有就是对于一些情况的判断选择更加合适的一些数学方法对结题会有很大的帮助。学习弥补类设计的缺陷,学会灵活运用类与类之间的多种关系,使代码简洁清晰。 直接判断解决完成,会使得题目复杂度过高,并且对于一些BUG修改和测试比较难以发现和修改,如果不去创建新的类,单单在main里一路写下来,代码将又长又复杂。不仅仅将目光放在实现结果上,同时关注代码的总体结构,结构清晰分工明确,对我们未来的学习也会带来更大的帮助。 总结: 通过本阶段的题目集学习,以及之后的总结,更好了理解了java中类、方法的设计,以及java中自带的各种方法,继承多态容器正则表达式类设计原则等,对于面向对象程序设计的概念理解又加深了许多,了解到很多新知识,对于Java语言的各种特性,逐渐熟练掌握,对于面向对象设计的各种原则以及各种设计模式也有了更深的了解和接触,可以说初步打开了面向对象程序设计的大门。但是同时还存在许多不足之处,面向对象的过程还不甚熟练,很多时候还是保持着惯性思维,需要多加练习、学习以及研究。Java语言的各种库以及方法给我们提供了很大便利,但同时我们也要学会合适的利用以及掌握,期望在今后的学习中,能够更加深入体会Java语言的魅力。 标签:p2,p1,题目,double,return,期中,getIntersectionpoint,&&,java From: https://www.cnblogs.com/2992616blogs/p/16830470.html