USING: arrays kernel math sequences accessors ui ui.gadgets ui.gadgets.canvas ui.render math.order math.rectangles math.vectors opengl opengl.gl namespaces ; IN: dragon : zig ( p1 p2 -- p1 pI p2 ) [ [ first2 ] bi@ [ [ rot swap + swap ] dip - + 2 / ] 4keep [ rot + swap ] dip + + 2 / 2array ] 2keep [ swap ] dip ; : zag ( p1 p2 -- p1 pA p2 ) [ [ first2 ] bi@ [ [ rot swap + swap ] dip + - 2 / ] 4keep [ rot swap - swap ] dip + + 2 / 2array ] 2keep [ swap ] dip ; : dragon' ( p1 p2 p3 d -- seq ) dup zero? [ 2drop 2array ] [ 1 - swap dupd swap [ dupd swap ] 2dip ! p1 p2 d p2 p3 d [ zig ] 4dip [ zag ] dip ! p1 pI p2 d p2 pA p3 d [ dragon' ] 4dip dragon' ! p1 pI p2 d dragon' / p2 pA p3 d dragon' append ] if ; : dragon ( -- seq ) ! [ math.vectors:normalize ] map 100 200 [ dup 2array ] bi@ zig 2 dragon' ; ! prettyprint:. ! GL_LINE_STRIP glBegin dragon [ glVertex2dv ] each glEnd : draw-dragon ( -- ) 8 2 - glLineWidth 8 2 - glPointSize 1.0 1.0 1.0 1.0 glColor4d GL_LINE_STRIP glBegin dragon [ normalize ] map [ glVertex2dv ] each glEnd ; TUPLE: dragon-canvas < canvas ; M: dragon-canvas layout* delete-canvas-dlist ; M: dragon-canvas draw-gadget* [ draw-dragon ] draw-canvas ; M: dragon-canvas pref-dim* drop { 400 400 } ; MAIN-WINDOW: dragon-window { { title "Dragon Curve" } } dragon-canvas new-canvas >>gadgets ; MAIN: dragon-window