Entering two different outputs inside one variable?

There are different ways you could approach this, but I’ve tried to deviate as little as possible from your code. Here’s a working version for the x-coordinates:

def circle_x(x):
    if x-diameter/2 < left_border:
        return left_border + circle_radius(diameter)
    elif x+diameter/2 > right_border:
        return right_border - circle_radius(diameter)
    return x

...

def draw():
    ...
    ellipse(circle_x(mouseX), ...

Your diameter, top_border, bottom_border, left_border, and right_border variables are in the global scope, so there’s no need to pass them to your functions. Also, you might prefer to use the circle() function if you’re not drawing ellipses. It’s fairly new to Processing, but it’s handy.

3 Likes