Entering two different outputs inside one variable?

I am working on a program where a circle must be drawn only within the walls(4 rectangles on the borders), I managed to get the circle to be drawn inside two walls(right and top borders), but the other remaining two it still goes through them. I thought that I could use a mix of logical operators inside ellipse(), and in circle_y and circle_x too, and return two different outputs, but no success. I would really appreciate if someone enlighten me on this problem.

diameter = 50
top_border = 50
bottom_border = 250
left_border = 50
right_border = 350

def setup():
def circle_radius(diameter):
    return diameter/2    
def circle_x(x,diameter, left_border):
    return max(x, left_border + circle_radius(diameter))

def circle_y(y, diameter, top_border):
    return max(y, top_border + circle_radius(diameter))
def draw():
    global diameter, top_border, bottom_border, left_border, right_border
    #draws all 4 borders
    rect(0,0, width, top_border)
    rect(0,bottom_border, width, height)
    rect(right_border,0,width, height)
    #draws circle
    ellipse(circle_x(mouseX,diameter,left_border),circle_y(mouseY,diameter, top_border),diameter, diameter)
1 Like

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.


It helped a lot! Thanks for keeping it simple, since I am trying to do it as my own work and I don’t want to risk violating any of my school policies or seem like as if it’s cheating.

1 Like

Glad to hear, and good luck, @macedorrn.

Keep in mind that if you want to, for example, say “this is homework, so please no complete answers” then you can just add that to your initial post and forum members should respect it.


Okay, good to know!
Thank you!

Hi @macedorrn – just FYI, we updated our homework FAQ – you are still encouraged to ask future homework related questions if you follow the guidelines!


I think the page is down or something, because I can’t access it.

Sorry – I think that fixed the link.