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.
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