Laboratory assignment · 5 November

Lab 12: Build a Graphics Library in Jack

Work in a team of two and implement fundamental screen-drawing operations in the Jack programming language.

Back to course home

Assignment

Build shapes from pixels

Implement and test each operation before using it to build the next one.

Team

Two students per group

Create a Jack class named Draw. Your routines must work for valid coordinates on the Hack screen.

Language
Jack
Screen
512 pixels wide and 256 pixels high; the top-left pixel is (0, 0).
Build order
Pixel → line → rectangle and circle.
Goal
Implement the drawing algorithms yourselves. Do not replace an assignment routine with the corresponding completed Jack OS drawing routine.

Required functions

Four drawing operations

Use the stated parameters and keep the naming consistent throughout your program.

1 · Pixel

Draw.Pixel(x, y)

Draw one pixel at screen coordinate (x, y). Determine the screen word containing the pixel and set the corresponding bit.

2 · Line

Draw.Line(x0, y0, x1, y1)

Draw a line from (x0, y0) to (x1, y1). The routine should work in every direction, including horizontal and vertical lines.

3 · Rectangle

Draw.Rectangle(x0, y0, x1, y1)

Draw the boundary of the rectangle whose opposite corners are (x0, y0) and (x1, y1). Reuse your line routine.

4 · Circle

Draw.Circle(x, y, r)

Draw a circle centred at (x, y) with radius r. Exploit symmetry so that one calculation can place pixels in several octants.

Testing

Demonstrate every routine

Use a small Jack test program and inspect the screen output in the VM emulator.

Minimum test cases

  1. Pixels near the top-left corner, near word boundaries, and near the bottom-right corner.
  2. Horizontal, vertical, diagonal, shallow, and steep lines, with endpoints supplied in both orders.
  3. Rectangles of different widths and heights.
  4. Circles with several centres and radii that remain inside the screen.

Submission: submit the Jack source files for the Draw class and the test program. Both group members must be able to explain the implementation.