You can use Line2D#contains
, Line2D#linesIntersect
methods inside java.awt.geom.Line2D
.
Edit:
Thinking in Math:
To detect whether a point is inside a triangle, draw three dashed lines, if the point is inside a triangle, each dashed line should intersect a side only once. if a dashed line intersect a side twice, then the point must be outside the triangle.
So We can use the way in the other question (you mentioned) like below:
public boolean contains(MyPoint p) {
// double area1 = calcArea(p, p1, p2);
// double area2 = calcArea(p, p2, p3);
// double area3 = calcArea(p, p3, p1);
// double area = Math.round((area1 + area2 + area3) * 100) / 100;
// double triangleArea = Math.round(getArea() * 100) / 100;
// return (triangleArea == area)
}
But it's not efficient way, so we will implement it as below, in order to reuse it with other cases.
We should have three methods one for check max x, y
, one for checking min x, y
and other for checking the lineSegment.
The lineSegment would be look like:
double position = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
return position <= 0.0000000001 && ((x0 <= x2 && x2 <= x1) || (x0 >= x2 && x2 >= x1));
/**
* returns true if the specified point is inside this triangle
**/
public boolean contains(MyPoint p) {
return contains(p.getX(), p.getY());
}
public boolean contains(double x, double y) {
// Get max X & Y
double maxX = getMax(p1.getX(), p2.getY(), p3.getX());
double maxY = getMax(p1.getY(), p2.getY(), p3.getX());
// Get min X & Y
double minX = getMin(p1.getX(), p2.getX(), p3.getX());
double minY = getMin(p1.getY(), p2.getY(), p3.getY());
// Outside the bounding rectangle of the triangle
if (x < minX || x > maxX || y < minY || y > maxY) return false;
// Check if point is the border of the triangle
MyPoint p = new MyPoint(x, y);
boolean side1 = p.onTheLineSegment(p1, p2); //assume A to B
boolean side2 = p.onTheLineSegment(p1, p3); //assume B to C
boolean side3 = p.onTheLineSegment(p2, p3); //assume C to A
return side1 || side2 || side3; //return true if any point of these vertices inside triangle.
}
So to check if triangle contain other one, our method would be look like:
public boolean contains(Triangle t) {
return contains(t.p1) && contains(t.p2) && contains(t.p3); //All three points is inside the triangle
}
Or simply by using Line2D
class as I mention above:
Line2D line2D = new Line2D.Double(p1.getX(), p1.getY(), p2.getX(), p2.getY());
return line2D.contains(....); //check if contain triangle or point