I am wondering what is the best way to position rectangles, the ways I have thought of are:

  1. Have an x and y at upper left, then have a width and height

  2. Have an x and y at upper left, and an x and y at lower right

  3. Have an x and y at center, and have width and height ("radii" of the rectangle)

I guess that the third way might make the most sense since it's the closest to how I would do a circle (position and radius) but I do not know if it is the best way to do it.

I would prefer to stay away from the second way, as I would want only one part for position

Is there a preferred way to do this?


You want method 1 or 3 probably. 2 is generally not preferred because you need to update 2 points whenever the rect moves as oppose to 1 point in method 1 or 3.

| improve this answer | |
  • \$\begingroup\$ Is one better than the other? Method 3 would make the format just like a circle, but I'm not sure if it's actually more useful? \$\endgroup\$ – inithyn Dec 4 '14 at 20:12
  • \$\begingroup\$ Its trivial to convert between 1 and 3 and algorithms such as rect collisions, intersections etc can be written in the form of either 1 or 3. If you are working in 3D, method 3 would probably be slightly better because models are usually mapped with 0,0,0 being the center. 2D wise most rects are represented with x,y,w,h. \$\endgroup\$ – user55564 Dec 4 '14 at 20:14
  • \$\begingroup\$ I guess it's more of a matter of preference then? How would one make tile positions? Let's say you sent a character to position (1,1), should he end up in the center, or at the side, or does that really matter, sorry, i may be overthinking this \$\endgroup\$ – inithyn Dec 4 '14 at 20:19
  • \$\begingroup\$ If you are consistent in your usage then it doesnt matter, ie, if your character has the same size as tile then he should always end in being in the center of tile graphically if they have the same position. This is true if both are mapped with x,y being their left top corner or their middle. \$\endgroup\$ – user55564 Dec 4 '14 at 20:21
  • \$\begingroup\$ Hmm, but what if there is an entity that is bigger, do people normally have it at the center or the side? \$\endgroup\$ – inithyn Dec 4 '14 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.