Home ContactTwitterFlickr

[November 2, 2005]

Graphing a Parametric Function Using Quad Curves

Filed under: ActionScript — @ 5:32 pm —

Patrick Mineault has posted an elegant solution (he says so himself, and I agree) for graphing a function in Flash using the curveTo method. He used it on the Gaussian curve and it’s efficient and it looks great. He shows a few other examples as well. The method, wrapped in an ActionScript class, adaptively adjusts the number of curves to better approximate the function.

I tried it on an ellipse and found two difficulties: I needed to guard against plotting NaNs, which drew lines heading off the screen, and I wanted the whole ellipse, not just the top or bottom half. I added some code to Patrick’s class for the NaN conditions, and to get the whole ellipse, I plotted two functions, one for the top and one for the bottom. But I could never get the two halves to quite close the gaps at their ends; they were still just barely visible with 4571 curves. So I’ve implemented a parametric version PGraph.as so I can represent the ellipse as x(theta) and y(theta), and voilà. (Or “whalla” as they say in the sticks.)

But then I ran it on some other conic sections, and found that I was drawing lines connecting the legs of the hyperbola — ugly! So I’ve also added the ability for the user to specify the discontinuities, as x values for the regular version, and as parameter values for the parameterized version. It would be nice to have the class find the discontinuities automatically, but I think that would be hard.

One thing that’s puzzling me is the results for sin(1/x). This function should never exceed (-1, +1) in the y direction, and yet the plot goes way outside that range.

You can download the modified Graph class, the PGraph class, and a DrawGraph test class with examples here.

Get Your Own Real Time Visitor Map!

Entire contents copyright © Alan Shaw 2005-2009. All rights reserved. You may not reprint or repost the contents of this site without the express written permission of the author.

25 queries. 0.404 seconds. Powered by WordPress version 2.8.4

Bad Behavior has blocked 574 access attempts in the last 7 days.