Check if mouse is in an arc

"float cx, cy;
float arcRadius;
float startAngle, endAngle, rotation;

void setup() {
size(400, 400);
background(255);

cx = width / 2;
cy = height / 2;
arcRadius = 150;
}

void draw() {
background(255);
translate(cx, cy);
rotate(rotation);

// Calculate the angle between the mouse and the center of the arc
float angleToMouse = atan2(mouseY - cy, mouseX - cx);

// Calculate the distance from the mouse to the center of the arc
float d = dist(mouseX, mouseY, cx, cy);

// Check if the mouse is inside the arc by comparing the angle and distance
if (d <= arcRadius && angleToMouse >= startAngle && angleToMouse <= endAngle) {
fill(0, 255, 0); // Set fill color to green
} else {
fill(0); // Set fill color to black
}

noStroke(); // Remove stroke
arc(0, 0, arcRadius * 2, arcRadius * 2, startAngle, endAngle);
}

void mousePressed() {
// Generate new random values for the parameters
startAngle = random(TWO_PI);
endAngle = startAngle + random(TWO_PI);
rotation = random(TWO_PI);
}
"
I am just testing this code where I want to check if mouse is inside an arc for some reason this is not working I am not sure why. Thanks in advance for the help :smile:

1 Like

When you move your mouse over the arc, do you get positive results?

Is the radius or the angle the problem?

Thanks for the reply and I have fixed the problem this code now works "
float cx, cy;
float arcRadius;
float startAngle, endAngle, rotation;

void setup() {
size(400, 400);
background(255);

cx = width / 2;
cy = height / 2;
arcRadius = 150;
}

void draw() {
background(255);
push();
translate(cx, cy);

// Calculate the angle between the mouse and the center of the arc
float angleToMouse = atan2(mouseY - cy, mouseX - cx);
if (angleToMouse < 0) angleToMouse = TWO_PI - abs(angleToMouse);

// Calculate the distance from the mouse to the center of the arc
float d = dist(mouseX, mouseY, cx, cy);

if(endAngle > TWO_PI) {
//println(“greater”);
endAngle -= TWO_PI;
}

if(startAngle > TWO_PI) {
startAngle -= TWO_PI;
}

if(endAngle < startAngle) {
float temp = endAngle;
endAngle = startAngle;
startAngle = temp;
}

// Check if the mouse is inside the arc by comparing the angle and distance
if (d <= arcRadius && angleToMouse >= startAngle && angleToMouse <= endAngle) {
fill(0, 255, 0); // Set fill color to green
} else {
fill(0); // Set fill color to black
}

noStroke(); // Remove stroke
arc(0, 0, arcRadius * 2, arcRadius * 2, startAngle, endAngle);
pop();
}

void mouseClicked() {
// Generate new random values for the parameters
startAngle = random(TWO_PI);
endAngle = startAngle + random(TWO_PI);
rotation = random(TWO_PI);
startAngle += rotation;
endAngle += rotation;
}
"

1 Like