Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define the Triangle2D class that contains: -Three points named p1, p2, p3 with t

ID: 3629172 • Letter: D

Question

Define the Triangle2D class that contains:
-Three points named p1, p2, p3 with the type MyPoint with get and set methods. MyPoint is defined in Exercise 10.4.
- A no-arg constructor that creates a default triangle with 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 t) that returns true if the specified triangle overlaps with this triangle.

Explanation / Answer

// ------------------------------------------------------------------------------ // MyPoint Class // ------------------------------------------------------------------------------ class MyPoint { public MyPoint() { } public MyPoint(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public void moveBy(int dx, int dy) { x += dx; y += dy; } public double distanceTo(MyPoint p) { double dx = x - p.x; double dy = y - p.y; return Math.sqrt(dx * dx + dy * dy); } public String toString() { return "(" + x + "," + y + ")"; } private double x, y; } // ------------------------------------------------------------------------------ // Triangle 2D Class // ------------------------------------------------------------------------------ class Triangle2D { private MyPoint p1, p2, p3; // Default constructor public Triangle2D() { p1 = new MyPoint(0,0); p2 = new MyPoint(1,1); p3 = new MyPoint(2,5); } public Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3) { this.p1 = p1; this.p2 = p2; this.p3 = p3; } public double getArea() { double s = getPerimeter() / 2.0; return Math.sqrt(s * (s - p1.distanceTo(p2)) * (s - p2.distanceTo(p3)) * (s - p3.distanceTo(p1))); } public double getPerimeter() { return p1.distanceTo(p2) + p2.distanceTo(p3) + p3.distanceTo(p1); } public boolean contains(MyPoint p) { double o1 = getOrientationResult(p1, p2, p); double o2 = getOrientationResult(p2, p3, p); double o3 = getOrientationResult(p3, p1, p); return (o1 == o2) && (o2 == o3); } public boolean overlaps(Triangle2D t) { return t.contains(p1) || t.contains(p2) || t.contains(p3); } // Returns the orientation of the triangle private static int getOrientationResult(MyPoint mp1, MyPoint mp2, MyPoint p) { double orientation = ((mp2.getX() - mp1.getX()) * (p.getY() - mp1.getY())) - ((p.getX() - mp1.getX()) * (mp2.getY() - mp1.getY())); if (orientation > 0) { return 1; } else if (orientation < 0) { return -1; } else { return 0; } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote