-- 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
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA