lairworks / nas2d-core

NAS2D is an open source, object oriented 2D game development framework written in portable C++.
http://nas2d.lairworks.com
zlib License
10 stars 5 forks source link

Add Point and Vector component operations to Rectangle #306

Closed ldicker83 closed 4 years ago

ldicker83 commented 4 years ago

From #303: Rectangle should provide ways to get corner Points, or a size Vector. From there, arithmetic can be done on the Point and Vector components, which can then be used to create a new Rectangle.

As part of that, Rectangle should be constructible from either two Points, or from a Point and a Vector (size).

Originally posted by @DanRStevens in https://github.com/lairworks/nas2d-core/issues/303#issuecomment-570127304

As a note, the title of this issue is very poor but I'm tired and can't think of a better way to name this.

cugone commented 4 years ago

We don't have a Vector class yet, but constructing from two Point_2d or Point_2df can be done as mins and maxs (i.e. the smallest x/y and largest x/y) then derive the mW and mH values to reduce confusion with Rectangle(x, y, w, h).

DanRStevens commented 4 years ago

I'm a little unclear what you mean by mins and maxs, though mathematically a Vector is defined by Point subtraction:

Vector vector = p2 - p1;

This implies the Point class has an operator- that looks something like:

Vector Point::operator-(Point rhs);

There would be no corresponding operator-=, since the data type of the result is different from operator's class type.

cugone commented 4 years ago

mins: The smallest x value and smallest y value pair contained by the rectangle i.e. a corner of the rectangle. maxs: The largest x value and largest y value pair contained by the rectangle i.e. the opposite corner of the rectangle.

I have to be deliberately vague because "smallest" by default according to Allegro 5/SDL2/windows is towards the upper left of the screen but to OpenGL it's the lower left and DirectX/Vulkan is whatever you want it to be when you set up your viewports. Similar but different for "largest".

The math still works correctly and independently of screen coordinate orientation (handedness) when using mins/maxs instead of x/y/w/h

DanRStevens commented 4 years ago

Ahh. Yes. I'd have been tempted to name them something like BottomLeft and TopRight. That would be context specific though. I suppose more general names would be in order.

I would also be tempted to number the points, though that's not very descriptive.