Anti-Grain Geometry Tutorial
agg_vpgen_clip_polygon.cpp
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 #include "agg_vpgen_clip_polygon.h"
17 #include "agg_clip_liang_barsky.h"
18 
19 namespace agg
20 {
21 
22  //------------------------------------------------------------------------
23  // Determine the clipping code of the vertex according to the
24  // Cyrus-Beck line clipping algorithm
25  //
26  // | |
27  // 0110 | 0010 | 0011
28  // | |
29  // -------+--------+-------- clip_box.y2
30  // | |
31  // 0100 | 0000 | 0001
32  // | |
33  // -------+--------+-------- clip_box.y1
34  // | |
35  // 1100 | 1000 | 1001
36  // | |
37  // clip_box.x1 clip_box.x2
38  //
39  //
40  unsigned vpgen_clip_polygon::clipping_flags(double x, double y)
41  {
42  if(x < m_clip_box.x1)
43  {
44  if(y > m_clip_box.y2) return 6;
45  if(y < m_clip_box.y1) return 12;
46  return 4;
47  }
48 
49  if(x > m_clip_box.x2)
50  {
51  if(y > m_clip_box.y2) return 3;
52  if(y < m_clip_box.y1) return 9;
53  return 1;
54  }
55 
56  if(y > m_clip_box.y2) return 2;
57  if(y < m_clip_box.y1) return 8;
58 
59  return 0;
60  }
61 
62  //----------------------------------------------------------------------------
63  void vpgen_clip_polygon::reset()
64  {
65  m_vertex = 0;
66  m_num_vertices = 0;
67  }
68 
69  //----------------------------------------------------------------------------
70  void vpgen_clip_polygon::move_to(double x, double y)
71  {
72  m_vertex = 0;
73  m_num_vertices = 0;
74  m_clip_flags = clipping_flags(x, y);
75  if(m_clip_flags == 0)
76  {
77  m_x[0] = x;
78  m_y[0] = y;
79  m_num_vertices = 1;
80  }
81  m_x1 = x;
82  m_y1 = y;
83  m_cmd = path_cmd_move_to;
84  }
85 
86 
87  //----------------------------------------------------------------------------
88  void vpgen_clip_polygon::line_to(double x, double y)
89  {
90  m_vertex = 0;
91  m_num_vertices = 0;
92  unsigned flags = clipping_flags(x, y);
93 
94  if(m_clip_flags == flags)
95  {
96  if(flags == 0)
97  {
98  m_x[0] = x;
99  m_y[0] = y;
100  m_num_vertices = 1;
101  }
102  }
103  else
104  {
105  m_num_vertices = clip_liang_barsky(m_x1, m_y1,
106  x, y,
107  m_clip_box,
108  m_x, m_y);
109  }
110 
111  m_clip_flags = flags;
112  m_x1 = x;
113  m_y1 = y;
114  }
115 
116 
117  //----------------------------------------------------------------------------
118  unsigned vpgen_clip_polygon::vertex(double* x, double* y)
119  {
120  if(m_vertex < m_num_vertices)
121  {
122  *x = m_x[m_vertex];
123  *y = m_y[m_vertex];
124  ++m_vertex;
125  unsigned cmd = m_cmd;
126  m_cmd = path_cmd_line_to;
127  return cmd;
128  }
129  return path_cmd_stop;
130  }
131 
132 
133 }
Definition: agg_arc.cpp:24