# Check if mouse is in an arc

**URL:** <https://discourse.processing.org/t/check-if-mouse-is-in-an-arc/43154>\
**Category:** Coding Questions\
**Created:** [November 5, 2023, 8:21pm UTC](https://discourse.processing.org/t/check-if-mouse-is-in-an-arc/43154 "2023-11-05T20:21:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Batman](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Batman](https://discourse.processing.org/u/Batman)\
**Post date:** [November 5, 2023, 8:21pm UTC](https://discourse.processing.org/t/check-if-mouse-is-in-an-arc/43154/1 "2023-11-05T20:21:47Z")

</div>

"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 😄

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 5, 2023, 9:12pm UTC](https://discourse.processing.org/t/check-if-mouse-is-in-an-arc/43154/2 "2023-11-05T21:12:28Z")

</div>

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

Is the radius or the angle the problem?

---

<div class="post-metadata">

**Author:** ![Batman](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@Batman](https://discourse.processing.org/u/Batman)\
**Post date:** [November 5, 2023, 10:36pm UTC](https://discourse.processing.org/t/check-if-mouse-is-in-an-arc/43154/3 "2023-11-05T22:36:27Z")

</div>

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;  
}  
"
