1 /*
2     Gri - A language for scientific graphics programming
3     Copyright (C) 2008 Daniel Kelley
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 // Define some useful types
21 #ifndef _gri_types_hh_
22 #include <stdio.h>
23 #include <math.h>
24 #define         _gri_types_hh_
25 
26 enum gr_textStyle {		// Text writing options
27 	TEXT_LJUST,
28 	TEXT_RJUST,
29 	TEXT_CENTERED
30 };
31 
32 enum units {			// Units for GriPath
33 	units_cm,
34 	units_pt,
35 	units_user
36 };
37 
38 
min4(double a,double b,double c,double d)39 static inline double min4(double a, double b, double c, double d) {
40 	double rval = a;
41 	if (b < a) rval = b;
42 	if (c < a) rval = c;
43 	if (d < a) rval = d;
44 	return rval;
45 }
max4(double a,double b,double c,double d)46 static inline double max4(double a, double b, double c, double d) {
47 	double rval = a;
48 	if (b > a) rval = b;
49 	if (c > a) rval = c;
50 	if (d > a) rval = d;
51 	return rval;
52 }
53 
54 class rectangle { // a rectangle oriented parallel to axis
55 public:
rectangle(double llx,double lly,double urx,double ury)56 	rectangle(double llx, double lly, double urx, double ury) {
57 		ll_x = llx;
58 		ll_y = lly;
59 		ur_x = urx;
60 		ur_y = ury;
61 	}
rectangle(void)62 	rectangle(void) { ll_x = ll_y = ur_x = ur_y = 0.0; }
~rectangle()63 	~rectangle() {}
set(double llx,double lly,double urx,double ury)64 	void set(double llx, double lly, double urx, double ury) {
65 		ll_x = llx;
66 		ll_y = lly;
67 		ur_x = urx;
68 		ur_y = ury;
69 	}
scale(double f)70 	void scale(double f) { ll_x *= f; ll_y *= f; ur_x *= f; ur_y *= f;}
llx(void) const71 	double llx(void) const { return ll_x; }
lly(void) const72 	double lly(void) const { return ll_y; }
urx(void) const73 	double urx(void) const { return ur_x; }
ury(void) const74 	double ury(void) const { return ur_y; }
set_llx(double llx)75 	void set_llx(double llx) { ll_x = llx;}
set_lly(double lly)76 	void set_lly(double lly) { ll_y = lly;}
set_urx(double urx)77 	void set_urx(double urx) { ur_x = urx;}
set_ury(double ury)78 	void set_ury(double ury) { ur_y = ury;}
shift_x(double dx)79 	void shift_x(double dx)  { ll_x += dx; ur_x += dx;}
shift_y(double dy)80 	void shift_y(double dy)  { ll_y += dy; ur_y += dy;}
rotate(double degrees_ccw)81 	void rotate(double degrees_ccw) {
82 		double rad_ccw = degrees_ccw / 57.29577951;
83 		double c = cos(rad_ccw);
84 		double s = sin(rad_ccw);
85 		double x1 = c * ll_x - s * ll_y;
86 		double y1 = s * ll_x + c * ll_y;
87 		double x2 = c * ur_x - s * ll_y;
88 		double y2 = s * ur_x + c * ll_y;
89 		double x3 = c * ur_x - s * ur_y;
90 		double y3 = s * ur_x + c * ur_y;
91 		double x4 = c * ll_x - s * ur_y;
92 		double y4 = s * ll_x + c * ur_y;
93 		ll_x = min4(x1, x2, x3, x4);
94 		ur_x = max4(x1, x2, x3, x4);
95 		ll_y = min4(y1, y2, y3, y4);
96 		ur_y = max4(y1, y2, y3, y4);
97 	}
get_llx()98 	double get_llx() { return ll_x; }
get_lly()99 	double get_lly() { return ll_y; }
get_urx()100 	double get_urx() { return ur_x; }
get_ury()101 	double get_ury() { return ur_y; }
102 private:
103 	double ll_x;		// lower-left
104 	double ll_y;
105 	double ur_x;		// upper-right
106 	double ur_y;
107 };
108 
109 #endif
110