A small graphics package
A few words about the X Window System. The window system we are using on the departments computers was developed by MIT. X11R6 (version 11, release 6) was released in May 1994. X11 is a client-server system. The server is the program that can produce the graphics (draw lines, circles etc., open windows and so on). The server performs these tasks in response to requests from clients (the programs the require that certain tasks are to be performed). Since the server and client can reside on different computers, it is possible to run Matlab, say, on one machine and show the Matlab-graphics on another. The server and client communicate via the X protocol. This means that if we have a slow connection between the computers, we may have to wait quite some time to see the graphics on our screen. If the server and client are running on the same machine, it is faster. When we are going to construct our Newton pictures it would be possible to plot point by point (pixel by pixel). This will, however, cause a lot of protocol traffic (especially if you are running on pom, i.e. over the network). In order to increase the performance I have used something called images. An image is a "rectangle" of pixels (on our case, just one line). This line is filled with colour values by the client (your program), and then sent (in one batch) to the server. This makes for a great improvement in speed.
I have written a small package, using libX11 (the lowest library level in X), which you can use to open a window and plot a line of pixels (image points) of different colours. For simplicity I have only given you eight colours to work with. The colours are numbered from 0 to 7, and they are
black, white, red, green, blue, cyan, magenta and yellow.
All the coordinates below are pixel-coordinates. The origin is in the upper
left corner and has coordinates (0, 0). y increases when you go down
(opposite direction from what we are used to). x behaves as normal. So,
if you create a window of width width
and height
height
, the upper right corner of the plotting area has coordinates
(width - 1
, 0). The lower left has coordinates (0, height
- 1
) and the lower right, (width - 1
, height -
1
). I leave it to you to make the transformation between pixels
coordinates and real coordinates.
Here is a list of the prototypes and a short description of the routines.
To make the prototypes available to your program include the following line in the beginning:
#include "defs.h"
void OpenWindow(int width, int height)
Connect to the server and open a window. width
= number of columns,
height
= number of rows. You should call this routine first
of all. You cannot open more than one window, so do not make repeated calls
to OpenWindow
.
void CloseWindow()
This should be the last call of the graphics package. It will wait for you to type the letter q in the window. It will then close the connection and delete the window.
void FlushWindow()
Call this routine when you have completed the plot to make sure that everything has been plotted.
void DrawLine(int y, int line[])
Plot a whole line of pixels in row y
. line
should
contain colour values. The length of line
must be the
same as width
in the call to OpenWindow
.
Here are some other routines you may not need:
void DrawPoint(int x, int y, int color)
Plot one point at coordinates (x, y)
and with colour
color
.
void DrawCircle(int x, int y, int radius, int color)
Draw a circle with centre at (x, y)
, radius r
and
filled with colour color
.
void WaitForButton(int *x, int *y, int *button)
This routine waits for a mouse click. It returns the position of where
the click occurred and the number of button that was used (1 = left
most, 2 = middle, 3 = right most). Note *
.
This routine can be used to implement a zoom option, for example.
Note that I have written all the routines above, so do not expect to find them in any of your X-libraries. My routines call routines from the libX11-library in turn.