Thanks to Tig for giving me access to the Testbed Server so that I could code this in GScript2.
I limited the allowed input to a maximum of 4 numbers because unlike Java's JOptionPane, Graal's GUIs do not automatically adjust in size with regards to length of text, so I tested the alignment out and everything aligned perfectly up to a max of 4 numbers.
Also, I attached a screenshot at the bottom. It's a bit blurry, but good enough to see how the program works. This is more of an educational thing, really, since you can easily find square roots through the square root function, only now you can see how square roots are derived.
PHP Code:
/*
*Newton-Raphson Method
*@author Gambet
*/
//#CLIENTSIDE
function onWeaponFired()
{
this.active = !this.active;
this.tab = " ";
if (this.active == true) Initialize();
else selectionClose();
}
function Initialize()
{
new GuiWindowCtrl("Selection_Window")
{
profile = GuiBlueWindowProfile;
x = (screenwidth/2);
y = (screenheight/2-100);
width = 340;
height = 90;
canclose = canmaximize = canminimize = canresize = "false";
text = "Newton-Raphson Method -By: Gambet-";
new GuiTextCtrl("Selection_Text")
{
profile = GuiBlueTextProfile;
x = 10;
y = 23;
height = 50;
text = "What number would you like to find the square root of?";
}
new GuiMLTextEditCtrl("Selection_TextBox")
{
profile = GuiBlueTextEditProfile;
x = 280;
y = 25;
width = 50;
height = 15;
maxchars = 5;
}
new GuiButtonCtrl("Selection_SubmitButton")
{
profile = GuiBlueButtonProfile;
x = 55;
y = 67;
width = 50;
height = 15;
text = "Submit";
}
new GuiButtonCtrl("Selection_CancelButton")
{
profile = GuiBlueButtonProfile;
x = 225;
y = 67;
width = 50;
height = 15;
text = "Cancel";
}
}
}
function Selection_SubmitButton.onAction()
{
temp.value = abs(Selection_TextBox.text);
NewtonRaphson(value);
selectionClose();
}
function Selection_CancelButton.onAction()
{
selectionClose();
}
function selectionClose()
{
Selection_Window.destroy();
this.active = false;
}
function NewtonRaphson(value)
{
temp.x0 = 2; //initial guess
temp.finished = false;
temp.iteration = temp.x1 = NULL;
temp.output = "";
while(finished == false && value > 0)
{
iteration++;
x1 = (x0 - (x0^2 - value) / (2 * x0)); //derivation of Newton-Raphson formula
output = output @ iteration @ this.tab @ this.tab @ this.tab @ this.tab @ this.tab @
this.tab @ this.tab @ this.tab @ x1 @ "\n";
if (x1 > x0 && (x1 - x0 < 0.000000005)) finished = true;
else if (x0 > x1 && (x0 - x1 < 0.000000005)) finished = true;
else if (x1 == x0) finished = true;
x0 = x1;
}
if (finished == true) displayResults(output, value, x1);
}
function displayResults(results, num, answer)
{
new GuiWindowCtrl("Results_Window")
{
profile = GuiBlueWindowProfile;
x = (screenwidth/2);
y = (screenheight/2-100);
width = 340;
height = 190;
canclose = canmaximize = canminimize = canresize = "false";
text = "Newton-Raphson Method Results";
new GuiScrollCtrl("Results_Scroll")
{
profile = GuiBlueScrollProfile;
x = 5;
y = 22;
width = 328;
height = 150;
hScrollBar = "alwaysOff";
vScrollBar = "dynamic";
new GuiMLTextCtrl("Results_ScrollText")
{
useownprofile = true;
profile = "GuiBlueTextProfile";
x = 8;
y = 5;
height = 40;
width = 210;
}
}
new GuiButtonCtrl("Results_CloseButton")
{
profile = GuiBlueButtonProfile;
x = 5;
y = 170;
width = 328;
height = 15;
text = "Close";
}
}
Results_ScrollText.text = "Iteration(s)" @ this.tab @ this.tab @ this.tab @ this.tab @ this.tab @
"Approximation(s)\n" @ results @
"\nThe square root of " @ num @ " is " @ answer;
}
function Results_CloseButton.onAction()
{
Results_Window.destroy();
this.active = false;
}
