how to limit mousePressed range into an ellipse

edited November 2014 in How To...

Hello, first post and I have a relatively quick question. I'm writing a program in which the user has to press a button and the button is an ellipse. How do I limit the range of available pressing area into an ellipse. Right now I have it as -

if (mouseX > width/2-55 && mouseX < width/2-5 && mouseY > 335 && mouseY < 385 && mousePressed) {

}

This is simply limiting the pressing area to the rectangle that the ellipse fits into. How do I make the click box an ellipse the same size as the object? This is the ellipse btw.

ellipse(width/2-30, 360, 50, 50);

Thanks. Sorry if this violates any rules. I will flag it completed or delete the post once I have some help.

Answers

  • edited November 2014

    In this online game example, which uses ellipse() balloons:
    http://studio.processingtogether.com/sp/pad/export/ro.9V7R1wu55fOiN/latest

    It uses this method for mouse-within-ellipse-area detection:

    boolean isHit() {
      return sq(mouseX - x)/(rw*rw) + sq(mouseY - y)/(rh*rh) < 1;
    }
    

    Where x & y are Balloon's coordinates and rw & rh are its radius. :-B

  • You don't "limit mousePressed range", you just check that the mousePressed event happened within your ellipse.

    Since your ellipse is just a circle, you can use the dist() function to check if the click coordinates falls within a given distance of the center of the circle.

  • edited November 2014

    Since your ellipse is just a circle,...

    Dunno how I've missed that was a mere circle, and not an ellipsoid shape! 8-}
    A much simplified formula for circles: :bz return sq(mouseX - x) + sq(mouseY - y) < rad*rad;

Sign In or Register to comment.