This may be more of a math question than an AS one, but how do you convert a slope into an angle (in degrees)? I can do it if using a calculator, but I’m a little rusty on how to do this without. I remember how to arrive at the slope (the rise over the run) and that a slope of 1 should be 45 degrees; but I can’t recall the necessary math. I’d appreciate any insight, TIA.
Slope = TAN(angle)
The precice solution depends on what language you are using and what trig functions it supports.
ArcTan (x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + …
There is at least on smart(=fast) function in “Algorithms” by Sedgewick.
I pretty sure that Satimage.osax, or one of the other free osax’s from Satimage has a tan function.
Best Regards
McUsr
Hi.
This (admittedly after minimal research) seems to give the right answers:
on angleFromSlope(h, l)
-- Satimage OSAX for atan2. Returns result in radians.
-- Degrees = radians * 180 / pi.
(atan2 {h, l}) * 180 / pi
end angleFromSlope
angleFromSlope(1, 1) --> 45.0
Vive La France!
@Nigel: that function you came up with is more than good enough for me.
Best Regards
McUsr
Hello,
Python’s math library could also come to your rescue …
http://docs.python.org/library/math.html
set n to 45 -- angle in degree
(do shell script "echo \"import math; print math.tan(math.radians(" & n & "))\" | /usr/bin/python #| /usr/bin/tr '.' ','") as number
set x to 1
set y to 1
(do shell script "echo \"import math; print math.degrees(math.atan2(" & x & "," & y & "))\" | /usr/bin/python #| /usr/bin/tr '.' ','") as number
Thanks for the help, guys.
The command line program “bc” can perform math. When you include the switch “-l” it loads a math library and therefore you can run more complex functions such as trig functions. In general you “echo” in some math equation to bc. Sin, Cos, and ArcTan are s(), c(), and a() respectively. All angles are in radians but of course we can convert to degrees (radians * 180 / pi). So with this knowledge we can do things like…
Here’s pi:
-- tan(pi/4) = 1
-- atan(1) = pi/4 thus
-- pi = 4*atan(1)
set pi to do shell script "echo \"4*a(1)\" | bc -l"
And here’s a script for your question:
set theRise to 1
set theRun to 1
-- echo "a(1/1)*180/(4*a(1))" | bc -l
set cmd to "echo \"a(" & theRise & "/" & theRun & ")*180/(4*a(1))\" | bc -l"
set theDegrees to do shell script cmd
Hello.
It sounds scary when you see a post starting with “This is of no “religious” importance to me” don’t it?
But aside from how easy accessible those functions are from within the now 64-Bit compatible Satimage.osax which is a free download, and that we apart from the religious side should use from the political side of this, that if we use it, they may actually continue to produce it. It is a fabulous freebie.
I think we generally think we should stick to stuff we can use internally with minimum fuzz. And Satimage.osax is such at thing. -You download it, drop it in a folder and off you go before you can say “cheese”.
Ruby and Python, and as a matter of fact everything, that makes the job done is a good thing, But here we have something made specially for AppleScript and works well with what it does. Why use anything else, than can’t compare in speed, in situations like this where we were only getting one result per call.
(And I would time it before using anything else to generate trig tables to import into AS.
About BC:
BC stands for “before christ” and refers to its production date :), I have no bad feelings about it, apart from it having a terse very late 60’ish syntax, and the terseness of its error reporting is “of another world” (Sorry Hank, I’m not even ranting, -I used it for a two months time solving diff. equations (and I must say: Never missed School and Mathemathica more than during that period of time).
PI: Is declared internally in AS with a precision of 11 digits which should suffice in most situations in AppleScript.
I think it is much wiser Idea to use Satimage.osax and build your own easy to read and maintain functions in AS based on their primitives than to start using BC, and creating libraries with it!.
There Is a library for using Mathematica with AppleScript here, although I haven’t tested it.
If I really wanted a free tool to do calculations from the outside of AppleScript I’d go for Maxima which can be found at maxima.org
The conclusion is that I prefer the ultimate practical solution and this special case favors Satimage over “external” solutions. But other times other solutions, involving Python, Ruby or Shell may be the best.
But from my point of view, that will never happen with bc; -it expired many years ago. Luckily by now it is just a painful memory.
Best Regards
McUsr
Not to be argumentative, because the reason there are many solutions is because they each have an advantage. People are free to decide which is best for them. The advantage of bc is that it’s installed already on everyone’s computer so if you plan to distribute your code then bc is much more viable than Satimage.osax. The other viable solution if you want to distribute your code was the python script, but that seemed too complex for me since I don’t know it well. As such I thought it was useful to show the bc code.
Hello.
I just felt I had to say it, as it is the share truth.
You must of course use what you find to be easiest Hank.
I’d recommend learning the necessary incantations using Python to bc, it wouldn’t take long time as pure mathematical functions usually are fairly simple to write and implement with regards to language constructs and such.
Many people have the same feelings as you about third-party installments, -I have the same, but it is really just a feeling.
Facts Which applies to more than Satimage.osax
Satimage.osax is only one or two clicks away after having googled it first.
After download it gets into the downloads folder.
It is really just to supply a script to the user which takes care of the download and installing into the ScriptingAdditions folder. I promise I will supply such a script the first time I either need to install Satimage.osax on somebody’s computer, or I need to reinstall it my self.
The other problem which might seem like a big obstacle, is testing whether it resides in the Local or Global Scripting Additions Folder on some users computer. I admit: such stuff feels like big problems, but they really aren’t when it comes to reality.
What I really tried to say is that this is more of a psychological barrier than a real problem. It is the possibility of yet another problem we fear -with no base in reality really, it is the fear for a stressful situation.
You should really try to download Satimage.osax and see for your self. It is a huge improvement to AppleScript.
- It makes my if not life, so scripting abilities richer!
AppleScript is also an wonderful environment to perform math in. Not the least because the environment the Script Editors provide. Satimage not only provides Satimage.osax but they also have free osax’s for linear algebra.
In this special case I can say that they should be really competent on numbers. But Satimage.osax is really much more than number functions, consisting of many different suites. It is a must have.
Apart from all of this. It will always be a good idea to generate tables with for instance trig values, if you or when you know which values should be used to save execution time, say in loops. But as a first Stage, doing simple function calls in that loop, osax’s like Satimage, will have huge advantages over any solutions that require a do shell script command. -It will be like 20 times faster per iteration - and that can really add up to something.
Programming time also counts, and if you know bc or Ruby or Python, from before and likes that - be my guest.
Like sed it is good at what it does, but can take a toll on your time. You should have a real good reason to use sed for instance if you can use an osax which provides the same functionality from within AppleScript.
I won’t speak anymore of this, it is really not like me at all. But I just felt I had to say it.
I think people sometimes starts to look for solutions outside of AppleScript too fast, but we may all have different reasons
and motivations behind our choices. Sometimes we do it out of belief, other times we do it consciously to leverage on certain languages we have learnt in order to gain development speed, Other times to gain efficient programs. Thats just how it is. So there is no right answer really. -It is really great to have an environment which can provide such a plethora of alternatives!
I just think that everything speaks in favor of Satimage.osax what math functions and other primitive functions concern, regarding executions speed, I couldn’t use it again before now when it’s 64-bit compatible.
And I Love it!
Best Regards
McUsr