On Friday 09 December 2005 01:22 pm, Eric Lavigne wrote:
> >Does Ruby have any modules useful in graphing equations like y=x**2+5,
> >y**2+x**2=16, 4y+3x=28, and the like?
>
> gnuplot does this: http://gnuplot.info/
> Gnuplot can create your graphs as files or display them on the screen.
> Gnuplot is ordinarily used as a standalone program which reads a
> script, but you can make it work with your program by putting gnuplot
> at the end of a pipeline. Your program writes gnuplot commands to
> standard-out:
>
> ruby myprog.rb | gnuplot
>
> If you need to do something more complicated (or if your OS doesn't
> support pipes), creating an object that acts as an output stream to a
> gnuplot process would be fairly easy.

Thanks Eric,

I already checked out gnuplot and had some problems:

1) It doesn't take y**2+x**2=16, but instead requires you to solve for y and 
put in (16-x**2)**0.5, which gives you only the upper half of the circle.

2) I thought, OK, I'll make all the points in Ruby, and pipe them to gnuplot. 
No joy -- gnuplot's interpolation between points is stupid -- all but bezier 
just produce a straight line, and bezier produces cusps where there aren't 
any. I spoze I could send LOTS of points, and that might work.

Which brings up the next point -- algorithm. Remember, I want the full 
y**2+x**2=16, not the right side solved for y.

I'm thinking I start by setting the right side to 0, so it would be 
y**2+x**2-16=0. That's OK in this situation. Then I walk up each axis until I 
find a point where the right side shifts from positive to negative or vice 
versa. Interpolate, try again, interpolate again til I have a reasonably 
close point. Now go out maybe 0.1 in each direction and find points. Now 
calculate slopes, go out some more along that slope and do it again. Pretty 
soon I've traced out all relevant points on a continuous curve (this won't 
work with discontinuous curves).

Now that you see the algorithm I was contemplating, you probably understand 
why I was hoping Ruby had a module that produces points for me :-)

Thanks

SteveT
 
Steve Litt
http://www.troubleshooters.com
slitt / troubleshooters.com