본문 바로가기
Digital Image Processing

Use of Two-Dimensional Vectors with Windows NT

by leo21c 2014. 7. 2.
SMALL

<참고>
http://blog.yezhucn.com/dngdi/msdn_vector.htm


What Is a Vector?

If you are thinking "Just what is a vector?", this section is for you! It's really not uncommon to wonder what a vector is. If your graphics programming experience has been limited to Windows and your application development did not involve graphics editors of one sort or another, you probably have never dealt with vectors. I will not attempt to describe vectors exhaustively; you can find the details in countless textbooks on the subject. I will describe them to such a point of understanding that we can move on to the practical use of vectors within a two-dimensional graphics application.

An important distinction to make up-front is that a vector is not a point, although the representation is similar for both. For example, a point, p, is typically described using an ordered pair of numbers, for example p = (100, 200). This is commonly interpreted as the point located 100 units in the x direction and 200 units in the y direction from some well-defined and consistent origin. The vector p (denoted as a vector by using bold text), where p = (100, 200), is interpreted very differently. The vector describes displacement. It may originate anywhere in the coordinate system. In this example, the vector is displaced 100 units in the x direction and 200 units in the y direction from some arbitrary point.

Figures 1 and 2 illustrate these concepts. In Figure 1, a line segment is drawn between the two points. In my attempt to distinguish between points and vectors, the important thing to note is that the points are positions within a coordinate system denoted by the ordered pairs (1, 1) and (2, 2).

Figure 1. Line segment between the points (1, 1) and (2, 2)

Figure 2 shows the vector defined by the same two points?(1, 1) and (2, 2). Because vectors describe displacement, the vector can be represented by the 2-tuple (1, 1). That is, the vector is displaced 1 unit in the x direction and 1 unit in the y direction.


Figure 2. Vector associated with the points (1, 1) and (2, 2)

That brings us to a very useful definition of a vector. The vector p is defined by the difference between the two points used to place the vector in the coordinate space: p = (x2 - x1, y2 - y1). In the example above, the vector p = (2 - 1, 2 - 1), which is simply (1, 1). The vector p in figure 2 is said to be bound to the point (1, 1), although, as Figure 3 illustrates, this vector could easily be bound to any point in the coordinate space. Note that all three vectors are described by the 2-tuple (1, 1).


Figure 3. Vectors may be bound to any point

So much for what a vector is and what it isn't. Let's move on to the various operations that can be performed on vectors.


The Vector Space

Vector space? As in "beam me up"? Not quite. As Foley et al. explain it, the vector space is a set composed of vectors upon which addition and scalar multiplication can take place. There are tons of analogies that can give you a sense of what the vector space is. You may want to insert your own at this point. Here are a few of my favorites: Think of the vector space as a croquet court. The vectors are represented by the motion of the balls as you hit them. Vector addition takes place when you place your ball next to your opponent's and smack it 50 feet in the opposite direction. Or there is the pool table analogy. Forget the balls for a moment. Just consider the pool cue. The pool cue has a length, as does a vector. Like a vector, the pool cue is bound to an origin that moves around the table as you try to line up shots. This example helps you think of the vector space in terms of a plane. This should give you a feel for the vector space. So as not to belabor it, let's move on to the motivation for using vectors.


Why Use Vectors?

Good question. I have some simple answers. Vectors are convenient. They allow you to do complex things using simple concepts. This simplicity is derived from the fact that, through vectors, many geometric concepts can be expressed algebraically with little consideration of the coordinate system. Vectors simplify your life. Once you have written a vector library, solutions to various graphics problems are reduced to creative use of the functions in your library.


Vector Operations

Remember what I said about the vector space? It includes addition and scalar multiplication of vectors. These are the two basic operations from which all vector use is derived. This section discusses vector addition, multiplication, and a few other operations. But remember, each additional operation is simply a use of the two operations found in the vector space?addition and multiplication.

Before jumping off into each operation, let's define the structure used to define a two-dimensional vector in this article and used in the C implementation of each operation. The following structure is composed of two values, the x and y components of the vector. These are doubled to permit the use of real number values. Real numbers are required to fulfill the requirement of scalar multiplication (multiplication by real numbers) in the vector space.

typedef struct tagVECTOR2D  { 
  double     x;
  double     y;
} VECTOR2D, *PVECTOR2D;

Throughout much of this article I will use two vectors, a and b. The a vector is represented by the 2-tuple (4, 5). The b vector is represented by the 2-tuple (2, 3).


VECTOR2D.DLL

VECTOR2D.DLL is a dynamic-link library for Windows NT. It includes all of the functions described in this article?and some functions not discussed in the article. The source code is included. You may use the DLL as-is or add functions to it.

The following macro and functions are contained within the DLL.

POINTS2VECTOR2D

The POINTS2VECTOR2D macro converts two points to a two-dimensional vector.

Syntax

POINTS2VECTOR2D(pt0, pt1, vect)

Parameters

pt0POINT structure containing the first coordinate of the two-dimensional vector.
pt1POINT structure containing the second coordinate of the two-dimensional vector.
vectVECTOR2D structure in which the x and y components of the two-dimensional vector are placed.

Return value

None.

vAddVectors

The vAddVectors function adds the components of one two-dimensional vector to another. The resultant vector c = (a1 + b1, a2 + b2).

Syntax

PVECTOR2D vAddVectors(PVECTOR2D v0, PVECTOR2D v1, PVECTOR2D v);

Parameters

v0A pointer to a VECTOR2D structure containing the components of the first two-dimensional vector.
v1A pointer to a VECTOR2D structure containing the components of the second two-dimensional vector.
vA pointer to a VECTOR2D structure in which the components of the two-dimensional vector obtained from the addition of the first two are placed.

Return value

A pointer to a VECTOR2D structure containing the new vector obtained from the addition of the first two parameters.

vDistFromPointToLine

The vDistFromPointToLine function computes the distance from the point ptTest to the line defined by endpoints pt0 and pt1. This is done by resolving the vector from pt0 to ptTest into its components. The length of the component vector that is attached to the head of the vector from pt0 to ptTest is the distance of ptTest from the line.

Syntax

double vDistFromPointToLine(LPPOINT pt0, LPPOINT pt1, LPPOINT ptTest);

Parameters

pt0A pointer to a POINT structure containing the first endpoint of the line.
pt1A pointer to a POINT structure containing the second endpoint of the line.
ptTestA pointer to a POINT structure containing the point for which the distance from the line is to be computed.

Return value

A double value that contains the distance of ptTest to the line defined by the endpoints pt0 and pt1.

vDotProduct

The vDotProduct function computes the dot product of two vectors. The dot product of two vectors is the sum of the products of the components of the vectors; that is, for the vectors a and b, the dot product = a1 * a2 + b1 * b2.

Syntax

double vDotProduct(PVECTOR2D v0, PVECTOR2D v1);

Parameters

v0A pointer to a VECTOR2D structure containing the first vector used for obtaining a dot product.
v1A pointer to a VECTOR2D structure containing the second vector used for obtaining a dot product.

Return value

A double value containing the scalar dot product value.

vIsPerpendicular

The vIsPerpendicular function determines if two vectors are perpendicular to one another by testing the dot product of the two vectors. If the dot product is zero, the vectors are perpendicular.

Syntax

BOOL vIsPerpendicular(PVECTOR2D v0, PVECTOR2D v1);

Parameters

v0A pointer to a VECTOR2D structure containing the first vector.
v1A pointer to a VECTOR2D structure containing the second vector.

Return value

TRUE if the two vectors are perpendicular; FALSE if the vectors are not perpendicular.

vLinearCombination

The vLinearCombination function scales the components of two vectors and adds them together to form a new vector having the linear combination. The resultant vector where u and v are scaling factors and a andb are vectors is c = ua + vb.

Syntax

PVECTOR2D vLinearCombination(PVECTOR2D ptScale, PVECTOR2D v0, PVECTOR2D v1,
   PVECTOR2D v);

Parameters

ptScaleA pointer to a VECTOR2D structure containing the scaling values.
v0A pointer to a VECTOR2D structure containing the first of two vectors to be combined linearly.
v1A pointer to a VECTOR2D structure containing the second of two vectors to be combined linearly.
vA pointer to a VECTOR2D structure in which the results of linearly combining vectors v0 and v1 are stored.

Return value

A pointer to a VECTOR2D structure containing a vector that is the result of the linear combination.

vNormalizeVector

A normalized vector is a vector with a length of 1. The resultant vector is often called a unit vector. ThevNormalizeVector function converts a vector into a normalized vector. To normalize a vector, the vector is scaled by the reciprocal of the magnitude of the vector: cn = c * 1/|c|.

Syntax

void vNormalizeVector(PVECTOR2D v0);

Parameter

v0A pointer to a VECTOR2D structure containing the vector to normalize.

Return value

Void.

vNormalVector

The vNormalVector function computes the vector that is normal to a given vector. For the vector a, the normal vector n = (-ay, ax).

Syntax

PVECTOR2D vNormalVector(PVECTOR2D v0, PVECTOR2D v);

Parameters

v0A pointer to a VECTOR2D structure containing the vector for which a normal vector is sought.
vA pointer to a VECTOR2D structure containing the computed normal vector.

Return value

A pointer to a VECTOR2D structure containing the normal vector.

vScaleVector

The vScaleVector function scales the components of a vector by a user-supplied scaling factor.

Syntax

PVECTOR2D  APIENTRY vScaleVector(PVECTOR2D v0, double dScaling, PVECTOR2D v);

Parameters

v0A pointer to a VECTOR2D structure containing the components of the two-dimensional vector to be scaled.
dScalingThe value by which to scale the components of v0.
vA pointer to a VECTOR2D structure in which the results of multiplying (scaling) the components of v0 by dScaling are stored.

Return value

A pointer to a VECTOR2D structure containing the scaled vector.

vSubtractVectors

The vSubtractVectors function subtracts the components of one two-dimensional vector from another. The resultant vector c = (a1 - b1, a2 - b2).

Syntax

PVECTOR2D vSubtractVectors(PVECTOR2D v0, PVECTOR2D v1, PVECTOR2D v);

Parameters

v0A pointer to a VECTOR2D structure containing the components of the first two-dimensional vector.
v1A pointer to a VECTOR2D structure containing the components of the second two-dimensional vector.
vA pointer to a VECTOR2D structure in which the components of the two-dimensional vector obtained from the subtraction of the first two are placed.

Return value

A pointer to a VECTOR2D structure containing the new vector obtained from the subtraction of the first two parameters.

vPointNormalForm

The vPointNormalForm function computes the components of the point normal equation of a line in a plane vector that is normal to a given vector. For the vector a, the normal vector n = (-ay, ax).

Syntax

BOOL vPointNormalForm(PVECTOR2D v0, PVECTOR2D v1, PPOINTNORMAL ppnPointNormal);

Parameters

v0A pointer to a VECTOR2D structure containing the vector for which a normal vector is sought.
v1A pointer to a VECTOR2D structure containing the computed normal vector.
ppnPointNormalA pointer to a VECTOR2D structure to contain the normal vector.

Return value

TRUE if the normal vector is computed successfully; FALSE if it is not.

vProjectAndResolve

The vProjectAndResolve function resolves a vector into two vector components. The first is a vector obtained by projecting vector v0 onto v1. The second is a vector that is perpendicular (normal) to the projected vector. It extends from the head of the projected vector v1 to the head of the original vector v0.

Syntax

void vProjectAndResolve(PVECTOR2D v0, PVECTOR2D v1, PPROJECTION ppProj);

Parameters

v0A pointer to a VECTOR2D structure containing the vector for which a normal vector is sought.
v1A pointer to a VECTOR2D structure containing the original line converted to a vector.
ppProjA pointer to a PROJECTION structure containing the resolved vectors and their lengths.

Return value

Void.

vVectorAngle

The vVectorAngle function computes the cosine of the angle between two vectors.

Syntax

double vVectorAngle(PVECTOR2D v0, PVECTOR2D v1);

Parameters

v0A pointer to a VECTOR2D structure containing the first vector.
v1A pointer to a VECTOR2D structure containing the second vector.

Return value

A double value indicating the cosine of the angle between the two vectors.

vVectorMagnitude

The vVectorMagnitude function determines the length of a vector by summing the squares of each component of the vector. The magnitude is equal to a.x * a.x + a.y * a.y.

Syntax

double vVectorMagnitude(PVECTOR2D v0);

Parameter

v0A pointer to a VECTOR2D structure containing the vector upon which to determine the magnitude.

Return value

A double value that is the magnitude of the vector.

vVectorSquared

The vVectorSquared function squares each component of the vector and adds them together to produce the squared value of the vector. SquaredValue = a.x * a.x + a.y * a.y.

Syntax

double vVectorSquared(PVECTOR2D v0);

Parameter

v0A pointer to a VECTOR2D structure containing the vector upon which to determine the squared value.

Return value

A double value that is the squared value of the vector.

LIST