User Tools

Site Tools


platform_agnostic:polar_and_rectangular_co_ordinate_conversion_functions

Polar and Rectangular Co-ordinate Conversion Functions

Any point on a conventional, flat plane may be expressed in one of two ways:

  1. A set of “Rectangular” (or Cartesian, planar) co-ordinates - these take the familiar form of a horizontal X axis bisected by a vertical Y axis. The origin is usually the point where the two lines cross at the zero value of both X & Y. This is a typical computer screen layout, graph paper etc
  2. A set of “Polar” co-ordinates which consist of an angle (Theta θ) and a radius. Any point on that plane is then expressed as a direction and distance to “travel”.

Both reach the same conclusion.

All co-ordinate systems require an origin - a point of reference. This could be a specific point on the plane -Absolute, or starting from another point which can be different every time -Relative. For the illustration below, we will consider the origin to be the absolute point at which the two axes bisect - 0,0 in rectangular terms.

Consider the point 1,1. Moving one point along both axes would arrive at that desired point, but you could also decide an angle and distance ; these polar co-ordinates would be 45 degrees and 1.4142 units. Both would determine the same point. If you trace a line from the origin to this point, it would be a diagonal line stretching in a “North-Easterly” direction for one “square”.

The following two functions (actually SUBs because they return more than one value) convert between these two systems. The third argument in each is an option that indicates whether the angles are expressed in Radians (0) or Degrees (non-zero).

Syntax:

Polar (X,Y,opt) - the output will be in the two global variables Th and R

Rect (Theta,radius,opt) - the output will be in the two global variables X and Y

Examples:

Polar 10,27,1 : Print Th,R > 20.3231 28.7924

Rect 20.3,28.7,1 : Print X,Y > 9.95705 26.9174

'Preamble

	CONST Deg2Rad=360/(2*Pi)
	Dim Float Th,R,X,Y	


	Sub Polar(x As Float,y As Float,opt As Integer) ' opt 0 = radians, Not 0=degrees
		If y=0 Then y=1e-6		'eliminates div zero errors

		R=SQR(X*X+Y*Y)
		
		Th=Atn(X/Y)
		If Y<0 Then
			Th=Th+Pi
		ElseIf X<0 Then 
			Th=Th+2*Pi
		EndIf
		
		If opt then 
			Th=Th*Deg2Rad
		EndIf
		'here Th and R contain the polar co-ordinates
	End Sub
	

	Sub Rect(Th As Float,R As Float,opt As Integer) ' opt 0 = radians, Not 0=degrees
		If opt then 
			Th=Th/Deg2Rad
		EndIf
		
		Y=R*(Cos(Th))
		X=R*(Sin(Th))
		'here X and Y contain the rectangular co-ordinates
	End Sub
platform_agnostic/polar_and_rectangular_co_ordinate_conversion_functions.txt · Last modified: 2024/02/01 09:42 by gerry