home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Programming / powerd / examples / Ferguson.d < prev    next >
Encoding:
Text File  |  2000-09-29  |  3.0 KB  |  127 lines

  1. // Ferguson.d - example of how to generate ferguson's cubics (curves) in D
  2. // requires workbench height of atleast ~340 pixels and fpu
  3.  
  4. MODULE    'intuition/intuition',
  5.             'utility/tagitem'
  6.  
  7. PROC main()
  8.     DEF    w:PTR TO Window,class,change=FALSE,code,x,y
  9.     DEF    id=-1,drag:PTR TO xy
  10.     IF w:=OpenWindowTags(NIL,
  11.             WA_InnerWidth,320,
  12.             WA_InnerHeight,320,
  13.             WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE,
  14.             WA_Flags,WFLG_DRAGBAR|WFLG_GIMMEZEROZERO|WFLG_RMBTRAP|WFLG_ACTIVATE|WFLG_CLOSEGADGET|WFLG_DEPTHGADGET|WFLG_REPORTMOUSE,
  15.             WA_Title,'Ferguson''s Cubic',
  16.             TAG_END)
  17.  
  18.         drag:=[            // draggable points
  19.             -50,0,        // position of point A
  20.             +50,100,        // position of vector in point A
  21.             +50,0,        // position of point B
  22.             +150,-100    // position of vector in point B
  23.             ]:xy
  24.  
  25.         draw    // watch the definition below
  26.  
  27.         EasyRequestArgs(0,[SIZEOF_EasyStruct,0,0,'Drag those points and enjoy :)','Yea']:EasyStruct,0,NIL)
  28.  
  29.         WHILE (class,code:=WaitIMessage(w))<>IDCMP_CLOSEWINDOW
  30.             SELECT class
  31.             CASE IDCMP_MOUSEBUTTONS
  32.                 SELECT code
  33.                 CASE SELECTDOWN
  34.                     x:=w.GZZMouseX-160
  35.                     y:=w.GZZMouseY-160
  36.                     IF (id:=Drag(x,y,drag,4))>=0 THEN change:=TRUE
  37.                 DEFAULT
  38.                     id:=-1
  39.                 ENDSELECT
  40.             CASE IDCMP_MOUSEMOVE
  41.                 IF id>=0
  42.                     x:=w.GZZMouseX-160
  43.                     y:=w.GZZMouseY-160
  44.                     drag[id].x:=x
  45.                     drag[id].y:=y
  46.                     change:=TRUE
  47.                 ENDIF
  48.             ENDSELECT
  49.         NEXTIF change=FALSE
  50.             draw    // watch the definition below
  51.             change:=FALSE
  52.         ENDWHILE
  53.  
  54.         CloseWindow(w)
  55.     ENDIF
  56.  
  57.     SUB draw        // this is new feature from v0.16 of PowerD :)
  58.         SetRast(w.RPort,0)
  59.         DrawLine(w.RPort,drag[0].x,drag[0].y,drag[1].x,drag[1].y)
  60.         DrawLine(w.RPort,drag[2].x,drag[2].y,drag[3].x,drag[3].y)
  61.         Ferguson(w.RPort,
  62.             drag[0].x,drag[0].y,
  63.             drag[0].x-drag[1].x,drag[0].y-drag[1].y,
  64.             drag[2].x,drag[2].y,
  65.             drag[2].x-drag[3].x,drag[2].y-drag[3].y,
  66.             100)            // # of intersections
  67.     ENDSUB
  68.  
  69. ENDPROC
  70.  
  71. OBJECT xy
  72.     x/y:L
  73.  
  74. /*
  75.     rp            - window rastport
  76.     xA,yA        - coordinates of point A
  77.     xa,ya        - vector in point A
  78.     xB,yB        - coordinates of point B
  79.     xb,yb        - vector in point B
  80.     steps        - number of intersections
  81. */
  82. PROC Ferguson(rp,xA:FLOAT,yA:FLOAT,xa:FLOAT,ya:FLOAT,xB:FLOAT,yB:FLOAT,xb:FLOAT,yb:FLOAT,steps)
  83.     DEFF    delta,t,x,y,f0,f1,f2,f3
  84.     DEF    i
  85.     delta:=1.0/steps
  86.     SetAPen(rp,2)
  87.     x:=xA
  88.     y:=yA
  89.     xa*=5                                        // this is needed only to look better
  90.     ya*=5
  91.     xb*=5
  92.     yb*=5
  93.     Move(rp,x+160,y+160)
  94.     FOR i:=0 TO steps
  95.         t:=delta*i
  96.         f0:=2.0*t*t*t-3.0*t*t+1.0        // Ferguson's polynoms
  97.         f1:=-2.0*t*t*t+3.0*t*t
  98.         f2:=t*t*t-2.0*t*t+t
  99.         f3:=t*t*t-t*t
  100.         x:=xA*f0+xB*f1+xa*f2+xb*f3        // parametrical representations for x and y coords
  101.         y:=yA*f0+yB*f1+ya*f2+yb*f3
  102.         Draw(rp,x+160,y+160)
  103.     ENDFOR
  104. ENDPROC
  105.  
  106. PROC DrawLine(rp,x1,y1,x2,y2)
  107.     x1+=160
  108.     y1+=160
  109.     x2+=160
  110.     y2+=160
  111.     SetAPen(rp,1)
  112.     Move(rp,x1,y1)
  113.     Draw(rp,x2,y2)
  114.     SetAPen(rp,3)
  115.     RectFill(rp,x1-2,y1-2,x1+2,y1+2)
  116.     RectFill(rp,x2-2,y2-2,x2+2,y2+2)
  117. ENDPROC
  118.  
  119. PROC Drag(x,y,drag:PTR TO xy,count)(L)
  120.     DEF    id
  121.     FOR id:=0 TO count-1
  122.         IF x>=drag[id].x-2 AND x<=drag[id].x+2 AND y>=drag[id].y-2 AND y<=drag[id].y+2 THEN RETURN id
  123.     ENDFOR
  124. ENDPROC -1
  125.  
  126. // MarK 29/9/2000
  127.