Next: , Previous: Auxiliary classes, Up: Auxiliary classes



8.1.1 The dimension class

The dimension class is used extensively by the printing classes to specify physical dimensions on the page. The class has three public data members and one public enumerated type:

     class dimension {
     public:
       enum units {
         points, inches, cm, mm
       };
     
       int n, d;
       units u;
     }

The integers n and d hold the numerator and denominator respectively of the dimension. The member u defines which units the dimension is given in. For example, a dimension object representing three quarters of an inch would have n equal to 3, d equal to 4, and u equal to dimension::inches. Points here are PostScript points, which are equal to 1/72 of an inch.

The dimension class has several member functions.

— Constructor: dimension::dimension (void);

This constructs a dimension equal to 0.

— Constructor: dimension::dimension (int i);

This constructs a dimension equal to i points.

— Constructor: dimension::dimension (int i, int j, dimension::units u);

This constructs a dimension equal to the fraction i/j in units u.

— Operator: dimension dimension::operator- (void)

This operator returns the negation of the dimension.

— Operator: dimension& dimension::operator*= (int i)
— Operator: dimension dimension::operator* (int i)

These operators multiply the dimension by the integer i.

— Operator: dimension& dimension::operator/= (int i)
— Operator: dimension dimension::operator/ (int i)

These operators divide the dimension by the integer i.

— Operator: bool dimension::operator== (int i);
— Operator: bool dimension::operator!= (int i);

These operators are only useful for comparing a dimension to 0.

— Function: void dimension::reduce (void);

This function reduces the fraction n/d to lowest terms.

— Function: float dimension::in_points (void);

This returns a floating point numbers representing the dimension in points.

— Function: void dimension::set_float (float value, int denom, units uu=points);

This function sets the dimension to an approximation of the given floating-point number value. Specifically, the denominator is set to denom, and the numerator is set to the integer closest to the product of value and denom.

— Function: void dimension::read (const string& s);
— Function: void dimension::read (const char* s);

These functions read a dimension from a string. The string should contain following parts:

For example, the following strings would all be recognised: 0.4 cm; 1 1/2", 56 pt. The allowed unit names are pt, points, point, in, inch, inches, ", cm, mm.

— Function: string& dimension::write (string& s);

This function writes the dimension to the string s.