#include ''vector2.h'' t_V2 ConsV2( float x, float y); t_V2 * DupV2(t_V2 in); int IsEqualV2(t_V2 v1, t_V2 v2); t_V2 DiffV2( t_V2 v1, t_V2 v2); t_V2 AddV2( t_V2 v1, t_V2 v2); t_V2 ScalV2( t_V2 v1, float ratio); float DotPV2( t_V2 v1, t_V2 v2); float CrossV2( t_V2 v1, t_V2 v2); t_V2 NormV2( t_V2 v); t_V2 NormClockV2( t_V2 v); t_V2 NormAClockV2( t_V2 v); float MagnV2( t_V2 v1 ); float MagnSqrV2( t_V2 v1 ); float AngleV2(t_V2 v1, t_V2 v2); float PolarAngleV2(t_V2 v); float DistV2( t_V2 point1, t_V2 point2); float DistVtoLine(t_V2 Point, t_V2 PonLine1, t_V2 PonLine2); t_V2 IntSecV2(t_V2 s1, t_V2 e1, t_V2 s2,t_V2 e2,float *p1,float *p2); t_V2 ProjV2( t_V2 Point, t_V2 PonLine, t_V2 LineDir); t_V2 ProjV2_v1( t_V2 Point, t_V2 PonLine, t_V2 LineDir); t_V2 RotV2 (t_V2 v, float rads); #define V2PI 3.14159265 #define Deg2Rad(a) ((a)*V2PI/180) #define RadV2eg(a) ((a)*180/V2PI)
typedef struct {float x,y;} t_V2;
They two components are accessed directly:
t_V2 v; v.x = 10; v.y = 20;
Vectors are passed in and returned out of functions by value (ie. vectors are copied in/out). Unless explicitly requested by user (by a call to DupV2), no dynamic memory is allocated.
DiffV2(v1,v2) returns the difference v1-v2. AddV2(v1,v2) returns the sum of v1,v2. ScalV2(v,f) scales v be factor f (can be negative). DotPV2(v1,v2) returns the dot product of v1.v2. CrossV2(v1,v2) returns the cross product v1xv2. Note that, unlike for DotPV2, the order of arguments is important.
NormV2(v) returns a vector n which is normal to v and of equal magnitude. Of the two normal vectors satisfying the condition the one that makes the pair (n,v) a right-handed orthogonal base was chosen (n is v rotated 90 degrees clockwise). NormClockV2 is identical to NormV2. NormAClockV2(v) returns the vector normal to v obtained by rotating v anti-clockwise.
MagnV2(v) returns the Euclidian norm of v. MagnSqrV2(v) returns the
square of the Euclidean norm, ie. the sum of squares of its components
(avoiding the call to sqrt makes MangSqr significantly faster than
MagnV2).
DistV2(v1,v2) returns the Euclidian distance between endpoints v1,v2.
AngleV2(v1,v2) returns the (orientated) angle between v1,v2 in the
range -PI,PI. If v1 can be aligned with 'v2' by an anti-clockwise
rotation (of v1) then AngleV2 returns a positive number.
If the norm of either of the vectors is close to zero (smaller then
ZERO_V2, see the include file), UNDEF_V2 is
returned (defined in the include file).
PolarAngleV2(v1) returns the angle between vector 'v1' and the x-axis.
The angle is positive if v.y is greater then 0.
If the norm of 'v1' close to zero (smaller then ZERO_V2), UNDEF_V2 is
returned (defined in the include file).
IsEqualV2(v1,v2) returns true (a non-zero value) if the two vectors are identical, ie. if both components are equal. The user should bear in mind the limitation of the '==' equality test for type 'float';
MagnSqrV2(DiffV2(v1,v2)) < EPSILONmight be safer.
ProjV2(p,pointOnLine,LineDirection) returns the perpendicular projection (foot) of the point 'p' on a line defined by a point 'pointOnLine' and direction 'LineDirection' (ie. the line uses the point + vector representation). DistVtoLine(p,pL1,pL2) returns the distance of the point 'p' from a line defined by two points pL1, pL2.
IntSecV2(s1,e1,s2,e2,p1,p2) computes an intersection of two lines. The first line is defined by two points s1,e1; the second by s2, e2. Parameters p1 (p2) define the position of the intersection:
intersect = s1 + p1 * (e1-s1) (mathematical notation) = s2 + p2 * (e2-s2) (mathematical notation) = AddV2(s1,ScalV2(DiffV2(e1,s1),p1) (library functions) = AddV2(s2,ScalV2(DiffV2(e2,s2),p2) (library functions)Therefore if line *segments* (s1,e1) and (s2,e2) intersect, both p1 and p2 will be in the interval [0,1]. If p1<0 then the intersection lies on the halfline defined by s1 not containing e1. If p1>1 then the intersection lies on the halfline defined by e1 not containing s1. Meaning of other combinations of p1 and p2 can be easily inferred. If the two lines are (almost) parallel then vector (UNDEF_V2,UNDEF_V2) is returned and p1,p2 are set to UNDEF_V2 (see the include file).
RotV2(v,angle) rotates vector 'v' by 'angle' (in radians). A positive angle rotates the vector 'v' in the direction from x-axis to y-axis. The following two macros define convinient conversions between radians and degrees.
#define Deg2Rad(a) ((a)*PI/180) #define RadV2eg(a) ((a)*180/PI)