This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Point in Hexagon  RSS feed

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm trying to come up with some code to check if a point lies in a hexagon. Can anybody help?
 
Saloon Keeper
Posts: 9132
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you already looked at the java.awt.Polygon class?
 
Ted Gress
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the coordinates need to be in any specific order for the polygon class?
 
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the Polygon documentation explain that? As an alternative there is Path2D and GeneralPath, which have similar uses.
 
Ted Gress
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't seem to get it to work.


 
Ted Gress
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and...

 
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with it?
I use the values you give, and stuck a couple of sample x/y coords and they work as expected.
 
Ted Gress
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the multiple posts. I forgot this.

I'm testing point 150,150 and it is showing up as not in the hexagon
 
Sheriff
Posts: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Junilu Lacar
Sheriff
Posts: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Junilu Lacar
Sheriff
Posts: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double check and make sure that the x and y values for the coordinate you're checking are really what you think they are.
 
Ted Gress
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I double checked and the coordinates I'm giving it is 150, 150 Is there wrong ordering in my vertices for the Polygon?
 
Dave Tolls
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code I used ot check:

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.
 
Ted Gress
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved it. I was passing in the wrong set of coordinates. Thanks guys.
 
Campbell Ritchie
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out and providing the explanation.
 
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it is a good thing to use a Path2D or similar, and use its method to determine if a given point lies inside or outside the shape. But certainly in the beginning it is big fun to grab the opportuniy to rehearse some of possibly long forgotten school maths.

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?
 
Junilu Lacar
Sheriff
Posts: 12198
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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." 
 
Stephan van Hulst
Saloon Keeper
Posts: 9132
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I first learned of that quote through the movie "The Big Short". Great quote. Great movie.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!