Angles (geometric), normalization and conversion: Difference between revisions

Content added Content deleted
m (Typo)
(add FreeBASIC)
Line 694: Line 694:
1000000 radians 339.5131 377.2368 6035.7881 5.9256
1000000 radians 339.5131 377.2368 6035.7881 5.9256
</pre>
</pre>

=={{header|FreeBASIC}}==
Provides a single function that does the normalising and converting. Supports D, G, M, R, T for degrees, gradians, mils, radians, and turns respectively.
<lang freebasic>#define PI 3.1415926535897932384626433832795028842
#define INVALID -99999

function clamp( byval n as double, lo as double, hi as double ) as double
while n <= lo
n += (hi - lo)/2
wend
while n >= hi
n += (lo - hi)/2
wend
return n
end function

function anglenc( byval angle as double, byval source as string, byval targ as string ) as double
source = ucase(source)
targ = ucase(targ)
select case source
case "D":
angle = clamp(angle, -360, 360)
select case targ
case "D":
return angle
case "G":
return angle*10/9
case "M":
return angle*160/9
case "R":
return angle*PI/180
case "T":
return angle/360
case else
return INVALID
end select
case "G":
angle = clamp(angle, -400, 400)
select case targ
case "D":
return angle*9/10
case "G":
return angle
case "M":
return angle*16
case "R":
return angle*PI/200
case "T":
return angle/400
case else
return INVALID
end select
case "M":
angle = clamp(angle, -6400, 6400)
select case targ
case "D":
return angle*9/160
case "G":
return angle/16
case "M":
return angle
case "R":
return angle*PI/3200
case "T":
return angle/6400
case else
return INVALID
end select
case "R":
angle = clamp(angle, -2*PI, 2*PI)
select case targ
case "D":
return angle*180/PI
case "G":
return angle*200/PI
case "M":
return angle*3200/PI
case "R":
return angle
case "T":
return angle/(2*PI)
case else
return INVALID
end select
case "T":
angle = clamp(angle, -1, 1)
select case targ
case "D":
return angle*360
case "G":
return angle*400
case "M":
return angle*6400
case "R":
return angle*2*PI
case "T":
return angle
case else
return INVALID
end select
case else:
return INVALID
end select
end function

function clip( st as string, num as uinteger ) as string
if len(st)<num then return st
return left(st, num)
end function

dim as string scales = "DGMRT", source, targ
dim as double angles(12) = {-2, -1, 0, 1, 2, 6.2831853, 16, 57.2957795, 359, 399, 6399, 1000000}
print "Angle Normalised Unit | D G M R T"
for k as ubyte = 0 to 11
for i as ubyte = 1 to 5
source = mid(scales,i,1)
print angles(k), clip(str(anglenc(angles(k), source, source )), 10), source, "|",
for j as ubyte = 1 to 5
targ = mid(scales, j, 1)
print clip(str(anglenc(angles(k), source, targ )), 10),
next j
print
next i
next k
</lang>
{{out}}
<pre>
Angle Normalised Unit | D G M R T
-2 -2 D | -2 -2.2222222 -35.555555 -0.0349065 -0.0055555
-2 -2 G | -1.8 -2 -32 -0.0314159 -0.005
-2 -2 M | -0.1125 -0.125 -2 -0.0019634 -0.0003125
-2 -2 R | -114.59155 -127.32395 -2037.1832 -2 -0.3183098
-2 0 T | 0 0 0 0 0
-1 -1 D | -1 -1.1111111 -17.777777 -0.0174532 -0.0027777
-1 -1 G | -0.9 -1 -16 -0.0157079 -0.0025
-1 -1 M | -0.05625 -0.0625 -1 -0.0009817 -0.0001562
-1 -1 R | -57.295779 -63.661977 -1018.5916 -1 -0.1591549
-1 0 T | 0 0 0 0 0
0 0 D | 0 0 0 0 0
0 0 G | 0 0 0 0 0
0 0 M | 0 0 0 0 0
0 0 R | 0 0 0 0 0
0 0 T | 0 0 0 0 0
1 1 D | 1 1.11111111 17.7777777 0.01745329 0.00277777
1 1 G | 0.9 1 16 0.01570796 0.0025
1 1 M | 0.05625 0.0625 1 0.00098174 0.00015625
1 1 R | 57.2957795 63.6619772 1018.59163 1 0.15915494
1 0 T | 0 0 0 0 0
2 2 D | 2 2.22222222 35.5555555 0.03490658 0.00555555
2 2 G | 1.8 2 32 0.03141592 0.005
2 2 M | 0.1125 0.125 2 0.00196349 0.0003125
2 2 R | 114.591559 127.323954 2037.18327 2 0.31830988
2 0 T | 0 0 0 0 0
6.2831853 6.2831853 D | 6.2831853 6.98131700 111.701072 0.10966227 0.01745329
6.2831853 6.2831853 G | 5.65486677 6.2831853 100.530964 0.09869604 0.01570796
6.2831853 6.2831853 M | 0.35342917 0.39269908 6.2831853 0.00616850 0.00098174
6.2831853 6.2831853 R | 359.999999 399.999999 6399.99999 6.2831853 0.99999999
6.2831853 0.28318530 T | 101.946708 113.274120 1812.38592 1.77930571 0.28318530
16 16 D | 16 17.7777777 284.444444 0.27925268 0.04444444
16 16 G | 14.4 16 256 0.25132741 0.04
16 16 M | 0.9 1 16 0.01570796 0.0025
16 3.43362938 R | 196.732472 218.591635 3497.46617 3.43362938 0.54647908
16 0 T | 0 0 0 0 0
57.2957795 57.2957795 D | 57.2957795 63.6619772 1018.59163 0.99999999 0.15915494
57.2957795 57.2957795 G | 51.5662015 57.2957795 916.732472 0.89999999 0.14323944
57.2957795 57.2957795 M | 3.22288759 3.58098621 57.2957795 0.05624999 0.00895246
57.2957795 0.74711173 R | 42.8063492 47.5626102 761.001764 0.74711173 0.11890652
57.2957795 0.29577950 T | 106.480620 118.311800 1892.98880 1.85843740 0.29577950
359 359 D | 359 398.888888 6382.22222 6.26573201 0.99722222
359 359 G | 323.1 359 5744 5.63915881 0.8975
359 359 M | 20.19375 22.4375 359 0.35244742 0.05609375
359 0.85843749 R | 49.1848451 54.6498279 874.397247 0.85843749 0.13662456
359 0 T | 0 0 0 0 0
399 39 D | 39 43.3333333 693.333333 0.68067840 0.10833333
399 399 G | 359.1 399 6384 6.26747734 0.99750000
399 399 M | 22.44375 24.9375 399 0.39171733 0.06234375
399 3.15932564 R | 181.016025 201.128917 3218.06267 3.15932564 0.50282229
399 0 T | 0 0 0 0 0
6399 279 D | 279 310 4960 4.86946861 0.775
6399 399 G | 359.1 399 6384 6.26747734 0.99750000
6399 6399 M | 359.94375 399.9375 6399 6.28220355 0.99984375
6399 2.71735729 R | 155.693104 172.992338 2767.87740 2.71735729 0.43248084
6399 0 T | 0 0 0 0 0
1000000 280 D | 280 311.111111 4977.77777 4.88692190 0.77777777
1000000 0 G | 0 0 0 0 0
1000000 1600 M | 90 100 1600 1.57079632 0.25
1000000 5.92562252 R | 339.513161 377.236846 6035.78954 5.92562252 0.94309211
1000000 0 T | 0 0 0 0 0</pre>


=={{header|Go}}==
=={{header|Go}}==