Bezier Path generator

From Director Online Wiki
Jump to: navigation, search

This is a parent script for using bezier curves. Put the code in a parent script.

---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
--BEZIER OBJECT
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DESCRIPTION
--
-- Calculates beziers
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DEPENDENCIES
--
-- none
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- INTERFACE
--
-- SetupBezier(Beginpoint,handle1,handle2,Endpoint)    -- Sets up bezier
-- GetPosition(pos)                                    -- returns loc of certain position in bezier (1 TO 100)
-- drawtostage()                                       -- uses imaging lingo to draw full bezier
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
 
---------------------------------------------------------------------------------------------------------
--VAR DECLARATION
---------------------------------------------------------------------------------------------------------
 
property ppath
 
---------------------------------------------------------------------------------------------------------
--EVENT HANDLERS
---------------------------------------------------------------------------------------------------------
 
on new me
  ppath = [:]
  return me
end
 
---------------------------------------------------------------------------------------------------------
-- PUBLIC
---------------------------------------------------------------------------------------------------------
 
on SetupBezier me, var1,var2,var3,var4
  ppath = [#p0:var1,#p1:var2,#p2:var3,#p3:var4]
  setaProp pPath, #dc, 3 * (pPath.p1 - pPath.p0)
  setaProp pPath, #db, 3 * (pPath.p2 - pPath.p1) - pPath.dc
  setaProp pPath, #da, pPath.p3 - pPath.p0 - pPath.dc - pPath.db
end
 
on GetPosition me, timetoget
  if ppath = [:] then return -1
  if timetoget > 100 then timetoget = 100
  if timetoget < 0 then timetoget = 0
  if timetoget = 100 then
    return  ppath.p3
  else
    vT1 = float (timetoget )/ 100.0
    vT2 = vT1 * vT1
    vT3 = vT2 * vT1
    vNewPosition = pPath.p0
    vModPoint = pPath.dc * vT1
    vNewPosition = vNewPosition + vModPoint
    vModPoint = pPath.db * vT2
    vNewPosition = vNewPosition + vModPoint
    vModPoint = pPath.da * vT3
    vNewPosition = vNewPosition + vModPoint
    return vNewPosition
  end if
end
 
on drawtostage me, stepsize
  if ppath = [:] then return -1
  (the stage).image.draw(ppath.p0,ppath.p1,rgb(255,255,0))
  (the stage).image.draw(ppath.p2,ppath.p3,rgb(255,255,0))
  repeat with i = 1 to (100/stepsize)
    (the stage).image.setpixel(me.GetPosition(i * stepsize), rgb(255,0,0))
  end repeat
  (the stage).image.fill(ppath.p1-point(2,2),ppath.p1+point(2,2), rgb(0,0,255))
  (the stage).image.fill(ppath.p2-point(2,2),ppath.p2+point(2,2), rgb(0,255,0))
  (the stage).image.fill(ppath.p3-point(2,2),ppath.p3+point(2,2), rgb(0,255,0))
  (the stage).image.fill(ppath.p0-point(2,2),ppath.p0+point(2,2), rgb(0,0,255)) 
end
 
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------

you can instantiate the parent script like this..

(eg you have called the parent script "bezier")

mybezier = script("bezier").new()
mybezier.setupbezier(Beginpoint, handler1, handler2, Endpoint)

you can then use the GetPosition() handler to get a specific position on the curve.

The drawToStage() handler is just there to make debugging easy by enabling you to draw the path on screen. it draws the handlers and curves.