Types point2D, vector2D and colorA [1/13]
Types point2D, vector2D and colorA
The type point2D represents points. It has two members named .x and .y. These members are getters and setters.
Points are used to specify a position in space. The position is given by the coordinates (x,y).
The initializer for type point2D accepts two values that specify the coordinates x and y.
The function discp draws a disc, where the center of the disc is given by the first argument, of type point2D.
The function linep draws a line, where endpoints of the line are given by the first two arguments, of type point2D.
The getter function .dist from type point2D returns the distance between the given points.
The type colorA specifies a color with transparency. It has 4 members: .r, .g, .b, .a.
The member .a from type colorA specifies the transparency. Transparency can range from fully opaque, represented by the value 100, to fully transparent, represented by the value 0.
The type colorA has two initializers. The initializer with 3 arguments is virtually identical to the initializer of type color. The transparency is set to fully opaque, setting the value of member .a to 100.
The initializer with 4 arguments sets the desired values to all 4 members of type colorA: .r, .g, .b, .a.
A value of type color can be automatically converted to type colorA, where the value of member .a is set to 100.
The function linewp draws a line of a given width. The endpoints of the line are given by the first two arguments of type point2D, and line width is the third argument. The fourth argument is optional, it specifies the color.
The signature of this function is:
- void linewp(
- point2D p1,
- point2D p2,
- num lineWidth,
- /colorA cl
- )
The keyword void at the beginning of the signature indicates that this function does not return a value.
The forward slash / indicates that the parameter cl is optional. While the same symbol (slash /) is also used for named initializers of type color (like /blue and /yellow), these two usages of the slash symbol are unrelated.
The function trianglep draws a triangle, where the vertices are given by the first three arguments, of type point2D.
The fourth argument of function trianglep is of type colorA. This argument is optional.
The signature of this function is:
- void trianglep(
- point2D p1,
- point2D p2,
- point2D p3,
- /colorA cl
- )
The keyword void indicates that the function does not return a value.
The forward slash / indicates that the parameter cl is optional.
This program draws a simple shape in one stroke of pen.
After running the program, try modifying the initial value of variables size and lineWidth.
Then, try to figure out the meaning of variables x1, x2, y1, y2. Also, you can try changing them to some concrete numbers.
Then, try changing the value of variable p1 in the initializer. Use some concrete numbers.
The function pointerPos returns the cursor position. The return value is of type point2D.
The signature of function pointerPos is:
- point2D pointerPos()
The function mouseHide hides the mouse cursor. The signature of this function is:
- void mouseHide(
- /bool isHide
- )
The function mouseHide can accept 1 argument or none, because the only parameter is optional.
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.
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.
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 a vector with 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.
<< F2:Prev - - F4:Next >>