Debian Codegraph @

extern/agg24-svn/include/ctrl/agg_polygon_ctrl.h

raw
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: [email protected]
12 // [email protected]
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // classes polygon_ctrl_impl, polygon_ctrl
17 //
18 //----------------------------------------------------------------------------
19
20 #ifndef POLYGON_CTRL_INCLUDED
21 #define POLYGON_CTRL_INCLUDED
22
23 #include "agg_array.h"
24 #include "agg_conv_stroke.h"
25 #include "agg_ellipse.h"
26 #include "agg_color_rgba.h"
27 #include "agg_ctrl.h"
28
29 namespace agg
30 {
31 class simple_polygon_vertex_source
32 {
33 public:
34 simple_polygon_vertex_source(const double* polygon, unsigned np,
35 bool roundoff = false,
36 bool close = true) :
37 m_polygon(polygon),
38 m_num_points(np),
39 m_vertex(0),
40 m_roundoff(roundoff),
41 m_close(close)
42 {
43 }
44
45 void close(bool f) { m_close = f; }
46 bool close() const { return m_close; }
47
48 void rewind(unsigned)
49 {
50 m_vertex = 0;
51 }
52
53 unsigned vertex(double* x, double* y)
54 {
55 if(m_vertex > m_num_points) return path_cmd_stop;
56 if(m_vertex == m_num_points)
57 {
58 ++m_vertex;
59 return path_cmd_end_poly | (m_close ? path_flags_close : 0);
60 }
61 *x = m_polygon[m_vertex * 2];
62 *y = m_polygon[m_vertex * 2 + 1];
63 if(m_roundoff)
64 {
65 *x = floor(*x) + 0.5;
66 *y = floor(*y) + 0.5;
67 }
68 ++m_vertex;
69 return (m_vertex == 1) ? path_cmd_move_to : path_cmd_line_to;
70 }
71
72 private:
73 const double* m_polygon;
74 unsigned m_num_points;
75 unsigned m_vertex;
76 bool m_roundoff;
77 bool m_close;
78 };
79
80
81
82
83 class polygon_ctrl_impl : public ctrl
84 {
85 public:
86 polygon_ctrl_impl(unsigned np, double point_radius=5);
87
88 unsigned num_points() const { return m_num_points; }
89 double xn(unsigned n) const { return m_polygon[n * 2]; }
90 double yn(unsigned n) const { return m_polygon[n * 2 + 1]; }
91 double& xn(unsigned n) { return m_polygon[n * 2]; }
92 double& yn(unsigned n) { return m_polygon[n * 2 + 1]; }
93
94 const double* polygon() const { return &m_polygon[0]; }
95
96 void line_width(double w) { m_stroke.width(w); }
97 double line_width() const { return m_stroke.width(); }
98
99 void point_radius(double r) { m_point_radius = r; }
100 double point_radius() const { return m_point_radius; }
101
102 void in_polygon_check(bool f) { m_in_polygon_check = f; }
103 bool in_polygon_check() const { return m_in_polygon_check; }
104
105 void close(bool f) { m_vs.close(f); }
106 bool close() const { return m_vs.close(); }
107
108 // Vertex source interface
109 unsigned num_paths() { return 1; }
110 void rewind(unsigned path_id);
111 unsigned vertex(double* x, double* y);
112
113 virtual bool in_rect(double x, double y) const;
114 virtual bool on_mouse_button_down(double x, double y);
115 virtual bool on_mouse_button_up(double x, double y);
116 virtual bool on_mouse_move(double x, double y, bool button_flag);
117 virtual bool on_arrow_keys(bool left, bool right, bool down, bool up);
118
119
120 private:
121 bool check_edge(unsigned i, double x, double y) const;
122 bool point_in_polygon(double x, double y) const;
123
124 pod_array<double> m_polygon;
125 unsigned m_num_points;
126 int m_node;
127 int m_edge;
128 simple_polygon_vertex_source m_vs;
129 conv_stroke<simple_polygon_vertex_source> m_stroke;
130 ellipse m_ellipse;
131 double m_point_radius;
132 unsigned m_status;
133 double m_dx;
134 double m_dy;
135 bool m_in_polygon_check;
136 };
137
138
139
140 //----------------------------------------------------------polygon_ctrl
141 template<class ColorT> class polygon_ctrl : public polygon_ctrl_impl
142 {
143 public:
144 polygon_ctrl(unsigned np, double point_radius=5) :
145 polygon_ctrl_impl(np, point_radius),
146 m_color(rgba(0.0, 0.0, 0.0))
147 {
148 }
149
150 void line_color(const ColorT& c) { m_color = c; }
151 const ColorT& color(unsigned i) const { return m_color; }
152
153 private:
154 polygon_ctrl(const polygon_ctrl<ColorT>&);
155 const polygon_ctrl<ColorT>& operator = (const polygon_ctrl<ColorT>&);
156
157 ColorT m_color;
158 };
159
160
161
162
163 }
164
165 #endif
166