Operator Overloaded Sub Objects

Started by
9 comments, last by NotAYakk 17 years, 7 months ago
Quote:Original post by Xai
I have never yet seen a case where I didn't want to be able to apply the same set of functionality to a vector and a point.

I DO what to scale points (grab the corner, multiple it by a scale factor, reset it) ... I DO want to add, difference, average two points (average of 2 points is there midpoint, difference of two points is the line segment which would connect them - and usefull for collision detection).


The difference of two points is a vector.

P=P+V // you can use a vector and add it to a point
V=P-P // vectors are differences between points
V=S*V // vectors can be scaled
V=0 // the zero vector makes sense

You don't want to scale a point. You want to take a set of points, subtract away a point from each element of the set and get a set of vectors, scale the set of vectors, then add a point to each element of the set and get a set of points back.

The average of two points:
V0 = (P2-P1)
V0 = V0/2
Pavg = P1 + V0

Quote:I think you have fallen into the OO trap without realizing it. You have made distinct objects out of concepts that are fundamentally one idea with multiple names. The fundamental advancement of math and science is to unify all that may be unified - and nothing that may not. So if the answer to the question "when would it not work to use an X instead of a Y" would be "never" then they should be one class.


The thing is, from a mathematical standpoint, points and vectors can quite rightly be viewed as members of different spaces.

The space of positions has no origin. There is no location in 3space that is special -- if you take all of 3space, and translate it left 500 km, you get the same space back.

The space of differences in position has an origin. Two things being at the same spot makes sense. If you take the space of differences in location, and translate it left 500 km, you get nonsense.

You missed another question. If "is there an operation that makes sense on X but not on Y", then you don't want X and Y to be the same class.

The advantage of splitting points and vectors is you get your compiler to make certain you aren't a dumbass and scale a point, or take a difference between a vector and a point.

This topic is closed to new replies.

Advertisement