From 684a4b47e05ec9eaab20d3f1a4f7ce8458b9fa9e Mon Sep 17 00:00:00 2001 From: Joaquim Monserrat Date: Sat, 27 Jan 2018 21:47:33 +0100 Subject: [PATCH] Added Exercise_10_12 --- .../Exercise_10_12/Exercise_10_12.class | Bin 0 -> 1405 bytes .../Exercise_10_12/Exercise_10_12.java | 43 ++++++ Exercise_10/Exercise_10_12/MyPoint.class | Bin 0 -> 697 bytes Exercise_10/Exercise_10_12/MyPoint.java | 53 ++++++++ Exercise_10/Exercise_10_12/Segment.class | Bin 0 -> 841 bytes Exercise_10/Exercise_10_12/Segment.java | 39 ++++++ Exercise_10/Exercise_10_12/Triangle2D.class | Bin 0 -> 2094 bytes Exercise_10/Exercise_10_12/Triangle2D.java | 127 ++++++++++++++++++ 8 files changed, 262 insertions(+) create mode 100644 Exercise_10/Exercise_10_12/Exercise_10_12.class create mode 100644 Exercise_10/Exercise_10_12/Exercise_10_12.java create mode 100644 Exercise_10/Exercise_10_12/MyPoint.class create mode 100644 Exercise_10/Exercise_10_12/MyPoint.java create mode 100644 Exercise_10/Exercise_10_12/Segment.class create mode 100644 Exercise_10/Exercise_10_12/Segment.java create mode 100644 Exercise_10/Exercise_10_12/Triangle2D.class create mode 100644 Exercise_10/Exercise_10_12/Triangle2D.java diff --git a/Exercise_10/Exercise_10_12/Exercise_10_12.class b/Exercise_10/Exercise_10_12/Exercise_10_12.class new file mode 100644 index 0000000000000000000000000000000000000000..ca4384f1db6f5be9fde37c64564dcb6ae4133c18 GIT binary patch literal 1405 zcmaJ>=~5Fx5dMZ^cL`At7E}r$AjbxQkOVJ+7$D#k4h2yzaY;s8DhIVJlstkD;sfZ9 zP$gBC{mAK~GmAGCK{I1|b1;&)P8Z9pJDB-mwOY^bidpSmnYpKCQ#8r&@(2fxe z6MRl`yaY>AyiNP?2#+;9Ve?ZB&-hF-GNWOZl{pRbd_Gt4LdAlBGHT^4XH1~d2rda! zP3F^5pe||UW`-Rl1SX%ev4K`G~^~1!bK<&J=t*msU&CC?mC@Uu%Nab@5 z6|x&JqW#ezLup9%eAD5#y&~o| zw`Ix^s5TM_ZdjXXvl4r^QD=V`()K!T-=TC+`kkKsU}4DPUuLeoCY~Hvz3QEke9*1y?x3mGc*S1=Sfz@0rEeK`YG-9T=i7nO6k_ze(DMNxMO( zptHK~5aKfl0=H>bipEyd;12GROjNN>-#h&QME4OYmXA=mtfNYYatO^;YIM|=l}kG6 zNIArnq(^T!N!J-?&pI8=E_KGEDmt!}gXub2$_gQ^E`9b3+K$k^e273pM**F;3bBU+d;yKwkkA>P(P+Cek)=5je;oA)zwpwX%nr2m}zK8LXfa cAJL6Z=)n*4;tvd=A}G4hM@_y*yb9d^2h_?v0{{R3 literal 0 HcmV?d00001 diff --git a/Exercise_10/Exercise_10_12/Exercise_10_12.java b/Exercise_10/Exercise_10_12/Exercise_10_12.java new file mode 100644 index 00000000..491d87de --- /dev/null +++ b/Exercise_10/Exercise_10_12/Exercise_10_12.java @@ -0,0 +1,43 @@ +/********************************************************************************* +* (Geometry: the Triangle2D class) Define the Triangle2D class that * +* contains: * +* * +* ■ Three points named p1, p2, and p3 of the type MyPoint with getter and setter * +* methods. MyPoint is defined in Programming Exercise 10.4. * +* ■ A no-arg constructor that creates a default triangle with the points (0, 0), * +* (1,1) and (2,5). * +* ■ A constructor that creates a triangle with the specified points. * +* ■ A method getArea() that returns the area of the triangle. * +* ■ A method getPerimeter() that returns the perimeter of the triangle. * +* ■ A method contains(MyPoint p) that returns true if the specified * +* point p is inside this triangle. * +* ■ A method contains(Triangle2D t) that returns true if the specified * +* triangle is inside this triangle. * +* ■ A method overlaps(Triangle2D r) that returns true if the specified * +* triangle overlaps with this triangle. * +* * +* Draw the UML diagram for the class and then implement the class. Write a test * +* program that creates a Triangle2D objects t1 using the constructor * +* new Triangle2D(new MyPoint(2.5, 2), new MyPoint(4.2, 3), new MyPoint(5, 3.5)), * +* displays its area and perimeter, and displays the result of t1.contains(3,3), * +* t1.contains(new Triangle2D(new MyPoint(2.9,2), new MyPoint(4,1), * +* new MyPoint(1,3.4))), and t1.overlaps(new Triangle2D(new MyPoint(2, 5.5), * +* new MyPoint(4, -3), new MyPoint(2, 6.5))). * +*********************************************************************************/ + +public class Exercise_10_12 { + /** Main method */ + public static void main(String[] args) { + // Create Triangle2D objects + Triangle2D t1 = new Triangle2D(new MyPoint(2.5, 2), new MyPoint(4.2, 3), new MyPoint(5, 3.5)); + Triangle2D t2 = new Triangle2D(new MyPoint(2.9,2), new MyPoint(4,1), new MyPoint(1,3.4)); + Triangle2D t3 = new Triangle2D(new MyPoint(2, 5.5), new MyPoint(4, -3), new MyPoint(2, 6.5)); + + // Display results + System.out.println("Area: " + t1.getArea()); + System.out.println("Perimeter: " + t1.getPerimeter()); + System.out.println("t1 contains (3,3): " + t1.contains(new MyPoint(3,3))); + System.out.println("t1 contains triangle t2: " + t1.contains(t2)); + System.out.println("t1 overlaps triangle t3: " + t1.overlaps(t3)); + } +} diff --git a/Exercise_10/Exercise_10_12/MyPoint.class b/Exercise_10/Exercise_10_12/MyPoint.class new file mode 100644 index 0000000000000000000000000000000000000000..d18956d82f30e65db469e26d9a84976150b7ab6d GIT binary patch literal 697 zcmZ8d+fLg+6r4>QJ28tBC=eh72!X^lw+H%0RUu*Z2?`Pj!J`wbV3ay-FfDu)LgFSW z^#l4*h*`%{s@mFf+c|S)&)>J#KL9>r!^8;YO^jlpz-UpKnyM}7I|QiDL>4q1CD^W;qiEG1@up06vm$gvbJ*~pzZd~1A*dR7zJOizqNw?vES;llCwQ80d#`+ zG(nR?F@srwk|Nmb`cY@|pmiCvV*zVe+V|rN3jN;u zMWI(e`n=U_&+$;>B1XBQ41bPHB2J~(d8h;e9~jkYuW6g>}^85o9-RsprP+S=+cK+)<>V>BhM=tn|hQdbW2g-lC4XwvX6C#*_nmj{x%d70^W!IgCwOS#jtzyoHXJP5$Y3SK=$^o; z+X#NG01g~aCIKr7^$ zL0@m59Jlpo&u{mbDY(;<(?AXwT|M5VaM~?T^HG-S$-$`}_59&G1;=f+boW@Zboq@! zrZx6EM_c|d)Mr}?c584l>gZR2)N>;8`k{a7D_D_j2Xm+j6a?lSWRY`_7bv3S;08(* z+8YIazuVIr4Pn(BtfNkiNKzp$qV*m*^_}*i?u_Z!Z8W5xrT_FfZA=ImHrxujeSPq9 zr0I-vYhamE&+vv~eCQ~QXo{=B+oDn8HU2$Dk`YkyE}-_a*$HA2@3y%m%2dGP27s8pJ0V`+m&-(6(PBOfz-a#;Ad7Ru+Nd6P1P<_N7)Y_ zQIm~oP1sk&@ZX$aS&7d`#J|v3EF4jhvjrz_V1?sa#U|F^b6sFIgV=ApwQ!s73u$;U HJht=~NAijF literal 0 HcmV?d00001 diff --git a/Exercise_10/Exercise_10_12/Segment.java b/Exercise_10/Exercise_10_12/Segment.java new file mode 100644 index 00000000..99cb9602 --- /dev/null +++ b/Exercise_10/Exercise_10_12/Segment.java @@ -0,0 +1,39 @@ +// Implement Segment class +public class Segment { + // Data fields + private MyPoint p1; + private MyPoint p2; + + // Constructors + /** Creates a default segment with end points + * (0,0) and (1,0) */ + public Segment() { + this(new MyPoint(0,0), new MyPoint(1,0)); + } + + /** Creates a segment with the specified end points */ + public Segment(MyPoint p1, MyPoint p2) { + this.p1 = p1; + this.p2 = p2; + } + + /** Returns end point p1 */ + public MyPoint getP1() { + return p1; + } + + /** Returns end point p2 */ + public MyPoint getP2() { + return p2; + } + + /** Returns true if the specified segment overlaps + * with this segment */ + public boolean overlaps(Segment s) { + MyPoint q1 = s.getP1(); + MyPoint q2 = s.getP2(); + + return new Triangle2D(p1,p2,q1).getSignedArea()*new Triangle2D(p1,p2,q2).getSignedArea() < 0 && + new Triangle2D(q1,q2,p1).getSignedArea()*new Triangle2D(q1,q2,p2).getSignedArea() < 0; + } +} diff --git a/Exercise_10/Exercise_10_12/Triangle2D.class b/Exercise_10/Exercise_10_12/Triangle2D.class new file mode 100644 index 0000000000000000000000000000000000000000..b9a148ad21222ad872adfdfcb55d8e4536534f4d GIT binary patch literal 2094 zcmZuxTT>cm7=9Lk#bpI!@RUlbQ6nH|qc)Av7!yfqQZcqzqvoQs;toX-93=L9*I&?^ zPIA%7U2l4vso1GAz3z0T{Y9O&&-?8Lhzjicy@%&}&&!|x{`Nb7DQxPPHlX3Af1a{@ z{PPiqF>nDh1}wFJP`6w$2$hPup~l8 zxMdxWGR@T4M1v4c>+Fwyhz&R4CcYPxrR$ z%Brz0e*G*XH+^}#y23EQSiBB!wTz{#4Cgy=P-erOx-4pe$w^onon;I8lD&PW zVp}S?v<978M>ZvxT`BC@RlA}gkSmv}R-tsDq1o{!9c8OlZZ~7?yJdu9ORJTFRmvCb ziS$gIh(P(!t`x2P1Gbdy(_m!FPb)e5enH9(H_&4{)*(~T-7cDV9V&S0?bx|0dlpNl)#%uhbxx26fuRO4V^u$73#~1-#~Vw>*~!3m;h<`j za;&hqVYGBM22&rScqh!wiCx10_v;dOw*?-lo18~VS0}6I8REmXa90P&&7+kZFqAlf zwh;;)!y`;M>@pFwMBt-v4E<-~G+ZGy=oRD!`4w-2uD4n0z0^b!#|R`wkIGYY zju33z?t+h)t3-}4dW;#xaS;jnV8>lgQJ#{QFYyaneqvzwBn&bFiB>n;M z+)y{4FX7a_Ee)x`vQ)(rT-bQ&X_Pz5)Iwi>1Ks;Aye+p} ztBQZdI_Fx0a{r9=y0TB+zjU+~E=&0f1Gem`rD7Hh`PQ^MkoQ}GKHtez|MZOU`Dyk2jVHIPF zV^i(XZm@j;CiM3Sx}-g-0mtahxUw1o<>e;XpnTIS3Q0z?5QWcGbi7pfn-md2=#p{I zb9^w$z2Nyc+3TyJ_c@l6y?({qOZIXu)Xb(_(`3puB~q?wpx5i#y2&~B+Q`YpXJ-a5 zh9Ik}pdAP3N0n=Fh`aa*S$xbz>% literal 0 HcmV?d00001 diff --git a/Exercise_10/Exercise_10_12/Triangle2D.java b/Exercise_10/Exercise_10_12/Triangle2D.java new file mode 100644 index 00000000..c02f4d2d --- /dev/null +++ b/Exercise_10/Exercise_10_12/Triangle2D.java @@ -0,0 +1,127 @@ +/********************************************************************** +* Triangle2D * +*---------------------------------------------------------------------* +* -p1: MyPoint * +* -p2: MyPoint * +* -p3: MyPoint * +* +Triangle2D() * +* +Triangle2D(p1: MyPoint, p2: MyPoint, p3: MyPoint) * +* +setP1(p1: MyPoint) * +* +setP2(p2: MyPoint) * +* +setP3(p3: MyPoint) * +* +getP1(): MyPoint * +* +getP2(): MyPoint * +* +getP3(): MyPoint * +* +getSignedArea(): double * +* +getArea(): double * +* +getPerimeter(): double * +* +contains(p: MyPoint): boolean * +* +contains(t: Triangle2D): boolean * +* +overlaps(t: Triangle2D): boolean * +**********************************************************************/ + +// Implement Triangle2D class +public class Triangle2D { + // Data fields + private MyPoint p1; + private MyPoint p2; + private MyPoint p3; + + // Constructors + /** Creates a default triangle with the points + * (0,0), (1,1) and (2,5) */ + Triangle2D() { + this(new MyPoint(0,0), new MyPoint(1,1), new MyPoint(2,5)); + } + + /** Creates a triangle with specified points p1, p2 and p3 */ + Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3) { + this.p1 = p1; + this.p2 = p2; + this.p3 = p3; + } + + // Methods + /** Return point p1 */ + public MyPoint getP1() { + return p1; + } + + /** Return point p2 */ + public MyPoint getP2() { + return p2; + } + + /** Return point p3 */ + public MyPoint getP3() { + return p3; + } + + /** Set p1 to specified point */ + public void setP1(MyPoint p1) { + this.p1 = p1; + } + + /** Set p2 to specified point */ + public void setP2(MyPoint p2) { + this.p2 = p2; + } + + /** Set p3 to specified point */ + public void setP3(MyPoint p3) { + this.p3 = p3; + } + + /** Returns the signed area of the triangle */ + public double getSignedArea() { + return (p1.getX()*p2.getY() + p2.getX()*p3.getY() + p3.getX()*p1.getY() + - p3.getX()*p2.getY() - p2.getX()*p1.getY() - p1.getX()*p3.getY()) / 2; + } + + /** Returns the area of the triangle */ + public double getArea() { + return Math.abs(this.getSignedArea()); + } + + /** Returns the perimeter of the triangle */ + public double getPerimeter() { + double side1 = p2.distance(p3); + double side2 = p1.distance(p3); + double side3 = p1.distance(p2); + return side1 + side2 + side3; + } + + /** Returns true if the specified point p is + * inside the triangle */ + public boolean contains(MyPoint p) { + boolean a = new Triangle2D(p1,p2,p).getSignedArea() > 0; + boolean b = new Triangle2D(p2,p3,p).getSignedArea() > 0; + boolean c = new Triangle2D(p3,p1,p).getSignedArea() > 0; + return a == b && b == c; + } + + /** Returns true if the specified triangle is + * inside this triangle */ + public boolean contains(Triangle2D t) { + return this.contains(t.getP1()) && + this.contains(t.getP2()) && + this.contains(t.getP3()); + } + + /** Returns true if the specified triangle + * overlaps with this triangle */ + public boolean overlaps(Triangle2D t) { + Segment s1 = new Segment(p2,p3); + Segment s2 = new Segment(p1,p3); + Segment s3 = new Segment(p1,p2); + + Segment q1 = new Segment(t.getP2(),t.getP3()); + Segment q2 = new Segment(t.getP1(),t.getP3()); + Segment q3 = new Segment(t.getP1(),t.getP2()); + + return this.contains(t) || t.contains(this) || + s1.overlaps(q1) || s1.overlaps(q2) || s1.overlaps(q3) || + s2.overlaps(q1) || s2.overlaps(q2) || s2.overlaps(q3) || + s3.overlaps(q1) || s3.overlaps(q2) || s3.overlaps(q3); + } +}