1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 /* geometric types and macros (e.g. points and boxes) with application to, but
15  * no specific dependence on graphs */
16 
17 #ifndef GV_GEOM_H
18 #define GV_GEOM_H
19 
20 #include "arith.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 typedef struct { int x, y; } point;
27 
28 typedef struct pointf_s { double x, y; } pointf;
29 
30 /* tell pathplan/pathgeom.h */
31 #define HAVE_POINTF_S
32 
33 typedef struct { point LL, UR; } box;
34 
35 typedef struct { pointf LL, UR; } boxf;
36 
37 
38 /* true if point p is inside box b */
39 #define INSIDE(p,b)	(BETWEEN((b).LL.x,(p).x,(b).UR.x) && BETWEEN((b).LL.y,(p).y,(b).UR.y))
40 
41 /* true if boxes b0 and b1 overlap */
42 #define OVERLAP(b0,b1)	(((b0).UR.x >= (b1).LL.x) && ((b1).UR.x >= (b0).LL.x) && ((b0).UR.y >= (b1).LL.y) && ((b1).UR.y >= (b0).LL.y))
43 
44 /* true if box b0 completely contains b1*/
45 #define CONTAINS(b0,b1)	(((b0).UR.x >= (b1).UR.x) && ((b0).UR.y >= (b1).UR.y) && ((b0).LL.x <= (b1).LL.x) && ((b0).LL.y <= (b1).LL.y))
46 
47 /* expand box b as needed to enclose point p */
48 #define EXPANDBP(b, p)	((b).LL.x = MIN((b).LL.x, (p).x), (b).LL.y = MIN((b).LL.y, (p).y), (b).UR.x = MAX((b).UR.x, (p).x), (b).UR.y = MAX((b).UR.y, (p).y))
49 
50 /* expand box b0 as needed to enclose box b1 */
51 #define EXPANDBB(b0, b1) ((b0).LL.x = MIN((b0).LL.x, (b1).LL.x), (b0).LL.y = MIN((b0).LL.y, (b1).LL.y), (b0).UR.x = MAX((b0).UR.x, (b1).UR.x), (b0).UR.y = MAX((b0).UR.y, (b1).UR.y))
52 
53 #define LEN2(a,b)		(SQR(a) + SQR(b))
54 #define LEN(a,b)		(sqrt(LEN2((a),(b))))
55 
56 #define DIST2(p,q)		(LEN2(((p).x - (q).x),((p).y - (q).y)))
57 #define DIST(p,q)		(sqrt(DIST2((p),(q))))
58 
59 #define POINTS_PER_INCH	72
60 #define POINTS_PER_PC		((double)POINTS_PER_INCH / 6)
61 #define POINTS_PER_CM		((double)POINTS_PER_INCH * 0.393700787)
62 #define POINTS_PER_MM		((double)POINTS_PER_INCH * 0.0393700787)
63 
64 #define POINTS(a_inches)	(ROUND((a_inches)*POINTS_PER_INCH))
65 #define INCH2PS(a_inches)	((a_inches)*(double)POINTS_PER_INCH)
66 #define PS2INCH(a_points)	((a_points)/(double)POINTS_PER_INCH)
67 
68 #define P2PF(p,pf)		((pf).x = (p).x,(pf).y = (p).y)
69 #define PF2P(pf,p)		((p).x = ROUND((pf).x),(p).y = ROUND((pf).y))
70 
71 #define B2BF(b,bf)		(P2PF((b).LL,(bf).LL),P2PF((b).UR,(bf).UR))
72 #define BF2B(bf,b)		(PF2P((bf).LL,(b).LL),PF2P((bf).UR,(b).UR))
73 
74 #define APPROXEQPT(p,q,tol)	(DIST2((p),(q)) < SQR(tol))
75 
76 /* common tolerance value */
77 #define MILLIPOINT .001
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #endif
84