DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | // |
| 2 | // "$Id: fl_vertex.cxx 8621 2011-04-23 15:46:30Z AlbrechtS $" |
| 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; 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. |
| 12 | // |
| 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. |
| 17 | // |
| 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 |
| 21 | // USA. |
| 22 | // |
| 23 | // Please report all bugs and problems on the following page: |
| 24 | // |
| 25 | // http://www.fltk.org/str.php |
| 26 | // |
| 27 | |
| 28 | /** |
| 29 | \file fl_vertex.cxx |
| 30 | \brief Portable drawing code for drawing arbitrary shapes with |
| 31 | simple 2D transformations. |
| 32 | */ |
| 33 | |
| 34 | // Portable drawing code for drawing arbitrary shapes with |
| 35 | // simple 2D transformations. See also fl_arc.cxx |
| 36 | |
| 37 | // matt: the Quartz implementation purposely doesn't use the Quartz matrix |
| 38 | // operations for reasons of compatibility and maintainability |
| 39 | |
| 40 | #include <config.h> |
| 41 | #include <FL/fl_draw.H> |
| 42 | #include <FL/x.H> |
| 43 | #include <FL/Fl.H> |
| 44 | #include <FL/math.h> |
| 45 | #include <stdlib.h> |
| 46 | |
| 47 | void Fl_Graphics_Driver::push_matrix() { |
| 48 | if (sptr==matrix_stack_size) |
| 49 | Fl::error("fl_push_matrix(): matrix stack overflow."); |
| 50 | else |
| 51 | stack[sptr++] = m; |
| 52 | } |
| 53 | |
| 54 | void Fl_Graphics_Driver::pop_matrix() { |
| 55 | if (sptr==0) |
| 56 | Fl::error("fl_pop_matrix(): matrix stack underflow."); |
| 57 | else |
| 58 | m = stack[--sptr]; |
| 59 | } |
| 60 | |
| 61 | void Fl_Graphics_Driver::mult_matrix(double a, double b, double c, double d, double x, double y) { |
| 62 | matrix o; |
| 63 | o.a = a*m.a + b*m.c; |
| 64 | o.b = a*m.b + b*m.d; |
| 65 | o.c = c*m.a + d*m.c; |
| 66 | o.d = c*m.b + d*m.d; |
| 67 | o.x = x*m.a + y*m.c + m.x; |
| 68 | o.y = x*m.b + y*m.d + m.y; |
| 69 | m = o; |
| 70 | } |
| 71 | |
| 72 | void Fl_Graphics_Driver::rotate(double d) { |
| 73 | if (d) { |
| 74 | double s, c; |
| 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); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void Fl_Graphics_Driver::begin_points() {n = 0; what = POINT_;} |
| 85 | |
| 86 | void Fl_Graphics_Driver::begin_line() {n = 0; what = LINE;} |
| 87 | |
| 88 | void Fl_Graphics_Driver::begin_loop() {n = 0; what = LOOP;} |
| 89 | |
| 90 | void Fl_Graphics_Driver::begin_polygon() {n = 0; what = POLYGON;} |
| 91 | |
| 92 | double Fl_Graphics_Driver::transform_x(double x, double y) {return x*m.a + y*m.c + m.x;} |
| 93 | |
| 94 | double Fl_Graphics_Driver::transform_y(double x, double y) {return x*m.b + y*m.d + m.y;} |
| 95 | |
| 96 | double Fl_Graphics_Driver::transform_dx(double x, double y) {return x*m.a + y*m.c;} |
| 97 | |
| 98 | double Fl_Graphics_Driver::transform_dy(double x, double y) {return x*m.b + y*m.d;} |
| 99 | |
| 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) { |
| 102 | if (n >= p_size) { |
| 103 | p_size = p ? 2*p_size : 16; |
| 104 | p = (XPOINT*)realloc((void*)p, p_size*sizeof(*p)); |
| 105 | } |
| 106 | p[n].x = x; |
| 107 | p[n].y = y; |
| 108 | n++; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void Fl_Graphics_Driver::transformed_vertex(double xf, double yf) { |
| 113 | #ifdef __APPLE_QUARTZ__ |
| 114 | transformed_vertex0(COORD_T(xf), COORD_T(yf)); |
| 115 | #else |
| 116 | transformed_vertex0(COORD_T(rint(xf)), COORD_T(rint(yf))); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 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)); |
| 122 | } |
| 123 | |
| 124 | void Fl_Graphics_Driver::end_points() { |
| 125 | #if defined(USE_X11) |
| 126 | if (n>1) XDrawPoints(fl_display, fl_window, fl_gc, p, n, 0); |
| 127 | #elif defined(WIN32) |
| 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); |
| 135 | } |
| 136 | if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false); |
| 137 | #else |
| 138 | # error unsupported platform |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | void Fl_Graphics_Driver::end_line() { |
| 143 | if (n < 2) { |
| 144 | fl_end_points(); |
| 145 | return; |
| 146 | } |
| 147 | #if defined(USE_X11) |
| 148 | if (n>1) XDrawLines(fl_display, fl_window, fl_gc, p, n, 0); |
| 149 | #elif defined(WIN32) |
| 150 | if (n>1) Polyline(fl_gc, p, n); |
| 151 | #elif defined(__APPLE_QUARTZ__) |
| 152 | if (n<=1) return; |
| 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); |
| 159 | #else |
| 160 | # error unsupported platform |
| 161 | #endif |
| 162 | } |
| 163 | |
| 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--; |
| 166 | } |
| 167 | |
| 168 | void Fl_Graphics_Driver::end_loop() { |
| 169 | fixloop(); |
| 170 | if (n>2) fl_transformed_vertex((COORD_T)p[0].x, (COORD_T)p[0].y); |
| 171 | fl_end_line(); |
| 172 | } |
| 173 | |
| 174 | void Fl_Graphics_Driver::end_polygon() { |
| 175 | fixloop(); |
| 176 | if (n < 3) { |
| 177 | fl_end_line(); |
| 178 | return; |
| 179 | } |
| 180 | #if defined(USE_X11) |
| 181 | if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, Convex, 0); |
| 182 | #elif defined(WIN32) |
| 183 | if (n>2) { |
| 184 | SelectObject(fl_gc, fl_brush()); |
| 185 | Polygon(fl_gc, p, n); |
| 186 | } |
| 187 | #elif defined(__APPLE_QUARTZ__) |
| 188 | if (n<=1) return; |
| 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); |
| 196 | #else |
| 197 | # error unsupported platform |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | void Fl_Graphics_Driver::begin_complex_polygon() { |
| 202 | fl_begin_polygon(); |
| 203 | gap_ = 0; |
| 204 | #if defined(WIN32) |
| 205 | numcount = 0; |
| 206 | #endif |
| 207 | } |
| 208 | |
| 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--; |
| 211 | if (n > gap_+2) { |
| 212 | fl_transformed_vertex((COORD_T)p[gap_].x, (COORD_T)p[gap_].y); |
| 213 | #if defined(WIN32) |
| 214 | counts[numcount++] = n-gap_; |
| 215 | #endif |
| 216 | gap_ = n; |
| 217 | } else { |
| 218 | n = gap_; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void Fl_Graphics_Driver::end_complex_polygon() { |
| 223 | fl_gap(); |
| 224 | if (n < 3) { |
| 225 | fl_end_line(); |
| 226 | return; |
| 227 | } |
| 228 | #if defined(USE_X11) |
| 229 | if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, 0, 0); |
| 230 | #elif defined(WIN32) |
| 231 | if (n>2) { |
| 232 | SelectObject(fl_gc, fl_brush()); |
| 233 | PolyPolygon(fl_gc, p, counts, numcount); |
| 234 | } |
| 235 | #elif defined(__APPLE_QUARTZ__) |
| 236 | if (n<=1) return; |
| 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); |
| 244 | #else |
| 245 | # error unsupported platform |
| 246 | #endif |
| 247 | } |
| 248 | |
| 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. |
| 252 | |
| 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; |
| 262 | |
| 263 | #if defined(USE_X11) |
| 264 | (what == POLYGON ? XFillArc : XDrawArc) |
| 265 | (fl_display, fl_window, fl_gc, llx, lly, w, h, 0, 360*64); |
| 266 | #elif defined(WIN32) |
| 267 | if (what==POLYGON) { |
| 268 | SelectObject(fl_gc, fl_brush()); |
| 269 | Pie(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0); |
| 270 | } else |
| 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); |
| 279 | #else |
| 280 | # error unsupported platform |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | // |
| 285 | // End of "$Id: fl_vertex.cxx 8621 2011-04-23 15:46:30Z AlbrechtS $". |
| 286 | // |