For this assignment, you will add to the drawing program new features that are similar to features that you already have. Apply object-oriented design concepts such as inheritance and polymorphism to make it easier to extend your program.
Instead of specifying transformations before the picture element, transformations will take as an argument a picture, which can be another transformation or picture element. There is also a new picture element, group, which groups a sequence of pictures. A transformation with a group as a parameter will apply the transformation to each member of the group.
You will add certain arithmetic operators, variable storage, and a looping construct to make it easier to build complex drawings. Variables store only numbers (described below). When encountering an assignment, evaluate the arithmetic expression immediately (strict evaluation) and store the value to the variable. Evaluate the reference to a variable as the current value stored for that variable name. The looping construct will iterate of a sequence of commands a specified number of times.
A drawing consists of zero or more commands. A command can be an assignment, a drawing parameter setting, a picture, or a control structure. Each command is written with a command name and parameters separated by whitespace and surrounded by parentheses. The parameters of a command can be numerical expressions or other pictures. A picture is either a picture element or a transform of a picture.
* Numerical Expressions
A numerical expression can be a primitive expression or compound expression. Primitive expressions are as follows:
- N where N is a floating-point number (e.g. 0, -1, 3.14)
- X where X is a symbol. A symbol is a sequence of non-whitespace characters other than parentheses and must start with a letter ('A'-'Z', 'a'-'z'). This is a variable with a corresponding value. The value of the variable is determined by the last assignment to that variable. If X does not have a value, then the expression is invalid and is prohibited in the input language.
Compound numerical expressions are compositions of other numerical expressions (primitive or compound):
- (+ A B) The sum of A and B
- (- A B) The difference of A and Bi
- (* A B) The product of A and B
- (/ A B) The quotient of A and B
- (sin A) The sine of A degrees
- (cos A) The cosine of A degrees
All numbers should be handled in double-precision floating-point.
A variable assignment is expressed as follows:
(:= X E) Where X is a symbol and E a numerical expression This command will evaluate the numerical expression E, and then store that value to the variable X. Any subsequent references to X before the next assignment to X will evaluate to that value. For example, the following code:
(:= X 1) (linewidth X) (:= X 2) (line 0 0 X X) should produce
0 0 moveto
2 2 lineto
Create picture elements using the commands below.
- (line X0 Y0 X1 Y1) Draw a line from (X0, Y0) to (X1, Y1).
- (rect X Y W H) Draw the outline of a rectangle whose lower left corner is at (X,Y), whose width is W, and whose height is H. The rectangle should be drawn counter-clockwise starting from the lower left corner.
- (filledrect X Y W H) Same as rect, but fill the rectangle rather than simply drawing its boundary.
- (tri X Y R) Draw the outline of a equilateral triangle whose center is at (X, Y) with one of the vertices at (X + R, Y).
- (filledtri X Y R) Same as tri, but fill the triangle rather than simply drawing its boundary.
- (square X Y R) Draw the outline of a square whose center is at (X, Y) with one of the vertices at (X + R, Y).
- (filledsquare X Y R) Same as square, but fill the square rather than simply drawing its boundary.
- (penta X Y R) Draw an outline of a regular pentagon whose center is at (X, Y) with one of the vertices at (X + R, Y).
- (filledpenta X Y R) Same as penta, but fill the pentagon rather than simply drawing its boundary.
- (hexa X Y R) Draw an outline of a regular hexagon whose center is at (X, Y) with one of the vertices at (X + R, Y).
- (filledhexa X Y R) Same as hexa, but fill the hexagon rather than simply drawing its boundary.
- (ngon X Y R N) Draw an outline of a regular N-gon for integer N > 2, whose center is at (X, Y) with one of the vertices at (X + R, Y).
- (filledngon X Y R N) Same as ngon, but fill the ngon rather than simply drawing its boundary.
- * (group P1 P2 ... PN) Draw a grouping of the pictures P1 to PN. Any transformations applied to this picture element will be applied to all of the members of the group.
Transformations are created using the commands below.
- (translate x y) Translate the following picture element by x units along the x-axis, and y units along the y-axis.
- (rotate x) Rotate the following picture element by x degress about the origin.
- (scale x) Scale the following picture element by a factor of x, where x > 0.
Create drawing parameters using the commands below.
- (color R G B) Set the current color to (R,G,B). The arguments must be numbers between 0 and 1, inclusive. Initially, R = G = B = 0 (i.e. black).
- (linewidth W) Set the current line width to W, which must be a non-negative number. This meaning of this width value is as that of Postscript, meaning you can pass this value directly to Postscript commands. The initial value of the line width is 1.
* Control Structure
The looping construct (for I L U E1 ... EN) will execute commands E1 to EN multiple times, first with variable I set to integer L, then to L+1, etc. up to and including U. Does nothing if U < L or n=0 (there are no commands). At the end of the command, the variable I has the value max(L,U).
Zero or more whitespaces is allowed between a parenthesis and the start or end of a command, and between commands. At least one whitespace is required to separate arguments within a command.
For example, the following would draw a thick blue square, rotated 45 degrees. (linewidth 5)
(color 0 0 1)
(rotate 45)(rect 100 0 100 100)
The output of your program will be a Postscript document. You can view a Postscript file with any Postscript viewer. A popular one is ghostview which you can run with the command 'gv' or you can download it for your platform. A Postscript file has the following form:
The commands of the Postscript language are in postfix notation. The actual Postscript language is free form, meaning operands and operators can be separated by arbitrary whitespace. Your output, however, must mimic the reference binary in order to make your program easier to debug and grade. Each command will start on a newline with operands separated by a single space.
The origin of a Postscript starts at (0,0) in the lower left corner, with x-coordinates increasing to the right and y-coordinates increasing to the top. To draw something, one builds a path, and then strokes or fills it. The Postscript interpreter maintains a graphics state, which for our purposes consists of
- A current point, (x,y) coordinate, which is intially undefined.
- A current path.
- A current color, a 3-tuple (r,g,b), where each color intensity r,g,b is between 0 and 1 inclusive. (0,0,0) is black and (1,1,1) is white.
- A current line thickness, which indicates the thickness of lines drawn when stroking.
You can use the following subset of the Postscript language to construct your Postscript file. In this subset, all operands are decimal numbers (e.g. 0.1, 2.3, 45).
- x y moveto Set the current point to (x,y).
- x y lineto Add a line segment to the current path starting at the current point and going to (x,y), which becomes the new current point.
- stroke Draw lines with the current line width and color over all the segments in the current path. Clear the current path and set the current point to undefined.
- fill Close the current line path and fill the interior with the current color. Clear the current path and set the current point to undefined.
- W setlinewidth Set the current line width to W. The initial value of the line width is 1.
- R G B setrgbcolor Set the current color to (R, G, B).
When drawing a picture element with Postscript, output floating point values with the default formatting (i.e. do not use any I/O manipulators). Valid output coordinates lie in the range of [INT_MIN,INT_MAX] specified by limits.h. If any point of a picture element lies outside this range, it is an error.
You may link against any standard C or C++ library except for any part of the Standard Template Library.