<참고>
http://blog.yezhucn.com/dngdi/msdn_vector.htm
What Is a Vector?
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 { |
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
pt0 | A POINT structure containing the first coordinate of the two-dimensional vector. |
pt1 | A POINT structure containing the second coordinate of the two-dimensional vector. |
vect | A VECTOR2D 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
v0 | A pointer to a VECTOR2D structure containing the components of the first two-dimensional vector. |
v1 | A pointer to a VECTOR2D structure containing the components of the second two-dimensional vector. |
v | A 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
pt0 | A pointer to a POINT structure containing the first endpoint of the line. |
pt1 | A pointer to a POINT structure containing the second endpoint of the line. |
ptTest | A 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
v0 | A pointer to a VECTOR2D structure containing the first vector used for obtaining a dot product. |
v1 | A 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
v0 | A pointer to a VECTOR2D structure containing the first vector. |
v1 | A 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
ptScale | A pointer to a VECTOR2D structure containing the scaling values. |
v0 | A pointer to a VECTOR2D structure containing the first of two vectors to be combined linearly. |
v1 | A pointer to a VECTOR2D structure containing the second of two vectors to be combined linearly. |
v | A 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
v0 | A 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
v0 | A pointer to a VECTOR2D structure containing the vector for which a normal vector is sought. |
v | A 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
v0 | A pointer to a VECTOR2D structure containing the components of the two-dimensional vector to be scaled. |
dScaling | The value by which to scale the components of v0. |
v | A 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
v0 | A pointer to a VECTOR2D structure containing the components of the first two-dimensional vector. |
v1 | A pointer to a VECTOR2D structure containing the components of the second two-dimensional vector. |
v | A 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
v0 | A pointer to a VECTOR2D structure containing the vector for which a normal vector is sought. |
v1 | A pointer to a VECTOR2D structure containing the computed normal vector. |
ppnPointNormal | A 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
v0 | A pointer to a VECTOR2D structure containing the vector for which a normal vector is sought. |
v1 | A pointer to a VECTOR2D structure containing the original line converted to a vector. |
ppProj | A 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
v0 | A pointer to a VECTOR2D structure containing the first vector. |
v1 | A 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
v0 | A 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
v0 | A 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.
'Digital Image Processing' 카테고리의 다른 글
GDI+ DrawString, Font 오류 문제 (0) | 2014.08.14 |
---|---|
Hit Testing Lines and Curves (라인, 커브 등 오브젝트를 선택하는 방법) (0) | 2014.07.02 |
PtInRegion() API 대체 방법 (0) | 2014.07.02 |
Bitmap Memory 저장시 4 바이트 배수 처리 (1) | 2014.07.01 |