26282 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Win a Nexus 7 with
reed.co.uk and The
Register
WTF is... LTE
Advanced?
"Lab-smashing"
Stuxnet HELPED
Iran"s nuke effort,
says brainiac
MYSTERY Nokia Lumia
with
gazillion-pixel
camera "spotted"
Machine learning
climbs atop Hadoop
New 4TB drive
spaffs half a telly
season into your
eyes AT ONCE
Buff American
beauties keen to
dominate Euro youth
in tech tussle
Indian "attacks"
Norwegian telco to
get at Pakistan,
China
German robots sent
to Oz to make GPS
millimetre-perfect
Dell"s
PC-on-a-stick
landing in July:
report
Slashdot
German Researchers
Hit 40 Gbps On
Wireless Link
The Hunt For
LulzSec"s Missing
Sixth Member
Latvian Police Raid
Teacher"s Home for
Uploading $4.00
Textbook
EFF Resumes
Accepting Bitcoin
Donations After Two
Year Hiatus
Google Drops XMPP
Support
Motion To Delay
Sanctions Against
Prenda Lawyers
Denied
NSA Data Center the
Focus of Tax
Controversy
Viruses In Mucus
Protect From
Infection
Reporters
Threatened, Labeled
Hackers For Finding
Security Hole
Judges Debate
Patents and If New
Software Makes a
Computer a "New
Machine"
Article viewer

Pixel Coordinates to Hexagonal Coordinates



Written by:dimport
Published by:dimport
Published on:2003-06-21 07:19:46
Topic:Miscellaneous
Search OSI about Miscellaneous.More articles by dimport.
 viewed 12603 times send this article printer friendly

Digg this!
    Rate this article :
Creating Hexagonal co-ordinates from pixel co-ordinates.
I'll post the routines I use to calculate which hex the mouse is in. First, I should explain the hexagon size and layout.

Each hexagon is 28 x 24 pixels, but since the columns overlap, the distance from the center of one hex to the center of the next column's hex is 21.
My coordinate system is offset-grid with no gaps. The lower left is (1,1); as you go up, the N coordinate increases. (I call them (M,N) instead of (X,Y) to distinguish between the hex and square coordinates.) Every other column is pushed up half a hexagon height.
First, this is the approach based on a rec.games.programmer article saved on my web pages. It is based on the view that hexagons are a projection of three dimensional cubes onto a plane. (See that web page for an explanation.)

// Note: HexCoord is a struct that just stores hex coordinates
HexCoord PointToHex( int xp, int yp )
{
// NOTE: HexCoord(0,0)'s x() and y() just define the origin
// for the coordinate system; replace with your own
// constants. (HexCoord(0,0) is the origin in the hex
// coordinate system, but it may be offset in the x/y
// system; that's why I subtract.)
double x = 1.0 * ( xp - HexCoord(0,0).x() ) / HexXSpacing;
double y = 1.0 * ( yp - HexCoord(0,0).y() ) / HexYSpacing;
double z = -0.5 * x - y;
y = -0.5 * x + y;
int ix = floor(x+0.5);
int iy = floor(y+0.5);
int iz = floor(z+0.5);
int s = ix+iy+iz;
if( s )
{
double abs_dx = fabs(ix-x);
double abs_dy = fabs(iy-y);
double abs_dz = fabs(iz-z);
if( abs_dx >= abs_dy && abs_dx >= abs_dz )
ix -= s;
else if( abs_dy >= abs_dx && abs_dy >= abs_dz )
iy -= s;
else
iz -= s;
}
return HexCoord( ix, ( iy - iz + (1-ix%2) ) / 2 );
}

Now, here's another approach that I'm now using. It's not as general, but it's faster.

HexCoord PointToHex( int xp, int yp )
{
// NOTE: First we subtract the origin of the coordinate
// system; replace with your own values
xp -= X_ORIGIN;
yp -= Y_ORIGIN;
int row = 1 + yp / 12;
int col = 1 + xp / 21;
int diagonal[2][12] = {
{7,6,6,5,4,4,3,3,2,1,1,0},
{0,1,1,2,3,3,4,4,5,6,6,7}
};
if( diagonal[(row+col)%2][yp%12] >= xp%21 )
col--;
return HexCoord( col, (row-(col%2))/2 );
}

In this approach, I first figure out which "half row" the (x,y) lies in, and put that in `row'. Each hexagon occupies two half rows, but every other column chooses different half rows to start with.
Then I figure out which column I'm in, approximately, and put that in 'col'. (Each approximate column is 21 pixels wide.)
I then look at the pixel locations of the diagonal. I can use the y coordinate (modulo the half row height) as an index into the diagonal. If the x coordinate (modulo the column width) is LESS than the diagonal value, then I need to move the coordinate to the LEFT.
- Amit

This article was originally written by Alizerin

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
Action
First one on Tue 12th Jan 10pm
Yaw, this is the first blog post, just to begin with something. Nice site here, cool features like OSIDrive, and so on. However, strange that an "OpenSource Institute" uses non-OpenSource softare to host its site (Windows and IIS). It's not correct... I t
bb
A Daily Profanity at dailyprofanity.com on Mon 21st Dec 11am
For anyone who likes viz, and roger mellies profanisaurus. There's a website called amusing daily profanity which dishes up a humorous profanity every day via rss, twitter email and a few other ways. Rather rude words, but very funny in my opinion,
hambone
Blog entry for Wed 25th Nov 7pm on Wed 25th Nov 7pm
wtf i can't do geek 12. I don't know what to do. i want to kill myself becuz of this
haziman
Blog entry for Mon 9th Nov 4am on Mon 9th Nov 4am
for all geekos out there...
echmil
fuck you all!!!!!!!! on Sat 7th Nov 11pm
jag har tjock med tyngate tråkigt-.-
goldie
Blog entry for Tue 5th May 6am on Tue 5th May 6am
import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.io.*; import java.util.Properties; public class SENDMAIL { public void sendMail(String mailServer, String from, String to,
bb
Bubble Graph on Wed 11th Mar 12pm
I love this graph bubble graph http://www.osix.net:80/modules/folder/in dex.php?tid=28125&action=vf
ketan404
My online resume! on Mon 9th Mar 8am
It is here http://www.listoffreelancers.com/profile s/ketankulkarni Simple and clean design. I like this website. Ketan
macrocat
Blog entry for Sun 8th Mar 3pm on Sun 8th Mar 3pm
Another site with some challenges. Basically, I'm linking this to get a measly five points ;O. Hellbound Hackers
Nightscript
Parapsychology - Fri 19th Dec 5am on Fri 19th Dec 5am
Yes i'm crazy but heres what ive been thinking about and it seems more reasonable that a lot of reality. Note that this ties into parapsychology/psychokinesis research. Mind is not over matter. Thats the wrong state of thinking for sure. Look, this


     
Your Ad Here
 
Copyright Open Source Institute, 2006