University of Calgary

simple graphics system

"Simple" Graphics System for Game Play
Source: Katrin Becker, 1999
For extra help, see: LAB

 
Implement a simple vector graphics untility using an object-oriented approach.
 
The system can be described as follows:
 
The utility consists of 3 main classes:
 
a Point class
A Point is specified by a coordinate pair: Point (x,y).
A Point is not a Shape
 
a Window class
Window (n, m) creates an area of size n (wide) X m (high) on the "screen".
Points on the "screen" are addressed using (x,y) coordinates (Cartesian).
A given Window w has a current position w.current(). This is like a cursor.
Initially, current is Point (0,0).
The current position can be set by w.current (p) where p is a Point.
Marks can be made on a Window by ploting Points (and possibly line segments which are made of a specified set of Points)
The Window is not actually seen on a real screen until displayed.
The Window does not know about Shapes
 
a Shape class
Class Shape is the common interface to Dots, Lines, Rectangles, Circles, etc. The origin of every Shape is always (0,0), but it also has a location (anchor) in the Window. Shapes can be created in a specified Window at a specified Point. They can then be drawn, erased, moveed, and resized. These are things 'they do for themselves'.
draw(W): draws the shape in the Window at its current location.
A Shape is invisible unless draw()n.
For example:
circle1.draw (w)
will cause the existing circle to plot an image in the Window
 
After draw()ing a Shape the current position is the Shape()'s translated origin (i.e. the location in the Window of this Shape's origin).
 
erase(W): removes the shape's image from the Window.
It does not make it cease to exist.
 
move(W,P): moves the shape from it's current location to the Point specified
(i.e. changes the location of its origin in the Window)
 
resize(factor): changes the size of the shape but not it's origin.
This value is expressed as a percent, so 50% reduces the Shape to half its original size.
 
 
There can be many instances of the same shape: in other words, there could be 5 different Circles in a particular Window; each has its own size and location; it will know whether it's been drawn on the screen or not and whether or not it has changed since it's been drawn.
 
A Dot, Dot( p ) can be used to represent a Point p on the "screen".
 
A Line is specified by a pair of Points: Line ( lower-left, upper-right );
Its origin is its lower-left Point.
 
A Rectangle is specified by it's bottom left and top right corner. Its origin is its bottom left Point.
 
A Circle is specified by it's centre point and a radius (measured in pixels)
For example:
Line( boxA.ul(), lineC.end()) creates a line from boxA's upper left corner to lineC's end point.
 
There can be many instances of the same shape: in other words, there could be 5 different Circles in a particular Window; each has its own size and location; it will know whether it's been drawn on the screen or not and whether or not it has changed since it's been d rawn.

 
General:
This 'system' does not need to be implemented as an interactive program, nor should it be animated in any way. It is not intended as a user application; rather it should be designed as a 'library system'. Programmers can link to this library and use its classes. Users of this utility must find a way to keep track of objects (i.e. which ones there are) themselves.
 
Testing:
As a simple test, write a small program that uses this library to display a simple "child's drawing of a house" with a roof, two windows, and a door. Write other small programs that demonstrate the functionality of the other features. Make SURE each part is well annotated. Suggestion: create a "table of contents" for your program listings and demo programs which will be the first file displayed in your final script.
 
BONUSES:
    1. [2 points] Make sure that points and line segments that fall outside the Window do not appear on the "screen". This is often called "clipping".
    2. [1 point each] Add other shapes (like triangle, polygon, ?oval?, etc).
    3. [5 points] Important aspects of Shape appear on the "screen" as a set of line segments. Implement operations to vary the appearanceof the segments: s.thickness(n) sets the line thickness to 0, 1, 2, or 3, where 2 is the default and 0 means invisible. In addition, a line segment can be solid, dashed, or dotted (this can be implemented by using different sets of characters)
    4. [5 points] Allow shapes to be rotated. Rotation by increments of 90° clockwise is sufficient.
    5. [5 points] Allow for a Window to be "any" size and modify display to show a specified portion of the given window.
    6. [20 points] Allow for shapes to be obscured by other objects (drawn later). In other words, overlapping objects should have one clearly "in front" and the other "behind" (hidden surface removal).
    7. [25 points] Write a program that will read a file containing drawing requests, process them and display the resultant 'picture' using the library.



Updated: August 28, 2005 09:56 AM