Chris@909
|
1 # Various mathematical calculations extracted from the PDF::Writer for Ruby gem.
|
Chris@909
|
2 # - http://rubyforge.org/projects/ruby-pdf
|
Chris@909
|
3 # - Copyright 2003 - 2005 Austin Ziegler.
|
Chris@909
|
4 # - Licensed under a MIT-style licence.
|
Chris@909
|
5 #
|
Chris@909
|
6
|
Chris@909
|
7 module RFPDF::Math
|
Chris@909
|
8 PI2 = ::Math::PI * 2.0
|
Chris@909
|
9
|
Chris@909
|
10 # One degree of arc measured in terms of radians.
|
Chris@909
|
11 DR = PI2 / 360.0
|
Chris@909
|
12 # One radian of arc, measured in terms of degrees.
|
Chris@909
|
13 RD = 360 / PI2
|
Chris@909
|
14 # One degree of arc, measured in terms of gradians.
|
Chris@909
|
15 DG = 400 / 360.0
|
Chris@909
|
16 # One gradian of arc, measured in terms of degrees.
|
Chris@909
|
17 GD = 360 / 400.0
|
Chris@909
|
18 # One radian of arc, measured in terms of gradians.
|
Chris@909
|
19 RG = 400 / PI2
|
Chris@909
|
20 # One gradian of arc, measured in terms of radians.
|
Chris@909
|
21 GR = PI2 / 400.0
|
Chris@909
|
22
|
Chris@909
|
23 # Truncate the remainder.
|
Chris@909
|
24 def remt(num, den)
|
Chris@909
|
25 num - den * (num / den.to_f).to_i
|
Chris@909
|
26 end
|
Chris@909
|
27
|
Chris@909
|
28 # Wrap radian values within the range of radians (0..PI2).
|
Chris@909
|
29 def rad2rad(rad)
|
Chris@909
|
30 remt(rad, PI2)
|
Chris@909
|
31 end
|
Chris@909
|
32
|
Chris@909
|
33 # Wrap degree values within the range of degrees (0..360).
|
Chris@909
|
34 def deg2deg(deg)
|
Chris@909
|
35 remt(deg, 360)
|
Chris@909
|
36 end
|
Chris@909
|
37
|
Chris@909
|
38 # Wrap gradian values within the range of gradians (0..400).
|
Chris@909
|
39 def grad2grad(grad)
|
Chris@909
|
40 remt(grad, 400)
|
Chris@909
|
41 end
|
Chris@909
|
42
|
Chris@909
|
43 # Convert degrees to radians. The value will be constrained to the
|
Chris@909
|
44 # range of radians (0..PI2) unless +wrap+ is false.
|
Chris@909
|
45 def deg2rad(deg, wrap = true)
|
Chris@909
|
46 rad = DR * deg
|
Chris@909
|
47 rad = rad2rad(rad) if wrap
|
Chris@909
|
48 rad
|
Chris@909
|
49 end
|
Chris@909
|
50
|
Chris@909
|
51 # Convert degrees to gradians. The value will be constrained to the
|
Chris@909
|
52 # range of gradians (0..400) unless +wrap+ is false.
|
Chris@909
|
53 def deg2grad(deg, wrap = true)
|
Chris@909
|
54 grad = DG * deg
|
Chris@909
|
55 grad = grad2grad(grad) if wrap
|
Chris@909
|
56 grad
|
Chris@909
|
57 end
|
Chris@909
|
58
|
Chris@909
|
59 # Convert radians to degrees. The value will be constrained to the
|
Chris@909
|
60 # range of degrees (0..360) unless +wrap+ is false.
|
Chris@909
|
61 def rad2deg(rad, wrap = true)
|
Chris@909
|
62 deg = RD * rad
|
Chris@909
|
63 deg = deg2deg(deg) if wrap
|
Chris@909
|
64 deg
|
Chris@909
|
65 end
|
Chris@909
|
66
|
Chris@909
|
67 # Convert radians to gradians. The value will be constrained to the
|
Chris@909
|
68 # range of gradians (0..400) unless +wrap+ is false.
|
Chris@909
|
69 def rad2grad(rad, wrap = true)
|
Chris@909
|
70 grad = RG * rad
|
Chris@909
|
71 grad = grad2grad(grad) if wrap
|
Chris@909
|
72 grad
|
Chris@909
|
73 end
|
Chris@909
|
74
|
Chris@909
|
75 # Convert gradians to degrees. The value will be constrained to the
|
Chris@909
|
76 # range of degrees (0..360) unless +wrap+ is false.
|
Chris@909
|
77 def grad2deg(grad, wrap = true)
|
Chris@909
|
78 deg = GD * grad
|
Chris@909
|
79 deg = deg2deg(deg) if wrap
|
Chris@909
|
80 deg
|
Chris@909
|
81 end
|
Chris@909
|
82
|
Chris@909
|
83 # Convert gradians to radians. The value will be constrained to the
|
Chris@909
|
84 # range of radians (0..PI2) unless +wrap+ is false.
|
Chris@909
|
85 def grad2rad(grad, wrap = true)
|
Chris@909
|
86 rad = GR * grad
|
Chris@909
|
87 rad = rad2rad(rad) if wrap
|
Chris@909
|
88 rad
|
Chris@909
|
89 end
|
Chris@909
|
90 end
|