How to detect the mouse within a hexagon?
in
Programming Questions
•
3 months ago
Hi ya'll, first post hurrah!
I've created a hexagon, but I'm stumped as to how I would detect if the mouse has entered within its specific boundaries. I thought that perhaps something along the lines of:
(dist (mouseX, mouseY, hex.x, hex.y) < hex.radius)
might suffice, however I am planning on placing these hexagons into a grid of hexagons. This would, of course, result in either a dead zone between hexagons, or 2 hexagons being selected.
So, any ideas?
Below is the code to create a single hexagon, with a boolean "userClicked" within the hex class waiting for the magic code to solve this issue. Thanks in advance everyone!
int w = 500;
int h = 500;
Hex hex = new Hex (w/2, h/2, 50, 50);
void setup() {
size (h, w);
}
void draw(){
background (255);
hex.drawHex();
}
class Hex {
float hexWidth = 50;
float hexHeight = 50;
float x = w/2;
float y = h/2;
float sideA_x, sideA_y;
float sideB_x, sideB_y;
float sideC_x, sideC_y;
float sideD_x, sideD_y;
float sideE_x, sideE_y;
float sideF_x, sideF_y;
float hexColor = 255;
boolean userClicked;
Hex (float tempX, float tempY, float tempWidth, float tempHeight) {
x = tempX;
y = tempY;
hexWidth = tempWidth;
hexHeight = tempHeight;
}
void drawHex () {
pushMatrix();
translate (x, y);
//Top point.
sideA_x = 0;
sideA_y = hexHeight*-1;
//Top left point.
sideB_x = hexWidth*-1;
sideB_y = (hexHeight*-0.5);
//Bottom left point.
sideC_x = hexWidth*-1;
sideC_y = hexHeight/2;
//Bottom point.
sideD_x = 0;
sideD_y = hexHeight;
//Bottom right point.
sideE_x = hexWidth;
sideE_y = hexHeight/2;
//Top right point.
sideF_x = hexWidth;
sideF_y = (hexHeight*-1) /2;
stroke(0);
strokeWeight(2);
//
//Attention! This is the problem part.
//
if (mousePressed && dist (mouseX, mouseY, x, y) < hexWidth || dist (mouseX, mouseY, x, y) < hexHeight) {
fill (0, hexColor, 0);
}
else {
fill (255);
}
//
//Attention! The problem part is above.
//
beginShape();
vertex(sideA_x, sideA_y);
vertex(sideB_x, sideB_y);
vertex(sideC_x, sideC_y);
vertex(sideD_x, sideD_y);
vertex(sideE_x, sideE_y);
vertex(sideF_x, sideF_y);
endShape(CLOSE);
popMatrix();
}
}
I've created a hexagon, but I'm stumped as to how I would detect if the mouse has entered within its specific boundaries. I thought that perhaps something along the lines of:
(dist (mouseX, mouseY, hex.x, hex.y) < hex.radius)
might suffice, however I am planning on placing these hexagons into a grid of hexagons. This would, of course, result in either a dead zone between hexagons, or 2 hexagons being selected.
So, any ideas?
Below is the code to create a single hexagon, with a boolean "userClicked" within the hex class waiting for the magic code to solve this issue. Thanks in advance everyone!
int w = 500;
int h = 500;
Hex hex = new Hex (w/2, h/2, 50, 50);
void setup() {
size (h, w);
}
void draw(){
background (255);
hex.drawHex();
}
class Hex {
float hexWidth = 50;
float hexHeight = 50;
float x = w/2;
float y = h/2;
float sideA_x, sideA_y;
float sideB_x, sideB_y;
float sideC_x, sideC_y;
float sideD_x, sideD_y;
float sideE_x, sideE_y;
float sideF_x, sideF_y;
float hexColor = 255;
boolean userClicked;
Hex (float tempX, float tempY, float tempWidth, float tempHeight) {
x = tempX;
y = tempY;
hexWidth = tempWidth;
hexHeight = tempHeight;
}
void drawHex () {
pushMatrix();
translate (x, y);
//Top point.
sideA_x = 0;
sideA_y = hexHeight*-1;
//Top left point.
sideB_x = hexWidth*-1;
sideB_y = (hexHeight*-0.5);
//Bottom left point.
sideC_x = hexWidth*-1;
sideC_y = hexHeight/2;
//Bottom point.
sideD_x = 0;
sideD_y = hexHeight;
//Bottom right point.
sideE_x = hexWidth;
sideE_y = hexHeight/2;
//Top right point.
sideF_x = hexWidth;
sideF_y = (hexHeight*-1) /2;
stroke(0);
strokeWeight(2);
//
//Attention! This is the problem part.
//
if (mousePressed && dist (mouseX, mouseY, x, y) < hexWidth || dist (mouseX, mouseY, x, y) < hexHeight) {
fill (0, hexColor, 0);
}
else {
fill (255);
}
//
//Attention! The problem part is above.
//
beginShape();
vertex(sideA_x, sideA_y);
vertex(sideB_x, sideB_y);
vertex(sideC_x, sideC_y);
vertex(sideD_x, sideD_y);
vertex(sideE_x, sideE_y);
vertex(sideF_x, sideF_y);
endShape(CLOSE);
popMatrix();
}
}
1