Graphics Library (#include "library.h")

The following lists the most generally useful things defined in the graphics library. If some essential function seems to be missing, send a message and I'll look into the possibility of adding something.

Important Notes:

Basic Drawing Functions

Shapes and Filling

The graphics system is capable of recording a complex shape and drawing or filling that shape later. A shape is assumed to be bounded by a number of connected straight lines (think of a triangle or a star), and the only information needed to completely define a shape is the positions of its corners (vertices). A shape is made by moving the pen around the vertices of the shape, recording each as it is reached.

Clipping and Regions

Colours and Pen Widths

Displaying Text

Events: mouse, keyboard, window, timer

Keyboard, Mouse, Input and Output functions

Dialogues (temporary pop-up windows)

Menus

Support for .BMP files

Consolidating Operations

Threads

Semaphores





Example 1

This program draws a simple square on the screen using calculated coordinates for the corners.
void main(void)
{ MakeWindow(400,400);
  MoveTo(100,100);
  DrawLineTo(300,100);
  DrawLineTo(300,300);
  DrawLineTo(100,300);
  DrawLineTo(100,100); }




Example 2

This program draws a simple square on the screen by drawing a straight line, turning right through 90 degrees, and repeating four times.
void main(void)
{ MakeWindow(400,400);
  DrawLineLength(100);
  TurnRightByDegrees(90);
  DrawLineLength(100);
  TurnRightByDegrees(90);
  DrawLineLength(100);
  TurnRightByDegrees(90);
  DrawLineLength(100);
  TurnRightByDegrees(90); }




Example 3

This program draws a simple square on the screen by turning towards each of the four cardinal directions in turn, and drawing a straight line.
void main(void)
{ MakeWindow(400,400);
  MoveTo(50,50);
  TurnToHeadingDegrees(90);
  DrawLineLength(200);
  TurnToHeadingDegrees(180);
  DrawLineLength(200);
  TurnToHeadingDegrees(270);
  DrawLineLength(200);
  TurnToHeadingDegrees(0);
  DrawLineLength(200); }




Example 4

This program draws a smooth parabola out of the word "Bah!" by varying x from 0 to the width of the window, calculating y as a quadratic function of x, and writing "Bah!" at position (x,y).
void main(void)
{ MakeWindow(430,400);
  for (int x=0; x<=400; x+=5)
  { int y=(x-200)*(x-200)/100;
    MoveTo(x,y);
    WriteText("Bah!"); } }




Example 5

This program follows the form of example 1, but draws a normal thin-sided square with a fat-sided square inside it.
void main(void)
{ MakeWindow(400,400);
  MoveTo(100,100);
  DrawLineTo(300,100);
  DrawLineTo(300,300);
  DrawLineTo(100,300);
  DrawLineTo(100,100);
  PenWidth(4);
  MoveTo(150,150);
  DrawLineTo(250,150);
  DrawLineTo(250,250);
  DrawLineTo(150,250);
  DrawLineTo(150,150); }




Example 6

This program follows the form of example 2, but draws a multi-coloured hexagon with the aid of PenColor.
void main(void)
{ MakeWindow(400,400);
  MoveTo(200,100);
  PenWidth(6);
  TurnToHeadingDegrees(120);
  PenColor(1.0, 0.0, 0.0);    // red
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor(1.0, 1.0, 0.0);    // yellow
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor(0.0, 1.0, 1.0);    // prussian blue
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor(1.0, 0.5, 0.0);    // orange
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor(1.0, 0.8, 0.8);    // pink
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor(0.7, 0.7, 0.7);    // gray
  DrawLineLength(70); }




Example 7

This program is the same as example 6, but uses the integer-based colour descriptions instead.
void main(void)
{ MakeWindow(400,400);
  MoveTo(200,100);
  PenWidth(6);
  TurnToHeadingDegrees(120);
  PenColor0255(255, 0, 0);    // red
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor0255(255, 255, 0);    // yellow
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor0255(0, 255, 255);    // prussian blue
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor0255(255, 127, 0);    // orange
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor0255(255, 200, 200);    // pink
  DrawLineLength(70);
  TurnRightByDegrees(60);
  PenColor0255(170, 170, 170);    // gray
  DrawLineLength(70); }




Example 8

This program draws four big dots at the vertices of a square.
void main(void)
{ MakeWindow(400,400);
  MoveTo(200,100);
  PenWidth(10);
  DrawPointAt(100,100);
  DrawPointAt(300,100);
  DrawPointAt(100,300);
  DrawPointAt(300,300); }




Example 9

This program uses HLStoRGB to generate a rainbow. By varying Hue continuously from 0 to 1, while keeping Lightness at 0.5 and Saturation at 1.0, it goes through all the colours of the virtual rainbow, keeping constant brightness.
void main(void)
{ MakeWindow(800,200);
  for (int i=0; i<600; i+=1)
  { double R, G, B;
    HLStoRGB(i/600.0, 0.5, 1.0, R, G, B);
    PenColor(R, G, B);
    MoveTo(100+i, 50);
    DrawLineTo(100+i, 150); } }




Example 10

This program draws blobs of random size at random points in the window; the colours are of random shades of blue to purple. It never terminates naturally, so use control-C on it.
void main(void)
{ MakeWindow(500,500);
  while (1)
  { int red=random_in_range(0,255), green=0, blue=255;
    int size=random_in_range(5,100);
    PenWidthColor0255(size, red, green, blue);
    int x=random_in_range(0,500);
    int y=random_in_range(0,500);
    DrawPointAt(x,y);
    Pause(0.05); } }




Example 11

This program repeatedly waits for the user to drag the mouse (i.e. press the mouse, move it while pressed, then release the mouse in a new position), when it draws a rectangle connecting the begin and end points of the mouse drag.

The program has no natural end, so will need a control-C to stop it. Mouse drags should be entirely within the graphics window to work properly.
void main(void)
{ MakeWindow(500,500);
  while (1)
  { int x0, y0, x1, y1, b;
    WaitForMouse(x0, y0, x1, y1, b);
    MoveTo(x0, y0);
    DrawLineTo(x0, y1);
    DrawLineTo(x1, y1);
    DrawLineTo(x1, y0);
    DrawLineTo(x0, y0); } }




Example 12

This program slowly draws a line in a fixed direction. At any time, the user may type one of the words "red", "green", or "blue", in which case the pen will change colour, or "stop". Additionally, the user may click the mouse anywhere within the window, in which case the pen will change direction and start moving towards the mouse click position.
void main(void)
{ MakeWindow(500,500);
  StartMonitoringMouse();
  while (1)
  { DrawLineLength(2);
    Pause(0.1);
    int inputrdy = CheckInputReady();
    int mouserdy = CheckMouseAction();
    if (inputrdy)
    { string command=read_string();
      if (command=="red")
        PenColor0255(255, 0, 0);
      else if (command=="green")
        PenColor0255(0, 180, 0);
      else if (command=="blue")
        PenColor(0, 0, 255);
      else if (command=="stop")
        break;
      else
      { print("Bad command '"); print(command); print("'"); newline(); } }
    if (mouserdy)
    { int x0, y0, x1, y1, b;
      GetMouseAction(x0, y0, x1, y1, b);
      double dir=DirectionTo(x1, y1);
      TurnToHeading(dir); } } }




Example 13

This program uses a dialog to select a .BMP (windows bitmap) graphic file, then displays the graphic from that file in a sutiably sized window.
void main(void)
{ string fname=OpenFileDialog("*.bmp", GetWorkingDirectory(), "Select a BMP");
  if (fname=="") return;
  Image *im=ImageFromFile(fname);
  if (im==NULL)
    TerminalErrorDialog("Can't read selected file");
  int w, h;
  im->GetSize(w,h);
  MakeWindow(w+10, h+10);
  DrawImage(im, 5, 5); }