About Monkey 2 › Forums › General Programming Discussion › (closed) Question about Cos & Sin
This topic contains 5 replies, has 4 voices, and was last updated by
Jesse
2 years, 8 months ago.
-
AuthorPosts
-
August 1, 2016 at 9:08 am #2609
Hi
I’m playing with MX2, and I decided to do a simple analog clock, porting some old code from BlitzMax.
I’ve a problem understanding why I get different results in just a simple ‘function’ like this one
Monkey1234For Local n:Float=0 Until 360 Step 30canvas.DrawLine(320,240,320+100*Sin(n),240+100*Cos(n))NextThis algorithm should draw a ‘regular’ shape – 12 rays with the same angle.
What’s wrong?
Attachments:
August 1, 2016 at 9:17 am #2613Nevermind
MX2 uses RADIAN, while BMAX degree.
Solved & closed!!!
Ps: I should have read BEFORE the manual!!!
August 1, 2016 at 9:34 am #2614You’re looking for degrees, Monkey 2 made the switch to radians.
Sin(n * (Pi / 180))
Here’s an example of how to convert to and from radians.
Monkey123456789101112131415161718192021222324Function RadiansToDegrees:Double(x:Double)Const RadianScalar:= (Pi / 180.0) ' 57.29577951308232Return (x * RadianScalar)EndFunction DegreesToRadians:Double(x:Double)Const DegreeScalar:= (180.0 / Pi) ' 0.017453292519943295Return (x * DegreeScalar)EndFunction Cosd:Double(x:Double)Return Cos(RadiansToDegrees(x))EndFunction Sind:Double(x:Double)Return Sin(RadiansToDegrees(x))EndFunction Main:Void()Print(Sind(10.0))Print(Cosd(30.0))EndEDIT: Just realized you figured it out.
August 1, 2016 at 10:02 am #2615Thanks
I’m starting from the ‘basis’… so porting old programs/algorithms I think is the best way to understand MX2 and the differences/features.
I’m thinking I need to create a ‘conversion-library’ to manage ‘old’ code (Sind is much better than Sin(Rad(n))…)
By the way, I think it should be available by default in the Math module some functions for translating from Radian to Degree and viceversa.
August 1, 2016 at 9:02 pm #2668Are there any built-in conversion functions? Reckon radians make sense to about 0.000000001% of all humans in comparison to degrees!
August 1, 2016 at 10:11 pm #2670I think you got both functions reversed:
Pi/180.0 is not 57.29577….
neither is 180.0 /Pi = 0.017453….
Monkey12345678910111213Function RadiansToDegrees:Double(x:Double)Const RadianScalar:= (Pi / 180.0) ' 57.29577951308232Return (x * RadianScalar)EndFunction DegreesToRadians:Double(x:Double)Const DegreeScalar:= (180.0 / Pi) ' 0.017453292519943295Return (x * DegreeScalar)End -
AuthorPosts
You must be logged in to reply to this topic.

