Graphing a Function
Date: Thu, 10 Nov 1994 22:33:26 -0600
From: Matthew Caldwell <sexkittn@BURN.DEMON.CO.UK>
Subject: Re: graphing a function in Director
Depends on how you want to display the graph. Probably the easiest way is
to use a single sprite with trails on to draw the curve, setting its
position for each value of x and y:
-- some parameters to govern the way the graph displays
-- set these before actually running drawGraph
global xMin, xMax -- range of values of x for which to compute y
global yMin, yMax -- range of values for y to display
global xPrecision -- number of steps to draw
-- for the sake of argument, we'll put a sprite in channel 1 to specify
-- the bounds within which the graph is to be drawn, and put the drawing
-- implement (a sprite containing a dot with trails on) in channel 2
on drawGraph
-- calculate scale factors
set xRange = XMax - XMin
set graphWidth = the right of sprite 1 - the left of sprite 1
set xScale = float(graphWidth)/xRange
set yRange = YMax - YMin
set graphHeight = the bottom of sprite 1 - the top of sprite 1
set yScale = float(graphHeight)/yRange
set xBase = the left of sprite 1
set yBase = the bottom of sprite 1
-- calculate the difference between consecutive x values used
set xInterval = float(xRange)/xPrecision
repeat with i = 0 to xPrecision
-- calculate x and y
set x = xMin + xInterval * i
set y = yourFunction(x)
-- scale and offset them to stage coordinates
set h = xBase + integer( i * xInterval * xScale )
set v = yBase - integer( (y - yMin) * yScale )
-- draw the point if it's within the constraining sprite
if v <= yBase and v >= the top of sprite 1 then
set the locH of sprite 2 = h
set the locV of sprite 2 = v
updateStage
end if
end repeat
end drawGraph
This of course produces a dotty, discontinuous graph. An alternative would
be to use something like the drawLine handler I posted the other day, and
draw line segments between subsequent (x,y) points. Anyway, you get the
basic idea.
____
\ / Matthew Caldwell <sexkittn@burn.demon.co.uk>
\/ Who's that woman? I know her well, all decked out head to toe...