Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
(Geometry: points in triangle?) Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0) and (0, 100). Write a program that prompts the user to enter a point with x- and y-coordinates and determines whether the point is inside the triangle.

Though there are many algorithms for this question. I was specifically concerned about the following one:
Java
System.out.println("Enter a point's x- and y-coordinates: ");
       double x = in.nextDouble();
       double y = in.nextDouble();

       double x1 = 0, y1 = 0, x2 = 200, y2 = 0,
              x3 = 0, y3 = 100; //Vertex coordinates for the given triangle

       double Dx, Dy, minD, maxD;

       String result = "";
       double determinant = (y2 - y3) * (x1 - x3) - (x3 - x2) * (y3 - y1);
       minD = Math.min(determinant, 0);
       maxD = Math.max(determinant, 0);
       Dx = x - x3;
       Dy = y - y3;

       double a = y2 - y3 * Dx + (x3 - x2) * Dy;
       if (a < minD || a > maxD) {
           result += "The point is not in the triangle.";
       }
       double b = (y3 - y1) * Dx + (x1 - x3) * Dy;
       if (b < minD || b > maxD) {
           result += "The point is not in the triangle.";
       }
       double c = determinant - a - b;
       if (c < minD || c > maxD) {
           result += "The point is not in the triangle.";

       } else {
           result += "The point is in the triangle.";
       }
       System.out.println(result);


Could anyone kindly take his/her time to explain this?

What I have tried:

I have tried areas comparison approach enclosed b/w the subtriangles and the main triangle. And cross vector product approach. I understood both.
Posted
Updated 4-Oct-23 3:02am
v2
Comments
Richard MacCutchan 4-Oct-23 8:29am    
"time to explain this?"
You should ask the person who wrote it.
Unknown Boy 2023 4-Oct-23 8:44am    
Man I found it on github. If you can't explain after my humble request than at least you can tell me about the method used here?
Richard MacCutchan 4-Oct-23 9:32am    
I have never seen that code before so how do you expect me, or anyone else here, to explain it. If you find code on the internet then make sure you understand it before you decide to download it.

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Comments
Unknown Boy 2023 4-Oct-23 9:09am    
Man then any idea of method used here?
Richard Deeming 5-Oct-23 9:29am    
A really really poor one - one that can generate a result of "The point is not in the triangle.The point is not in the triangle.The point is in the triangle."

Either go back to where you found that code and ask the author to explain it to you, or find a better source.
To check if a point is inside a triangle, there are several approaches you can use. One commonly used method is the "Barycentric Coordinate System" approach. Barycentric coordinates represent a point as a weighted sum of the vertices of the triangle. Here's how it works:

Calculate the Barycentric Coordinates:
Given a triangle with vertices A, B, and C and a point P that you want to check, you can calculate the Barycentric coordinates (u, v, w) of point P with respect to the triangle ABC using the following formulas:



u = ((v1.y - v2.y) * (p.x - v2.x) + (v2.x - v1.x) * (p.y - v2.y)) /
((v1.y - v2.y) * (v0.x - v2.x) + (v2.x - v1.x) * (v0.y - v2.y))

v = ((v2.y - v0.y) * (p.x - v2.x) + (v0.x - v2.x) * (p.y - v2.y)) /
((v1.y - v2.y) * (v0.x - v2.x) + (v2.x - v1.x) * (v0.y - v2.y))

w = 1 - u - v


Where (v0, v1, v2) are the vertices of the triangle, (p.x, p.y) is the point you want to check, and (u, v, w) are the Barycentric coordinates.

Check the Barycentric Coordinates:
If all the Barycentric coordinates (u, v, w) are between 0 and 1 (inclusive), it means that the point P is inside the triangle. If any of the coordinates are outside this range, the point is outside the triangle.

Here's a simple Java code example to check if a point is inside a triangle using the Barycentric Coordinate System:

public class PointInTriangle {
public static boolean isPointInTriangle(Point v0, Point v1, Point v2, Point p) {
double u = ((v1.y - v2.y) * (p.x - v2.x) + (v2.x - v1.x) * (p.y - v2.y)) /
((v1.y - v2.y) * (v0.x - v2.x) + (v2.x - v1.x) * (v0.y - v2.y));
double v = ((v2.y - v0.y) * (p.x - v2.x) + (v0.x - v2.x) * (p.y - v2.y)) /
((v1.y - v2.y) * (v0.x - v2.x) + (v2.x - v1.x) * (v0.y - v2.y));
double w = 1 - u - v;

return u >= 0 && v >= 0 && w >= 0 && u <= 1 && v <= 1 && w <= 1;
}

public static void main(String[] args) {
Point v0 = new Point(0, 0);
Point v1 = new Point(1, 0);
Point v2 = new Point(0, 1);
Point p = new Point(0.5, 0.5);

if (isPointInTriangle(v0, v1, v2, p)) {
System.out.println("Point is inside the triangle.");
} else {
System.out.println("Point is outside the triangle.");
}
}
}

class Point {
double x;
double y;

public Point(double x, double y) {
this.x = x;
this.y = y;
}
}


In this example, we define the isPointInTriangle method to check if a point is inside a triangle. You can use this method to determine whether a point lies inside a given triangle defined by its vertices.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900