Chris@909: # Various mathematical calculations extracted from the PDF::Writer for Ruby gem. Chris@909: # - http://rubyforge.org/projects/ruby-pdf Chris@909: # - Copyright 2003 - 2005 Austin Ziegler. Chris@909: # - Licensed under a MIT-style licence. Chris@909: # Chris@909: Chris@909: module RFPDF::Math Chris@909: PI2 = ::Math::PI * 2.0 Chris@909: Chris@909: # One degree of arc measured in terms of radians. Chris@909: DR = PI2 / 360.0 Chris@909: # One radian of arc, measured in terms of degrees. Chris@909: RD = 360 / PI2 Chris@909: # One degree of arc, measured in terms of gradians. Chris@909: DG = 400 / 360.0 Chris@909: # One gradian of arc, measured in terms of degrees. Chris@909: GD = 360 / 400.0 Chris@909: # One radian of arc, measured in terms of gradians. Chris@909: RG = 400 / PI2 Chris@909: # One gradian of arc, measured in terms of radians. Chris@909: GR = PI2 / 400.0 Chris@909: Chris@909: # Truncate the remainder. Chris@909: def remt(num, den) Chris@909: num - den * (num / den.to_f).to_i Chris@909: end Chris@909: Chris@909: # Wrap radian values within the range of radians (0..PI2). Chris@909: def rad2rad(rad) Chris@909: remt(rad, PI2) Chris@909: end Chris@909: Chris@909: # Wrap degree values within the range of degrees (0..360). Chris@909: def deg2deg(deg) Chris@909: remt(deg, 360) Chris@909: end Chris@909: Chris@909: # Wrap gradian values within the range of gradians (0..400). Chris@909: def grad2grad(grad) Chris@909: remt(grad, 400) Chris@909: end Chris@909: Chris@909: # Convert degrees to radians. The value will be constrained to the Chris@909: # range of radians (0..PI2) unless +wrap+ is false. Chris@909: def deg2rad(deg, wrap = true) Chris@909: rad = DR * deg Chris@909: rad = rad2rad(rad) if wrap Chris@909: rad Chris@909: end Chris@909: Chris@909: # Convert degrees to gradians. The value will be constrained to the Chris@909: # range of gradians (0..400) unless +wrap+ is false. Chris@909: def deg2grad(deg, wrap = true) Chris@909: grad = DG * deg Chris@909: grad = grad2grad(grad) if wrap Chris@909: grad Chris@909: end Chris@909: Chris@909: # Convert radians to degrees. The value will be constrained to the Chris@909: # range of degrees (0..360) unless +wrap+ is false. Chris@909: def rad2deg(rad, wrap = true) Chris@909: deg = RD * rad Chris@909: deg = deg2deg(deg) if wrap Chris@909: deg Chris@909: end Chris@909: Chris@909: # Convert radians to gradians. The value will be constrained to the Chris@909: # range of gradians (0..400) unless +wrap+ is false. Chris@909: def rad2grad(rad, wrap = true) Chris@909: grad = RG * rad Chris@909: grad = grad2grad(grad) if wrap Chris@909: grad Chris@909: end Chris@909: Chris@909: # Convert gradians to degrees. The value will be constrained to the Chris@909: # range of degrees (0..360) unless +wrap+ is false. Chris@909: def grad2deg(grad, wrap = true) Chris@909: deg = GD * grad Chris@909: deg = deg2deg(deg) if wrap Chris@909: deg Chris@909: end Chris@909: Chris@909: # Convert gradians to radians. The value will be constrained to the Chris@909: # range of radians (0..PI2) unless +wrap+ is false. Chris@909: def grad2rad(grad, wrap = true) Chris@909: rad = GR * grad Chris@909: rad = rad2rad(rad) if wrap Chris@909: rad Chris@909: end Chris@909: end