Graphics

Table of contents

  1. Module graphics — computer graphics
  2. Module plotlib — graphing data and functions

Module graphics

Computer graphics interface.

sleep(x)
Sleep x seconds. Note that x may be a floating point number und thus parts of a second are possible.
canvas(w,h)
Create a new canvas of width w and height h pixels.
Type Canvas, c: Canvas
c.clear(r,g,b)
Clear the canvas c by RGB color. Note: r,g,b∈[0,1].
c.flush()
Flush the written data of canvas c to the screen. This operation is somewhat time consuming and thus should not be done before a larger bunch of pixels is drawn.
c.fill(x,y,w,h)
Fill a rect of width w and height h pixels at pixel (x,y).
c.rgb(r,g,b,a=1)
Set the color state using the RGB color model. Note: r,g,b,a∈[0,1].
c.hsl(h,s,l)
Set the color state using the HSL color model. Note: h∈[0,2π] and s,l∈[0,1].
c.key()
Pick the next key of the keyboard input queue.
c.point(x,y)
Draw a smooth point at position (x,y). This operation uses the device independent coordinate system instead of pixel coordinates.
c.circle(x,y,r)
Draw a circle of radius r at position (x,y). This operation uses the device independent coordinate system instead of pixel coordinates.
c.disc(x,y,r)
Draw a disc of radius r at position (x,y). This operation uses the device independent coordinate system instead of pixel coordinates.
c.box(x,y,r)
Draw a box of incircle radius r at position (x,y). This operation uses the device independent coordinate system instead of pixel coordinates.
c.scale(sx,sy)
Set the scale of the device independent coordinate system.
c.origin(x,y)
Set the origin of the device independent coordinate system.
c.vflush()
Flush the vector graphics buffer.
c.vcflush()
Flush and clear the vector graphics buffer.

Module plotlib

Graphing data and functions.

A basic example:

use plotlib: system
use math: pi, sin

s = system()
s.plot(|x| sin(pi*x))
s.idle()

plotlib.system

system({w=960, h=640, origin=[0,0], scale=[1,1], count=10, dark=false, grid=true, align=["center","center"]})
Create a new coordinate system. The resolution will be w*h pixels.
Type System, s: System
s.idle()
Idle until "q" (quit) is pressed.
s.plot(f)
Graph the function f.
s.vplot(f,{t0=0,t1=2*pi,n=1000})
Graph a vector curve f as [x,y]=f(t) using n points for t∈[t0,t1].
s.plot_zero_set(F,{N=20,n=100,density=1})
Graph the solutions of F(x,y)==0. Uses the bisection method with a maximum of N iterations, applied to n subintervals. A larger value of n leads to better results. For high quality plots, set the fibration density to density=2 or larger.
s.plot_level(F,{n=4,freq=1,alpha=0.4})
Draw z=F(x,y) by color. The color change frequency is given by freq. Alpha blending is adjusted by alpha. The integer number n is the raster size in pixels.
s.cplot(f,{n=4})
Graph a complex function f as w=f(x+y*1i) where the color (HSL) is H=arg(w) and L depends on abs(w). The integer number n is the raster size in pixels.
s.line(p1,p2)
Draw a line from [x1,y1]=p1 to [x2,y2]=p2.
s.vector(p1,p2)
Draw a vector arrow from [x1,y1]=p1 to [x2,y2]=p2.
s.scatter(a,{type="disc",radius=0.008})
Draw a list of scatter points [x,y]=a[k]. The type is "disc", "circle" or "box".
s.animte(callback,{clear=true})
Animate callback(tstep) until "q" (quit) is pressed. The argument tstep is a stroboscope that you can scale to obtain your preferred time parameter. Note that if the FPS is too slow, you might want to compute an animated gif instead. That is currently not supported, but planned for the future.
s.lock_color(lock=true)
After s.plot(f) and s.vplot(f) the color is changed automatically to the next one in the palette. This is turned off by s.lock_color() in order to achieve unimpaired manual control over the color state. Note that s.animate(callback) automatically applies s.lock_color().
s.color(n)
Set the color state. Use color n≥0 from the color palette. If n is out of bounds, n%size is used.
s.rgb(r,g,b)
Set the color state by the RGB color model.
s.hsl(h,s,l)
Set the color state by the HSL color model.
s.palette
The color palette, containing [r,g,b] colors. A random color is picked this way:
rand = rng(s.palette)
s.rgb(*rand())
s.desaturate()
Desaturate the colors.
s.desaturate_red_green()
Try to simulate red-green color blindness.
s.save(filename)
Save the plot as an image file. Supported formats are PPM, PNG and JPEG. The software package netpbm is required for PNG and JPEG.