Procedures
The user-defined function-procedure TriangleOutline
draws a triangle outline. It draws a line for each of the three edges of a triangle.
The return type void (before the function name)
denotes that this function does not return a value, which makes it a procedure.
A procedure call can be used as a statement. This is in contrast to ordinary functions,
where a function call is used as an expression. In this program, the function-procedure
TriangleOutline is called three times.
Since function-procedures do not need to return a value, there is no need to use
return statement in function body.
The given function-procedure lineREdge
draws a line with a rounded edge.
The edges are intentionally drawn in a different color to make the inner
workings of this function-procedure more obvious.
The function-procedure lineREdge calls
built-in function-procedures linewp and
discp.
The function-procedure linewp draws a line of a specified width.
The function-procedure discp draws a disc.
These function-procedures were previously demonstrated.
This improvement of function-procedure lineREdge
adds a fourth parameter, named cl of type color.
The type of the second parameter, named b,
is point2D. When type of a parameter is not
given, it assumes the type of the previous parameter
(Note: this feature is available only in the ZedLX programming language).
The signature of this function is:
- void lineREdge(
- point2D a,
- point2D b,
- num width,
- color cl
- )
This program uses the function-procedure lineREdge
to draw a house shape in one stroke of the pen.
This version of function-procedure lineREdge
does not use a color parameter.
This program adds a function-procedure lineREdgeSleep
which makes the program wait after a line is drawn.
The function-procedure lineREdgeSleep calls
the function lineREdge. This is allowed, more precisely, a body of one
procedure can make calls to other functions or procedures.
Try changing the number 200 in the body of function-procedure lineREdgeSleep
to make the drawing slower or faster.
The function-procedure multiCircle draws a shape consisting
of many concentric circles.
This program defines two versions of function-procedure multiCircle.
The first version has 2 parameters, and the second version has an additional parameter
cl of type color.
This effectively makes the parameter cl optional.
In this example, two different function-procedures share the same name
multiCircle. When two differerent functions share the same name,
it is said that the name has been overloaded.
In this case, we can unify the signature of both functions into one. That signature would be:
- void multiCircle(
- point2D center,
- num radius,
- /color cl
- )
In this signature, the forward slash (/) indicates that the parameter
cl is optional.
The function-procedure multiCircle can produce
interesting moire patterns when it is employed in a simple animation.
Note: If the animation is too slow on your computer,
try reducing the value of variable size.
The given function-procedure stripedBox draws
a box consisting of many horizontal lines.
This version of function-procedure stripedBox
draws a striped box in color.
This program demonstrates the user-defined function-procedure boxGradient,
which draws a gradient between two given colors.
The gradient is produced in a manner similar to the previous example: many horizontal lines are drawn,
but in this case the lines are close together. First, the underlying line is drawn
with a color c1.
Then, a semi-transparent line is drawn above the first line with a color over.
A color over is derived from color c2 by modifying the opacity parameter
(.a). The opacity is set to increase downwards.
Unfortunately, the colors are not as vivid as expected and the entire box is semi-transparent.
This is due to arrangement of the coordinate system and pixel grid. In this arrangement,
a line with whole-pixel coordinates necessarily spans exactly in between two pixel rows.
Drawing such a line changes both rows by half the required intensity.
This program fixes the transparency issue by drawing lines at half-pixel coordinates.
Such lines are drawn in a single pixel row at full intensity.
Here is another way to fix the transparency issue.
The built-in function-procedure hline draws a horizontal line, but only on full pixels.
Both coordinates and the length must be of type int.
The getter function floor from type num
can be used to convert the coordinates to type int.
The getter function round from type num
is best used to convert the length to type int.
The signature of the hline procedure is:
- void hline(
- int x,
- int y,
- int length,
- /colorA color
- )