Physics 21 Reading List for the week of 1/10/2005
1. On g2
In the first lab session, you have asked some questions about the usage of the g2 graphical library. I must agree that the g2 documentation is not very pedagogical, since it documents the library code rather than its use. So let's walk together through some standard steps to graphical output.
First, you must open a graphical device, which could be an X11 window on the screen (X11 is the standard UNIX graphical interface), or just a figure file (either EPS, JPEG, or PNG). While opening devices other than EPS, you must specify their size, which will be measured in pixels.
Note that postscript files with bitmapped images tend to be very bulky. It's much better to use JPEG; if an EPS is needed (for instance, for easy inclusion in LaTeX documents), you can convert JPEG to EPS with jpeg2ps (installed in the lab).
With the handle (dev) that you have obtained from the g2_open functions, you can now use the g2 line plotting functions, which are all listed and briefly described at their manual page ("relative" functions employ a LOGO-like model). You can modify the lines being plotted with g2_set_line_width(dev,width) and g2_set_dash(dev,N,dashes) with N the number of dash components and dashes their lengths.
The default coordinate system of your device has origin (0,0) at the bottom left corner, and goes up to coordinate (width,height) at the top right corner. The EPSF device is special in this respect, since its size will grow as needed according to your commands (I suspect the size of the resulting EPS is set by equating pixels to typographical points, 1/72 of an inch). You can redefine the coordinate system with
where the new origin and the scaling factors do not need to be integers.
As for color management, g2 is a palette-based system, which means that you get to work with a limited number of colors, which you need to define beforehand. Each color that you define is identified by a unique number that you can use for all subsequent operations. You define colors with
where red, green, and blue are values between 0.0 and 1.0 that describe the quantity of each of these primary colors in the color that you are defining. (See below for a reading about this additive color model.) Once you have your colors, you can set the window background with g2_set_background(dev,colid), and set the pen with g2_pen(dev,colid) (which affects all subsequent g2_plot, g2_line, etc). You can also use a list of colors to define a bitmapped image that you will plot with
g2_image(dev,x,y,x_size,y_size,colid_array)
as explained in this week's assignment. Now for text:
will draw a string of text at position (x,y), using the font size chosen (in pixels) with
For the X11 device only, g2_query_pointer(dev) will query the pointer, and return (in Python) a 3-tuple containing the x coordinate, the y coordinate, and the button state (on my system, 256 for the left button, 1024 for the right button; your installation might give a different result).
What more? g2_clear(dev) will clear a device; g2_flush(dev) will immediately execute all pending graphical operations (which may be queued to minimize execution time). When you're done drawing, don't forget to close your device with g2_close(dev).
2. On color
We were saying just above that in g2 you define colors by setting their content of red, green, and blue. This model mirrors the way that colors are displayed on CRT and LCD monitors (and indeed, on TVs). The internet offers a wealth of fascinating information about the different ways that color (a subjective experience dependent on our physiology) can be described in mathematics and used in technology. See one or more of the following:
- the RGB and CMYK color models at Mike's Sketchpad.
- color models at Wikipedia;
- an interactive digital color converter by J. L. Mohler;
- a more extensive tutorial on the use of color in scientific illustration (disregard the exercises).
In the first lab session, you have asked some questions about the usage of the g2 graphical library. I must agree that the g2 documentation is not very pedagogical, since it documents the library code rather than its use. So let's walk together through some standard steps to graphical output.
First, you must open a graphical device, which could be an X11 window on the screen (X11 is the standard UNIX graphical interface), or just a figure file (either EPS, JPEG, or PNG). While opening devices other than EPS, you must specify their size, which will be measured in pixels.
dev = g2_open_X11(width_in_pixels,height_in_pixels)
dev = g2_open_EPSF(filename)
dev = g2_open_gd(filename,
width_in_pixels,height_in_pixels,g2_gd_jpeg)
dev = g2_open_gd(filename,
width_in_pixels,height_in_pixels,g2_gd_png)
Note that postscript files with bitmapped images tend to be very bulky. It's much better to use JPEG; if an EPS is needed (for instance, for easy inclusion in LaTeX documents), you can convert JPEG to EPS with jpeg2ps (installed in the lab).
With the handle (dev) that you have obtained from the g2_open functions, you can now use the g2 line plotting functions, which are all listed and briefly described at their manual page ("relative" functions employ a LOGO-like model). You can modify the lines being plotted with g2_set_line_width(dev,width) and g2_set_dash(dev,N,dashes) with N the number of dash components and dashes their lengths.
The default coordinate system of your device has origin (0,0) at the bottom left corner, and goes up to coordinate (width,height) at the top right corner. The EPSF device is special in this respect, since its size will grow as needed according to your commands (I suspect the size of the resulting EPS is set by equating pixels to typographical points, 1/72 of an inch). You can redefine the coordinate system with
g2_set_coordinate_system(dev,x_origin,y_origin,x_mul,y_mul)
where the new origin and the scaling factors do not need to be integers.
As for color management, g2 is a palette-based system, which means that you get to work with a limited number of colors, which you need to define beforehand. Each color that you define is identified by a unique number that you can use for all subsequent operations. You define colors with
colid = g2_ink(dev,red,green,blue)
where red, green, and blue are values between 0.0 and 1.0 that describe the quantity of each of these primary colors in the color that you are defining. (See below for a reading about this additive color model.) Once you have your colors, you can set the window background with g2_set_background(dev,colid), and set the pen with g2_pen(dev,colid) (which affects all subsequent g2_plot, g2_line, etc). You can also use a list of colors to define a bitmapped image that you will plot with
g2_image(dev,x,y,x_size,y_size,colid_array)
as explained in this week's assignment. Now for text:
g2_string(dev,x,y,text_string)
will draw a string of text at position (x,y), using the font size chosen (in pixels) with
g2_set_font_size(dev,size)
For the X11 device only, g2_query_pointer(dev) will query the pointer, and return (in Python) a 3-tuple containing the x coordinate, the y coordinate, and the button state (on my system, 256 for the left button, 1024 for the right button; your installation might give a different result).
What more? g2_clear(dev) will clear a device; g2_flush(dev) will immediately execute all pending graphical operations (which may be queued to minimize execution time). When you're done drawing, don't forget to close your device with g2_close(dev).
2. On color
We were saying just above that in g2 you define colors by setting their content of red, green, and blue. This model mirrors the way that colors are displayed on CRT and LCD monitors (and indeed, on TVs). The internet offers a wealth of fascinating information about the different ways that color (a subjective experience dependent on our physiology) can be described in mathematics and used in technology. See one or more of the following:
- the RGB and CMYK color models at Mike's Sketchpad.
- color models at Wikipedia;
- an interactive digital color converter by J. L. Mohler;
- a more extensive tutorial on the use of color in scientific illustration (disregard the exercises).

0 Comments:
Post a Comment
<< Home