It is currently September 16th, 2019, 11:31 pm

math.atan2 question

Discuss the use of Lua in Script measures.
User avatar
balala
Rainmeter Sage
Posts: 8618
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

math.atan2 question

balala » June 11th, 2014, 6:06 pm

Hi all,
I hope this question can be admitted here, despite of it has nothing to do with Rainmeter, but it has with lua language. So, I have a well working little lua script and I need to create something similar into another programming language (Delphi). I've started the translation but somewhere I found a math.atan2 function and I don't know what to do with it. I tried this: arctan(x/y). Sometimes it gives the proper result, but in some cases it isn't. I googled the web, but I'm still confused. Can somebody with strong lua knowledge, to tell how this function works? Somewhere I found that it "uses the signs of both parameters to find the quadrant of the result." What means this?
Thanks a lot.
User avatar
VasTex
Posts: 408
Joined: September 20th, 2012, 3:17 pm
Location: USA - Montana

Re: math.atan2 question

VasTex » June 11th, 2014, 7:24 pm

I know little about Lua, but enough to get around and even less of Delphi. However, a few minutes on the Google and I managed to find these links, maybe they'll help you (I've also added a snippet of each site with what I thought was important):

Delphi Basics - ATan Function:

Code: Select all

program Project1;
{$APPTYPE CONSOLE}

var
  float : Double;
begin
  // The ArcTan of 1.0 should give 45 degrees
  float := System.Math.ATan(1.0);

  // Convert to degrees to show the value
  float := float * 180 / PI;

  Console.WriteLine('ATan(1.0) = {0} degrees', float.ToString);

  Console.ReadLine;
end.
  ATan(1.0) = 45 degrees
Delphi Basics - ATan2 Function:

Code: Select all

program Project1;
{$APPTYPE CONSOLE}

var
  float : Double;
begin
  // Given the following triangle :

  //         .
  //        /|
  //       / |
  //      /  | Y = 2.0
  //     /   |
  //    /A   |
  //   .-----.
  //   X = 1.0

  // Angle A = ArcTan(Y/X) = ArcTan(2/1) = ArcTan(0.5) = 63.43495 degrees
  float := System.Math.ATan2(2.0, 1.0);

  // Convert to degrees to show the value
  float := float * 180 / PI;

  Console.WriteLine('ATan2(2.0, 1.0) = {0} degrees', float.ToString);

  Console.ReadLine;
end.
   ATan2(2.0, 1.0) = 63.434948822922 degrees
Stack Overflow - Inverse of ATan2 (LUA):

Code: Select all

Given only the angle you can only derive a unit vector pointing to (dx, dy). In order to get the original (dx, dy) you also need to know the length of the vector (dx, dy), which I'll call len. You also have to convert the angle you derived from degrees back to radians and then use the trig equations mentioned elsewhere in this post. That is you have:

  local dy = y1-y2 
  local dx = x1-x2
  local angle = atan2(dy,dx) * 180 / pi
  local len = sqrt(dx*dx + dy*dy)
Given angle (in degrees) and the vector length, len, you can derive dx and dy by:

  local theta = angle * pi / 180
  local dx = len * cos(theta)
  local dy = len * sin(theta)
*I suggest reading the full post on this one.



Anyway, hopefully this helps you. Like I said I know very little about these particular languages, but I think this is probably what you're looking for.
01010100 01100101 01100011 01101000 01101110 01101111 01101100 01101111 01100111 01101001
01100011 01100001 01101100 00100000 01000010 01100001 01100100 01100001 01110011 01110011
User avatar
balala
Rainmeter Sage
Posts: 8618
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: math.atan2 question

balala » June 11th, 2014, 7:58 pm

VasTex wrote:I know little about Lua, but enough to get around and even less of Delphi. However, a few minutes on the Google and I managed to find these links, maybe they'll help you (I've also added a snippet of each site with what I thought was important):

Delphi Basics - ATan Function:

Code: Select all

program Project1;
{$APPTYPE CONSOLE}

var
  float : Double;
begin
  // The ArcTan of 1.0 should give 45 degrees
  float := System.Math.ATan(1.0);

  // Convert to degrees to show the value
  float := float * 180 / PI;

  Console.WriteLine('ATan(1.0) = {0} degrees', float.ToString);

  Console.ReadLine;
end.
  ATan(1.0) = 45 degrees
Delphi Basics - ATan2 Function:

Code: Select all

program Project1;
{$APPTYPE CONSOLE}

var
  float : Double;
begin
  // Given the following triangle :

  //         .
  //        /|
  //       / |
  //      /  | Y = 2.0
  //     /   |
  //    /A   |
  //   .-----.
  //   X = 1.0

  // Angle A = ArcTan(Y/X) = ArcTan(2/1) = ArcTan(0.5) = 63.43495 degrees
  float := System.Math.ATan2(2.0, 1.0);

  // Convert to degrees to show the value
  float := float * 180 / PI;

  Console.WriteLine('ATan2(2.0, 1.0) = {0} degrees', float.ToString);

  Console.ReadLine;
end.
   ATan2(2.0, 1.0) = 63.434948822922 degrees
Stack Overflow - Inverse of ATan2 (LUA):

Code: Select all

Given only the angle you can only derive a unit vector pointing to (dx, dy). In order to get the original (dx, dy) you also need to know the length of the vector (dx, dy), which I'll call len. You also have to convert the angle you derived from degrees back to radians and then use the trig equations mentioned elsewhere in this post. That is you have:

  local dy = y1-y2 
  local dx = x1-x2
  local angle = atan2(dy,dx) * 180 / pi
  local len = sqrt(dx*dx + dy*dy)
Given angle (in degrees) and the vector length, len, you can derive dx and dy by:

  local theta = angle * pi / 180
  local dx = len * cos(theta)
  local dy = len * sin(theta)
*I suggest reading the full post on this one.



Anyway, hopefully this helps you. Like I said I know very little about these particular languages, but I think this is probably what you're looking for.
Thank you, I also tried to find something with Google, but had no success. But this is great, I had no idea about this ArcTan2 Delphi function! Thanks!
User avatar
VasTex
Posts: 408
Joined: September 20th, 2012, 3:17 pm
Location: USA - Montana

Re: math.atan2 question

VasTex » June 11th, 2014, 8:25 pm

Happy to help apparently!
01010100 01100101 01100011 01101000 01101110 01101111 01101100 01101111 01100111 01101001
01100011 01100001 01101100 00100000 01000010 01100001 01100100 01100001 01110011 01110011