Operations on Vectors [2/13]
Operations on Vectors
The getter function .alti from type vector2D returns string representing the given vector, with aligned coordinate component strings.
The getter function .alti accepts one or two arguments. The first argument specifies the number of characters before the decimal point, and the second argument specifies the number of characters after the decimal point
A corresponding getter function named .alti is also available from type point2D.
The getter function .alti is not really a function at all. It is a procedure, because it modifies the displayed output.
The getter function .mul from type vector2D multiplies a vector by a number. The number is given as the argument.
A vector is multiplied by multiplying all its coordinate components.
The multiplication operation multiplies the vector's length.
Points cannot be multiplied by a number, because such an operation has no sense. Therefore, the type point2D does not have a getter function .mul.
The getter function .mul is a pure function. Pure functions don't change anything, they just return a value.
This demonstrational program illustrates the operation of multiplying a vector by a number 2.
For drawing an arrow of a certain width, a function-procedure named DrawArrowW was written. This function uses some more advanced features. The remaining parts of the program (the top half) should be comprehensible.
Generally speaking, all vectors have a magnitude and direction. Magnitude is the length of the vector. Direction describes where the vector is pointing to. There is, however, one important exception.
A null-vector doesn't have a direction. The length of a null-vector equals 0, and it's coordinate components are also equal to 0.
The getter .is0 from type vector2D returns true when a vector is a null-vector.
The getter .is0 from type vector2D is a pure function.
A unit vector is any vector that has a length of 1. A unit vector can be created from an existing vector by applying the getter .unit.
The getter .unit from type vector2D is a pure function.
There is an exception: a unit vector cannot be created from a null-vector, because the null-vector does not have a direction. Consequentially, applying a getter .unit to a null vector raises a runtime error.
Uncomment the erroneous lines to see the error description.
This demonstrational program illustrates the use of unit vectors. Since a unit vector has a length of 1, which is too small to draw, this program draws a unit vector multiplied by a number 100, which always produces a vector of length 100.
The operation of creating a unit vector is not entirely exact. It is generally impossible to exactly compute a unit vector when the number of digits in the computation is finite. Therefore, the calculated unit vector and the vector of length 100 are the approximations of the exact result.
This demonstrational program illustrates the application of getter function .rotdg from type vector2D, which rotates the vector by a given angle. The getter function .rotdg takes one argument which represents the angle in degrees.
A full angle has 360 degrees.
Don't forget to rotate the vector by pressing the mouse button.
This program draws a circular shape consisting of many discs and lines.
The program uses getter function .rotdg from type vector2D to rotate the vector v by the angle i.
The program uses getter function .add from type point2D to move the point center by the vector vRot. This addition was explained in the chapter on points and vectors.
This program draws a star-like shape. The number of lines that make up the star is random.
The program uses getter function .rotdg to calculate endpoints of the star.
The getter .rotdg from type vector2D is a pure function.
This is a modification of the previous program. It draws regular polygons.
This modification of the previous program uses a double for loop to draw multiple concentric regular polygons.
The getter function .add from type vector2D adds one vector to another.
The addition of vectors is performed by adding up their respective coordinate components.
This demonstrational program illustrates the addition of vectors. The logic of the program is substantially more complex than in the previous examples, due to the necessity of switching between vectors.
<< F2:Prev - - F4:Next >>