Skip to main content
gamedev.net
🔒 Locked

point functions

Started by Seriema 11 replies 700+ views
Original Post
Seriema
Seriema
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]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
Zipster
Zipster
You are always adding and subtracting points, but multiplication and division of points isn''t ordinarily defined, unless you give it your own definition.
stevie56
stevie56
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.



StevieDon't follow me, I'm lost.
Punty50
Punty50
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"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
Thunder_Hawk
Thunder_Hawk
I''ve seen several implementations where someone did something similar to this:
typedef VECTOR POINT;
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
JuNC
JuNC
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.
Fruny
Fruny
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
JuNC
JuNC
Yeah, but to me that destroys the understanding of the operation, I''m not converting a point to a vector, I''m subtracting one point from a point at the origin. Obviously this depends on how you want to look at the operation.
Seriema
Seriema
ok, but I''ll be making a cVector later These are just points...

so addition and subtraction are what is used, division and multiplication isn''t needed?

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made c
Punty50
Punty50
Hm- I thought the general direction of this conversation was you are adding vectors, but if you want to define addition and subtraction so that (0,4) + (3,0) = (3,4), then yes, addition and subtraction are going to be your major operations.
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
JuNC
JuNC
Yeah, points really don''t do much, they sit in space and get transformed. Vectors are much more interesting.
Vlion
Vlion
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
~V'lionBugle4d
JuNC
JuNC
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.