Studio/OpenGL/BraidKnot

From J Wiki
Jump to: navigation, search

Braid1.png

Knots can be drawn in J by defining a verb that traces out the path taken by the center of the knot. The J utility makefknot steps along the path, drawing the points along a circle centered on the path. As it moves along the path, it turns slightly, enough to ensure that the start and ending points line up, so that the knot is not twisted.

In the following code, the verb fn defines the path, and the noun BRAID contains the points required for OpenGL.

NB. 3 braid
NB.
NB. The display shows the polygons
NB. generated by the nurbs renderer.
NB.
NB. Press F6 or other fkeys to rotate.
NB.
NB. Try commenting out the assignment
NB. of GS_DISPLAYMODE, then redisplay

GS_DISPLAYMODE=: GLU_OUTLINE_POLYGON
GS_COLOR=: 0 1 1

r=. 3"_ + sin@(10&*)
fx=. r * cos@(3&*)
fy=. r * sin@(3&*)
fz=. 2"_ * sin@(5&*)
fn=: (0.6"_ * fx,fy,fz) f.
BRAID=: fn gsmakefknot (steps 0 2p1 50);11;0.7

paint=: 3 : 0
if. gsinit GS_LIGHT do.
  gsdrawknot makelist 1 BRAID
end.
glCallList 1
gsfini''
)

View from another angle:

Braidr1.png

Close up showing the tessellation produced by the nurbs renderer:

Braidt1.png