The type vector2D
A vector is an abstract concept. While one can imagine to be seeing
points, discs and triangles, a vector is an object that
cannot be seen. Therefore, it is impossible to draw a vector.
The given demonstrational program makes an attempt to visualize a vector.
The program draws many arrows. An individual arrow is not a vector.
However, if we were to group together all the arrows displayed on a single screen,
even including all the possible similar arrows that have not been displayed,
than this entire group together would form one vector.
Vectors are used to represent sizes, movements, velocities, forces, and accelerations.
Vectors do not have a position in space. They are, at the same time, everywhere and nowhere.
Similarly, quantities that we call size, velocity, force and
acceleration all lack a certain position in space (because those quantities are all vectors,
and vectors don't have a position in space).
Every vector has coordinate components. Two-dimensional vectors have
two coordinate components commonly named x
and y.
The type vector2D has two members named
.x and .y
that represent the vector's coordinate components. Those two members are both
getters and setters.
The getter .len from type
vector2D returns the length of a vector.
This member is not a setter, meaning that a new length
cannot be directly assigned to the vector.
The initializer for type vector2D accepts two
values that specify the coordinate components x
and y.
Uncomment the erroneous lines to see the error description.
It is possible to convert a point to a vector and reverse.
The getter .v from type
point2D returns the corresponding vector.
This vector has coordinate components equal the coordinates of the point.
A vector obtained in this manner is called a
position vector or a radius vector.
Reversely, the getter .p from type
vector2D returns the corresponding point.
An important difference between points and vectors is that a
vector has length .len.
Of course, points do not have lengths,
since such a concept does not make any sense.
A difference between two points is a vector.
The getter .vecTo from type
point2D returns the vector of difference
from one point to the other.
This program illustrates a vector with a
line ended by a disc.
The getter .vecTo from type
point2D is employed in this program
to compute a difference between the center point and the
mouse position.
The difference between two points is always a vector. Therefore,
the getter .vecTo returns a vector.
The vector v then stores this
vector of differece between the center point and
the mouse position.
A point can be 'moved' by a vector.
The getter function .add from type
point2D accepts an argument of type
vector2D.
The result of adding a vector to a point is a new point.
The position of the new point is what one would get if the original
point was moved by the given vector.
Getter functions always produce a new value,
they cannot change the original value, therefore the variable
p in this program is not modified by the getter add.
While a vector can be added to a point,
it is not allowed to add two points, because such operation makes no sense.
This program illustrates how to create motion by using vectors.
[Run the program]
The point position represents the balls's
current position. The initial position is (400, 500) .
The vector velocity represents the balls's
speed and direction.
The velocity is constant and equal to (2, -3) .
On each display frame, the position of the ball is
moved by the vector of motion velocity.
This rule is implemented in the penultimate statement of the
main loop:
position = position.add(velocity);
Exercise:
Change the initial value of vector velocity.
Try various positive and negative values for the vector's
coordinate components x and y.
This program is a small improvement over the previous program.
It uses the function vsync to create a smooth animation.
When the vsync function is employed,
the elapsed time between animation frames needs to be accounted for.
[Run the program]
The vector velocity represents the balls's
speed and direction.
The initial velocity is 20 pixels per second to the leftwards
and 50 pixels per second downwards.
The point position represents the balls's
current position.
On each display frame, the position of the ball is updated according
to the rule of motion vectors.
The rule is implemented in the penultimate statement of the
main loop:
position = position.add(velocity * elapsedTime);
This statement changes position of the ball by adding a
motion vector to the point position.
The position must be moved by farther
when the elapsed time between two frames is greater.
Therefore, the vector velocity must first be scaled by the
elapsedTime, before being added to the position.
Exercises:
1. In the source code, uncomment the line for printing the
point position on the display.
2. Change the initial value of vector velocity.
Try various positive and negative values for the vector's
coordinate components x and y.
This program allows a user to move a ball with a keyboard.
The movement is accomplished by using vector calculations.
[Run the program]
The vector velocity represents the balls's
speed and direction. The velocity is modified by pressing the arrow keys.
The point position represents the balls's
current position.
The program is very similar to the program on the previous page, except
that the code for keyboard key presses has been inserted into the main loop.
Exercise:
In the source code, uncomment the line for printing the
point position on the display.
This is, again, the program illustrating a vector by a
line ended by a disc.
This demonstrational program illustrates a vector with an
arrow. This program is virtually identical to the previous one, except
for the function DrawArrow.
While vectors cannot be drawn, an arrow can be used to
represent a vector. However, vectors are not arrows since arrows have a
beginning and an end, while a vector doesn't have such properties.
To draw the arrow, a function-procedure named
DrawArrow was written.
Functions and procedures are explained in the next two chapters.
The remaining part of the program (the top half, above the comment)
should be comprehensible.