Types point2D and colorA [7/8]
Types point2D 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.
The function boxFill can be used to draw a square.
The program displays a surreal image, and prints out a surreal question. Don't take it seriously. Also, why can't you grab the objects displayed on the monitor?
<< F2:Prev - - F4:Next >>