Vector Arithmetic and Vector Drawings [1/10]
Vector Arithmetic and Vector Drawings
This program uses vector arithmetic to calculate vertices of a regular triangle. Then it displays those vertices as discs. Then, it connects the vertices with lines of different colors.
This program draws three circles with three loops, employing slightly different computations in each one.
In all three loops, the member function .rotdg from type vector2D rotates the vector v.
In the first loop, the member function .add from type point2D calculates the position of a point on a circle, by adding the vector vr to the circle's center point.
In the second loop, the arithmetic operator + is used instead of the member function .add to add a vector to a point.
In the last loop, the intermediate variable vr is eliminated by substitution.
Arithmetic operator + can be used instead of the memeber function .add to add points and vectors. However, not all combinations of types are allowed.
Arithmetic operator + can add two vectors, resulting in a new vector. It can not add two points.
Also, operator + can move a point by a vector. In this case, the point must be a left operand of operator + , and the vector must be a right operand. The opposite order is not allowed.
Operator - can be used in the same way as operator +.
Uncomment the commented lines to see the error descriptions.
This program draws three half-lines, employing slightly different computations in each one.
In all the loops, line points are drawn by the function discp. The vector mv represents the difference between two line points.
In the first loop, the member function .mul from type vector2D calculates the position of a point on a line, by multiplying the vector mv.
In the second loop, the arithmetic operator * is used instead of the member function .mul to multiply the vector mv.
The third line is decorative. It is drawn with a blue-red color gradient.
Arithmetic operator * can be used instead of the memeber function .mul to multiply vectors. However, not all combinations of types are allowed.
Operator * can multipliy a vector by a number, or a number by a vector. The result is a new vector. Points can not be multiplied.
Operator / can divide a vector by a number, where the vector is a left operand and the number is a right operand. The opposite is not allowed. Points can not be divided.
Uncomment the erroneous lines to see error descriptions.
This program combines a linear and a circular motion into a single one.
This program combines two circular motions into a single motion. The first circular motion has an amplitude of 200, and the second one has an amplitude of 50.
This program combines two circular motions into a single motion. In this case, both circular motions have the same amplitude equal to 150.
* * *
-- Orientations of Coordinate Systems --
The coordinate system commonly used in 2D computer graphics has the y-coordinate increasing downwards. This would be quite unusual in common mathematics, where the y-coordinate commonly increases upwards.
One of the consequences of this discrepancy reflects in the direction of rotation. In 2D computer graphics, the positive rotation (i.e. a rotation by a positive angle) is in the clockwise direction, while in common mathematics the positive rotation is anti-clockwise.
The choice of this coordinate system for 2D computer graphics is due to the scanning direction of electron guns in old televisions. A television's electron gun scans the screen from left to right and from top to bottom.
In any number of dimensions, two orientations of coordinate systems are possible. A long time ago, those two orientations were given some arbitrary names by mathematicians. The orientation of common 2D coordinate system in mathematics is called the right orientation, while the orientation of the coordinate system in 2D computer graphics is called the left orientation.
This program draws lines that connect two concentric circular motions.
Here is the random jumping rotating zombie program.
To slow down the jumping-around speed, change the value of variable speedMul to 0.5, or even to 0.3.
This size of this program prevents easy analysis. Besides that, the program employs linear interpolation, which involves a bit of mathematics. But, if you feel like it, with some peristence you can analyze and understand the program.
The resulting position is linearly interpolated between positions posStart and posEnd. This can be easily observed while the program is running, especially when it is slowed down.
The formula for linear interpolation between any two numbers, for example between angles a1 and a2 is:
- angle = a1 + t*(a2-a1)
In this formula, t is some number between 0 and 1. When t is close to 0, the resulting angle is close to angle a1. When t is close to 1, the resulting angle is close to angle a2.
<< F2:Prev - - F4:Next >>