fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_vertex.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Portable drawing routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2011 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
25 // Portable drawing code for drawing arbitrary shapes with
26 // simple 2D transformations. See also fl_arc.cxx
27 
28 // matt: the Quartz implementation purposely doesn't use the Quartz matrix
29 // operations for reasons of compatibility and maintainability
30 
31 #include <config.h>
32 #include <FL/fl_draw.H>
33 #include <FL/x.H>
34 #include <FL/Fl.H>
35 #include <FL/math.h>
36 #include <stdlib.h>
37 
40  Fl::error("fl_push_matrix(): matrix stack overflow.");
41  else
42  stack[sptr++] = m;
43 }
44 
46  if (sptr==0)
47  Fl::error("fl_pop_matrix(): matrix stack underflow.");
48  else
49  m = stack[--sptr];
50 }
51 
52 void Fl_Graphics_Driver::mult_matrix(double a, double b, double c, double d, double x, double y) {
53  matrix o;
54  o.a = a*m.a + b*m.c;
55  o.b = a*m.b + b*m.d;
56  o.c = c*m.a + d*m.c;
57  o.d = c*m.b + d*m.d;
58  o.x = x*m.a + y*m.c + m.x;
59  o.y = x*m.b + y*m.d + m.y;
60  m = o;
61 }
62 
64  if (d) {
65  double s, c;
66  if (d == 0) {s = 0; c = 1;}
67  else if (d == 90) {s = 1; c = 0;}
68  else if (d == 180) {s = 0; c = -1;}
69  else if (d == 270 || d == -90) {s = -1; c = 0;}
70  else {s = sin(d*M_PI/180); c = cos(d*M_PI/180);}
71  mult_matrix(c,-s,s,c,0,0);
72  }
73 }
74 
76 
78 
80 
82 
83 double Fl_Graphics_Driver::transform_x(double x, double y) {return x*m.a + y*m.c + m.x;}
84 
85 double Fl_Graphics_Driver::transform_y(double x, double y) {return x*m.b + y*m.d + m.y;}
86 
87 double Fl_Graphics_Driver::transform_dx(double x, double y) {return x*m.a + y*m.c;}
88 
89 double Fl_Graphics_Driver::transform_dy(double x, double y) {return x*m.b + y*m.d;}
90 
92  if (!n || x != p[n-1].x || y != p[n-1].y) {
93  if (n >= p_size) {
94  p_size = p ? 2*p_size : 16;
95  p = (XPOINT*)realloc((void*)p, p_size*sizeof(*p));
96  }
97  p[n].x = x;
98  p[n].y = y;
99  n++;
100  }
101 }
102 
103 void Fl_Graphics_Driver::transformed_vertex(double xf, double yf) {
104 #ifdef __APPLE_QUARTZ__
106 #else
107  transformed_vertex0(COORD_T(rint(xf)), COORD_T(rint(yf)));
108 #endif
109 }
110 
111 void Fl_Graphics_Driver::vertex(double x,double y) {
112  transformed_vertex0(COORD_T(x*m.a + y*m.c + m.x), COORD_T(x*m.b + y*m.d + m.y));
113 }
114 
116 #if defined(USE_X11)
117  if (n>1) XDrawPoints(fl_display, fl_window, fl_gc, p, n, 0);
118 #elif defined(WIN32)
119  for (int i=0; i<n; i++) SetPixel(fl_gc, p[i].x, p[i].y, fl_RGB());
120 #elif defined(__APPLE_QUARTZ__)
121  if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
122  for (int i=0; i<n; i++) {
123  CGContextMoveToPoint(fl_gc, p[i].x, p[i].y);
124  CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
125  CGContextStrokePath(fl_gc);
126  }
127  if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
128 #else
129 # error unsupported platform
130 #endif
131 }
132 
134  if (n < 2) {
135  end_points();
136  return;
137  }
138 #if defined(USE_X11)
139  if (n>1) XDrawLines(fl_display, fl_window, fl_gc, p, n, 0);
140 #elif defined(WIN32)
141  if (n>1) Polyline(fl_gc, p, n);
142 #elif defined(__APPLE_QUARTZ__)
143  if (n<=1) return;
144  CGContextSetShouldAntialias(fl_gc, true);
145  CGContextMoveToPoint(fl_gc, p[0].x, p[0].y);
146  for (int i=1; i<n; i++)
147  CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
148  CGContextStrokePath(fl_gc);
149  CGContextSetShouldAntialias(fl_gc, false);
150 #else
151 # error unsupported platform
152 #endif
153 }
154 
155 void Fl_Graphics_Driver::fixloop() { // remove equal points from closed path
156  while (n>2 && p[n-1].x == p[0].x && p[n-1].y == p[0].y) n--;
157 }
158 
160  fixloop();
161  if (n>2) transformed_vertex((COORD_T)p[0].x, (COORD_T)p[0].y);
162  end_line();
163 }
164 
166  fixloop();
167  if (n < 3) {
168  end_line();
169  return;
170  }
171 #if defined(USE_X11)
172  if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, Convex, 0);
173 #elif defined(WIN32)
174  if (n>2) {
175  SelectObject(fl_gc, fl_brush());
176  Polygon(fl_gc, p, n);
177  }
178 #elif defined(__APPLE_QUARTZ__)
179  if (n<=1) return;
180  CGContextSetShouldAntialias(fl_gc, true);
181  CGContextMoveToPoint(fl_gc, p[0].x, p[0].y);
182  for (int i=1; i<n; i++)
183  CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
186  CGContextSetShouldAntialias(fl_gc, false);
187 #else
188 # error unsupported platform
189 #endif
190 }
191 
193  begin_polygon();
194  gap_ = 0;
195 #if defined(WIN32)
196  numcount = 0;
197 #endif
198 }
199 
201  while (n>gap_+2 && p[n-1].x == p[gap_].x && p[n-1].y == p[gap_].y) n--;
202  if (n > gap_+2) {
204 #if defined(WIN32)
205  counts[numcount++] = n-gap_;
206 #endif
207  gap_ = n;
208  } else {
209  n = gap_;
210  }
211 }
212 
214  gap();
215  if (n < 3) {
216  end_line();
217  return;
218  }
219 #if defined(USE_X11)
220  if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, 0, 0);
221 #elif defined(WIN32)
222  if (n>2) {
223  SelectObject(fl_gc, fl_brush());
224  PolyPolygon(fl_gc, p, counts, numcount);
225  }
226 #elif defined(__APPLE_QUARTZ__)
227  if (n<=1) return;
228  CGContextSetShouldAntialias(fl_gc, true);
229  CGContextMoveToPoint(fl_gc, p[0].x, p[0].y);
230  for (int i=1; i<n; i++)
231  CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
234  CGContextSetShouldAntialias(fl_gc, false);
235 #else
236 # error unsupported platform
237 #endif
238 }
239 
240 // shortcut the closed circles so they use XDrawArc:
241 // warning: these do not draw rotated ellipses correctly!
242 // See fl_arc.c for portable version.
243 
244 void Fl_Graphics_Driver::circle(double x, double y,double r) {
245  double xt = transform_x(x,y);
246  double yt = transform_y(x,y);
247  double rx = r * (m.c ? sqrt(m.a*m.a+m.c*m.c) : fabs(m.a));
248  double ry = r * (m.b ? sqrt(m.b*m.b+m.d*m.d) : fabs(m.d));
249  int llx = (int)rint(xt-rx);
250  int w = (int)rint(xt+rx)-llx;
251  int lly = (int)rint(yt-ry);
252  int h = (int)rint(yt+ry)-lly;
253 
254 #if defined(USE_X11)
255  (what == POLYGON ? XFillArc : XDrawArc)
256  (fl_display, fl_window, fl_gc, llx, lly, w, h, 0, 360*64);
257 #elif defined(WIN32)
258  if (what==POLYGON) {
259  SelectObject(fl_gc, fl_brush());
260  Pie(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0);
261  } else
262  Arc(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0);
263 #elif defined(__APPLE_QUARTZ__)
264  // Quartz warning: circle won't scale to current matrix!
265  // Last argument must be 0 (counter-clockwise) or it draws nothing under __LP64__ !!!!
266  CGContextSetShouldAntialias(fl_gc, true);
267  CGContextAddArc(fl_gc, xt, yt, (w+h)*0.25f, 0, 2.0f*M_PI, 0);
268  (what == POLYGON ? CGContextFillPath : CGContextStrokePath)(fl_gc);
269  CGContextSetShouldAntialias(fl_gc, false);
270 #else
271 # error unsupported platform
272 #endif
273 }
274 
275 //
276 // End of "$Id$".
277 //
Fl.H
Fl_Graphics_Driver::what
int what
Definition: Fl_Device.H:126
fl_brush
FL_EXPORT HBRUSH fl_brush()
Definition: fl_color_win32.cxx:122
M_PI
#define M_PI
Definition: math.h:38
Fl_Graphics_Driver::LOOP
Definition: Fl_Device.H:141
XPOINT
#define XPOINT
Definition: Fl_Device.H:62
Fl_Graphics_Driver::POLYGON
Definition: Fl_Device.H:141
Fl_Graphics_Driver::transform_dx
double transform_dx(double x, double y)
see fl_transform_dx(double x, double y).
Definition: fl_vertex.cxx:87
Fl_Graphics_Driver::fixloop
void fixloop()
Definition: fl_vertex.cxx:155
x.H
Fl_Graphics_Driver::matrix::d
double d
Definition: Fl_Device.H:114
Fl_Graphics_Driver::end_complex_polygon
virtual void end_complex_polygon()
see fl_end_complex_polygon().
Definition: fl_vertex.cxx:213
Fl_Graphics_Driver::matrix_stack_size
static const int matrix_stack_size
Definition: Fl_Device.H:121
Fl_Graphics_Driver::end_points
virtual void end_points()
see fl_end_points().
Definition: fl_vertex.cxx:115
CGContextFillPath
#define CGContextFillPath(a)
Definition: cgdebug.h:155
b
long b
Definition: jpegint.h:397
Fl_Graphics_Driver::LINE
Definition: Fl_Device.H:141
Fl_Graphics_Driver::end_loop
virtual void end_loop()
see fl_end_loop().
Definition: fl_vertex.cxx:159
Fl_Graphics_Driver::rotate
void rotate(double d)
see fl_rotate(double d).
Definition: fl_vertex.cxx:63
Fl_Graphics_Driver::POINT_
Definition: Fl_Device.H:141
Fl_Graphics_Driver::transformed_vertex0
void transformed_vertex0(COORD_T x, COORD_T y)
Definition: fl_vertex.cxx:91
math.h
Fl_Graphics_Driver::p
XPoint * p
Definition: Fl_Device.H:125
Fl_Graphics_Driver::matrix::c
double c
Definition: Fl_Device.H:114
fl_RGB
COLORREF fl_RGB()
Definition: win32.H:120
Fl_Graphics_Driver::circle
virtual void circle(double x, double y, double r)
see fl_circle(double x, double y, double r).
Definition: fl_vertex.cxx:244
fl_draw.H
utility header to pull drawing functions together
Fl_Graphics_Driver::transform_dy
double transform_dy(double x, double y)
see fl_transform_dy(double x, double y).
Definition: fl_vertex.cxx:89
CGContextMoveToPoint
#define CGContextMoveToPoint(a, b, c)
Definition: cgdebug.h:145
Fl_Graphics_Driver::matrix::x
double x
Definition: Fl_Device.H:114
Fl_Graphics_Driver::begin_loop
virtual void begin_loop()
see fl_begin_loop().
Definition: fl_vertex.cxx:79
Fl_Graphics_Driver::matrix::a
double a
Definition: Fl_Device.H:114
fl_display
FL_EXPORT HINSTANCE fl_display
END TIMERS.
Definition: Fl_win32.cxx:2147
Fl_Graphics_Driver::gap_
int gap_
Definition: Fl_Device.H:124
Fl_Graphics_Driver::transform_y
double transform_y(double x, double y)
see fl_transform_y(double x, double y).
Definition: fl_vertex.cxx:85
Fl_Graphics_Driver::mult_matrix
void mult_matrix(double a, double b, double c, double d, double x, double y)
see fl_mult_matrix(double a, double b, double c, double d, double x, double y).
Definition: fl_vertex.cxx:52
Fl_Graphics_Driver::stack
matrix stack[32]
Definition: Fl_Device.H:122
Fl_Graphics_Driver::transform_x
double transform_x(double x, double y)
see fl_transform_x(double x, double y).
Definition: fl_vertex.cxx:83
Fl_Graphics_Driver::end_line
virtual void end_line()
see fl_end_line().
Definition: fl_vertex.cxx:133
x
int x
Definition: test.c:73
Fl_Graphics_Driver::matrix::b
double b
Definition: Fl_Device.H:114
Fl_Graphics_Driver::begin_points
virtual void begin_points()
see fl_begin_points().
Definition: fl_vertex.cxx:75
Fl_Graphics_Driver::vertex
virtual void vertex(double x, double y)
see fl_vertex(double x, double y).
Definition: fl_vertex.cxx:111
Fl_Graphics_Driver::sptr
int sptr
Definition: Fl_Device.H:120
Fl_Graphics_Driver::end_polygon
virtual void end_polygon()
see fl_end_polygon().
Definition: fl_vertex.cxx:165
fl_gc
CGContextRef fl_gc
Definition: Fl_win32.cxx:2568
Fl_Graphics_Driver::transformed_vertex
virtual void transformed_vertex(double xf, double yf)
see fl_transformed_vertex(double xf, double yf).
Definition: fl_vertex.cxx:103
COORD_T
short COORD_T
Definition: Fl_Device.H:61
y
int y
Definition: test.c:74
f
Fl_Box_Draw_F * f
Definition: fl_boxtype.cxx:285
Fl_Graphics_Driver::pop_matrix
void pop_matrix()
see fl_pop_matrix().
Definition: fl_vertex.cxx:45
Fl_Graphics_Driver::n
int n
Definition: Fl_Device.H:124
Fl_Graphics_Driver::matrix::y
double y
Definition: Fl_Device.H:114
Fl::error
static void(* error)(const char *,...)
Definition: Fl.H:513
Fl_Graphics_Driver::begin_polygon
virtual void begin_polygon()
see fl_begin_polygon().
Definition: fl_vertex.cxx:81
CGContextClosePath
#define CGContextClosePath(a)
Definition: cgdebug.h:165
Fl_Graphics_Driver::begin_complex_polygon
virtual void begin_complex_polygon()
see fl_begin_complex_polygon().
Definition: fl_vertex.cxx:192
Fl_Graphics_Driver::m
matrix m
Definition: Fl_Device.H:123
Fl_Graphics_Driver::push_matrix
void push_matrix()
see fl_push_matrix().
Definition: fl_vertex.cxx:38
fl_window
FL_EXPORT Window fl_window
Definition: Fl_win32.cxx:2571
Fl_Graphics_Driver::matrix
Definition: Fl_Device.H:114
Fl_Graphics_Driver::p_size
int p_size
Definition: Fl_Device.H:124
Fl_Graphics_Driver::gap
virtual void gap()
see fl_gap().
Definition: fl_vertex.cxx:200
Fl_Graphics_Driver::begin_line
virtual void begin_line()
see fl_begin_line().
Definition: fl_vertex.cxx:77