I'm trying to come up with some code to check if a point lies in a hexagon. Can anybody help?
I use the values you give, and stuck a couple of sample x/y coords and they work as expected.
I'm testing point 150,150 and it is showing up as not in the hexagon
Ted Gress wrote:Can't seem to get it to work.
Please explain what you mean by that. What input did you provide and what output did you expect vs. what you got?
Edit: Never mind, you beat me to the punch.
Ted Gress wrote:I'm testing point 150,150 and it is showing up as not in the hexagon
Looks like it works for me. See this: https://repl.it/@jlacar/PointInsideShape
which is pretty much a copy of the code you have, substituting the values you gave earlier.
So, if that works and your code doesn't, debug everything in your method.
Print out hex (create a toString method to handle the String production), so you can see exactly what values it has, and also print out (as Junilu says) x and y, because one or more of those values are incorrect.
For instance: say we have a hexagon, with center M, and a point P. Now, from the 6 points at the corners of the circumference, determine the two most nearby points C1 and C2 to P.
Then two strategies for now:
1) determine the angle M-C1-P and M-C1-C2 (well, that last one should not be difficult). What if the first angle is smaller, equal or larger than the second?
2) determine the intersection of the line: lambda * (p - m) + m and the line through C1 and C2, and solve for lambda. What if lambda > 1, < 1, = 1?
Ted Gress wrote:I double checked and the coordinates I'm giving it is 150, 150
Solved it. I was passing in the wrong set of coordinates.
Ah. Mark Twain once wrote "It ain't what you don't know that gets you in trouble, it's what you know for sure that ain't so."