面向对像程序设计第二次博客作业
一、前言
1、oop-2022-4的题目考察了正则表达式和容器的运用以及对类的设计,题量适中,第一三题不难,经过前三次大作业的锻炼,面对这种题目已经能够从容应对了,第二题是建立在前三次大作业中对图形的理解,用到了许多关于三角形、线和点的属性和方法,难度较大
2、期中考试的题目则考察的主要的类的设计以及继承和多态的使用和容器,题量适中,在规定考试时间内能够按时完成,题目简单。
3、oop-2022-5的题目也是建立在第四次大作业上,考察对类的设计以及对于各种数学方法的运用,题量大,难度大。
二、设计与分析
(1)第四次作业的设计分析总结
1、第一题是从一段文本中获取出数字并对其进行加和,棋本质考察的就是正则表达式,可以创建一个Pattern类的对象,里面包含数字的正则表达式,再运用matcher类里的.find方法对其文本中的数字进行查找,找到后再将其强制转换为double型最后加和输出即可,带代码如下
2、第二题主要考察对四边形的计算,我们可以对四边形进行拆分,创建出点、线、三角形以及四边形类,每个类中包含有自己的 属性和方法,比如Point类中包含有x和y两个属性,并含有各自的setter和getter方法,Line类中则包含两个Point类的属性,并有众多数学方法。三角形和四边形则更是如此,含有许多的数学方法,代码如下

1 import java.text.DecimalFormat; 2 import java.util.ArrayList; 3 import java.util.Arrays; 4 import java.util.Scanner; 5 6 public class Main { 7 public static void main(String[] args) { 8 Scanner in = new Scanner(System.in); 9 String s = in.nextLine(); 10 InputData d = new InputData(); 11 ParseInput.paseInput(s, d); 12 int choice = d.getChoice(); 13 ArrayList ps = d.getPoints(); 14 switch (choice) { 15 case 1: 16 handle1(ps); 17 break; 18 case 2: 19 handle2(ps); 20 break; 21 case 3: 22 handle3(ps); 23 break; 24 case 4: 25 handle4(ps); 26 break; 27 case 5: 28 handle5(ps); 29 break; 30 } 31 } 32 33 public static void handle1(ArrayList<Point> ps) { 34 35 PointInputError.wrongNumberOfPoints(ps, 4); 36 Tetragon t = new Tetragon(ps.get(0), ps.get(1), ps.get(2), ps.get(3)); 37 if(t.isTetragon()) { 38 if(t.IsParallelogram()) 39 System.out.println(true+" "+true); 40 else 41 System.out.println(true+" "+false); 42 } 43 else 44 System.out.println(false+" "+false); 45 } 46 47 public static void handle2(ArrayList<Point> ps) { 48 boolean b1,b2,b3; 49 PointInputError.wrongNumberOfPoints(ps, 4); 50 Tetragon t = new Tetragon(ps.get(0), ps.get(1), ps.get(2), ps.get(3)); 51 if(t.isTetragon()) { 52 b1=t.Isrhombus(); 53 b2=t.IsRectangle(); 54 if(b1&&b2) 55 b3=true; 56 else 57 b3=false; 58 System.out.println(b1+" "+b2+" "+b3); 59 } 60 else 61 System.out.println("not a quadrilateral"); 62 } 63 64 public static void handle3(ArrayList<Point> ps) { 65 boolean b; 66 PointInputError.wrongNumberOfPoints(ps, 4); 67 Tetragon t = new Tetragon(ps.get(0), ps.get(1), ps.get(2), ps.get(3)); 68 if(t.isTetragon()) { 69 b=t.IsConvex(); 70 System.out.println(b+" "+OutFormat.doubleFormat(t.getPerimter())+" "+OutFormat.doubleFormat(t.getArea())); 71 } 72 else 73 System.out.println("not a quadrilateral"); 74 } 75 76 public static void handle4(ArrayList<Point> ps) { 77 PointInputError.wrongNumberOfPoints(ps, 6); 78 Line l = new Line(ps.get(0), ps.get(1)); 79 Tetragon t = new Tetragon(ps.get(2), ps.get(3), ps.get(4), ps.get(5)); 80 Triangle tri; 81 82 if(t.isTetragon()) { 83 if(t.judgeLineCoincide(l)) 84 System.out.println("The line is coincide with one of the lines"); 85 else 86 System.out.println("1"); 87 } 88 else if(t.points()!=null) 89 { 90 tri = new Triangle(t.points()[0],t.points()[1],t.points()[2]); 91 if(tri.judgeLineCoincide(l)) { 92 System.out.println("The line is coincide with one of the lines"); 93 } 94 else { 95 if(tri.getIntersections(l).size()==2) { 96 Point A = tri.getIntersections(l).get(0); 97 Point B = tri.getIntersections(l).get(1); 98 99 if(A.equals(tri.getX())||A.equals(tri.getY())||A.equals(tri.getZ())) { 100 System.out.println(tri.getIntersections(l).size()+" "+OutFormat.doubleFormat(tri.calTriangleArea(A, B)[0])+" "+OutFormat.doubleFormat(tri.calTriangleArea(A, B)[1])); 101 } 102 else 103 System.out.println(tri.getIntersections(l).size()+" "+OutFormat.doubleFormat(tri.calArea(A, B)[0])+" "+OutFormat.doubleFormat(tri.calArea(A, B)[1])); 104 } 105 else 106 System.out.println(tri.getIntersections(l).size()); 107 } 108 } 109 else if(t.PointsisTriangle()!=null) 110 { 111 tri = t.PointsisTriangle(); 112 if(tri.judgeLineCoincide(l)) { 113 System.out.println("The line is coincide with one of the lines"); 114 } 115 else { 116 if(tri.getIntersections(l).size()==2) { 117 Point A = tri.getIntersections(l).get(0); 118 Point B = tri.getIntersections(l).get(1); 119 120 if(A.equals(tri.getX())||A.equals(tri.getY())||A.equals(tri.getZ())) { 121 System.out.println(tri.getIntersections(l).size()+" "+OutFormat.doubleFormat(tri.calTriangleArea(A, B)[0])+" "+OutFormat.doubleFormat(tri.calTriangleArea(A, B)[1])); 122 } 123 else 124 System.out.println(tri.getIntersections(l).size()+" "+OutFormat.doubleFormat(tri.calArea(A, B)[0])+" "+OutFormat.doubleFormat(tri.calArea(A, B)[1])); 125 } 126 else 127 System.out.println(tri.getIntersections(l).size()); 128 } 129 } 130 else 131 System.out.println("not a quadrilateral or triangle"); 132 } 133 134 public static void handle5(ArrayList<Point> ps) { 135 PointInputError.wrongNumberOfPoints(ps, 5); 136 Point p = ps.get(0); 137 Tetragon t = new Tetragon(ps.get(1), ps.get(2), ps.get(3), ps.get(4)); 138 Triangle tri; 139 140 if(t.isTetragon()) { 141 if(t.isInside(p)==1) { 142 System.out.println("in the quadrilateral"); 143 } 144 else if(t.isInside(p)==0) 145 System.out.println("on the quadrilateral"); 146 else 147 System.out.println("outof the quadrilateral"); 148 } 149 else if(t.points()!=null) { 150 tri = new Triangle(t.points()[0],t.points()[1],t.points()[2]); 151 if(tri.isInside(p)==1) { 152 System.out.println("in the triangle"); 153 } 154 else if(t.isInside(p)==0) 155 System.out.println("on the triangle"); 156 else 157 System.out.println("outof the triangle"); 158 } 159 else if(t.PointsisTriangle()!=null) { 160 tri = t.PointsisTriangle(); 161 if(tri.isInside(p)==1) { 162 System.out.println("in the triangle"); 163 } 164 else if(t.isInside(p)==0) 165 System.out.println("on the triangle"); 166 else 167 System.out.println("outof the triangle"); 168 } 169 else 170 System.out.println("not a quadrilateral or triangle"); 171 } 172 } 173 class InputData { 174 private int choice;;//用户输入的选择项 175 private ArrayList<Point> points = new ArrayList();//用户输入的点坐标 176 public int getChoice() { 177 return choice; 178 } 179 public void setChoice(int choice) { 180 this.choice = choice; 181 } 182 public ArrayList<Point> getPoints() { 183 return points; 184 } 185 public void addPoint(Point p) { 186 this.points.add(p); 187 } 188 } 189 class Line { 190 private Point p1;//线上的第一个点 191 private Point p2;//线上的第二个点 192 193 public Line(double x1, double y1, double x2, double y2) { 194 Point p1 = new Point(x1, y1); 195 Point p2 = new Point(x2, y2); 196 LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出 197 this.p1 = p1; 198 this.p2 = p2; 199 } 200 201 public double getlength() { 202 return Math.sqrt((p1.getX() - p2.getX())*(p1.getX() - p2.getX())+(p1.getY() - p2.getY())*(p1.getY() - p2.getY())); 203 } 204 205 public Line(Point p1, Point p2) { 206 LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出 207 this.p1 = p1; 208 this.p2 = p2; 209 } 210 211 public Double getSlope() { 212 213 return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); 214 } 215 216 public boolean isOnline(Point x) { 217 218 // 点重合 219 if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) { 220 return true; 221 } 222 Line l = new Line(p1, x); 223 if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) { 224 return true; 225 } 226 227 double b1 = l.getSlope(), b2 = this.getSlope(); 228 229 return Math.abs(b1 - b2) < 0.00000000001;// b1==b2; 230 } 231 232 public double getDistance(Point x) { 233 double distY = p2.getY() - p1.getY(); 234 double distX = p2.getX() - p1.getX(); 235 return Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX) 236 / p1.getDistance(p2); 237 } 238 239 public boolean isBetween(Point x) { 240 //System.out.println("isBetween" + " " + this.p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y); 241 if (!this.isOnline(x)) { 242 return false; 243 } 244 245 if (x.equals(p1) || x.equals(p2)) { 246 return false; 247 } 248 double d = p2.getDistance(p1); 249 boolean b = x.getDistance(p2) < d && x.getDistance(p1) < d; 250 251 return b; 252 } 253 254 public boolean isSameSide(Point x) { 255 return isOnline(x) && !isBetween(x); 256 } 257 258 public Point getMiddlePoint() { 259 Point p = new Point(); 260 p.setX((p1.getX() + p2.getX()) / 2); 261 p.setY((p1.getY() + p2.getY()) / 2); 262 return p; 263 } 264 265 public Point getPointA() { 266 return p1; 267 } 268 269 public Point getPointB() { 270 return p2; 271 } 272 273 public double getAngle(Line l) { 274 double k2 = getSlope(); 275 double k1 = l.getSlope(); 276 return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度 277 } 278 279 public boolean isParallel(Line l) { 280 Double b1 = this.getSlope(); 281 Double b2 = l.getSlope(); 282 if ((b1.isInfinite()) && (b2.isInfinite())) { 283 return true; 284 } else { 285 return (this.getSlope().doubleValue() == l.getSlope().doubleValue()); 286 } 287 } 288 289 public boolean isCoincide(Line l) { 290 if (!this.isParallel(l)) { 291 return false; 292 } 293 if (this.isOnline(l.p1)) { 294 return true; 295 } 296 return false; 297 } 298 299 public Point getIntersection(Line l) { 300 // LineInputError.isParallelError(this, l); 301 if (this.isParallel(l)) { 302 return null; 303 } 304 if (p1.equals(l.p1) || p1.equals(l.p2)) { 305 return p1; 306 } 307 if (p2.equals(l.p1) || p2.equals(l.p2)) { 308 return p2; 309 } 310 Point p3 = l.p1, p4 = l.p2; 311 double x_member, x_denominator, y_member, y_denominator; 312 Point cross_point = new Point(); 313 x_denominator = p4.getX() * p2.getY() - p4.getX() * p1.getY() - p3.getX() * p2.getY() + p3.getX() * p1.getY() - p2.getX() * p4.getY() + p2.getX() * p3.getY() + p1.getX() * p4.getY() 314 - p1.getX() * p3.getY(); 315 316 x_member = p3.getY() * p4.getX() * p2.getX() - p4.getY() * p3.getX() * p2.getX() - p3.getY() * p4.getX() * p1.getX() + p4.getY() * p3.getX() * p1.getX() 317 - p1.getY() * p2.getX() * p4.getX() + p2.getY() * p1.getX() * p4.getX() + p1.getY() * p2.getX() * p3.getX() - p2.getY() * p1.getX() * p3.getX(); 318 319 if (x_denominator == 0) 320 cross_point.setX(0); 321 else 322 cross_point.setX(x_member / x_denominator); 323 324 y_denominator = p4.getY() * p2.getX() - p4.getY() * p1.getX() - p3.getY() * p2.getX() + p1.getX() * p3.getY() - p2.getY() * p4.getX() + p2.getY() * p3.getX() + p1.getY() * p4.getX() 325 - p1.getY() * p3.getX(); 326 327 y_member = -p3.getY() * p4.getX() * p2.getY() + p4.getY() * p3.getX() * p2.getY() + p3.getY() * p4.getX() * p1.getY() - p4.getY() * p3.getX() * p1.getY() 328 + p1.getY() * p2.getX() * p4.getY() - p1.getY() * p2.getX() * p3.getY() - p2.getY() * p1.getX() * p4.getY() + p2.getY() * p1.getX() * p3.getY(); 329 330 if (y_denominator == 0) 331 cross_point.setY(0); 332 else 333 cross_point.setY(y_member / y_denominator); 334 return cross_point; // 平行返回(0,0) 335 } 336 } 337 class LineInputError { 338 public static void pointsCoincideError(Point p1, Point p2) { 339 if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) { 340 System.out.println("points coincide"); 341 System.exit(0); 342 } 343 } 344 } 345 class OutFormat { 346 public static Double doubleFormat(double b) { 347 DecimalFormat df = new DecimalFormat("#.000"); 348 Double output = Double.valueOf(df.format(b)); 349 return output; 350 } 351 } 352 class ParseInput { 353 public static void paseInput(String s, InputData d) { 354 PointInputError.wrongChoice(s); 355 d.setChoice(getChoice(s)); 356 s = s.substring(2); 357 pasePoints(s, d); 358 } 359 public static int getChoice(String s) { 360 char c = s.charAt(0); 361 return c-48; 362 } 363 public static void pasePoints(String s, InputData d) { 364 String[] ss = s.split(" "); 365 if (ss.length == 0) 366 return; 367 for (int i = 0; i < ss.length; i++) { 368 d.addPoint(readPoint(ss[i])); 369 } 370 } 371 public static Point readPoint(String s) { 372 PointInputError.wrongPointFormat(s); 373 String[] ss = s.split(","); 374 double x = Double.parseDouble(ss[0]); 375 double y = Double.parseDouble(ss[1]); 376 return new Point(x, y); 377 } 378 } 379 class Point { 380 private double x; 381 private double y; 382 383 public Point(double x,double y) { 384 this.x=x; 385 this.y=y; 386 } 387 public Point() { 388 389 } 390 public void setX(double x) { 391 this.x = x; 392 } 393 public void setY(double y) { 394 this.y = y; 395 } 396 public double getX() { 397 return x; 398 } 399 public double getY() { 400 return y; 401 } 402 public boolean equals(Point p) { 403 boolean b = false; 404 if(this.x==p.getX()&&this.y==p.getY()) { 405 b=true; 406 } 407 return b; 408 } 409 public double getDistance(Point p) { 410 return Math.sqrt((x-p.getX()) * (x-p.getX())+(y-p.getY()) * (y-p.getY())); 411 } 412 } 413 class PointInputError { 414 public static void wrongNumberOfPoints(ArrayList ps, int num) { 415 if (ps.size() != num) { 416 System.out.println("wrong number of points"); 417 System.exit(0); 418 } 419 } 420 public static void wrongPointFormat(String s) { 421 if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) { 422 System.out.println("Wrong Format"); 423 System.exit(0); 424 } 425 } 426 public static void wrongChoice(String s) { 427 if (!s.matches("[1-5]:.+")) { 428 System.out.println("Wrong Format"); 429 System.exit(0); 430 } 431 } 432 public static void Is_coincide_Points(ArrayList ps, int num) { 433 if (ps.size() != num) { 434 } 435 } 436 } 437 class Tetragon{ 438 private Point p1; 439 private Point p2; 440 private Point p3; 441 private Point p4; 442 443 protected Tetragon(Point p1, Point p2, Point p3, Point p4) { 444 super(); 445 this.p1 = p1; 446 this.p2 = p2; 447 this.p3 = p3; 448 this.p4 = p4; 449 } 450 451 public boolean isTetragon() { 452 Line l1 = new Line(p1,p2); 453 Line l2 = new Line(p2,p3); 454 Line l3 = new Line(p3,p4); 455 Line l4 = new Line(p4,p1); 456 if(l1.isParallel(l2)||l2.isParallel(l3)||l3.isParallel(l4)||l4.isParallel(l1)) 457 return false; 458 else if(l1.getIntersection(l3)!=null) { 459 if(l1.isBetween(l1.getIntersection(l3))&& l3.isBetween(l1.getIntersection(l3))) { 460 return false; 461 } 462 else { 463 return true; 464 } 465 }else if(l2.getIntersection(l4)!=null) { 466 if(l2.isBetween(l2.getIntersection(l4))&& l4.isBetween(l2.getIntersection(l4))) { 467 return false; 468 } 469 else { 470 return true; 471 } 472 } 473 else 474 return true; 475 } 476 477 public Triangle PointsisTriangle() { 478 Line l1 = new Line(p1,p3); 479 Line l2 = new Line(p2,p4); 480 481 482 if(l1.getIntersection(l2).equals(p1)&&l2.isBetween(p1)) { 483 Triangle t = new Triangle(p2,p3,p4); 484 return t; 485 } 486 else if(l1.getIntersection(l2).equals(p2)&&l1.isBetween(p2)) { 487 Triangle t = new Triangle(p1,p3,p4); 488 return t; 489 } 490 else if(l1.getIntersection(l2).equals(p3)&&l2.isBetween(p3)) { 491 Triangle t = new Triangle(p1,p2,p4); 492 return t; 493 } 494 else if(l1.getIntersection(l2).equals(p4)&&l1.isBetween(p4)) { 495 Triangle t = new Triangle(p1,p2,p3); 496 return t; 497 } 498 else 499 return null; 500 501 502 } 503 504 public double getArea() { 505 Triangle t1 = new Triangle(p2,p3,p4); 506 Triangle t2 = new Triangle(p1,p3,p4); 507 Triangle t3 = new Triangle(p1,p2,p4); 508 Triangle t4 = new Triangle(p1,p2,p3); 509 510 if(IsConvex()) 511 return Math.abs(p1.getX() * p2.getY() + p2.getX() * p3.getY() + p3.getX() * p1.getY() - p1.getY() * p2.getX() - p2.getY() * p3.getX() - p3.getY() * p1.getX()) / 2.0+ 512 Math.abs(p1.getX() * p4.getY() + p4.getX() * p3.getY() + p3.getX() * p1.getY() - p1.getY() * p4.getX() - p4.getY() * p3.getX() - p3.getY() * p1.getX()) / 2.0; 513 else { 514 if(t1.isInside(p1)==1||t3.isInside(p3)==1) { 515 Triangle a = new Triangle(p1,p2,p3); 516 Triangle b = new Triangle(p1,p4,p3); 517 return a.getArea()+b.getArea(); 518 } 519 else { 520 Triangle a = new Triangle(p2,p1,p4); 521 Triangle b = new Triangle(p2,p3,p4); 522 return a.getArea()+b.getArea(); 523 } 524 } 525 } 526 public double getPerimter() { 527 Line l1 = new Line(p1,p2); 528 Line l2 = new Line(p2,p3); 529 Line l3 = new Line(p3,p4); 530 Line l4 = new Line(p4,p1); 531 return l1.getlength()+l2.getlength()+l3.getlength()+l4.getlength(); 532 } 533 public boolean IsConvex() { 534 Triangle a = new Triangle(p1,p2,p3); 535 Triangle b = new Triangle(p3,p4,p1); 536 Triangle c = new Triangle(p2,p3,p4); 537 Triangle d = new Triangle(p4,p1,p2); 538 if((a.getArea() + b.getArea()) == (c.getArea() + d.getArea())) 539 return true; 540 else 541 return false; 542 } 543 public boolean IsParallelogram() { 544 Line l1 = new Line(p1,p2); 545 Line l2 = new Line(p2,p3); 546 Line l3 = new Line(p3,p4); 547 Line l4 = new Line(p4,p1); 548 if((l1.getlength()==l3.getlength())&&l2.getlength()==l4.getlength()) { 549 return true; 550 } 551 else 552 return false; 553 } 554 public boolean Isrhombus() { 555 Line l1 = new Line(p1,p2); 556 Line l2 = new Line(p2,p3); 557 Line l3 = new Line(p3,p4); 558 Line l4 = new Line(p4,p1); 559 if(l1.getlength()==l2.getlength()&&l2.getlength()==l3.getlength()&&l3.getlength()==l4.getlength()) { 560 return true; 561 } 562 else 563 return false; 564 } 565 public boolean IsRectangle() { 566 if(p1.getDistance(p3)==p2.getDistance(p4)) 567 return true; 568 else 569 return false; 570 } 571 572 public Point[] points() { 573 if(p1.equals(p2)) { 574 Point[] p = {p3,p4,p1}; 575 if((p3.getDistance(p4)+p3.getDistance(p1)>p4.getDistance(p1))&&(p3.getDistance(p4)-p3.getDistance(p1)<p4.getDistance(p1))) 576 return p; 577 else 578 return null; 579 } 580 else if(p1.equals(p3)) { 581 Point[] p = {p2,p4,p1}; 582 if((p2.getDistance(p4)+p2.getDistance(p1)>p4.getDistance(p1))&&(p2.getDistance(p4)-p2.getDistance(p1)<p4.getDistance(p1))) 583 return p; 584 else 585 return null; 586 } 587 else if(p1.equals(p4)) { 588 Point[] p = {p2,p3,p1}; 589 if((p2.getDistance(p3)+p2.getDistance(p1)>p3.getDistance(p1))&&(p2.getDistance(p1)-p2.getDistance(p1)<p3.getDistance(p1))) 590 return p; 591 else 592 return null; 593 } 594 else if(p2.equals(p3)) { 595 Point[] p = {p1,p4,p2}; 596 if((p1.getDistance(p4)+p1.getDistance(p2)>p4.getDistance(p2))&&(p1.getDistance(p4)-p1.getDistance(p2)<p4.getDistance(p2))) 597 return p; 598 else 599 return null; 600 } 601 else if(p2.equals(p4)) { 602 Point[] p = {p1,p3,p2}; 603 if((p1.getDistance(p3)+p1.getDistance(p2)>p3.getDistance(p2))&&(p1.getDistance(p3)-p1.getDistance(p2)<p3.getDistance(p2))) 604 return p; 605 else 606 return null; 607 } 608 else if(p3.equals(p4)){ 609 Point[] p = {p1,p2,p3}; 610 if((p1.getDistance(p2)+p1.getDistance(p3)>p2.getDistance(p3))&&(p1.getDistance(p2)-p1.getDistance(p3)<p2.getDistance(p3))) 611 return p; 612 else 613 return null; 614 } 615 else 616 return null; 617 } 618 public boolean judgeLineCoincide(Line l) { 619 Line l1 = new Line(p1,p2); 620 Line l2 = new Line(p2,p3); 621 Line l3 = new Line(p3,p4); 622 Line l4 = new Line(p4,p1); 623 624 if(l.isCoincide(l1)||l.isCoincide(l2)||l.isCoincide(l3)||l.isCoincide(l4)) { 625 return true; 626 } 627 else 628 return false; 629 } 630 public int isInside(Point p) { 631 if (this.isOnTheEdge(p)) { 632 return 0; 633 } 634 if (isVertex(p)) { 635 return 0; 636 } 637 Triangle a =new Triangle(p, p1, p2); 638 Triangle b =new Triangle(p, p2, p3); 639 Triangle c =new Triangle(p, p3, p4); 640 Triangle d =new Triangle(p, p1, p4); 641 642 if(Math.abs(this.getArea() - a.getArea()-b.getArea()-c.getArea()-d.getArea() )< 1E-10) { 643 return 1; 644 } 645 if(this.getArea() < a.getArea()+b.getArea()+c.getArea()+d.getArea()) { 646 return -1; 647 } 648 return 0; 649 } 650 private boolean isVertex(Point p) { 651 652 return p.equals(p1) || p.equals(p2) || p.equals(p3)|| p.equals(p4); 653 } 654 private boolean isOnTheEdge(Point p) { 655 656 Line l1 = new Line(p1,p2); 657 Line l2 = new Line(p2,p3); 658 Line l3 = new Line(p3,p4); 659 Line l4 = new Line(p4,p1); 660 661 if(l1.isBetween(p)||l2.isBetween(p)||l3.isBetween(p)||l4.isBetween(p)) { 662 return true; 663 } 664 else 665 return false; 666 } 667 } 668 class Triangle { 669 private Point x; 670 private Point y; 671 private Point z; 672 673 public Triangle(Point x, Point y, Point z) { 674 this.x = x; 675 this.y = y; 676 this.z = z; 677 if (!this.isTriangle()) { 678 System.out.println("data error"); 679 System.exit(0); 680 } 681 } 682 private boolean isTriangle() { 683 if((x.getDistance(y)+x.getDistance(z)>y.getDistance(z))&&(x.getDistance(y)-x.getDistance(z)<y.getDistance(z))) 684 return true; 685 else 686 return false; 687 } 688 public Point getMidpoint() { 689 Point p = new Point(); 690 p.setX((this.x.getX() + this.y.getX() + this.z.getX()) / 3); 691 p.setY((this.x.getY() + this.y.getY() + this.z.getY()) / 3); 692 return p; 693 } 694 public Line[] getSideline() { 695 Line line1 = new Line(x, y); 696 Line line2 = new Line(x, z); 697 Line line3 = new Line(y, z); 698 699 Line[] lines = { line1, line2, line3 }; 700 return lines; 701 } 702 public double getArea() { 703 double s = (x.getDistance(y) + y.getDistance(z) + z.getDistance(x))/2.0; 704 return Math.sqrt(s * (s - x.getDistance(y)) * (s - y.getDistance(z)) * (s - z.getDistance(x))); 705 } 706 public double getPerimeter() { 707 return x.getDistance(y) + y.getDistance(z) + z.getDistance(x); 708 } 709 710 public boolean isVertex(Point p) { 711 return p.equals(x) || p.equals(y) || p.equals(z); 712 } 713 714 public int isInside(Point p) { 715 if (this.isOnTheEdge(p)) { 716 return 0; 717 } 718 if (isVertex(p)) { 719 return 0; 720 } 721 Triangle t1 = new Triangle(x,y,p); 722 Triangle t2 = new Triangle(p,y,z); 723 Triangle t3 = new Triangle(x,p,z); 724 Triangle t4 = new Triangle(x,y,z); 725 if((float)(t1.getArea()+t2.getArea()+t3.getArea())==(float)(t4.getArea())) { 726 return 1; 727 } 728 else { 729 return -1; 730 } 731 } 732 public ArrayList<Point> getIntersections(Line l) { 733 ArrayList<Point> p = new ArrayList<Point> (); 734 Line a = new Line(x,y); 735 Line b = new Line(y,z); 736 Line c = new Line(x,z); 737 if(l.getIntersection(a).equals(l.getIntersection(b))&&l.getIntersection(a)!=null){ 738 p.add(l.getIntersection(a)); 739 if(l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))) 740 p.add(l.getIntersection(c)); 741 return p; 742 } 743 else if(l.getIntersection(b).equals(l.getIntersection(c))&&l.getIntersection(b)!=null) { 744 p.add(l.getIntersection(b)); 745 if(l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))) { 746 p.add(l.getIntersection(a)); 747 } 748 return p; 749 }else if(l.getIntersection(c).equals(l.getIntersection(a))&&l.getIntersection(c)!=null) { 750 p.add(l.getIntersection(c)); 751 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 752 p.add(l.getIntersection(b)); 753 } 754 return p; 755 } 756 757 if(l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))) { 758 p.add(l.getIntersection(a)); 759 } 760 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 761 p.add(l.getIntersection(b)); 762 } 763 if(l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))) 764 p.add(l.getIntersection(c)); 765 return p; 766 } 767 public double[] calArea(Point p1, Point p2) { 768 Line L = new Line(p1,p2); 769 Line a = new Line(y,z); 770 Line b = new Line(x,z); 771 Line c = new Line(x,y); 772 Triangle t; 773 Tetragon tet; 774 775 if((L.getIntersection(a)!=null&&L.getIntersection(b)!=null)) { 776 if((L.getIntersection(a).equals(p1)&&L.getIntersection(b).equals(p2))){ 777 t = new Triangle(z,p1,p2); 778 tet = new Tetragon(p1,p2,x,y); 779 } 780 else { 781 t = new Triangle(z,p1,p2); 782 tet = new Tetragon(p1,p2,y,x); 783 } 784 } 785 else if((L.getIntersection(a)!=null&&L.getIntersection(c)!=null)) { 786 if((L.getIntersection(a).equals(p1)&&L.getIntersection(c).equals(p2))){ 787 t = new Triangle(y,p1,p2); 788 tet = new Tetragon(p1,p2,x,z); 789 } 790 else { 791 t = new Triangle(y,p1,p2); 792 tet = new Tetragon(p1,p2,z,x); 793 } 794 } 795 else { 796 if((L.getIntersection(b).equals(p1)&&L.getIntersection(c).equals(p2))){ 797 t = new Triangle(x,p1,p2); 798 tet = new Tetragon(p1,p2,y,z); 799 } 800 else { 801 t = new Triangle(x,p1,p2); 802 tet = new Tetragon(p1,p2,z,y); 803 } 804 } 805 double[] area = {t.getArea(),tet.getArea()}; 806 if(area[0]>area[1]) { 807 double m=0; 808 m=area[0]; 809 area[0]=area[1]; 810 area[1]=m; 811 } 812 return area; 813 } 814 public double[] calTriangleArea(Point p1, Point p2) { 815 Triangle t1; 816 Triangle t2; 817 if(p1.equals(x)) { 818 t1 = new Triangle(p1,p2,y); 819 t2 = new Triangle(p1,p2,z); 820 } 821 else if(p1.equals(y )) { 822 t1 = new Triangle(p1,p2,x); 823 t2 = new Triangle(p1,p2,z); 824 } 825 else { 826 t1 = new Triangle(p1,p2,x); 827 t2 = new Triangle(p1,p2,y); 828 } 829 double[] area = {t1.getArea(),t2.getArea()}; 830 if(area[0]>area[1]) { 831 double m=0; 832 m=area[0]; 833 area[0]=area[1]; 834 area[1]=m; 835 } 836 return area; 837 } 838 public double calAreaDiffrence(Triangle t1) { 839 double area = t1.getArea(); 840 area = getArea() - area; 841 return area; 842 } 843 public boolean judgeLineCoincide(Line l) { 844 Line l1 = new Line(x,y); 845 Line l2 = new Line(y,z); 846 Line l3 = new Line(z,x); 847 848 if(l.isCoincide(l1)||l.isCoincide(l2)||l.isCoincide(l3)) { 849 return true; 850 } 851 else 852 return false; 853 } 854 public boolean isOnTheEdge(Point p) { 855 Line l1 = new Line(x,y); 856 Line l2 = new Line(y,z); 857 Line l3 = new Line(z,x); 858 859 if(l1.isBetween(p)||l2.isBetween(p)||l3.isBetween(p)) { 860 return true; 861 } 862 else 863 return false; 864 } 865 866 public Point getX() { 867 return x; 868 } 869 public void setX(Point x) { 870 this.x = x; 871 } 872 public Point getY() { 873 return y; 874 } 875 public void setY(Point y) { 876 this.y = y; 877 } 878 public Point getZ() { 879 return z; 880 } 881 public void setZ(Point z) { 882 this.z = z; 883 } 884 }
下面讲讲选项五,输入六个点的坐标,然后前两个点构成一条直线,再判断后四个点能否构成四边形,若构成四边形则用找到直线与四边形的交点,若交点为两个则输出分割成两部分的面积,若交点为1或0则直接输出。若构不成四边形则判断四个点能否构成三角形,能则找出面积最大的那个,再找交点、输出面积。既不是四边形也不是三角形则输出构不成多边形。详细的我将在后面的题目仔细介绍。
3、第三题没什么 难点,只要按题目要求输出特定的东西。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import java.util.Scanner; public class BankBusiness { public static String bankName= "中国银行" ; private String name; private String passward; private double ballence; Scanner input = new Scanner(System. in ); protected BankBusiness(String name, String passward) { super (); this .name = name; this .passward = passward; this .ballence = 0; } public static void welcome() { System.out.println(bankName+ "欢迎您的到来!" ); } public static void welcomeNext() { System.out.println( "请收好您的证件和物品,欢迎您下次光临!" ); } public void deposit() { String s =input.nextLine(); String[] tokens = s.split( "[\\s]" ); String PassWard=tokens[0]; if (judge(PassWard)) { ballence=Double.parseDouble(tokens[1]); System.out.println( "您的余额有" +ballence+ "元。" ); } else System.out.println( "您的密码错误!" ); } public void withdraw() { double money; String s =input.nextLine(); String[] tokens = s.split( "[\\s]" ); String PassWard=tokens[0]; money=Double.parseDouble(tokens[1]); if (judge(PassWard)) { handle(money); } else System.out.println( "您的密码错误!" ); } public boolean judge(String PassWard) { if (passward.equals(PassWard)) return true ; else return false ; } public void handle(double money) { if (money<=ballence) { ballence-=money; System.out.println( "请取走钞票,您的余额还有" +ballence+ "元。" ); } else System.out.println( "您的余额不足!" ); } } |
(2)期中考试题目设计与分析
期中考试的试题考察的就是类的设计和继承和多态的运用,难度不大,只需按照题目要求来即可,唯一可能算难点的便是父类的创建,里面需要用到许多的容器函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.util.ArrayList; public class GeometryObject { ArrayList<Element> element = new ArrayList<Element>(); public void add(Element e) { element.add(e); } public void remove(int i) { element.remove(i); } public ArrayList<Element> getList() { return element; } } |
(3)第五次作业设计与分析
这一题也是建立在前几次作业的基础上考察五边形的相关问题。
选项一和选项二比较简单分别需要完成对五边形的判断和凹凸性的判断,计算方面就是计算五边形面积和周长
五边形的判断:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public boolean IsPentagon() { Line l1 = new Line(p1,p2); Line l2 = new Line(p2,p3); Line l3 = new Line(p3,p4); Line l4 = new Line(p4,p5); Line l5 = new Line(p5,p1); if (l1.isParallel(l2)||l2.isParallel(l3)||l3.isParallel(l4)||l4.isParallel(l5)||l5.isParallel(l1)) return false ; if ((l1.getIntersection(l3) != null && l1.isBetween(l1.getIntersection(l3)) && l3.isBetween(l1.getIntersection(l3))) || (l1.getIntersection(l4) != null && l1.isBetween(l1.getIntersection(l4)) && l4.isBetween(l1.getIntersection(l4))) || (l2.getIntersection(l4) != null && l2.isBetween(l2.getIntersection(l4)) && l4.isBetween(l2.getIntersection(l4))) || (l2.getIntersection(l5) != null && l2.isBetween(l2.getIntersection(l5)) && l5.isBetween(l2.getIntersection(l5))) || (l3.getIntersection(l5) != null && l3.isBetween(l3.getIntersection(l5)) && l5.isBetween(l3.getIntersection(l5))) || l1.isBetween(p4) || l2.isBetween(p5) || l3.isBetween(p1) || l4.isBetween(p2) || l5.isBetween(p3)){ //相隔一条线的两条线没有交点 return false ; } else { return true ; } } |
凹凸性的判断就是运用向量叉乘,如果是凸的,那必然任意两条线的方向向量叉乘的符号必然相同,并且我们可以用点来代表向量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public boolean IsTuPentagon() { Point a = new Point(p2.getX()-p1.getX(), p2.getY()-p1.getY()); Point b = new Point(p3.getX()-p2.getX(), p3.getY()-p2.getY()); Point c = new Point(p4.getX()-p3.getX(), p4.getY()-p3.getY()); Point d = new Point(p5.getX()-p4.getX(), p5.getY()-p4.getY()); Point e = new Point(p1.getX()-p5.getX(), p1.getY()-p5.getY()); double[] crossProduct = new double[5]; crossProduct[0]=a.getX()*b.getY()-a.getY()*b.getX(); crossProduct[1]=b.getX()*c.getY()-b.getY()*c.getX(); crossProduct[2]=c.getX()*d.getY()-c.getY()*d.getX(); crossProduct[3]=d.getX()*e.getY()-d.getY()*e.getX(); crossProduct[4]=e.getX()*a.getY()-e.getY()*a.getX(); if (crossProduct[0]*crossProduct[1]<0||crossProduct[0]*crossProduct[2]<0||crossProduct[0]*crossProduct[3]<0) { return false ; } else return true ; } |
面积和周长比较简单,面积只需要将五边形切割成三角形或者四边形即可。
选项三:
题目:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
首先我们的思路就是先判断它是否为五边形,不是五边形则再判断是否为四边形,还不是那再判断是否为三角形,是对应的图形则再去找交点,并且判断焦点数量,再输出判断好大小的两部分的面积。
找线与五边形的焦点:

1 public ArrayList<Point> getIntersections(Line l){ 2 ArrayList<Point> p = new ArrayList<Point> (); 3 Line a = new Line(p1,p2); 4 Line b = new Line(p2,p3); 5 Line c = new Line(p3,p4); 6 Line d = new Line(p4,p5); 7 Line e = new Line(p5,p1); 8 9 if(l.getIntersection(e)!=null&&l.getIntersection(a)!=null&&l.getIntersection(e).equals(l.getIntersection(a))) { 10 p.add(l.getIntersection(e)); 11 if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) 12 p.add(l.getIntersection(b)); 13 else if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d))) 14 p.add(l.getIntersection(d)); 15 else if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c)))) 16 p.add(l.getIntersection(c)); 17 else if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b)))) 18 p.add(l.getIntersection(b)); 19 else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d)))) 20 p.add(l.getIntersection(d)); 21 return p; 22 } 23 else if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b))) { 24 p.add(l.getIntersection(a)); 25 if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d))) 26 p.add(l.getIntersection(c)); 27 else if(l.getIntersection(d)!=null&&l.getIntersection(e)!=null&&l.getIntersection(d).equals(l.getIntersection(e))) 28 p.add(l.getIntersection(d)); 29 else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d)))) 30 p.add(l.getIntersection(d)); 31 else if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c)))) 32 p.add(l.getIntersection(c)); 33 else if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e)))) 34 p.add(l.getIntersection(e)); 35 return p; 36 } 37 else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) { 38 p.add(l.getIntersection(b)); 39 if(l.getIntersection(e)!=null&&l.getIntersection(d)!=null&&l.getIntersection(e).equals(l.getIntersection(d))) 40 p.add(l.getIntersection(d)); 41 else if(l.getIntersection(a)!=null&&l.getIntersection(e)!=null&&l.getIntersection(a).equals(l.getIntersection(e))) 42 p.add(l.getIntersection(a)); 43 else if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e)))) 44 p.add(l.getIntersection(e)); 45 else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a)))) 46 p.add(l.getIntersection(a)); 47 else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d)))) 48 p.add(l.getIntersection(d)); 49 return p; 50 } 51 else if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d))) { 52 p.add(l.getIntersection(c)); 53 if(l.getIntersection(e)!=null&&l.getIntersection(a)!=null&&l.getIntersection(e).equals(l.getIntersection(a))) 54 p.add(l.getIntersection(e)); 55 else if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b))) 56 p.add(l.getIntersection(a)); 57 else if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e)))) 58 p.add(l.getIntersection(e)); 59 else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a)))) 60 p.add(l.getIntersection(a)); 61 else if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b)))) 62 p.add(l.getIntersection(b)); 63 return p; 64 } 65 else if(l.getIntersection(e)!=null&&l.getIntersection(d)!=null&&l.getIntersection(e).equals(l.getIntersection(d))) { 66 p.add(l.getIntersection(d)); 67 if(l.getIntersection(b)!=null&&l.getIntersection(a)!=null&&l.getIntersection(b).equals(l.getIntersection(a))) 68 p.add(l.getIntersection(b)); 69 else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) 70 p.add(l.getIntersection(c)); 71 else if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c)))) 72 p.add(l.getIntersection(c)); 73 else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a)))) 74 p.add(l.getIntersection(a)); 75 else if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b)))) 76 p.add(l.getIntersection(b)); 77 return p; 78 } 79 80 if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a)))) 81 p.add(l.getIntersection(a)); 82 if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b)))) 83 p.add(l.getIntersection(b)); 84 if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c)))) 85 p.add(l.getIntersection(c)); 86 if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d)))) 87 p.add(l.getIntersection(d)); 88 if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e)))) 89 p.add(l.getIntersection(e)); 90 91 return p; 92 93 }
将焦点传入,计算两部分的面积并传出:

1 public double[] calPentagonArea(Point x, Point y) { 2 Line a = new Line(p1,p2); 3 Line b = new Line(p2,p3); 4 Line c = new Line(p3,p4); 5 Line d = new Line(p4,p5); 6 Line e = new Line(p5,p1); 7 8 double[] area = new double[2]; 9 10 if(x.equals(p1)) { 11 if(y.equals(p3)) { 12 Triangle t1 = new Triangle(x,y,p2); 13 Tetragon t2 = new Tetragon(x,y,p4,p5); 14 area[0] = t1.getArea(); 15 area[1] = t2.getArea(); 16 } 17 else if(y.equals(p4)) { 18 Triangle t1 = new Triangle(x,y,p5); 19 Tetragon t2 = new Tetragon(x,y,p3,p2); 20 area[0] = t1.getArea(); 21 area[1] = t2.getArea(); 22 } 23 else if(b.isBetween(y)) { 24 Triangle t1 = new Triangle(x,y,p2); 25 Pentagon t2 = new Pentagon(x,y,p3,p4,p5); 26 area[0] = t1.getArea(); 27 area[1] = t2.getArea(); 28 } 29 else if(c.isBetween(y)) { 30 Tetragon t1 = new Tetragon(x,y,p3,p2); 31 Tetragon t2 = new Tetragon(x,y,p4,p5); 32 area[0] = t1.getArea(); 33 area[1] = t2.getArea(); 34 } 35 else { 36 Triangle t1 = new Triangle(x,y,p5); 37 Pentagon t2 = new Pentagon(x,y,p4,p3,p2); 38 area[0] = t1.getArea(); 39 area[1] = t2.getArea(); 40 } 41 } 42 else if(x.equals(p2)) { 43 if(y.equals(p4)) { 44 Triangle t1 = new Triangle(x,y,p3); 45 Tetragon t2 = new Tetragon(x,y,p5,p1); 46 area[0] = t1.getArea(); 47 area[1] = t2.getArea(); 48 } 49 else if(y.equals(p5)) { 50 Triangle t1 = new Triangle(x,y,p1); 51 Tetragon t2 = new Tetragon(x,y,p4,p3); 52 area[0] = t1.getArea(); 53 area[1] = t2.getArea(); 54 } 55 else if(c.isBetween(y)) { 56 Triangle t1 = new Triangle(x,y,p3); 57 Pentagon t2 = new Pentagon(x,y,p4,p5,p1); 58 area[0] = t1.getArea(); 59 area[1] = t2.getArea(); 60 } 61 else if(d.isBetween(y)) { 62 Tetragon t1 = new Tetragon(x,y,p4,p3); 63 Tetragon t2 = new Tetragon(x,y,p5,p1); 64 area[0] = t1.getArea(); 65 area[1] = t2.getArea(); 66 } 67 else { 68 Triangle t1 = new Triangle(x,y,p1); 69 Pentagon t2 = new Pentagon(x,y,p5,p4,p3); 70 area[0] = t1.getArea(); 71 area[1] = t2.getArea(); 72 } 73 } 74 else if(x.equals(p3)) { 75 if(y.equals(p5)) { 76 Triangle t1 = new Triangle(x,y,p4); 77 Tetragon t2 = new Tetragon(x,y,p1,p2); 78 area[0] = t1.getArea(); 79 area[1] = t2.getArea(); 80 } 81 else if(y.equals(p1)) { 82 Triangle t1 = new Triangle(x,y,p2); 83 Tetragon t2 = new Tetragon(x,y,p5,p4); 84 area[0] = t1.getArea(); 85 area[1] = t2.getArea(); 86 } 87 else if(d.isBetween(y)) { 88 Triangle t1 = new Triangle(x,y,p4); 89 Pentagon t2 = new Pentagon(x,y,p5,p1,p2); 90 area[0] = t1.getArea(); 91 area[1] = t2.getArea(); 92 } 93 else if(e.isBetween(y)) { 94 Tetragon t1 = new Tetragon(x,y,p5,p4); 95 Tetragon t2 = new Tetragon(x,y,p1,p2); 96 area[0] = t1.getArea(); 97 area[1] = t2.getArea(); 98 } 99 else { 100 Triangle t1 = new Triangle(x,y,p2); 101 Pentagon t2 = new Pentagon(x,y,p1,p5,p4); 102 area[0] = t1.getArea(); 103 area[1] = t2.getArea(); 104 } 105 } 106 else if(x.equals(p4)) { 107 if(y.equals(p1)) { 108 Triangle t1 = new Triangle(x,y,p5); 109 Tetragon t2 = new Tetragon(x,y,p2,p3); 110 area[0] = t1.getArea(); 111 area[1] = t2.getArea(); 112 } 113 else if(y.equals(p2)) { 114 Triangle t1 = new Triangle(x,y,p3); 115 Tetragon t2 = new Tetragon(x,y,p1,p5); 116 area[0] = t1.getArea(); 117 area[1] = t2.getArea(); 118 } 119 else if(e.isBetween(y)) { 120 Triangle t1 = new Triangle(x,y,p5); 121 Pentagon t2 = new Pentagon(x,y,p1,p2,p3); 122 area[0] = t1.getArea(); 123 area[1] = t2.getArea(); 124 } 125 else if(a.isBetween(y)) { 126 Tetragon t1 = new Tetragon(x,y,p1,p5); 127 Tetragon t2 = new Tetragon(x,y,p2,p3); 128 area[0] = t1.getArea(); 129 area[1] = t2.getArea(); 130 } 131 else { 132 Triangle t1 = new Triangle(x,y,p3); 133 Pentagon t2 = new Pentagon(x,y,p2,p1,p5); 134 area[0] = t1.getArea(); 135 area[1] = t2.getArea(); 136 } 137 } 138 else if(x.equals(p5)) { 139 if(y.equals(p2)) { 140 Triangle t1 = new Triangle(x,y,p1); 141 Tetragon t2 = new Tetragon(x,y,p3,p4); 142 area[0] = t1.getArea(); 143 area[1] = t2.getArea(); 144 } 145 else if(y.equals(p3)) { 146 Triangle t1 = new Triangle(x,y,p4); 147 Tetragon t2 = new Tetragon(x,y,p2,p1); 148 area[0] = t1.getArea(); 149 area[1] = t2.getArea(); 150 } 151 else if(a.isBetween(y)) { 152 Triangle t1 = new Triangle(x,y,p1); 153 Pentagon t2 = new Pentagon(x,y,p2,p3,p4); 154 area[0] = t1.getArea(); 155 area[1] = t2.getArea(); 156 } 157 else if(b.isBetween(y)) { 158 Tetragon t1 = new Tetragon(x,y,p2,p1); 159 Tetragon t2 = new Tetragon(x,y,p3,p4); 160 area[0] = t1.getArea(); 161 area[1] = t2.getArea(); 162 } 163 else { 164 Triangle t1 = new Triangle(x,y,p4); 165 Pentagon t2 = new Pentagon(x,y,p3,p2,p1); 166 area[0] = t1.getArea(); 167 area[1] = t2.getArea(); 168 } 169 } 170 else if((a.isBetween(x)&&e.isBetween(y))||(a.isBetween(y)&&e.isBetween(x))) { 171 if(a.isBetween(y)&&e.isBetween(x)) { 172 Triangle t1 = new Triangle(x,y,p1); 173 Tetragon t2 = new Tetragon(x,y,p2,p5); 174 Tetragon t3 = new Tetragon(p2,p3,p4,p5); 175 area[0] = t1.getArea(); 176 area[1] = t2.getArea()+t3.getArea(); 177 } 178 else { 179 Triangle t1 = new Triangle(x,y,p1); 180 Tetragon t2 = new Tetragon(x,y,p5,p2); 181 Tetragon t3 = new Tetragon(p2,p3,p4,p5); 182 area[0] = t1.getArea(); 183 area[1] = t2.getArea()+t3.getArea(); 184 } 185 } 186 else if((a.isBetween(x)&&b.isBetween(y))||(a.isBetween(y)&&b.isBetween(x))) { 187 if(a.isBetween(x)&&b.isBetween(y)) { 188 Triangle t1 = new Triangle(x,y,p2); 189 Tetragon t2 = new Tetragon(x,y,p3,p1); 190 Tetragon t3 = new Tetragon(p3,p4,p5,p1); 191 area[0] = t1.getArea(); 192 area[1] = t2.getArea()+t3.getArea(); 193 } 194 else { 195 Triangle t1 = new Triangle(x,y,p1); 196 Tetragon t2 = new Tetragon(x,y,p1,p3); 197 Tetragon t3 = new Tetragon(p3,p4,p5,p1); 198 area[0] = t1.getArea(); 199 area[1] = t2.getArea()+t3.getArea(); 200 } 201 } 202 else if((b.isBetween(x)&&c.isBetween(y))||(b.isBetween(y)&&c.isBetween(x))) { 203 if(b.isBetween(x)&&c.isBetween(y)) { 204 Triangle t1 = new Triangle(x,y,p3); 205 Tetragon t2 = new Tetragon(x,y,p4,p2); 206 Tetragon t3 = new Tetragon(p4,p5,p1,p2); 207 area[0] = t1.getArea(); 208 area[1] = t2.getArea()+t3.getArea(); 209 } 210 else { 211 Triangle t1 = new Triangle(x,y,p3); 212 Tetragon t2 = new Tetragon(x,y,p2,p4); 213 Tetragon t3 = new Tetragon(p4,p5,p1,p2); 214 area[0] = t1.getArea(); 215 area[1] = t2.getArea()+t3.getArea(); 216 } 217 } 218 else if((c.isBetween(x)&&d.isBetween(y))||(c.isBetween(y)&&d.isBetween(x))) { 219 if(c.isBetween(x)&&d.isBetween(y)) { 220 Triangle t1 = new Triangle(x,y,p4); 221 Tetragon t2 = new Tetragon(x,y,p5,p3); 222 Tetragon t3 = new Tetragon(p5,p1,p2,p3); 223 area[0] = t1.getArea(); 224 area[1] = t2.getArea()+t3.getArea(); 225 } 226 else { 227 Triangle t1 = new Triangle(x,y,p4); 228 Tetragon t2 = new Tetragon(x,y,p3,p5); 229 Tetragon t3 = new Tetragon(p5,p1,p2,p3); 230 area[0] = t1.getArea(); 231 area[1] = t2.getArea()+t3.getArea(); 232 } 233 } 234 else { 235 if(d.isBetween(x)&&e.isBetween(y)) { 236 Triangle t1 = new Triangle(x,y,p5); 237 Tetragon t2 = new Tetragon(x,y,p1,p4); 238 Tetragon t3 = new Tetragon(p1,p2,p3,p4); 239 area[0] = t1.getArea(); 240 area[1] = t2.getArea()+t3.getArea(); 241 } 242 else { 243 Triangle t1 = new Triangle(x,y,p5); 244 Tetragon t2 = new Tetragon(x,y,p4,p1); 245 Tetragon t3 = new Tetragon(p1,p2,p3,p4); 246 area[0] = t1.getArea(); 247 area[1] = t2.getArea()+t3.getArea(); 248 } 249 } 250 251 if(area[0]>area[1]) { 252 double m=0; 253 m=area[0]; 254 area[0]=area[1]; 255 area[1]=m; 256 } 257 return area; 258 }
判断五个点是否构成四边形:

1 /* 判断p1\p2\p3\p4\P5五个点的坐标是否能构成一个四边形 */ 2 public Tetragon PointsisTetragon() { 3 Tetragon t1 = new Tetragon(p2,p3,p4,p5); 4 Tetragon t2 = new Tetragon(p1,p3,p4,p5); 5 Tetragon t3 = new Tetragon(p1,p2,p4,p5); 6 Tetragon t4 = new Tetragon(p1,p2,p3,p5); 7 Tetragon t5 = new Tetragon(p1,p2,p3,p4); 8 Line l1 = new Line(p2,p5); 9 Line l2 = new Line(p1,p3); 10 Line l3 = new Line(p2,p4); 11 Line l4 = new Line(p3,p5); 12 Line l5 = new Line(p1,p4); 13 14 if(p1.equals(p2)||p1.equals(p3)||p1.equals(p4)||p1.equals(p5)||l1.isBetween(p1)) 15 return t1; 16 else if(p2.equals(p1)||p2.equals(p3)||p2.equals(p4)||p2.equals(p5)||l2.isBetween(p2)) 17 return t2; 18 else if(p3.equals(p1)||p3.equals(p2)||p3.equals(p4)||p3.equals(p5)||l3.isBetween(p3)) 19 return t3; 20 else if(p4.equals(p1)||p4.equals(p2)||p4.equals(p3)||p4.equals(p5)||l4.isBetween(p4)) 21 return t4; 22 else if(p5.equals(p1)||p5.equals(p2)||p5.equals(p3)||p5.equals(p5)||l5.isBetween(p5)) 23 return t5; 24 else 25 return null; 26 }
找线与四边形的焦点:

1 // 获取直线l与四边形的交点,如果没有,数组为空。 2 public ArrayList<Point> getIntersections(Line l) { 3 ArrayList<Point> p = new ArrayList<Point> (); 4 Line a = new Line(p1,p2); 5 Line b = new Line(p2,p3); 6 Line c = new Line(p3,p4); 7 Line d = new Line(p4,p1); 8 if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b))){ 9 p.add(l.getIntersection(a)); 10 if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c)))) 11 p.add(l.getIntersection(c)); 12 else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d)))) 13 p.add(l.getIntersection(d)); 14 else if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d))) 15 p.add(l.getIntersection(c)); 16 return p; 17 } 18 else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) { 19 p.add(l.getIntersection(b)); 20 if(l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))) { 21 p.add(l.getIntersection(a)); 22 } 23 else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d)))) 24 p.add(l.getIntersection(d)); 25 else if(l.getIntersection(a)!=null&&l.getIntersection(d)!=null&&l.getIntersection(a).equals(l.getIntersection(d))) 26 p.add(l.getIntersection(a)); 27 return p; 28 }else if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d))) { 29 p.add(l.getIntersection(c)); 30 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 31 p.add(l.getIntersection(b)); 32 } 33 else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a)))) 34 p.add(l.getIntersection(a)); 35 else if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b))) 36 p.add(l.getIntersection(a)); 37 return p; 38 } 39 else if(l.getIntersection(d)!=null&&l.getIntersection(a)!=null&&l.getIntersection(d).equals(l.getIntersection(a))) { 40 p.add(l.getIntersection(c)); 41 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 42 p.add(l.getIntersection(b)); 43 } 44 else if((l.getIntersection(c)!=null&&a.isBetween(l.getIntersection(c)))) 45 p.add(l.getIntersection(c)); 46 else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) 47 p.add(l.getIntersection(b)); 48 return p; 49 } 50 51 if(l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))) { 52 p.add(l.getIntersection(a)); 53 } 54 55 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 56 p.add(l.getIntersection(b)); 57 } 58 59 if(l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))) { 60 p.add(l.getIntersection(c)); 61 } 62 63 if(l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d))) { 64 p.add(l.getIntersection(d)); 65 } 66 67 return p; 68 }
将焦点传入,输出将四边形分割成两部分的面积并排序:

1 public double[] calTetragonArea(Point x, Point y) { 2 Line a = new Line(p1,p2); 3 Line b = new Line(p2,p3); 4 Line c = new Line(p3,p4); 5 Line d = new Line(p4,p1); 6 double[] area = new double[2]; 7 8 if(p1.equals(x)) { 9 if(b.isBetween(y)) { 10 Tetragon t1 = new Tetragon(x,y,p3,p4); 11 Triangle t2 = new Triangle(p2,x,y); 12 area[0] = t1.getArea(); 13 area[1] = t2.getArea(); 14 } 15 else if(c.isBetween(y)) { 16 Tetragon t1 = new Tetragon(x,y,p3,p2); 17 Triangle t2 = new Triangle(p4,x,y); 18 area[0] = t1.getArea(); 19 area[1] = t2.getArea(); 20 } 21 else { 22 Triangle t1 = new Triangle(p1,p2,p3); 23 Triangle t2 = new Triangle(p1,p3,p4); 24 area[0] = t1.getArea(); 25 area[1] = t2.getArea(); 26 } 27 } 28 else if(p2.equals(x)) { 29 if(c.isBetween(y)) { 30 Tetragon t1 = new Tetragon(x,y,p4,p1); 31 Triangle t2 = new Triangle(x,y,p3); 32 area[0] = t1.getArea(); 33 area[1] = t2.getArea(); 34 } 35 else if(d.isBetween(y)) { 36 Tetragon t1 = new Tetragon(x,y,p4,p3); 37 Triangle t2 = new Triangle(x,y,p1); 38 area[0] = t1.getArea(); 39 area[1] = t2.getArea(); 40 } 41 else { 42 Triangle t1 = new Triangle(p1,p2,p4); 43 Triangle t2 = new Triangle(p3,p2,p4); 44 area[0] = t1.getArea(); 45 area[1] = t2.getArea(); 46 } 47 } 48 else if(p3.equals(x)) { 49 if(a.isBetween(y)) { 50 Tetragon t1 = new Tetragon(x,y,p1,p4); 51 Triangle t2 = new Triangle(x,y,p2); 52 area[0] = t1.getArea(); 53 area[1] = t2.getArea(); 54 } 55 else if(d.isBetween(y)) { 56 Tetragon t1 = new Tetragon(x,y,p1,p2); 57 Triangle t2 = new Triangle(x,y,p4); 58 area[0] = t1.getArea(); 59 area[1] = t2.getArea(); 60 } 61 else { 62 Triangle t1 = new Triangle(p1,p2,p3); 63 Triangle t2 = new Triangle(p1,p3,p4); 64 area[0] = t1.getArea(); 65 area[1] = t2.getArea(); 66 } 67 } 68 else if(p4.equals(x)) { 69 if(a.isBetween(y)) { 70 Tetragon t1 = new Tetragon(x,y,p2,p3); 71 Triangle t2 = new Triangle(p1,x,y); 72 area[0] = t1.getArea(); 73 area[1] = t2.getArea(); 74 } 75 else if(b.isBetween(y)) { 76 Tetragon t1 = new Tetragon(p1,p2,y,x); 77 Triangle t2 = new Triangle(p3,x,y); 78 area[0] = t1.getArea(); 79 area[1] = t2.getArea(); 80 } 81 else { 82 Triangle t1 = new Triangle(p1,p2,p4); 83 Triangle t2 = new Triangle(p3,p2,p4); 84 area[0] = t1.getArea(); 85 area[1] = t2.getArea(); 86 } 87 }else if(a.isBetween(x)) { 88 if(b.isBetween(y)) { 89 Triangle t1 = new Triangle(p2,x,y); 90 Pentagon t2 = new Pentagon(x,y,p3,p4,p1); 91 area[0] = t1.getArea(); 92 area[1] = t2.getArea(); 93 } 94 else if(c.isBetween(y)) { 95 Tetragon t1 = new Tetragon(x,y,p4,p1); 96 Tetragon t2 = new Tetragon(x,y,p3,p2); 97 area[0] = t1.getArea(); 98 area[1] = t2.getArea(); 99 } 100 else { 101 Triangle t1 = new Triangle(p1,x,y); 102 Pentagon t2 = new Pentagon(x,y,p4,p3,p2); 103 area[0] = t1.getArea(); 104 area[1] = t2.getArea(); 105 } 106 }else if(b.isBetween(x)) { 107 if(a.isBetween(y)) { 108 Triangle t1 = new Triangle(p2,x,y); 109 Pentagon t2 = new Pentagon(x,y,p1,p4,p3); 110 area[0] = t1.getArea(); 111 area[1] = t2.getArea(); 112 } 113 else if(d.isBetween(y)) { 114 Tetragon t1 = new Tetragon(x,y,p4,p3); 115 Tetragon t2 = new Tetragon(x,y,p1,p2); 116 area[0] = t1.getArea(); 117 area[1] = t2.getArea(); 118 } 119 else { 120 Triangle t1 = new Triangle(p3,x,y); 121 Pentagon t2 = new Pentagon(x,y,p4,p1,p2); 122 area[0] = t1.getArea(); 123 area[1] = t2.getArea(); 124 } 125 }else if(c.isBetween(x)) { 126 if(b.isBetween(y)) { 127 Triangle t1 = new Triangle(p3,x,y); 128 Pentagon t2 = new Pentagon(x,y,p2,p1,p4); 129 area[0] = t1.getArea(); 130 area[1] = t2.getArea(); 131 } 132 else if(a.isBetween(y)) { 133 Tetragon t1 = new Tetragon(x,y,p2,p3); 134 Tetragon t2 = new Tetragon(x,y,p1,p4); 135 area[0] = t1.getArea(); 136 area[1] = t2.getArea(); 137 } 138 else { 139 Triangle t1 = new Triangle(p4,x,y); 140 Pentagon t2 = new Pentagon(x,y,p1,p2,p3); 141 area[0] = t1.getArea(); 142 area[1] = t2.getArea(); 143 } 144 }else { 145 if(c.isBetween(y)) { 146 Triangle t1 = new Triangle(p4,x,y); 147 Pentagon t2 = new Pentagon(x,y,p3,p2,p1); 148 area[0] = t1.getArea(); 149 area[1] = t2.getArea(); 150 } 151 else if(b.isBetween(y)) { 152 Tetragon t1 = new Tetragon(x,y,p2,p1); 153 Tetragon t2 = new Tetragon(x,y,p3,p4); 154 area[0] = t1.getArea(); 155 area[1] = t2.getArea(); 156 } 157 else { 158 Triangle t1 = new Triangle(p1,x,y); 159 Pentagon t2 = new Pentagon(x,y,p2,p3,p4); 160 area[0] = t1.getArea(); 161 area[1] = t2.getArea(); 162 } 163 } 164 165 if(area[0]>area[1]) { 166 double m=0; 167 m=area[0]; 168 area[0]=area[1]; 169 area[1]=m; 170 } 171 return area; 172 }
判断四个点能否构成三角形:

1 /* 判断四个点点的坐标是否能构成一个三角形 */ 2 public Triangle PointsisTriangle() { 3 Line l1 = new Line(p1,p3); 4 Line l2 = new Line(p2,p4); 5 6 7 if(l1.getIntersection(l2).equals(p1)&&l2.isBetween(p1)) { 8 Triangle t = new Triangle(p2,p3,p4); 9 return t; 10 } 11 else if(l1.getIntersection(l2).equals(p2)&&l1.isBetween(p2)) { 12 Triangle t = new Triangle(p1,p3,p4); 13 return t; 14 } 15 else if(l1.getIntersection(l2).equals(p3)&&l2.isBetween(p3)) { 16 Triangle t = new Triangle(p1,p2,p4); 17 return t; 18 } 19 else if(l1.getIntersection(l2).equals(p4)&&l1.isBetween(p4)) { 20 Triangle t = new Triangle(p1,p2,p3); 21 return t; 22 } 23 else 24 return null; 25 26 27 }
找线与三角形的焦点:

1 // 获取直线l与三角形的交点,如果没有,数组为空。 2 public ArrayList<Point> getIntersections(Line l) { 3 ArrayList<Point> p = new ArrayList<Point> (); 4 Line a = new Line(x,y); 5 Line b = new Line(y,z); 6 Line c = new Line(x,z); 7 if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b))){ 8 p.add(l.getIntersection(a)); 9 if(l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))) 10 p.add(l.getIntersection(c)); 11 return p; 12 } 13 else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) { 14 p.add(l.getIntersection(b)); 15 if(l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))) { 16 p.add(l.getIntersection(a)); 17 } 18 return p; 19 }else if(l.getIntersection(c)!=null&&l.getIntersection(a)!=null&&l.getIntersection(c).equals(l.getIntersection(a))) { 20 p.add(l.getIntersection(c)); 21 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 22 p.add(l.getIntersection(b)); 23 } 24 return p; 25 } 26 27 if(l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))) { 28 p.add(l.getIntersection(a)); 29 } 30 if(l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))) { 31 p.add(l.getIntersection(b)); 32 } 33 if(l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))) 34 p.add(l.getIntersection(c)); 35 return p; 36 }
将焦点传入,计算将三角形分割成两部分的面积:

1 public double[] calTriangleArea(Point p1, Point p2) { 2 Triangle t1; 3 Triangle t2; 4 Triangle[] a = new Triangle[3]; 5 Tetragon[] b = new Tetragon[3]; 6 7 if(p1.equals(x)||p1.equals(y)||p1.equals(z)||p2.equals(x)||p2.equals(y)||p2.equals(z)) { 8 if(p1.equals(x)||p2.equals(x)) { 9 t1 = new Triangle(p1,p2,y); 10 t2 = new Triangle(p1,p2,z); 11 } 12 else if(p1.equals(y)||p2.equals(y)) { 13 t1 = new Triangle(p1,p2,x); 14 t2 = new Triangle(p1,p2,z); 15 } 16 else { 17 t1 = new Triangle(p1,p2,x); 18 t2 = new Triangle(p1,p2,y); 19 } 20 21 double[] area = {t1.getArea(),t2.getArea()}; 22 if(area[0]>area[1]) { 23 double m=0; 24 m=area[0]; 25 area[0]=area[1]; 26 area[1]=m; 27 } 28 return area; 29 } 30 else { 31 a[0] = new Triangle(p1,p2,x); 32 a[1] = new Triangle(p1,p2,y); 33 a[2] = new Triangle(p1,p2,z); 34 b[0] = new Tetragon(p1,p2,y,z); 35 b[1] = new Tetragon(p1,p2,x,z); 36 b[2] = new Tetragon(p1,p2,x,y); 37 38 if(this.getArea()-(a[0].getArea()+b[0].getArea())<0.00001) { 39 double[] area = {a[0].getArea(),b[0].getArea()}; 40 if(area[0]>area[1]) { 41 double m=0; 42 m=area[0]; 43 area[0]=area[1]; 44 area[1]=m; 45 } 46 return area; 47 } 48 else if(this.getArea()-(a[1].getArea()+b[1].getArea())<0.00001) { 49 double[] area = {a[1].getArea(),b[1].getArea()}; 50 if(area[0]>area[1]) { 51 double m=0; 52 m=area[0]; 53 area[0]=area[1]; 54 area[1]=m; 55 } 56 return area; 57 } 58 else { 59 double[] area = {a[2].getArea(),b[2].getArea()}; 60 if(area[0]>area[1]) { 61 double m=0; 62 m=area[0]; 63 area[0]=area[1]; 64 area[1]=m; 65 } 66 return area; 67 } 68 69 } 70 71 }
选项四:
任然需要用到选项三判断五个点构成什么图形的方法,然后在判断两图形的关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public Point[] points() { Line[] L = new Line[6]; L[0] = new Line(p1,p2); L[1] = new Line(p1,p3); L[2] = new Line(p1,p4); L[3] = new Line(p1,p5); L[4] = new Line(p2,p3); L[5] = new Line(p2,p4); L[6] = new Line(p2,p5); L[7] = new Line(p3,p4); L[8] = new Line(p3,p5); L[9] = new Line(p4,p5); if (L[6].isBetween(p1)||p1.equals(p2)||p1.equals(p5)) { Point[] p = {p2,p3,p4,p5}; return p; } if (L[1].isBetween(p2)||p2.equals(p1)||p2.equals(p3)) { Point[] p = {p1,p3,p4,p5}; return p; } if (L[5].isBetween(p3)||p3.equals(p2)||p3.equals(p4)) { Point[] p = {p1,p2,p4,p5}; return p; } if (L[2].isBetween(p5)||p5.equals(p4)||p5.equals(p1)) { Point[] p = {p1,p2,p3,p4}; return p; } if (L[8].isBetween(p5)||p4.equals(p3)||p4.equals(p5)) { Point[] p = {p1,p2,p3,p5}; return p; } return null ; } |
选项五 :
找焦点
选项六:
运用面积法,即判断点与其他点构成的图形的面积之和是否等于整个图形的面积,若等于,则说明在图形内部,在判断点是否在线或者端点上,若都不是,则是在图形外。
public ArrayList<Point> getIntersections(Line l){
ArrayList<Point> p = new ArrayList<Point> ();
Line a = new Line(p1,p2);
Line b = new Line(p2,p3);
Line c = new Line(p3,p4);
Line d = new Line(p4,p5);
Line e = new Line(p5,p1);
if(l.getIntersection(e)!=null&&l.getIntersection(a)!=null&&l.getIntersection(e).equals(l.getIntersection(a))) {
p.add(l.getIntersection(e));
if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c)))
p.add(l.getIntersection(b));
else if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d)))
p.add(l.getIntersection(d));
else if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))))
p.add(l.getIntersection(c));
else if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))))
p.add(l.getIntersection(b));
else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d))))
p.add(l.getIntersection(d));
return p;
}
else if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b))) {
p.add(l.getIntersection(a));
if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d)))
p.add(l.getIntersection(c));
else if(l.getIntersection(d)!=null&&l.getIntersection(e)!=null&&l.getIntersection(d).equals(l.getIntersection(e)))
p.add(l.getIntersection(d));
else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d))))
p.add(l.getIntersection(d));
else if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))))
p.add(l.getIntersection(c));
else if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e))))
p.add(l.getIntersection(e));
return p;
}
else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c))) {
p.add(l.getIntersection(b));
if(l.getIntersection(e)!=null&&l.getIntersection(d)!=null&&l.getIntersection(e).equals(l.getIntersection(d)))
p.add(l.getIntersection(d));
else if(l.getIntersection(a)!=null&&l.getIntersection(e)!=null&&l.getIntersection(a).equals(l.getIntersection(e)))
p.add(l.getIntersection(a));
else if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e))))
p.add(l.getIntersection(e));
else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))))
p.add(l.getIntersection(a));
else if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d))))
p.add(l.getIntersection(d));
return p;
}
else if(l.getIntersection(c)!=null&&l.getIntersection(d)!=null&&l.getIntersection(c).equals(l.getIntersection(d))) {
p.add(l.getIntersection(c));
if(l.getIntersection(e)!=null&&l.getIntersection(a)!=null&&l.getIntersection(e).equals(l.getIntersection(a)))
p.add(l.getIntersection(e));
else if(l.getIntersection(a)!=null&&l.getIntersection(b)!=null&&l.getIntersection(a).equals(l.getIntersection(b)))
p.add(l.getIntersection(a));
else if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e))))
p.add(l.getIntersection(e));
else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))))
p.add(l.getIntersection(a));
else if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))))
p.add(l.getIntersection(b));
return p;
}
else if(l.getIntersection(e)!=null&&l.getIntersection(d)!=null&&l.getIntersection(e).equals(l.getIntersection(d))) {
p.add(l.getIntersection(d));
if(l.getIntersection(b)!=null&&l.getIntersection(a)!=null&&l.getIntersection(b).equals(l.getIntersection(a)))
p.add(l.getIntersection(b));
else if(l.getIntersection(b)!=null&&l.getIntersection(c)!=null&&l.getIntersection(b).equals(l.getIntersection(c)))
p.add(l.getIntersection(c));
else if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))))
p.add(l.getIntersection(c));
else if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))))
p.add(l.getIntersection(a));
else if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))))
p.add(l.getIntersection(b));
return p;
}
if((l.getIntersection(a)!=null&&a.isBetween(l.getIntersection(a))))
p.add(l.getIntersection(a));
if((l.getIntersection(b)!=null&&b.isBetween(l.getIntersection(b))))
p.add(l.getIntersection(b));
if((l.getIntersection(c)!=null&&c.isBetween(l.getIntersection(c))))
p.add(l.getIntersection(c));
if((l.getIntersection(d)!=null&&d.isBetween(l.getIntersection(d))))
p.add(l.getIntersection(d));
if((l.getIntersection(e)!=null&&e.isBetween(l.getIntersection(e))))
p.add(l.getIntersection(e));
return p;
}
三、踩坑心得
1、还是没有养成边码代码边写注释的习惯,再加上这次题目十分复杂,代码行数已经达到了2000行以上,方法数十分地多,这就导致我经常忘记哪个方法是干什么的,以后一定养成边码代码边写注释的习惯。
2、没有对重复出现的代码方法话,导致代码复用率特别高,尤其是在穷举时。
3、用了大量的判断语句导致代码的圈复杂度特别高
4、没有找到合适的数学方法,导致在判断某些东西时漏掉情况。
四、改进建议
1. 在定义方法时,选取高效的算法降低代码运行时占用的内存,并降低运行时间,若算法差,则代码冗长,可读性差。
2. 尽可能减少代码复用,提高代码质量。
3. 需要养成及时写注释的习惯,在这次写博客的过程中就深刻体会到了这一点,因为前两次作业都过了有一段时间,这就导致我完全不记得流程,这就浪费了我大量时间来回顾题目复习代码,实在是费时费力。并且希望以后在审完题后花一段时间来构思代码,合理分配职责,让代码的复杂度下降,并提高代码整体的美观性。
4.依据题目要求设计类,设计类的固有属性和方法,分配好各类间的职责,尽量做到职责单一化,并且规划好各类的关系。把需要重复出现的语句设计成一个方法,需要用时只需调用即可,降低代码的耦合度。
五、总结
这三次作业考试最大的收获是深刻地理解了继承和多态的应用,让我深深地体会到了其的便利性,这是面向过程所不能带来的。这次的经历也让我知道了自己在算法方面也存在不足,无法设计出优良的代码结构。最后,在面对程序出现的各种错误时去面对出现的各种错误,遇到问题冷静处理分析,多方面寻找原因去解决,从而慢慢提高自己的代码能力,这也是一个合格的程序员所必须具备的能力。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~