summaryrefslogtreecommitdiffstats
path: root/kviewshell/length.h
blob: 4578d5e0f69c7da354413cc20f316658409d4870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Class: length
//
// Part of KVIESHELL
//
// (C) 2005 Stefan Kebekus. Distributed under the GPL.


#ifndef _length_h_
#define _length_h_

#define mm_per_cm 10.0
#define mm_per_m 1000.0
#define mm_per_inch 25.4
#define mm_per_TeXPoint (2540.0/7227.0)
#define mm_per_bigPoint (25.4/72.0)
#define mm_per_pica (25.4/6.0)
#define mm_per_didot (25.4*0.0148)
#define mm_per_cicero (25.4*0.178)
#define mm_per_scaledPoint (25.4/(72.27 * 65536.0))


#include <math.h>

/** @short Represents a phyical length

    This class is used to represent a physical length. Its main purpose
    it to help in the conversion of units, and to avoid confusion
    about units. To avoid misunderstandings, there is no default
    constructor so that this class needs to be explicitly initialized
    with one of the functions below.

    @warning Lengths are stored internally in mm. If you convert to
    or from any other unit, expect floating point round-off errors.

    @author Stefan Kebekus <kebekus@kde.org>
    @version 1.0.0
*/

class Length
{
 public:
  /** constructs a 'length = 0mm' object */
  Length() {length_in_mm = 0;}

  /** sets the length in millimeters */
  void setLength_in_mm(double l)  {length_in_mm = l;} 

  /** sets the length in centimeters */
  void setLength_in_cm(double l)  {length_in_mm = l*mm_per_cm;}

  /** sets the length in meters */
  void setLength_in_m(double l)  {length_in_mm = l*mm_per_m;} 

  /** sets the length in inches */
  void setLength_in_inch(double l)  {length_in_mm = l*mm_per_inch;} 

  /** sets the length in TeX points */
  void setLength_in_TeXPoints(double l)  {length_in_mm = l*mm_per_TeXPoint;} 

  /** sets the length in big points (1/72 of an inch) */
  void setLength_in_bigPoints(double l)  {length_in_mm = l*mm_per_bigPoint;}

  /** sets the length in picas (1/6 of an inch) */
  void setLength_in_pica(double l)  {length_in_mm = l*mm_per_pica;} 

  /** sets the length in didots (0.0148 inches) */
  void setLength_in_didot(double l)  {length_in_mm = l*mm_per_didot;} 

  /** sets the length in ciceros (0.178 inches) */
  void setLength_in_cicero(double l)  {length_in_mm = l*mm_per_cicero;} 

  /** sets the length in scaled points (1 scaled point = 65536 TeX points) */
  void setLength_in_scaledPoints(double l) {length_in_mm = l*mm_per_scaledPoint;}


  /** returns the length in millimeters */
  double getLength_in_mm() const {return length_in_mm;} 

  /** returns the length in centimeters */
  double getLength_in_cm() const {return length_in_mm/mm_per_cm;}

  /** returns the length in meters */
  double getLength_in_m() const {return length_in_mm/mm_per_m;} 

  /** returns the length in inches */
  double getLength_in_inch() const {return length_in_mm/mm_per_inch;} 

  /** returns the length in TeX points */
  double getLength_in_TeXPoints() const {return length_in_mm/mm_per_TeXPoint;} 

  /** returns the length in big points (1/72 of an inch) */
  double getLength_in_bigPoints() const {return length_in_mm/mm_per_bigPoint;}

  /** returns the length in picas (1/6 of an inch) */
  double getLength_in_pica() const {return length_in_mm/mm_per_pica;} 

  /** returns the length in didots (0.0148 inches) */
  double getLength_in_didot() const {return length_in_mm/mm_per_didot;} 

  /** returns the length in ciceros (0.178 inches) */
  double getLength_in_cicero() const {return length_in_mm/mm_per_cicero;} 

  /** returns the length in scaled points (1 scaled point = 65536 TeX points) */
  double getLength_in_scaledPoints() const {return length_in_mm/mm_per_scaledPoint;}

  /** returns true is lengths differ by no more than 2mm */
  bool isNearlyEqual(const Length &o) const {return fabs(length_in_mm-o.getLength_in_mm()) <= 2.0;}

  /** Comparison of two lengthes */
  bool operator > (const Length &o) const {return (length_in_mm > o.getLength_in_mm());}
  bool operator < (const Length &o) const {return (length_in_mm < o.getLength_in_mm());}

  /** Comparison of two lengthes */
  bool operator >= (const Length &o) const {return (length_in_mm >= o.getLength_in_mm());}
  bool operator <= (const Length &o) const {return (length_in_mm <= o.getLength_in_mm());}

  /** Ratio of two lengthes 

  @warning There is no safeguared to prevent you from division by
  zero. If the length in the denominator is near 0.0, a floating point
  exception may occur.

  @returns the ratio of the two lengthes as a double
  */
  double operator / (const Length &o) const {return (length_in_mm/o.getLength_in_mm());}

  /** Sum of two lengthes

  @returns the sum of the lengthes as a Length
  */
  Length operator + (const Length &o) const {Length r; r.length_in_mm = length_in_mm + o.length_in_mm; return r; }

  /** Difference of two lengthes

  @returns the difference of the lengthes as a Length
  */
  Length operator - (const Length &o) const {Length r; r.length_in_mm = length_in_mm - o.length_in_mm; return r; }

  /** Division of a length

  @warning There is no safeguared to prevent you from division by
  zero. If the number in the denominator is near 0.0, a floating point
  exception may occur.

  @returns a fraction of the original length as a Length
  */
  Length operator / (const double l) const {Length r; r.length_in_mm = length_in_mm/l; return r; }

  /** Multiplication of a length

  @returns a multiplied length as a Length
  */
  Length operator * (const double l) const {Length r; r.length_in_mm = length_in_mm*l; return r; }

 private:
  /** Length in millimeters */
  double length_in_mm;
};

#undef mm_per_cm
#undef mm_per_m
#undef mm_per_inch
#undef mm_per_TeXPoint
#undef mm_per_bigPoint
#undef mm_per_pica
#undef mm_per_didot
#undef mm_per_cicero
#undef mm_per_scaledPoint


#endif