Anti-Grain Geometry Tutorial
agg_bounding_rect.h
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // bounding_rect function template
17 //
18 //----------------------------------------------------------------------------
19 #ifndef AGG_BOUNDING_RECT_INCLUDED
20 #define AGG_BOUNDING_RECT_INCLUDED
21 
22 #include "agg_basics.h"
23 
24 namespace agg
25 {
26 
27  //-----------------------------------------------------------bounding_rect
28  template<class VertexSource, class GetId, class CoordT>
29  bool bounding_rect(VertexSource& vs, GetId& gi,
30  unsigned start, unsigned num,
31  CoordT* x1, CoordT* y1, CoordT* x2, CoordT* y2)
32  {
33  unsigned i;
34  double x;
35  double y;
36  bool first = true;
37 
38  *x1 = CoordT(1);
39  *y1 = CoordT(1);
40  *x2 = CoordT(0);
41  *y2 = CoordT(0);
42 
43  for(i = 0; i < num; i++)
44  {
45  vs.rewind(gi[start + i]);
46  unsigned cmd;
47  while(!is_stop(cmd = vs.vertex(&x, &y)))
48  {
49  if(is_vertex(cmd))
50  {
51  if(first)
52  {
53  *x1 = CoordT(x);
54  *y1 = CoordT(y);
55  *x2 = CoordT(x);
56  *y2 = CoordT(y);
57  first = false;
58  }
59  else
60  {
61  if(CoordT(x) < *x1) *x1 = CoordT(x);
62  if(CoordT(y) < *y1) *y1 = CoordT(y);
63  if(CoordT(x) > *x2) *x2 = CoordT(x);
64  if(CoordT(y) > *y2) *y2 = CoordT(y);
65  }
66  }
67  }
68  }
69  return *x1 <= *x2 && *y1 <= *y2;
70  }
71 
72 
73  //-----------------------------------------------------bounding_rect_single
74  template<class VertexSource, class CoordT>
75  bool bounding_rect_single(VertexSource& vs, unsigned path_id,
76  CoordT* x1, CoordT* y1, CoordT* x2, CoordT* y2)
77  {
78  double x;
79  double y;
80  bool first = true;
81 
82  *x1 = CoordT(1);
83  *y1 = CoordT(1);
84  *x2 = CoordT(0);
85  *y2 = CoordT(0);
86 
87  vs.rewind(path_id);
88  unsigned cmd;
89  while(!is_stop(cmd = vs.vertex(&x, &y)))
90  {
91  if(is_vertex(cmd))
92  {
93  if(first)
94  {
95  *x1 = CoordT(x);
96  *y1 = CoordT(y);
97  *x2 = CoordT(x);
98  *y2 = CoordT(y);
99  first = false;
100  }
101  else
102  {
103  if(CoordT(x) < *x1) *x1 = CoordT(x);
104  if(CoordT(y) < *y1) *y1 = CoordT(y);
105  if(CoordT(x) > *x2) *x2 = CoordT(x);
106  if(CoordT(y) > *y2) *y2 = CoordT(y);
107  }
108  }
109  }
110  return *x1 <= *x2 && *y1 <= *y2;
111  }
112 
113 
114 }
115 
116 #endif
Definition: agg_arc.cpp:24