2 // "$Id: fl_vertex.cxx 8621 2011-04-23 15:46:30Z AlbrechtS $"
4 // Portable drawing routines for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2011 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
30 \brief Portable drawing code for drawing arbitrary shapes with
31 simple 2D transformations.
34 // Portable drawing code for drawing arbitrary shapes with
35 // simple 2D transformations. See also fl_arc.cxx
37 // matt: the Quartz implementation purposely doesn't use the Quartz matrix
38 // operations for reasons of compatibility and maintainability
41 #include <FL/fl_draw.H>
47 void Fl_Graphics_Driver::push_matrix() {
48 if (sptr
==matrix_stack_size
)
49 Fl::error("fl_push_matrix(): matrix stack overflow.");
54 void Fl_Graphics_Driver::pop_matrix() {
56 Fl::error("fl_pop_matrix(): matrix stack underflow.");
61 void Fl_Graphics_Driver::mult_matrix(double a
, double b
, double c
, double d
, double x
, double y
) {
67 o
.x
= x
*m
.a
+ y
*m
.c
+ m
.x
;
68 o
.y
= x
*m
.b
+ y
*m
.d
+ m
.y
;
72 void Fl_Graphics_Driver::rotate(double d
) {
75 if (d
== 0) {s
= 0; c
= 1;}
76 else if (d
== 90) {s
= 1; c
= 0;}
77 else if (d
== 180) {s
= 0; c
= -1;}
78 else if (d
== 270 || d
== -90) {s
= -1; c
= 0;}
79 else {s
= sin(d
*M_PI
/180); c
= cos(d
*M_PI
/180);}
80 mult_matrix(c
,-s
,s
,c
,0,0);
84 void Fl_Graphics_Driver::begin_points() {n
= 0; what
= POINT_
;}
86 void Fl_Graphics_Driver::begin_line() {n
= 0; what
= LINE
;}
88 void Fl_Graphics_Driver::begin_loop() {n
= 0; what
= LOOP
;}
90 void Fl_Graphics_Driver::begin_polygon() {n
= 0; what
= POLYGON
;}
92 double Fl_Graphics_Driver::transform_x(double x
, double y
) {return x
*m
.a
+ y
*m
.c
+ m
.x
;}
94 double Fl_Graphics_Driver::transform_y(double x
, double y
) {return x
*m
.b
+ y
*m
.d
+ m
.y
;}
96 double Fl_Graphics_Driver::transform_dx(double x
, double y
) {return x
*m
.a
+ y
*m
.c
;}
98 double Fl_Graphics_Driver::transform_dy(double x
, double y
) {return x
*m
.b
+ y
*m
.d
;}
100 void Fl_Graphics_Driver::transformed_vertex0(COORD_T x
, COORD_T y
) {
101 if (!n
|| x
!= p
[n
-1].x
|| y
!= p
[n
-1].y
) {
103 p_size
= p
? 2*p_size
: 16;
104 p
= (XPOINT
*)realloc((void*)p
, p_size
*sizeof(*p
));
112 void Fl_Graphics_Driver::transformed_vertex(double xf
, double yf
) {
113 #ifdef __APPLE_QUARTZ__
114 transformed_vertex0(COORD_T(xf
), COORD_T(yf
));
116 transformed_vertex0(COORD_T(rint(xf
)), COORD_T(rint(yf
)));
120 void Fl_Graphics_Driver::vertex(double x
,double y
) {
121 transformed_vertex0(COORD_T(x
*m
.a
+ y
*m
.c
+ m
.x
), COORD_T(x
*m
.b
+ y
*m
.d
+ m
.y
));
124 void Fl_Graphics_Driver::end_points() {
126 if (n
>1) XDrawPoints(fl_display
, fl_window
, fl_gc
, p
, n
, 0);
128 for (int i
=0; i
<n
; i
++) SetPixel(fl_gc
, p
[i
].x
, p
[i
].y
, fl_RGB());
129 #elif defined(__APPLE_QUARTZ__)
130 if (fl_quartz_line_width_
> 1.5f
) CGContextSetShouldAntialias(fl_gc
, true);
131 for (int i
=0; i
<n
; i
++) {
132 CGContextMoveToPoint(fl_gc
, p
[i
].x
, p
[i
].y
);
133 CGContextAddLineToPoint(fl_gc
, p
[i
].x
, p
[i
].y
);
134 CGContextStrokePath(fl_gc
);
136 if (fl_quartz_line_width_
> 1.5f
) CGContextSetShouldAntialias(fl_gc
, false);
138 # error unsupported platform
142 void Fl_Graphics_Driver::end_line() {
148 if (n
>1) XDrawLines(fl_display
, fl_window
, fl_gc
, p
, n
, 0);
150 if (n
>1) Polyline(fl_gc
, p
, n
);
151 #elif defined(__APPLE_QUARTZ__)
153 CGContextSetShouldAntialias(fl_gc
, true);
154 CGContextMoveToPoint(fl_gc
, p
[0].x
, p
[0].y
);
155 for (int i
=1; i
<n
; i
++)
156 CGContextAddLineToPoint(fl_gc
, p
[i
].x
, p
[i
].y
);
157 CGContextStrokePath(fl_gc
);
158 CGContextSetShouldAntialias(fl_gc
, false);
160 # error unsupported platform
164 void Fl_Graphics_Driver::fixloop() { // remove equal points from closed path
165 while (n
>2 && p
[n
-1].x
== p
[0].x
&& p
[n
-1].y
== p
[0].y
) n
--;
168 void Fl_Graphics_Driver::end_loop() {
170 if (n
>2) fl_transformed_vertex((COORD_T
)p
[0].x
, (COORD_T
)p
[0].y
);
174 void Fl_Graphics_Driver::end_polygon() {
181 if (n
>2) XFillPolygon(fl_display
, fl_window
, fl_gc
, p
, n
, Convex
, 0);
184 SelectObject(fl_gc
, fl_brush());
185 Polygon(fl_gc
, p
, n
);
187 #elif defined(__APPLE_QUARTZ__)
189 CGContextSetShouldAntialias(fl_gc
, true);
190 CGContextMoveToPoint(fl_gc
, p
[0].x
, p
[0].y
);
191 for (int i
=1; i
<n
; i
++)
192 CGContextAddLineToPoint(fl_gc
, p
[i
].x
, p
[i
].y
);
193 CGContextClosePath(fl_gc
);
194 CGContextFillPath(fl_gc
);
195 CGContextSetShouldAntialias(fl_gc
, false);
197 # error unsupported platform
201 void Fl_Graphics_Driver::begin_complex_polygon() {
209 void Fl_Graphics_Driver::gap() {
210 while (n
>gap_
+2 && p
[n
-1].x
== p
[gap_
].x
&& p
[n
-1].y
== p
[gap_
].y
) n
--;
212 fl_transformed_vertex((COORD_T
)p
[gap_
].x
, (COORD_T
)p
[gap_
].y
);
214 counts
[numcount
++] = n
-gap_
;
222 void Fl_Graphics_Driver::end_complex_polygon() {
229 if (n
>2) XFillPolygon(fl_display
, fl_window
, fl_gc
, p
, n
, 0, 0);
232 SelectObject(fl_gc
, fl_brush());
233 PolyPolygon(fl_gc
, p
, counts
, numcount
);
235 #elif defined(__APPLE_QUARTZ__)
237 CGContextSetShouldAntialias(fl_gc
, true);
238 CGContextMoveToPoint(fl_gc
, p
[0].x
, p
[0].y
);
239 for (int i
=1; i
<n
; i
++)
240 CGContextAddLineToPoint(fl_gc
, p
[i
].x
, p
[i
].y
);
241 CGContextClosePath(fl_gc
);
242 CGContextFillPath(fl_gc
);
243 CGContextSetShouldAntialias(fl_gc
, false);
245 # error unsupported platform
249 // shortcut the closed circles so they use XDrawArc:
250 // warning: these do not draw rotated ellipses correctly!
251 // See fl_arc.c for portable version.
253 void Fl_Graphics_Driver::circle(double x
, double y
,double r
) {
254 double xt
= fl_transform_x(x
,y
);
255 double yt
= fl_transform_y(x
,y
);
256 double rx
= r
* (m
.c
? sqrt(m
.a
*m
.a
+m
.c
*m
.c
) : fabs(m
.a
));
257 double ry
= r
* (m
.b
? sqrt(m
.b
*m
.b
+m
.d
*m
.d
) : fabs(m
.d
));
258 int llx
= (int)rint(xt
-rx
);
259 int w
= (int)rint(xt
+rx
)-llx
;
260 int lly
= (int)rint(yt
-ry
);
261 int h
= (int)rint(yt
+ry
)-lly
;
264 (what
== POLYGON
? XFillArc
: XDrawArc
)
265 (fl_display
, fl_window
, fl_gc
, llx
, lly
, w
, h
, 0, 360*64);
268 SelectObject(fl_gc
, fl_brush());
269 Pie(fl_gc
, llx
, lly
, llx
+w
, lly
+h
, 0,0, 0,0);
271 Arc(fl_gc
, llx
, lly
, llx
+w
, lly
+h
, 0,0, 0,0);
272 #elif defined(__APPLE_QUARTZ__)
273 // Quartz warning: circle won't scale to current matrix!
274 // Last argument must be 0 (counter-clockwise) or it draws nothing under __LP64__ !!!!
275 CGContextSetShouldAntialias(fl_gc
, true);
276 CGContextAddArc(fl_gc
, xt
, yt
, (w
+h
)*0.25f
, 0, 2.0f
*M_PI
, 0);
277 (what
== POLYGON
? CGContextFillPath
: CGContextStrokePath
)(fl_gc
);
278 CGContextSetShouldAntialias(fl_gc
, false);
280 # error unsupported platform
285 // End of "$Id: fl_vertex.cxx 8621 2011-04-23 15:46:30Z AlbrechtS $".