🔒 Locked
point functions
Advertisement
Original Post
Hi!
I''m making a mathlib in C++. Right now I''m making a c2DPoint and a c3DPoint. I''ve never actually thought about it, but do you ever take one point and add it with another? Or subtract, multiplicate, divide? (I''m making the vector classes later, this is plane old points in space)
"No lies of sugar can sweeten the sournes of reality"
}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
Yes... well, soometimes.
Consider the problem of finding the distance between 2 points, or diferentiating a velocity along a vector (which can be described on a point structure).
Another use I put a point structure to is RGB colour-coding. For example, to graduate a colour, it helps to it them represented as a 3D point and increment each component.
Other uses will suggest themselves if you make your definition flexible enough.
Consider the problem of finding the distance between 2 points, or diferentiating a velocity along a vector (which can be described on a point structure).
Another use I put a point structure to is RGB colour-coding. For example, to graduate a colour, it helps to it them represented as a 3D point and increment each component.
Other uses will suggest themselves if you make your definition flexible enough.
I wouldn''t say you are adding and subtracting points. Points are zero dimensional objects that define a location in space. What you are doing is adding and subtracting the vectors that go from some fixed origin to the points, typically called directional vectors. So, you really should be looking at the vector math behind such operations.
Brendan
Brendan
I''ve seen several implementations where someone did something similar to this:
typedef VECTOR POINT;
typedef VECTOR POINT;
The most useful set of operations to limit yourself to:
vector = point - point
point = point + vector
Now this will mean that to get a vector representation for a point you will need to do something like
vector3 pvec = point - point3(0,0,0)
But I find it really can help understanding the logic (and no performance hit if everything is properly inlined. There are some cases where this might hinder performance but you can always get around them (and they''re rare enough for it not to matter).
As always though it''s a matter of taste and how your understanding of the math affects the way you code.
vector = point - point
point = point + vector
Now this will mean that to get a vector representation for a point you will need to do something like
vector3 pvec = point - point3(0,0,0)
But I find it really can help understanding the logic (and no performance hit if everything is properly inlined. There are some cases where this might hinder performance but you can always get around them (and they''re rare enough for it not to matter).
As always though it''s a matter of taste and how your understanding of the math affects the way you code.
quote:
Original post by JuNC
Now this will mean that to get a vector representation for a point you will need to do something like
vector3 pvec = point - point3(0,0,0)
You can also declare the constructors explicit, to inhibit implicit conversion :
class vector3
{
public:
explicit vector3( const point3& p );
};
vector3 pvec = point; // Error, explicit constructor
vector3 pvec = vector3( point ); // OK
This lets you preserve performance and correctness. And you can define conversions going both ways without confusing your compiler with ambiguities.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
Essentially, you can regard points as a specialization of a vector.
Vectors can be scaled, added, and negated(which leads to subtraction).
scaling means this:
where k is a real(scalar), and V a nx1 vector;
kV = (k*x1,k*x2...k*xN)
Thats a coordinate representation of a vector; for normal math purposes its a column.
Adding you know, and negating is where you essentially multiply everything by -1.
Now, with a vector, what you can do it multiply it by a matrix, which will produce a linear transformation.
nxn matrix x a nx1 vector returns a vector.
This is a critical fact for graphics, both 2D and 3D.
You do most transformations using matrixes multiplied against vectors(read points).
~V''lion
Bugle4d
Vectors can be scaled, added, and negated(which leads to subtraction).
scaling means this:
where k is a real(scalar), and V a nx1 vector;
kV = (k*x1,k*x2...k*xN)
Thats a coordinate representation of a vector; for normal math purposes its a column.
Adding you know, and negating is where you essentially multiply everything by -1.
Now, with a vector, what you can do it multiply it by a matrix, which will produce a linear transformation.
nxn matrix x a nx1 vector returns a vector.
This is a critical fact for graphics, both 2D and 3D.
You do most transformations using matrixes multiplied against vectors(read points).
~V''lion
Bugle4d
quote:
vectors(read points).
Thats precisely the point I was trying to make, points are *not* vectors. They each have different properties (although some are similar). For instance, you never translate a vector, but you can translate points (which is equivalent to adding the translation vector to the vector representing the point, to produce a new vector representing the new point).
It''s perfectly acceptable to just use vectors but I think if you do make use of both then there should be a clear separation of properties.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement