Graphics

Table of contents

  1. Module graphics — computer graphics
  2. Module plotlib — graphing data and functions
  3. Module plotlib.implicit — graphing zero sets

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, n=1000, scale=1, px=0, py=0,
wx=10/scale, wy=10/scale, dark=false, grid=true,
alignx="center", aligny="center"})
Create a new coordinate system with center (px,py) where the visible range is x∈[px-wx,px+wx] and y∈[py-wy,py+wy] by assuming w==h. The resolution will be w*h pixels.
Type System, s: System
s.idle()
Idle until "q" (quit) is pressed.
s.plot(f,{n=s.n})
Graph a function f as y=f(x), using n points for x∈[px-wx,0] and n points for x∈[0,px+wx].
s.vplot(f,{n=s.n, t0=0, t1=2*pi})
Graph a vector curve f as [x,y]=f(t) using n points for t∈[t0,t1].
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 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:
rng = rand(s.palette)
s.rgb(*rng())

Module plotlib.implicit

Patches plotlib with functionality to draw zero sets.

A basic example:

use plotlib: system
use plotlib.implicit

s = system()

s.plot_zero_set(|x,y| x^2-y^2-1)

s.idle()
Type System, s: System
s.plot_zero_set(F,{n=1000,m=10,method="bisection"})
Draw the solutions of F(x,y)=0. A large line density needs a large m. If there are gaps, try to increase m to 20 or 100.
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.