You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
8.1KB

  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. \file fl_vertex.cxx
  29. \brief Portable drawing code for drawing arbitrary shapes with
  30. simple 2D transformations.
  31. */
  32. // Portable drawing code for drawing arbitrary shapes with
  33. // simple 2D transformations. See also fl_arc.cxx
  34. // matt: the Quartz implementation purposely doesn't use the Quartz matrix
  35. // operations for reasons of compatibility and maintainability
  36. #include <config.h>
  37. #include <FL/fl_draw.H>
  38. #include <FL/x.H>
  39. #include <FL/Fl.H>
  40. #include <FL/math.h>
  41. #include <stdlib.h>
  42. void Fl_Graphics_Driver::push_matrix() {
  43. if (sptr==matrix_stack_size)
  44. Fl::error("fl_push_matrix(): matrix stack overflow.");
  45. else
  46. stack[sptr++] = m;
  47. }
  48. void Fl_Graphics_Driver::pop_matrix() {
  49. if (sptr==0)
  50. Fl::error("fl_pop_matrix(): matrix stack underflow.");
  51. else
  52. m = stack[--sptr];
  53. }
  54. void Fl_Graphics_Driver::mult_matrix(double a, double b, double c, double d, double x, double y) {
  55. matrix o;
  56. o.a = a*m.a + b*m.c;
  57. o.b = a*m.b + b*m.d;
  58. o.c = c*m.a + d*m.c;
  59. o.d = c*m.b + d*m.d;
  60. o.x = x*m.a + y*m.c + m.x;
  61. o.y = x*m.b + y*m.d + m.y;
  62. m = o;
  63. }
  64. void Fl_Graphics_Driver::rotate(double d) {
  65. if (d) {
  66. double s, c;
  67. if (d == 0) {s = 0; c = 1;}
  68. else if (d == 90) {s = 1; c = 0;}
  69. else if (d == 180) {s = 0; c = -1;}
  70. else if (d == 270 || d == -90) {s = -1; c = 0;}
  71. else {s = sin(d*M_PI/180); c = cos(d*M_PI/180);}
  72. mult_matrix(c,-s,s,c,0,0);
  73. }
  74. }
  75. void Fl_Graphics_Driver::begin_points() {n = 0; what = POINT_;}
  76. void Fl_Graphics_Driver::begin_line() {n = 0; what = LINE;}
  77. void Fl_Graphics_Driver::begin_loop() {n = 0; what = LOOP;}
  78. void Fl_Graphics_Driver::begin_polygon() {n = 0; what = POLYGON;}
  79. double Fl_Graphics_Driver::transform_x(double x, double y) {return x*m.a + y*m.c + m.x;}
  80. double Fl_Graphics_Driver::transform_y(double x, double y) {return x*m.b + y*m.d + m.y;}
  81. double Fl_Graphics_Driver::transform_dx(double x, double y) {return x*m.a + y*m.c;}
  82. double Fl_Graphics_Driver::transform_dy(double x, double y) {return x*m.b + y*m.d;}
  83. void Fl_Graphics_Driver::transformed_vertex0(COORD_T x, COORD_T y) {
  84. if (!n || x != p[n-1].x || y != p[n-1].y) {
  85. if (n >= p_size) {
  86. p_size = p ? 2*p_size : 16;
  87. p = (XPOINT*)realloc((void*)p, p_size*sizeof(*p));
  88. }
  89. p[n].x = x;
  90. p[n].y = y;
  91. n++;
  92. }
  93. }
  94. void Fl_Graphics_Driver::transformed_vertex(double xf, double yf) {
  95. #ifdef __APPLE_QUARTZ__
  96. transformed_vertex0(COORD_T(xf), COORD_T(yf));
  97. #else
  98. transformed_vertex0(COORD_T(rint(xf)), COORD_T(rint(yf)));
  99. #endif
  100. }
  101. void Fl_Graphics_Driver::vertex(double x,double y) {
  102. transformed_vertex0(COORD_T(x*m.a + y*m.c + m.x), COORD_T(x*m.b + y*m.d + m.y));
  103. }
  104. void Fl_Graphics_Driver::end_points() {
  105. #if defined(USE_X11)
  106. if (n>1) XDrawPoints(fl_display, fl_window, fl_gc, p, n, 0);
  107. #elif defined(WIN32)
  108. for (int i=0; i<n; i++) SetPixel(fl_gc, p[i].x, p[i].y, fl_RGB());
  109. #elif defined(__APPLE_QUARTZ__)
  110. if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  111. for (int i=0; i<n; i++) {
  112. CGContextMoveToPoint(fl_gc, p[i].x, p[i].y);
  113. CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
  114. CGContextStrokePath(fl_gc);
  115. }
  116. if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  117. #else
  118. # error unsupported platform
  119. #endif
  120. }
  121. void Fl_Graphics_Driver::end_line() {
  122. if (n < 2) {
  123. fl_end_points();
  124. return;
  125. }
  126. #if defined(USE_X11)
  127. if (n>1) XDrawLines(fl_display, fl_window, fl_gc, p, n, 0);
  128. #elif defined(WIN32)
  129. if (n>1) Polyline(fl_gc, p, n);
  130. #elif defined(__APPLE_QUARTZ__)
  131. if (n<=1) return;
  132. CGContextSetShouldAntialias(fl_gc, true);
  133. CGContextMoveToPoint(fl_gc, p[0].x, p[0].y);
  134. for (int i=1; i<n; i++)
  135. CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
  136. CGContextStrokePath(fl_gc);
  137. CGContextSetShouldAntialias(fl_gc, false);
  138. #else
  139. # error unsupported platform
  140. #endif
  141. }
  142. void Fl_Graphics_Driver::fixloop() { // remove equal points from closed path
  143. while (n>2 && p[n-1].x == p[0].x && p[n-1].y == p[0].y) n--;
  144. }
  145. void Fl_Graphics_Driver::end_loop() {
  146. fixloop();
  147. if (n>2) fl_transformed_vertex((COORD_T)p[0].x, (COORD_T)p[0].y);
  148. fl_end_line();
  149. }
  150. void Fl_Graphics_Driver::end_polygon() {
  151. fixloop();
  152. if (n < 3) {
  153. fl_end_line();
  154. return;
  155. }
  156. #if defined(USE_X11)
  157. if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, Convex, 0);
  158. #elif defined(WIN32)
  159. if (n>2) {
  160. SelectObject(fl_gc, fl_brush());
  161. Polygon(fl_gc, p, n);
  162. }
  163. #elif defined(__APPLE_QUARTZ__)
  164. if (n<=1) return;
  165. CGContextSetShouldAntialias(fl_gc, true);
  166. CGContextMoveToPoint(fl_gc, p[0].x, p[0].y);
  167. for (int i=1; i<n; i++)
  168. CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
  169. CGContextClosePath(fl_gc);
  170. CGContextFillPath(fl_gc);
  171. CGContextSetShouldAntialias(fl_gc, false);
  172. #else
  173. # error unsupported platform
  174. #endif
  175. }
  176. void Fl_Graphics_Driver::begin_complex_polygon() {
  177. fl_begin_polygon();
  178. gap_ = 0;
  179. #if defined(WIN32)
  180. numcount = 0;
  181. #endif
  182. }
  183. void Fl_Graphics_Driver::gap() {
  184. while (n>gap_+2 && p[n-1].x == p[gap_].x && p[n-1].y == p[gap_].y) n--;
  185. if (n > gap_+2) {
  186. fl_transformed_vertex((COORD_T)p[gap_].x, (COORD_T)p[gap_].y);
  187. #if defined(WIN32)
  188. counts[numcount++] = n-gap_;
  189. #endif
  190. gap_ = n;
  191. } else {
  192. n = gap_;
  193. }
  194. }
  195. void Fl_Graphics_Driver::end_complex_polygon() {
  196. fl_gap();
  197. if (n < 3) {
  198. fl_end_line();
  199. return;
  200. }
  201. #if defined(USE_X11)
  202. if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, 0, 0);
  203. #elif defined(WIN32)
  204. if (n>2) {
  205. SelectObject(fl_gc, fl_brush());
  206. PolyPolygon(fl_gc, p, counts, numcount);
  207. }
  208. #elif defined(__APPLE_QUARTZ__)
  209. if (n<=1) return;
  210. CGContextSetShouldAntialias(fl_gc, true);
  211. CGContextMoveToPoint(fl_gc, p[0].x, p[0].y);
  212. for (int i=1; i<n; i++)
  213. CGContextAddLineToPoint(fl_gc, p[i].x, p[i].y);
  214. CGContextClosePath(fl_gc);
  215. CGContextFillPath(fl_gc);
  216. CGContextSetShouldAntialias(fl_gc, false);
  217. #else
  218. # error unsupported platform
  219. #endif
  220. }
  221. // shortcut the closed circles so they use XDrawArc:
  222. // warning: these do not draw rotated ellipses correctly!
  223. // See fl_arc.c for portable version.
  224. void Fl_Graphics_Driver::circle(double x, double y,double r) {
  225. double xt = fl_transform_x(x,y);
  226. double yt = fl_transform_y(x,y);
  227. double rx = r * (m.c ? sqrt(m.a*m.a+m.c*m.c) : fabs(m.a));
  228. double ry = r * (m.b ? sqrt(m.b*m.b+m.d*m.d) : fabs(m.d));
  229. int llx = (int)rint(xt-rx);
  230. int w = (int)rint(xt+rx)-llx;
  231. int lly = (int)rint(yt-ry);
  232. int h = (int)rint(yt+ry)-lly;
  233. #if defined(USE_X11)
  234. (what == POLYGON ? XFillArc : XDrawArc)
  235. (fl_display, fl_window, fl_gc, llx, lly, w, h, 0, 360*64);
  236. #elif defined(WIN32)
  237. if (what==POLYGON) {
  238. SelectObject(fl_gc, fl_brush());
  239. Pie(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0);
  240. } else
  241. Arc(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0);
  242. #elif defined(__APPLE_QUARTZ__)
  243. // Quartz warning: circle won't scale to current matrix!
  244. // Last argument must be 0 (counter-clockwise) or it draws nothing under __LP64__ !!!!
  245. CGContextSetShouldAntialias(fl_gc, true);
  246. CGContextAddArc(fl_gc, xt, yt, (w+h)*0.25f, 0, 2.0f*M_PI, 0);
  247. (what == POLYGON ? CGContextFillPath : CGContextStrokePath)(fl_gc);
  248. CGContextSetShouldAntialias(fl_gc, false);
  249. #else
  250. # error unsupported platform
  251. #endif
  252. }
  253. //
  254. // End of "$Id: fl_vertex.cxx 8621 2011-04-23 15:46:30Z AlbrechtS $".
  255. //