Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Exercise_10/Exercise_10_12/Exercise_10_12.class
Binary file not shown.
43 changes: 43 additions & 0 deletions Exercise_10/Exercise_10_12/Exercise_10_12.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Binary file added Exercise_10/Exercise_10_12/MyPoint.class
Binary file not shown.
53 changes: 53 additions & 0 deletions Exercise_10/Exercise_10_12/MyPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**********************************
* MyPoint *
*---------------------------------*
* -x: double *
* -y: double *
* +MyPoint() *
* +MyPoint(x: double, y: double) *
* +getX(): double *
* +getY(): double *
* +distance(myPoint: MyPoint) *
* +distance(x: double, y: double) *
**********************************/

// Implement MyPoint class
public class MyPoint {
// Data fields
private double x;
private double y;

/** Constructor that creates a point (0, 0) */
MyPoint() {
this(0, 0);
}

/** Constructs a point with specified coordinates */
MyPoint(double x, double y) {
this.x = x;
this.y = y;
}

/** Return x */
public double getX() {
return x;
}

/** Return y */
public double getY() {
return y;
}

/** Returns the distance from this point to
* a specified point of the MyPoint type */
public double distance(MyPoint myPoint) {
return Math.sqrt(Math.pow(myPoint.getX() - x, 2) +
Math.pow(myPoint.getY() - y, 2));
}

/** Returns the distance from this point to another
* point with specified x- and y-coordinates. */
public double distance(double x, double y) {
return distance(new MyPoint(x, y));
}
}
Binary file added Exercise_10/Exercise_10_12/Segment.class
Binary file not shown.
39 changes: 39 additions & 0 deletions Exercise_10/Exercise_10_12/Segment.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Binary file added Exercise_10/Exercise_10_12/Triangle2D.class
Binary file not shown.
127 changes: 127 additions & 0 deletions Exercise_10/Exercise_10_12/Triangle2D.java
Original file line number Diff line number Diff line change
@@ -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);
}
}