Thread: Is this random coordinate inside of the triangle?

1. Member
Join Date
May 2014
Posts
8
Rep Power
0

Is this random coordinate inside of the triangle?

I'm having a random x and y coordinate generate and I need to check if the coordinates are inside of a right triangle on this imaginary coordinate plane:

If anybody could give me some if statements to use to find if it is within the triangle, I'd be really happy. Thank you!

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,750
Rep Power
10

Re: Is this random coordinate inside of the triangle?

Well, we don't provide code here (except to illustrate general concepts). To solve your problem you first need to figure out how you would do it on paper. You clearly know the height and the width of the triangle. So all you need to worry about is whether it is below the hypotenuse and above the base. So x should be easy. Once you have x, then think about the equation of a straight line to determine y. Once you figure it out on paper, it should be trivial to translate into Java.

Note: The approach above assumes at least one of the two shorter sides is parallel to the x-axis.

Regards,
Jim
Last edited by jim829; 05-24-2014 at 03:39 AM.

3. Member
Join Date
May 2014
Posts
8
Rep Power
0

Re: Is this random coordinate inside of the triangle?

Originally Posted by jim829
Well, we don't provide code here (except to illustrate general concepts). To solve your problem you first need to figure out how you would do it on paper. You clearly know the height and the width of the triangle. So all you need to worry about is whether it is below the hypotenuse and above the base. So x should be easy. Once you have x, then about the equation of a straight line to determine y. Once you figure it out on paper, it should be trivial to translate into Java.

Note: The approach above assumes at least one of the two shorter sides is parallel to the x-axis.

Regards,
Jim
Thanks Jim, it's my first post and I wasn't aware that you didn't provide code. I'm glad you don't provide it because it forces people to think. I get the idea of what you're saying, thanks! :D

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,750
Rep Power
10

Re: Is this random coordinate inside of the triangle?

You're welcome. And once you try and code it in Java, we can certainly help you with any syntactic or even logic problems you may encounter.

Regards,
Jim