next up previous
Next: 3.7 Computer Networking Up: 3 Example Models Previous: 3.5 Object Programming

3.6 Computer Graphics

The J programming language provides several facilities for various kinds of graphics programming including an interface to the OpenGL libraries if available on the host system. Models can be built for a variety of graphics topics. Included are models of the 2D transformations (using homogeneous coordinates) scale, rotate and translate.

mat_product =: +/ . *
scale =: monad def '3 3 reshape (0 from y.), 0 0 0 , (1 from y.), 0 0 0 1'
translate =: monad def '3 3 reshape 1 0 0  0 1 0  , y. , 1'
rotate =: monad def '((2 2 reshape 1 1 _1 1 * 2 1 1 2 o. (o. y.) % 180),.0),0 0 1'

   NB. A square data object
   square =: 5 2 $ 0 0 10 0 10 10 0 10 0 0
   square
 0  0
10  0
10 10
 0 10
 0  0
   translate 10 _10
 1   0 0
 0   1 0
10 _10 1
   (square,.1) mat_product translate 10 _10
10 _10 1
20 _10 1
20   0 1
10   0 1
10 _10 1
   NB. Don't do unnecessary multiplications
   (square,.1) mat_product 3 2 {. translate 10 _10
10 _10
20 _10
20   0
10   0
10 _10
   rotate 180
_1  0 0
 0 _1 0
 0  0 1
   (square,.1) mat_product 3 2 {. rotate 180
  0   0
_10   0
_10 _10
  0 _10
  0   0
   new_square =: (square,.1) mat_product 3 2 {. translate 10 10
   new_square
10 10
20 10
20 20
10 20
10 10
   NB. Rotate this square 90 degrees about the point 10 10
   xform =: (translate _10 _10)mat_product (rotate 90) mat_product translate 10 10
   xform
 0 1 0
_1 0 0
20 0 1
   (new_square,. 1) mat_product 3 2 {. xform
10 10
10 20
 0 20
 0 10
10 10


next up previous
Next: 3.7 Computer Networking Up: 3 Example Models Previous: 3.5 Object Programming
2003-12-20