Learn creative coding writing simple programs

95. Is the mouse inside a square?

Calculating if the mouse is inside a circle is very easy. We just need to know the distance from the center of the circle to the mouse pointer, and if this distance is less than the radius of the circle, we are inside. The radius of the circle is the same as half the size of the circle.
Calculating if the mouse is inside a square is a bit more tricky. We need to know four things. Is the mouse below the top border of the square? is the mouse above the bottom of the square? is it on the left side of the right side of the square? is is on the right side of the left border? If those four conditions are all true, then the mouse pointer is inside the square.

Tags: point, rect, square, inside

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```