user iconMikey Z on February 17th 2014
3294 views

topic icontopic iconRequesting a graphing calculator.

Can it be done?

In school I'm starting to work with sine waves... and after seeing some examples the teacher was using I was thinking it would be kinda cool to see in this. Would a script that fills a layer with a graph of a given equation be difficult to implement? I imagine it'd be relatively simple, I mean it'd just say "paint each pixel where (X = <insert equation>) blue" wouldn't it?

user iconVlasta on February 17th 2014 0

It is not too hard. Here is a basic script:

var x_min = 0;
var x_max = 10;
var fun = function(x) { return Math.sin(x); }

var image = Document.RasterImage;
var sx = image.sizeX;
var sy = image.sizeY;

var values = [];
for (var i = 0; i < sx; ++i)
	values[i] = fun(i*(x_max-x_min)/(sx-1)+x_min);

var y_min = values[0];
var y_max = values[0];
for (var i = 1; i < sx; ++i)
{
	if (values[i] < y_min) y_min = values[i];
	if (values[i] > y_max) y_max = values[i];
}

var coords = [];
for (var i = 0; i < sx; ++i)
{
	coords[i+i] = i+0.5;
	coords[i+i+1] = sy-(values[i]-y_min)/(y_max-y_min)*(sy-1)-0.5;
}

DrawTool.SetFillColor(0, 0, 0, 1);
DrawTool.LINE(Document, 3, "JROUND", "CROUND", coords);

You would probably want to change the first 3 lines. x_min and x_max define the range of values that you are interested in and the 3rd line defines the function that you want to graph.

user iconMikey Z on February 19th 2014 0

You're awesome, Vlasta! Thanks!

Hey, is there a way to do a script in Vector mode while we're at it?

user iconVlasta on February 20th 2014 0

Well, yes and no. You can have scripts working with vector images, but the button that you use to bring up the window is not visible. The UI would have to be re-configured to show a variant of that - I'll include the button in the next version.

Also, the last 2 lines of the script would have to be changed, at least in the current version.

user iconMikey Z on March 4th 2014 0

Ah, OK thanks

user iconAnonymous on December 12th 2019 0

it's best thanks

user icon