merge the formfield patch from ooo-build
[ooovba.git] / agg / source / agg_vpgen_clip_polyline.cpp
blob9b0bc0fc0e946cca919f665234222fcea1ddcb89
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
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 //----------------------------------------------------------------------------
16 #include <math.h>
17 #include "agg_vpgen_clip_polyline.h"
19 namespace agg
21 static double clip_epsilon = 1e-10;
24 //----------------------------------------------------------------------------
25 void vpgen_clip_polyline::reset()
27 m_vertex = 0;
28 m_num_vertices = 0;
31 //----------------------------------------------------------------------------
32 void vpgen_clip_polyline::move_to(double x, double y)
34 m_vertex = 0;
35 m_num_vertices = 0;
36 m_f1 = clipping_flags(x, y);
37 if(m_f1 == 0)
39 m_x[0] = x;
40 m_y[0] = y;
41 m_cmd[0] = path_cmd_move_to;
42 m_num_vertices = 1;
44 m_x1 = x;
45 m_y1 = y;
49 //----------------------------------------------------------------------------
50 bool vpgen_clip_polyline::move_point(double& x, double& y, unsigned& flags)
52 double bound;
54 if(flags & (clip_x1 | clip_x2))
56 bound = (flags & clip_x1) ? m_clip_box.x1 : m_clip_box.x2;
57 y = (bound - m_x1) * (m_y2 - m_y1) / (m_x2 - m_x1) + m_y1;
58 x = bound;
59 flags = clipping_flags_y(y);
61 if(fabs(m_y2 - m_y1) < clip_epsilon && fabs(m_x2 - m_x1) < clip_epsilon)
63 return false;
65 if(flags & (clip_y1 | clip_y2))
67 bound = (flags & clip_y1) ? m_clip_box.y1 : m_clip_box.y2;
68 x = (bound - m_y1) * (m_x2 - m_x1) / (m_y2 - m_y1) + m_x1;
69 y = bound;
71 flags = 0;
72 return true;
75 //----------------------------------------------------------------------------
76 void vpgen_clip_polyline::clip_line_segment()
78 if((m_f1 & m_f2) == 0)
80 if(m_f1)
82 if(!move_point(m_x1, m_y1, m_f1)) return;
83 if(m_f1) return;
84 m_x[0] = m_x1;
85 m_y[0] = m_y1;
86 m_cmd[0] = path_cmd_move_to;
87 m_num_vertices = 1;
89 if(m_f2)
90 { // Move Point 2
91 if(!move_point(m_x2, m_y2, m_f2)) return;
93 m_x[m_num_vertices] = m_x2;
94 m_y[m_num_vertices] = m_y2;
95 m_cmd[m_num_vertices++] = path_cmd_line_to;
101 //----------------------------------------------------------------------------
102 void vpgen_clip_polyline::line_to(double x, double y)
104 m_vertex = 0;
105 m_num_vertices = 0;
106 unsigned f = m_f2 = clipping_flags(m_x2 = x, m_y2 = y);
108 if(m_f2 == m_f1)
110 if(m_f2 == 0)
112 m_x[0] = x;
113 m_y[0] = y;
114 m_cmd[0] = path_cmd_line_to;
115 m_num_vertices = 1;
118 else
120 clip_line_segment();
123 m_f1 = f;
124 m_x1 = x;
125 m_y1 = y;
129 //----------------------------------------------------------------------------
130 unsigned vpgen_clip_polyline::vertex(double* x, double* y)
132 if(m_vertex < m_num_vertices)
134 *x = m_x[m_vertex];
135 *y = m_y[m_vertex];
136 return m_cmd[m_vertex++];
138 return path_cmd_stop;