There are a few ways of tackling this, a simple way is to generate the x and y points first and then generate the width and height with maximum values of the frame size minus the location ie max rect width = frame width - x and max rect height = frame height - y