Search code examples
javacoordinatesrectangles

How do you determine the upper-left coordinates of a rectangle based on the center coordinates?


Based on the coordinate system, how would you determine the upper left coordinates of a rectangle if you are only given the center coordinates of the rectangle and the width and height?

For example, the center coordinates for a rectangle are (40,40) and the rectangle has a width of 90 and a height of 60.


Solution

  • If center is (x,y) width = w height = h

    Assuming the rectangle is sitting flat relative to the coordinate plane, the upper left corner is exactly (x-w/2, y-h/2).