cancel
Showing results for
Did you mean:
Developer forum
cancel
Showing results for
Did you mean:

## GDL Trigonometry question

Hi,

I have a label where i move the marker around freely through hotspots (xins and yins).

I don't understand how to code the line to stop at the perimeter of the circle. If I knew how ATAN works I could do this but i get an error when trying to use ATAN.

Br,

Mats

AC 25 SWE Full

HP Zbook Fury 15,6 G8. 32 GB RAM. Nvidia RTX A3000.
1 ACCEPTED SOLUTION
Solution
Rockstar

@Mats_Knutsson See if this works after your hotspots...

``````dLine = SQR(xIns^2 + yIns^2) - rsp
IF xIns > 0 THEN aLine = ATN(yIns / xIns) ELSE aLine = ATN(yIns / xIns) + 180
LINE2 0,0, dline * COS(aLine), dLine * SIN(aLine)
CIRCLE2 xIns, yIns, rsp
TEXT2 xIns, yIns, stName``````
Apple iMac macOS Monterey / AC25UKI (most recent builds)
11 REPLIES 11
Expert

Gegenkathete / Ankathete = TAN(alfa)

alfa = ATN(Gegenkathete / Ankathete)

Joachim Suehlo . AC12-25 . MAC OSX 10.15 . WIN10
Graphisoft

Hi!

If I understood correctly, you would like to get the arc tangent of x, but you get an error message, correct? The function uses the argument "ATN" instead of "ATAN", see if you get the desired outcome with that.

Ah...I read a doc where it was ATAN. Will try ATN instead. Thx

AC 25 SWE Full

HP Zbook Fury 15,6 G8. 32 GB RAM. Nvidia RTX A3000.
Solution
Rockstar

@Mats_Knutsson See if this works after your hotspots...

``````dLine = SQR(xIns^2 + yIns^2) - rsp
IF xIns > 0 THEN aLine = ATN(yIns / xIns) ELSE aLine = ATN(yIns / xIns) + 180
LINE2 0,0, dline * COS(aLine), dLine * SIN(aLine)
CIRCLE2 xIns, yIns, rsp
TEXT2 xIns, yIns, stName``````
Apple iMac macOS Monterey / AC25UKI (most recent builds)

Gr8 thanks 🙂

https://youtu.be/NPs0vPolWhs

AC 25 SWE Full

HP Zbook Fury 15,6 G8. 32 GB RAM. Nvidia RTX A3000.
Graphisoft

Also be extra careful not to divide by 0.

Péter Baksa
Software Engineer, Library as a Platform
Graphisoft SE, Budapest

Living on the edge...

AC 25 SWE Full

HP Zbook Fury 15,6 G8. 32 GB RAM. Nvidia RTX A3000.

@Peter Baksa I did think about it & it only fails at exactly (?) 90 / 270 degrees. I know GDL has a problem with real numbers and zero comparison checks e.g. If A = B is replaced with ABS ( A - B ) < eps. What tolerance / formula would be relevant to

ATN ( A / B )  to error trap when B = 0.0?

Apple iMac macOS Monterey / AC25UKI (most recent builds)
Graphisoft

Yes, there are circumstances when you can be sure the input isn't on the Y axis because some parameters forbid that, and it wouldn't be sensible architecturally. But there could be circumstances where a draggable hotspot would be set to 90° deliberately.

Maybe this is where the ATN / ATAN confusion came from: most programming languages have a non-trigonometric ATAN2 function taking an x and an y coordinate and returning the points' polar coordinate, handling all the cases where ATN could go wrong. GDL doesn't have such, but it can be written as a short subroutine containing a few ifs.

EPS can be avoided by using > from the other way you would use <=.

A working example returning degrees in range (-180...180]:

`if abs(_x) > 0 then    _atan2 = atn(_y / _x) + 90 * (1 - sgn(_x)) * (1 - 2 * (sgn(_y) < 0))else    _atan2 = sgn(_y * (abs(_y) > 0)) * 90endif`
Péter Baksa
Software Engineer, Library as a Platform
Graphisoft SE, Budapest

Great snippet!!! Could you at HQ arrange a site with clever code snippets like this?

AC 25 SWE Full

HP Zbook Fury 15,6 G8. 32 GB RAM. Nvidia RTX A3000.
Rockstar

Thank you. I will tuck that one away for future use. 👍

Apple iMac macOS Monterey / AC25UKI (most recent builds)

Latest solutions

Most liked posts