Converting Delphi Angles between Degrees, Radians and X/Y

If you need to convert angles in Delphi between degrees (0 – 360), radians and X/Y coordinates you have come to the right place.  Ill show you how to do it in just one line of code, two if you count the USES entry

angle_conversions

Download

Download the full source code and demo project in Delphi 10.1 (Berlin) format.
These will most likely work in many earlier versions of Delphi

Some of the angle conversions are easy

To convert Radian to Degrees use Delphi’s built in RadToDeg function in the System.Math unit

To convert Degrees to Radian use Delphi’s built in DegToRad function in the System.Math unit

Some are more difficult.  Use my routines for those

This is an overview of my conversion routines in the AngleConvertU.pas unit, plus the built in Delphi conversions routines

angle_conversions

Boring Behind The Scenes Bits

The Radian to X / Y routines are based on SIN and COS functions.

x := sin (myRadian); 
y := cos (myRadian);

The SIN and COS functions are radian based, not degree based.  That might confuse you at first if you are used to 360 Degrees, but remember you can easily convert Radian to Degrees (see above for example)

Zero Angle At The Bottom – weoh thats craaazy !

Delphi’s co-ordinate system has the top left pixel at (0,0) and the Y axis increases in value as it stretches down to the bottom of the screen.   That is the reverse of how we normally use angles where the Y axis decreases towards the bottom.  As a result, my conversion routines use 0 degrees at the bottom like this.  If that doesn’t float your goat, you have access my source code so rewrite it or add a wrapper to my functions.

angles_down_is_0

Done !

Its that simple.   Just use my unit or the built in Delphi function.
Its just one line of code to call each angle conversion routines

var
  vRadian : single;
begin
  vRadian := zzXYtoDegreeReal(X, Y);
  
end;

Download

Download the source code and demo project in Delphi 10.1 (Berlin) format.

It should open in earlier versions of Delphi, although with some complaints

+1 this post

I am trying to get this blog listed on DelphiFeeds.com
If you like this post, please +1 for me here on Delphi Feeds

and here on BeginEnd

Thank You !

About The Author

scott_circle
The Usual Suspect
– Scott Hollows –

  • Oracle and Delphi software developer.
  • Australian Delphi User Group – Western Australia Chief Cat Herder
  • Australian Delphi User Group – President
blog email linkedinlogo

Author: Scott Hollows

Enterprise software developer based in Perth, Western Australia. Focused on Oracle, Delphi, Data Warehouse design and ETL, Data Architect, Business Intelligence, Oracle performance tuning. President of the Australian Delphi User Group (ADUG) LinkedIn www.linkedin.com/in/scotthollows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s