Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding whether a point is within a triangle

Tags:

java

geometry

I have been on this for hours, attempting different methods looking at just about every question. Perhaps I have it completely wrong, but I feel that I have my math of it correct, but no matter what numbers I input, I get the same output. My code is off somewhere and I have to turn it in by midnight.

It is the all so fun: Find if a point is within a triangle code. (for beginners)

import java.util.Scanner;

public class PointsTriangle {

    // checks if point entered is within the triangle
    //given points of triangle are (0,0) (0,100) (200,0)
    public static void main (String [] args) {
        //obtain point (x,y) from user
        System.out.print("Enter a point's x- and y-coordinates: ");
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();

        //find area of triangle with given points
        double ABC = ((0*(100-0  )+0*(0  -0)+200*(0-100))/2.0);
        double PAB = ((x*(0  -100)+0*(100-y)+0  *(y-  0))/2.0);
        double PBC = ((x*(100-0  )+0*(0  -y)+200*(y-100))/2.0);
        double PAC = ((x*(0  -100)+0*(100-y)+200*(y-  0))/2.0);

        boolean isInTriangle = PAB + PBC + PAC == ABC;

        if (isInTriangle)
            System.out.println("The point is in the triangle");
        else
            System.out.println("The point is not in the triangle");
    }//end main
}//end PointsTriangle
like image 743
Lish Avatar asked Feb 03 '13 04:02

Lish


2 Answers

You placed the wrong value order into your formula; therefore, the result is wrong. If the 3 vertices are as the following

A(x1, y1) B(x2, y2), C(x3, y3)

then the area is calculated as

double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2;

After that, you just replace each vertex with the input point, we will have the following triangles: PBC, APC, ABP.

Put everything together, we will have the correct one

int x1 = 0, y1 = 0;
int x2 = 0, y2 = 100;
int x3 = 200, y3 = 0;

// no need to divide by 2.0 here, since it is not necessary in the equation
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
double ABP = Math.abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
double APC = Math.abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
double PBC = Math.abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));

boolean isInTriangle = ABP + APC + PBC == ABC;
like image 71
Thai Tran Avatar answered Oct 08 '22 17:10

Thai Tran


If you draw a picture, you can see the point has to satisfy simple inequalities (below / above / to the right of certain lines). Whether "on the edge" is in or out I will leave up to you:

Y > 0 (above the X axis)
X > 0 (to the right of the Y axis)
X + 2* Y < 200 (below the hypotenuse)

Write an if statement around these three and you're done:

if( (y > 0) && (x > 0) && (x + 2*y < 200) ) 
  System.out.println("The point is in the triangle");
else
  System.out.println("The point is not in the triangle");
like image 39
Floris Avatar answered Oct 08 '22 18:10

Floris