Graphing program

MacMan

BattleForums Senior Member
Joined
May 17, 2003
Messages
1,943
Reaction score
1
So school is over and I'm not making programs for my TI-83+ anymore. I got the urge to do some math, and I figured I could work it into one of my game's map editors. (This is my first script in this language, so there's tons of redundant things to be sure it works right.) Basically what it does is graph curves (curves are the only thing hard to make in this editor) as a sequence of lines. The cool part is that you can rotate polygons or sin/cos functions any number of degrees. Think about it, a goddamn sine wave at a 66° slant with its coordinates graphed on the plane. How fucking awesome is that!!
Anyway, this was only done in applescript, not xtools, so I can't give you guys a port. Just thought you might want to see some simple mac programming. See if you can follow the math... muhahahah!
(I know, I know, there are things line sin (2pi) as mutipliers with no variables, but that's just me being cautious.)



(source continued in next post)

try
set iniAsk to display better dialog ¬
"" fields ¬
{{kind:popup menu, name:"Figure type:", menu items:{"Regular polygon", "Sine wave", "Cosine wave"}, value:"Regular polygon"}} ¬
buttons {"Enter", "Quit"} default button 1
set figType to value of item 1 of fields returned of iniAsk
if button returned of iniAsk is "Quit" then
quit
end if
if value of item 1 of fields returned of iniAsk is "Regular polygon" then
set myResult to display better dialog ¬
"Regular polygon:" fields ¬
{{kind:text field, name:"Number of segments:", value:""}, ¬
{kind:popup menu, name:"Calculate figure using:", menu items:{"Radius of limiting circle", "Length of each segment"}, value:"Radius of limiting circle"}, ¬
{kind:text field, name:"Value for calculations:", value:"1024"}, ¬
{kind:text field, name:"X value of center coordinate:", value:"0"}, ¬
{kind:text field, name:"Y value of center coordinate:", value:"0"}, ¬
{kind:text field, name:"Degrees rotated:", value:"90"}, ¬
{kind:text field, name:"Width (±points):", value:"0"}, ¬
{kind:check box, name:"(ignore this)", value:unchecked state}} ¬
buttons {"Enter", "Quit"} default button 1
set numSides to value of item 1 of fields returned of myResult
if value of item 2 of fields returned of myResult is "Length of each segment" then
set LEN to value of item 3 of fields returned of myResult
set RAD to LEN / (sqrt ((1 - (cos of ((2 * pi) / numSides))) * 2))
--set RAD to (LEN / (sqrt (2 * (2 * (cos of ((2 * pi) / numSides))))))
else
set RAD to value of item 3 of fields returned of myResult
end if
set Xcons to value of item 4 of fields returned of myResult
set Ycons to value of item 5 of fields returned of myResult
set DEG to value of item 6 of fields returned of myResult
--set DEG to DEG + -180
set RAD to RAD - (2 * RAD)
set WID to value of item 7 of fields returned of myResult
end if
 

MacMan

BattleForums Senior Member
Joined
May 17, 2003
Messages
1,943
Reaction score
1
if value of item 1 of fields returned of iniAsk is "Sine wave" or value of item 1 of fields returned of iniAsk is "Cosine wave" then
set myResult to display better dialog ¬
"Sine/cosine wave:" fields ¬
{{kind:text field, name:"Number of segments:", value:""}, ¬
{kind:text field, name:"Amplitude:", value:"512"}, ¬
{kind:popup menu, name:"Calculate wave using:", menu items:{"Initial and terminal points", "Length of the total period"}, value:"Initial and terminal points"}, ¬
{kind:text field, name:"X value of initial point:", value:"0"}, ¬
{kind:text field, name:"Y value of initial point:", value:"0"}, ¬
{kind:text field, name:"X value of terminal point:", value:"4096"}, ¬
{kind:text field, name:"Y value of terminal point:", value:"0"}, ¬
{kind:text field, name:"Period length:", value:"4096"}, ¬
{kind:text field, name:"Degrees rotated:", value:"0"}, ¬
{kind:text field, name:"Width (±points):", value:"0"}} ¬
buttons {"Enter", "Quit"} default button 1
set numSides to value of item 1 of fields returned of myResult
set Xcons to value of item 4 of fields returned of myResult
set WID to value of item 10 of fields returned of myResult
set Ycons to value of item 5 of fields returned of myResult
set XcordF to value of item 6 of fields returned of myResult
set YcordF to value of item 7 of fields returned of myResult
if value of item 3 of fields returned of myResult is "Initial and terminal points" then
set PER to sqrt ((Xcons - XcordF) ^ 2 + (Ycons - YcordF) ^ 2)
if Xcons = XcordF then
if Ycons is greater than YcordF then
set DEG to 90
set DEG to (pi * DEG) / 180
else
set DEG to 270
set DEG to (pi * DEG) / 180
set DEG to -DEG
end if
end if
if Xcons is not equal to XcordF then
set DEG to (atan of ((YcordF - Ycons) / (XcordF - Xcons)))
set DEG to -DEG
end if
else
set DEG to value of item 9 of fields returned of myResult
set DEG to (pi * DEG) / 180
set PER to value of item 8 of fields returned of myResult
end if
set AMP to value of item 2 of fields returned of myResult
set DEG to -DEG
if -Xcons is less than -XcordF then
if value of item 3 of fields returned of myResult is "Initial and terminal points" then
set DEG to DEG + pi
end if
end if
end if
if button returned of myResult is "Quit" then
quit
end if
tell application "Pfhorge"
tell front map
if figType = "Regular polygon" then
if DEG is greater than 0 then
set YcordI to (((sin of (2 * pi)) * (cos of ((pi * DEG) / 180)) + (cos of (2 * pi)) * (sin of ((pi * DEG) / 180))) * RAD) + Ycons
set XcordI to -(((cos of (2 * pi)) * (cos of ((pi * DEG) / 180)) - (sin of (2 * pi)) * (sin of ((pi * DEG) / 180))) * RAD) + Xcons
else
set YcordI to Ycons
set XcordI to -RAD + Xcons
end if
set YcordI to round YcordI rounding to nearest
set XcordI to round XcordI rounding to nearest
make new point at end of points with properties {x:XcordI, y:YcordI}
set firstPointIndex to count of points
set firstPointIndex to firstPointIndex - 1
set lastPointIndex to numSides + (count of points) - 2
repeat with i from 1 to numSides - 1
if DEG is greater than 0 then
set YcordF to (((sin of ((2 * pi * i) / numSides)) * (cos of ((pi * DEG) / 180)) + (cos of ((2 * pi * i) / numSides)) * (sin of ((pi * DEG) / 180))) * RAD) + Ycons
set XcordF to -(((cos of ((2 * pi * i) / numSides)) * (cos of ((pi * DEG) / 180)) - (sin of ((2 * pi * i) / numSides)) * (sin of ((pi * DEG) / 180))) * RAD) + Xcons
else
set YcordF to RAD * [sin of [[2 * pi * i] / numSides]] + Ycons
set XcordF to -RAD * [cos of [[2 * pi * i] / numSides]] + Xcons
end if
set YcordF to round YcordF rounding to nearest
set XcordF to round XcordF rounding to nearest
lineToPoint at {x:XcordF, y:YcordF}
end repeat
lineFromPoint at firstPointIndex to lastPointIndex
if (abs of WID) is greater than 0 then
if DEG is greater than 0 then
set YcordI to (((sin of (2 * pi)) * (cos of ((pi * DEG) / 180)) + (cos of (2 * pi)) * (sin of ((pi * DEG) / 180))) * (RAD - WID)) + Ycons
set XcordI to (((cos of (2 * pi)) * (cos of ((pi * DEG) / 180)) - (sin of (2 * pi)) * (sin of ((pi * DEG) / 180))) * (RAD - WID)) + Xcons
else
set YcordI to Ycons
set XcordI to (RAD - WID) + Xcons
end if
set YcordI to round YcordI rounding to nearest
set XcordI to round XcordI rounding to nearest
make new point at end of points with properties {x:XcordI, y:YcordI}
set firstPointIndex to count of points
set firstPointIndex to firstPointIndex - 1
set lastPointIndex to numSides + (count of points) - 2
repeat with i from 1 to numSides - 1
if DEG is greater than 0 then
set YcordF to (((sin of ((2 * pi * i) / numSides)) * (cos of ((pi * DEG) / 180)) + (cos of ((2 * pi * i) / numSides)) * (sin of ((pi * DEG) / 180))) * (RAD - WID)) + Ycons
set XcordF to (((cos of ((2 * pi * i) / numSides)) * (cos of ((pi * DEG) / 180)) - (sin of ((2 * pi * i) / numSides)) * (sin of ((pi * DEG) / 180))) * (RAD - WID)) + Xcons
else
set YcordF to (RAD - WID) * [sin of [[2 * pi * i] / numSides]] + Ycons
set XcordF to (RAD - WID) * [cos of [[2 * pi * i] / numSides]] + Xcons
end if
set YcordF to round YcordF rounding to nearest
set XcordF to round XcordF rounding to nearest
lineToPoint at {x:XcordF, y:YcordF}
end repeat
lineFromPoint at firstPointIndex to lastPointIndex
end if
activate
refresh
end if
if figType = "Sine wave" then
make new point at end of points with properties {x:0 + Xcons, y:0 + Ycons}
repeat with i from 1 to numSides
set Xcord to ((PER * i) / numSides)
set Ycord to (-AMP * (sin of ((2 * pi * i) / numSides)))
set Xcord2 to (Xcord * (cos of DEG)) - (Ycord * (sin of DEG))
set Ycord2 to (Ycord * (cos of DEG)) + (Xcord * (sin of DEG))
set Xcord2 to round Xcord2 rounding to nearest
set Ycord2 to round Ycord2 rounding to nearest
lineToPoint at {x:(Xcord2 + Xcons), y:(Ycord2 + Ycons)}
end repeat
if (abs of WID) is greater than 0 then
make new point at end of points with properties {x:0 + Xcons, y:0 + Ycons - WID}
repeat with i from 1 to numSides
set Xcord to ((PER * i) / numSides)
set Ycord to (-AMP * (sin of ((2 * pi * i) / numSides)))
set Xcord2 to (Xcord * (cos of DEG)) - (Ycord * (sin of DEG))
set Ycord2 to (Ycord * (cos of DEG)) + (Xcord * (sin of DEG))
set Xcord2 to round Xcord2 rounding to nearest
set Ycord2 to round Ycord2 rounding to nearest
lineToPoint at {x:(Xcord2 + Xcons), y:(Ycord2 + Ycons - WID)}
end repeat
end if
activate
refresh
end if
if figType = "Cosine wave" then
set Xcord to 0
set Ycord to (-AMP) + AMP
set Xcord2 to (-(Ycord * (sin of DEG)))
set Ycord2 to (Ycord * (cos of DEG))
set Xcord2 to round Xcord2 rounding to nearest
set Ycord2 to round Ycord2 rounding to nearest
make new point at end of points with properties {x:(Xcord2 + Xcons), y:(Ycord2 + Ycons)}
repeat with i from 1 to numSides
set Xcord to ((PER * i) / numSides)
set Ycord to (-AMP * (cos of ((2 * pi * i) / numSides))) + AMP
set Xcord2 to (Xcord * (cos of DEG)) - (Ycord * (sin of DEG))
set Ycord2 to (Ycord * (cos of DEG)) + (Xcord * (sin of DEG))
set Xcord2 to round Xcord2 rounding to nearest
set Ycord2 to round Ycord2 rounding to nearest
lineToPoint at {x:(Xcord2 + Xcons), y:(Ycord2 + Ycons)}
end repeat
if (abs of WID) is greater than 0 then
set Xcord to 0
set Ycord to (-AMP) + AMP - WID
set Xcord2 to (-(Ycord * (sin of DEG)))
set Ycord2 to (Ycord * (cos of DEG))
set Xcord2 to round Xcord2 rounding to nearest
set Ycord2 to round Ycord2 rounding to nearest
make new point at end of points with properties {x:(Xcord2 + Xcons), y:(Ycord2 + Ycons)}
repeat with i from 1 to numSides
set Xcord to ((PER * i) / numSides)
set Ycord to (-AMP * (cos of ((2 * pi * i) / numSides))) + AMP - WID
set Xcord2 to (Xcord * (cos of DEG)) - (Ycord * (sin of DEG))
set Ycord2 to (Ycord * (cos of DEG)) + (Xcord * (sin of DEG))
set Xcord2 to round Xcord2 rounding to nearest
set Ycord2 to round Ycord2 rounding to nearest
lineToPoint at {x:(Xcord2 + Xcons), y:(Ycord2 + Ycons)}
end repeat
end if
activate
refresh
end if
end tell
end tell
on error
display dialog {"You messed something up."} buttons {"Oops!"} default button 1
quit
end try
end
 

MacMan

BattleForums Senior Member
Joined
May 17, 2003
Messages
1,943
Reaction score
1
AppleScript is a mac-native programming language. I'm just proud of the math (especially because the grid I'm working with has an inversed y-axis). Thought you might want to see something new and unusual. I'll be teaching myself C++ next, weeeee!
 

Sly

Member!
Joined
Feb 25, 2004
Messages
3,211
Reaction score
2
Location
In Spain, under Ace's bed.
Website
Visit site
wow, thas confusing
 

NewPosts

New threads

Top