whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

30 Visitors Online


 
...find true angle of a vector or complex number (Arctan)?
Autor: Uffe Jakobsen
[ Print tip ]  

Tip Rating (12):  
     



{
A simple but usefull tiny tip.
This version of Arctan is valid throughout the
interval [-PI/2;3/2*PI] unlike normally only ]-PI/2;PI/2[.
It is of use when you need to convert a given vector from rectangular to
polar format. It is quite handy when you do complex numbers.

Furthermore it allows finding angles of vertical lines or purely imaginery complex numbers.
A similar function exists in the standard libraries of MOSCOW-ML and C/C++, but not in DELPHI math.
}

function ArcTan2(x, y: Double): Double;
var
  
retur: Double;
begin
  
Retur := 0.0;
  if ABS(x) < 0.000000001 then //Close to zero! We declare it to be zero!
  
begin //The small margin gives a slight error, but improves reliability.
    
if y > 0 then
    begin
      
Retur := PI / 2; //90 degrees
    
end
    else
    begin
      
Retur := PI / (-2); //-90 degrees
    
end;
  end
  else
  begin
    if 
x > 0 then // 1. or 4. quadrant
    
begin
      
Retur := ArcTan(Y / X); // Easy stuff. Normal ArcTan is valid for 1. and 4.
    
end
    else 
// 2. or 3. quadrant
    
begin
      
Retur := PI - ArcTan(-Y / X);
    end;
  end;
  ArcTan2 := Retur;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners