Page 1 of 1

some math in ruby

Posted: Sat Apr 02, 2016 1:18 am
by Nubeat7
i need to find out an angle between 2 lines
both start at centerpoint and thje zero line is fixed at 12 o'clock

here is what i have so far, but i really don't know how to go further

Re: some math in ruby

Posted: Sat Apr 02, 2016 4:29 am
by RJHollins
Hi NuBeat,

Don't know if this would be on any help ... but something I found in one of the toolboxes might give clue
Pie angle.fsm
(4.83 KiB) Downloaded 987 times

Re: some math in ruby

Posted: Sat Apr 02, 2016 5:43 am
by tulamide
What you need is vector math (google for it for details). Here's a vector pad I made some time ago, that should help you a good deal:
http://www.dsprobotics.com/support/viewtopic.php?f=3&t=3900

Re: some math in ruby

Posted: Sat Apr 02, 2016 5:59 am
by martinvicanek
Try angle = arctan2(x,y) or arctan2(y,x) depending on whether you measure against the x- or the y-axis.

Re: some math in ruby

Posted: Sat Apr 02, 2016 8:39 am
by Tronic
angle = Math.atan2((@c[1] - y), (@c[0] - x)) :roll:

Re: some math in ruby

Posted: Sat Apr 02, 2016 9:49 am
by Tronic

Code: Select all

angle = Math.atan2( (@c[1]-y), (@c[0]-x) )
offset = Math::PI/2.0 # 12 o'clock
radians = (angle+offset).modulo(2*Math::PI) - Math::PI
degress = (angle-offset).modulo(2*Math::PI) * (180/Math::PI)

Re: some math in ruby

Posted: Sat Apr 02, 2016 12:30 pm
by tulamide
Did any of you both look at my Ruby example, whose link I posted? Because it's all in there. So, basically you're just repeating what is already there ;)

However, Nubeat, I want to point ypour attention to something important. Unlike something like distance, angle detection is always dependent on the point of view. From where to where you try to get the angle. That's always to consider. There are always two angles between two points, because angles are counted clockwise. So, if from a to b the angle is 120 degree, then from b to a it is 240 degree.

In my schematic that's made easy: Call the angle method always with that point first, from whose perspective you want to get the angle.

Re: some math in ruby

Posted: Sat Apr 02, 2016 3:51 pm
by Nubeat7
thank you guys!

i'm still at work, but had the chance to try it , tronic's example works perfectly.. thank you!

but also your example, Tulamide, does exactly what i need, since i'm not very comfortable with all that circular calculations i will have a close look to your example, thanks for the link and sharing!

Re: some math in ruby

Posted: Sat Apr 02, 2016 7:56 pm
by Tronic
@tulamide
hehe sorry,
I sometimes can not resist for an intellectual challenge :D

Re: some math in ruby

Posted: Sat Apr 02, 2016 8:41 pm
by RJHollins
Everyone's post can help :mrgreen: I appreciate it.

Usually I don't have immediate need of angles, degrees, radians ... but it does come up when setting Knob swing parameters.

I always get confused by these settings for some reason. It seems the Knob and Dial design use 2 different references. I do get it to work ... but it gets a bit of a brain-tease. :lol:

Reading one of the posts here really helped to confirm the now obvious. [I don't make Knobs and dials all the time, so enough time passes that I forget the basics].

Anyway ... thanks for the Links, examples, and dialog 8-)