XY pairs to Angle / Distance

John Daniel's icon

Hello all!
My brain hurts trying to make this work. Intuitively seems very simple, but....

I want to:
feed Max 2 x/y pairs (pair one is say x=-22.5 y=11) (pair two is say x=16.2 y=-3)
I want to get the angle in degrees between these two points. The distance would be a cool thing too, if easy.

The help file for cartopol sort of kinda spits out the info i am looking for, in that i can hack it to feed it one of the pairs, but the other pair seems to be locked to the origin (0,0).

Totally beyond my limited math skills, help me spock ; )

thanks!
jd

Jesse's icon
Max Patch
Copy patch and select New From Clipboard in Max.
John Daniel's icon

Works dandy !! THANKS!

steprec's icon

Just what I was looking for! Thanks!

driectiontrack.maxpat
Max Patch
John Daniel's icon

Hello All!

I have the 2017 version of the same question.

Say I have two objects in XYZ space - their positions are:

Obj_a 1, 1, 1

Obj_b -2, -2, -2

How do I calc the angle between these two objects? I already trolled thru the forums and found a great circuit that gives me the distance...

jd

Floating Point's icon

while you can have distance, you can't have an angle between two points-- you need 3 points. In 2d space the third point is assumed to be any point along the x axis to the right of the straight line that is defined by the two points which intersects the x axis.

but you can have an angle between two vectors, so I guess you could define the the vectors as having a common point (0,0,0) and your other two points define the other ends of the vectors (i think this is called a normalized vector angle).

this page has some maths about it

I think the jit.quat object may provide the simplest approach

MakePatchesNotWar's icon

Have a look at the patch here. Works with negative numbers as well.

https://cycling74.com/forums/share-create-a-line-perpendicular-to-another

John Daniel's icon

thanks so much for your replies gang!

I am realizing my math is.... waaay lacking. the euclidianspace.com website is inspiring. Xlent resource. I also need to spend some time on Khan Academy

Maybe I put my problem another way...

I have two spheres, both are moved by the user independantly in XYZ space. I simply want to draw a cube or cylinder that stretches to connect the two spheres no matter what their position in space is, or their distance between each other. A Line whose size/diameter can be set would do just fine also.

My approach is calculating the midpoint between the two spheres, (got it) - assigning the cylinder to that position, (got it) - and then stretching my cylinder in the Z the right amount to "reach" the control spheres...(got it) - but the setting of the orientation of the cylinder eludes me. (don't got it)

In the past in Quartz composer I did this with a line object that allowed me to set it's two XYZ endpoints. I ended up with what looked like two tennis balls on the ends of a broomstick.

thanks much for any insight.

cheers!

jd

Floating Point's icon

probably the simplest way is to use sketch commands (particularly lineto), but this example uses a thick line as a 'fake' cylinder to join the spheres:

Max Patch
Copy patch and select New From Clipboard in Max.

maybe there's a way you can do it better (better gl command sequence)

John Daniel's icon

I just did a quicky test. Thank You! That is exactly the kinda thing I was lookin for...

I will dig in and dissect it tonight (and bend it to my will... Bwooo haa haa!)

Thank you!!!!

jd

Marijn C's icon

Hi! Thanks for bringing up this topic

@jesse

It works amazing

I was wondering how I can expand this expression to 3 dimensions XYZ, producing distance, and angles for azimuth and elevation