Why can't I do mouseX/mouseY?

Hi, so for a project that I'm doing I'm trying to divide mouseX by mouseY, however when I try I can't start the program because I get a divide by zero message. I'm guessing it's doing this because mouseY could POTENTIALLY end up at zero, even though it doesnt start off there, so it just blocks off the program from starting to prevent it from happening (that's my guess although I could be wrong). How can I fix this?

Answers

  • Answer ✓
    int mxy = 0;
    if (mouseY != 0)  mxy = mouseX/mouseY;
    
  • edited March 2017

    @john832 --

    I'm guessing it's doing this because mouseY could POTENTIALLY end up at zero

    No, this is an actual (runtime) error, not a potential one. On frame 1 mouseX and mouseY may each be initialized to zero, and updated thereafter with the first mouse cursor position on frame 2.

    int mxy = 0;
    void draw(){
      println(frameCount, mouseX, mouseY);
      mxy = mouseX/mouseY; // error happens here on frame 1
    }
    

    @GoToLoop's solution prevents the assignment on that first frame -- or anytime the mouse hits the top edge of the sketch frame thereafter.

Sign In or Register to comment.