some math in ruby

For general discussion related FlowStone
Post Reply
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

some math in ruby

Post 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
Attachments
circular_experiments.fsm
(1.13 KiB) Downloaded 1054 times
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: some math in ruby

Post 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
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: some math in ruby

Post 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
"There lies the dog buried" (German saying translated literally)
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

Re: some math in ruby

Post by martinvicanek »

Try angle = arctan2(x,y) or arctan2(y,x) depending on whether you measure against the x- or the y-axis.
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: some math in ruby

Post by Tronic »

angle = Math.atan2((@c[1] - y), (@c[0] - x)) :roll:
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: some math in ruby

Post 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)
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: some math in ruby

Post 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.
"There lies the dog buried" (German saying translated literally)
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: some math in ruby

Post 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!
Tronic
Posts: 539
Joined: Wed Dec 21, 2011 12:59 pm

Re: some math in ruby

Post by Tronic »

@tulamide
hehe sorry,
I sometimes can not resist for an intellectual challenge :D
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: some math in ruby

Post 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-)
Post Reply