-1

Create a Java Program to create a 2 dimensional virtual triangle and check if a point falls inside it. Console input/output functionality will be as follows:

INPUT: Ask user to enter X,Y integer coordinates for 1st vertex of triangle INPUT: Ask user to enter X,Y integer coordinates for 2nd vertex of triangle INPUT: Ask user to enter X,Y integer coordinates for 3rd vertex of triangle INPUT: Ask user to enter X,Y integer coordinates for a test point OUTPUT: Check and print if the point is inside or outside the triangle

1 Answers1

0

Here's what I found from a quick search:

The gist is that you take the point (Point Z) that you're trying to figure out if it's inside or outside the triangle and draw a horizontal line from Point Z's x value to ∞. If this line (Line Z) intersects only and exactly one of the triangles sides, then we can conclude that Point Z is inside the triangle. In order to determine if it intersects the one of the sides, a good strategy would be to form y=mx+b equations from the sides. Given these equations, you can then determine how many sides Line Z intersects with. Note that if you find that Line Z intersects with two lines or more lines, check whether or not Point Z is one of the vertices of the triangle. If it is, then it's up to you if you want to consider that as inside or outside. If Point Z is not a vertex, then it is guaranteed to not be inside the triangle at all.