/* * Problem 2, Part 1, Lab 7, Wednesday, 24-09-2008 Modify the class Triangle which has already been provided in the class notes, to include the following method: • public boolean isInside (Point r) to determine if point r lies inside this triangle. Please observe that a point r lies within a triangle if for each line segment of the triangle, the third point of the triangle and r lie on the same side of the line obtained by extending that line segment. So, you should be using isAbove method of the class LineSegment. */ package Geometry; public class Triangle { Point P; Point Q; Point R; public Point getP() { return this.P; } public Point getQ() { return this.Q; } public Point getR() { return this.R; } public Triangle(Point A, Point B, Point C) { this.P = new Point(A.x,A.y); this.Q = new Point(B.x,B.y); this.R = new Point(C.x,C.y); } public Triangle(double x1,double y1,double x2,double y2,double x3,double y3) { this.P = new Point(x1,y1); this.Q = new Point(x2,y2); this.R = new Point(x3,y3); } public double Perimeter() { double peri; peri = this.P.distance_from_point(this.Q); peri = peri + this.P.distance_from_point(this.R); peri = peri + this.Q.distance_from_point(this.R); return peri; } public void Translate(double x_trans, double y_trans) { this.P.translate(x_trans,y_trans); this.Q.translate(x_trans,y_trans); this.R.translate(x_trans,y_trans); } public double Area() { double peri,area,s,a,b,c; peri = this.Perimeter(); s = peri/2; a = this.P.distance_from_point(this.Q); b = this.P.distance_from_point(this.R); c = this.Q.distance_from_point(this.R); area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); return area; } //New method 'isInside(Point r)' added public boolean isInside(Point r) { LineSegment l1 = new LineSegment(P,Q); LineSegment l2 = new LineSegment(Q,R); LineSegment l3 = new LineSegment(R,P); /* A point r lies within a triangle if for each line segment of the triangle, the third point of the triangle and r lie on the same side of the line obtained by extending that line segment */ if((l1.isAbove(r) && l1.isAbove(R)) || (!l1.isAbove(r) && !l1.isAbove(R))) { if((l2.isAbove(r) && l2.isAbove(P)) || (!l2.isAbove(r) && !l2.isAbove(P))) { if((l3.isAbove(r) && l3.isAbove(Q)) || (!l3.isAbove(r) && !l3.isAbove(Q))) { return true; } } } return false; } }