SRL Archive - Other Scripts

Function Grapher


A G E N T

2008-10-15 21:52:21

Hey all,
A combination of boredom and having to find intercepts of functions led me to create this script.
You set the function (right now, it has parabola, square root, and absolute value), and write the parameters of said function in function notation [f(x), etc], the script parses the function, graphs it, and tells you the x- and y-intercepts (if any).
Note - I can't include inverse function (1/x) because the canvas only accepts integers, and rounding the returns of 1/x sort of defeats the purpose.

Another note - this is really a modified version of SCAR/Scripts/misc/parabola.scar - the scale is VERY large, and I'm not sure how to fix it.


program Parabola;
const
which = 'parabola'; //'parabola' [y=x^2], 'abs' [y=|x|], 'sqrt' for now
func = 'f(x)'; //the function you want, in function notation
//expanded, this is really 1f(1x+0)+0
//1f(... means that the parabola is neither stretched nor compressed vertically
//1x means that it is neither stretched nor compressed horizontally
//the +0's mean that it is neither shifted horizontally or vertically
//remember that x+1 is a shift LEFT, x-1 is a shift RIGHT
//default - 1f(1x+0)+0 is the parabola y = x^2
//example: -5f(-0.5x+22.5)-34.4

var
x : Extended;
dx, dy: Integer;
dic: TCanvas;

yscl, xscl, xsh, ysh : Extended;

procedure GetPoints;
begin
if(pos('f', func) = 1)then
begin
yscl := 1;
end else
begin
//writeln(copy(func, 1, pos('f', func)-1));
if(copy(func, 1, pos('f', func)-1) = '-')then
begin
yscl := -1;
end else
begin
yscl := StrToFloat(copy(func, 1, pos('f', func)-1));
end;
end;
if(between('(', 'x', func) = '')then
begin
xscl := 1;
end else
begin
if(between('(', 'x', func) = '-')then
begin
xscl := -1;
end else
begin
xscl := StrToFloat(between('(', 'x', func));
end;
end;
if(between('x', ')', func) = '')then
begin
xsh := 0;
end else
begin
xsh := StrToFloat(between('x', ')', func));
end;
if(copy(func, pos(')',func)+1, length(func)) = '')then
begin
ysh := 0;
end else
begin
ysh := StrToFloat(copy(func, pos(')',func)+1, length(func)));
end;
writeln('Y-scale: '+FloatToStr(yscl));
writeln('X-scale: '+FloatToStr(xscl));
writeln('X-Shift: '+FloatToStr(xsh)+' [effective: '+FloatToStr(-xsh)+']');
writeln('Y-Shift: '+FloatToStr(ysh));
ysh := -ysh;
end;

procedure DrawParabola;
begin
dic := GetDebugCanvas;
dic.Brush.Color := clBlack;
dic.Brush.Style := bsSolid;
dic.Pen.Color := clBlack;
dic.Rectangle(0, 0, 500, 500);
//display axes
dic.Pen.Color := clWhite;
dic.MoveTo(0, 250);
dic.LineTo(500, 250);
dic.MoveTo(250, 0);
dic.LineTo(250, 500);

//display graph
//x := -500 / scale;
x := -500;
repeat
dx := Round(x) + 250;
case which of
'parabola':
begin
dy := Round(yscl * (-sqr(xscl * (x) + xsh))+ysh) + 250;
if((dy > -1) and (dy < 1))then
begin
Writeln('X-Intercept 1: '+IntToStr(dy));
end;
if(dx = 250)then
begin
Writeln('Y-Intercept:'+IntToStr(-(dy-250)));
end;
end;
'abs':
begin
dy := Round(yscl * (-abs(xscl * (x) + xsh))+ysh) + 250;
if((dy-250) = 0)then
begin
Writeln('X-Intercept: '+IntToStr(dx-250));
end;
if(dx + xsh = 250)then
begin
Writeln('Y-Intercept:'+IntToStr(-(dy-250)));
end;
end;
'sqrt':
begin
if((xscl * (x + xsh)) >= 0)then
begin
dy := Round(yscl * (-sqrt(xscl * (x) + xsh))+ysh) + 250;
end;
if(dx = 250)then
begin
Writeln('Y-Intercept:'+IntToStr(-(dy-250)));
end;
end;
end;
dic.Pixels[dx, dy] := clGreen;
x := x + 1;
until (x > 500);
end;

procedure SafeDrawParabola;
var
V: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('DrawParabola', V);
end;

begin
GetPoints;
DisplayDebugImgWindow(500, 500);
SafeDrawParabola;
end.
-Agent

shaunthasheep

2008-10-16 11:27:05

thats pretty neat and cool.
i would of had use of this like last week cause i had to graph these for hw.

my math teacher taught us those a week or two ago.
we had to do a project showing all of those types of graphs in a music video using are hands lol.

http://www.youtube.com/watch?v=wrmwdQ3wfAY
im the one in the hat

Sumilion

2008-10-16 12:17:20

Whaha lol I'm so happy I never had to do that :D

A G E N T

2008-10-16 12:30:13

Haha, your math teacher has a cruel sense of humour.
Mine is just boring.

Laur€ns

2008-10-22 09:18:35

From: The_Rs_Monkey
thats pretty neat and cool.
i would of had use of this like last week cause i had to graph these for hw.

my math teacher taught us those a week or two ago.
we had to do a project showing all of those types of graphs in a music video using are hands lol.

http://www.youtube.com/watch?v=wrmwdQ3wfAY
im the one in the hat


Are you the fatty? :p

Ron

2008-10-23 14:31:57

RS Monkey, lol did you lose your place in line when you did the flying-in-a-circle dance? I would have used a banana for a microphone and have one guy dressed in a tiger suit. :D Nice video.

Oh and actually on topic. Could you make a script where it would graph any valid equation?

const
func = x^2 + sin(x);

because I saw a somewhat function grapher made by kaitnieks in the test folder some time ago. All in all, nice script. :)

A G E N T

2008-10-24 09:17:57

I think it could be done, but I'm not quite certain how to go around it. I know if it's not a number, then I assume it's another function call or something, but then I'd need a really big case or something. Ex, In PHP I can say:
[PHP]function test($string, $arg)
{
return $string($arg);
}
echo test('sin', 15);
[/PHP]

But I don't think there is such an equivalent in SCAR.

BownPistol

2009-03-28 15:54:51

Very cool! Thank you. ^^ This will be very useful for math class.