g_bez.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains the bezier2 and bezier3 methods, which are GNU
  16. extensions to libplot. Each of them draws an object: a quadratic and a
  17. cubic Bezier path segment, respectively. */
  18. #include "sys-defines.h"
  19. #include "extern.h"
  20. int
  21. _API_fbezier2 (R___(Plotter *_plotter) double x0, double y0, double x1, double y1, double x2, double y2)
  22. {
  23. int prev_num_segments;
  24. plPoint p0, p1, p2;
  25. if (!_plotter->data->open)
  26. {
  27. _plotter->error (R___(_plotter)
  28. "fbezier2: invalid operation");
  29. return -1;
  30. }
  31. if (_plotter->drawstate->path != (plPath *)NULL
  32. && (_plotter->drawstate->path->type != PATH_SEGMENT_LIST
  33. ||
  34. (_plotter->drawstate->path->type == PATH_SEGMENT_LIST
  35. && _plotter->drawstate->path->primitive)))
  36. /* There's a simple path under construction (so that endsubpath() must
  37. not have been invoked), and it contains a closed primitive
  38. (box/circle/ellipse). So flush out the whole compound path. (It
  39. may include other, previously drawn simple paths.) */
  40. _API_endpath (S___(_plotter));
  41. /* if new segment not contiguous, move to its starting point (first
  42. flushing out the compound path under construction, if any) */
  43. if (x0 != _plotter->drawstate->pos.x
  44. || y0 != _plotter->drawstate->pos.y)
  45. {
  46. if (_plotter->drawstate->path)
  47. _API_endpath (S___(_plotter));
  48. _plotter->drawstate->pos.x = x0;
  49. _plotter->drawstate->pos.y = y0;
  50. }
  51. p0.x = x0; p0.y = y0;
  52. p1.x = x1; p1.y = y1;
  53. p2.x = x2; p2.y = y2;
  54. if (_plotter->drawstate->path == (plPath *)NULL)
  55. /* begin a new path, of segment list type */
  56. {
  57. _plotter->drawstate->path = _new_plPath ();
  58. prev_num_segments = 0;
  59. _add_moveto (_plotter->drawstate->path, p0);
  60. }
  61. else
  62. prev_num_segments = _plotter->drawstate->path->num_segments;
  63. /* Trivial case: if linemode is "disconnected", just plot a line segment
  64. from (x0,y0) to (x2,y2). Only the endpoints will appear on the
  65. display. */
  66. if (!_plotter->drawstate->points_are_connected)
  67. _add_line (_plotter->drawstate->path, p2);
  68. /* Another trivial case: treat a zero-length arc as a line segment */
  69. else if (x0 == x2 && y0 == y2)
  70. _add_line (_plotter->drawstate->path, p2);
  71. else
  72. /* standard (non-trivial) case */
  73. {
  74. /* if segment buffer is occupied by a single arc, replace arc by a
  75. polyline if that's called for (Plotter-dependent) */
  76. if (_plotter->data->have_mixed_paths == false
  77. && _plotter->drawstate->path->num_segments == 2)
  78. {
  79. _pl_g_maybe_replace_arc (S___(_plotter));
  80. if (_plotter->drawstate->path->num_segments > 2)
  81. prev_num_segments = 0;
  82. }
  83. /* add new quadratic Bezier segment to the path buffer */
  84. if (_plotter->data->allowed_quad_scaling == AS_ANY)
  85. /* add as a primitive element, since it's allowed */
  86. _add_bezier2 (_plotter->drawstate->path, p1, p2);
  87. else if (_plotter->data->allowed_cubic_scaling == AS_ANY)
  88. /* add quadratic Bezier as a cubic Bezier, since it's allowed */
  89. {
  90. /* (control points need to be computed) */
  91. plPoint p, pc, pd;
  92. p.x = x2;
  93. p.y = y2;
  94. pc.x = (2.0 * x1 + x0) / 3.0;
  95. pc.y = (2.0 * y1 + y0) / 3.0;
  96. pd.x = (2.0 * x1 + x2) / 3.0;
  97. pd.y = (2.0 * y1 + y2) / 3.0;
  98. _add_bezier3 (_plotter->drawstate->path, pc, pd, p);
  99. }
  100. else
  101. /* add quadratic Bezier segment as a polygonal approximation */
  102. _add_bezier2_as_lines (_plotter->drawstate->path, p1, p2);
  103. }
  104. /* move to endpoint */
  105. _plotter->drawstate->pos = p2;
  106. /* pass all the newly added segments to the Plotter-specific function
  107. maybe_paint_segments(), since some Plotters plot paths in real time,
  108. i.e., prepaint them, rather than waiting until endpath() is called */
  109. _plotter->maybe_prepaint_segments (R___(_plotter) prev_num_segments);
  110. /* If the path is getting too long (and it doesn't have to be filled),
  111. flush it out by invoking endpath(), and begin a new one. `Too long'
  112. is Plotter-dependent; some don't do this flushing at all. */
  113. if ((_plotter->drawstate->path->num_segments
  114. >= _plotter->data->max_unfilled_path_length)
  115. && (_plotter->drawstate->fill_type == 0)
  116. && _plotter->path_is_flushable (S___(_plotter)))
  117. _API_endpath (S___(_plotter));
  118. return 0;
  119. }
  120. int
  121. _API_fbezier3 (R___(Plotter *_plotter) double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
  122. {
  123. int prev_num_segments;
  124. plPoint p0, p1, p2, p3;
  125. if (!_plotter->data->open)
  126. {
  127. _plotter->error (R___(_plotter)
  128. "fbezier3: invalid operation");
  129. return -1;
  130. }
  131. if (_plotter->drawstate->path != (plPath *)NULL
  132. && (_plotter->drawstate->path->type != PATH_SEGMENT_LIST
  133. ||
  134. (_plotter->drawstate->path->type == PATH_SEGMENT_LIST
  135. && _plotter->drawstate->path->primitive)))
  136. /* There's a simple path under construction (so that endsubpath() must
  137. not have been invoked), and it contains a closed primitive
  138. (box/circle/ellipse). So flush out the whole compound path. (It
  139. may include other, previously drawn simple paths.) */
  140. _API_endpath (S___(_plotter));
  141. /* if new segment not contiguous, move to its starting point (first
  142. flushing out the compound path under construction, if any) */
  143. if (x0 != _plotter->drawstate->pos.x
  144. || y0 != _plotter->drawstate->pos.y)
  145. {
  146. if (_plotter->drawstate->path)
  147. _API_endpath (S___(_plotter));
  148. _plotter->drawstate->pos.x = x0;
  149. _plotter->drawstate->pos.y = y0;
  150. }
  151. p0.x = x0; p0.y = y0;
  152. p1.x = x1; p1.y = y1;
  153. p2.x = x2; p2.y = y2;
  154. p3.x = x3; p3.y = y3;
  155. if (_plotter->drawstate->path == (plPath *)NULL)
  156. /* begin a new path, of segment list type */
  157. {
  158. _plotter->drawstate->path = _new_plPath ();
  159. prev_num_segments = 0;
  160. _add_moveto (_plotter->drawstate->path, p0);
  161. }
  162. else
  163. prev_num_segments = _plotter->drawstate->path->num_segments;
  164. /* Trivial case: if linemode is "disconnected", just plot a line segment
  165. from (x0,y0) to (x2,y2). Only the endpoints will appear on the
  166. display. */
  167. if (!_plotter->drawstate->points_are_connected)
  168. _add_line (_plotter->drawstate->path, p3);
  169. /* Another trivial case: treat a zero-length arc as a line segment */
  170. else if (x0 == x3 && y0 == y3)
  171. _add_line (_plotter->drawstate->path, p3);
  172. else
  173. /* standard (non-trivial) case */
  174. {
  175. /* if segment buffer is occupied by a single arc, replace arc by a
  176. polyline if that's called for (Plotter-dependent) */
  177. if (_plotter->data->have_mixed_paths == false
  178. && _plotter->drawstate->path->num_segments == 2)
  179. {
  180. _pl_g_maybe_replace_arc (S___(_plotter));
  181. if (_plotter->drawstate->path->num_segments > 2)
  182. prev_num_segments = 0;
  183. }
  184. /* add new cubic Bezier segment to the segment buffer */
  185. if (_plotter->data->allowed_cubic_scaling == AS_ANY)
  186. /* add as a primitive element, since it's allowed */
  187. _add_bezier3 (_plotter->drawstate->path, p1, p2, p3);
  188. else
  189. /* add cubic Bezier segment as a polygonal approximation */
  190. _add_bezier3_as_lines (_plotter->drawstate->path, p1, p2, p3);
  191. }
  192. /* move to endpoint */
  193. _plotter->drawstate->pos = p3;
  194. /* pass all the newly added segments to the Plotter-specific function
  195. maybe_paint_segments(), since some Plotters plot paths in real time,
  196. i.e., prepaint them, rather than waiting until endpath() is called */
  197. _plotter->maybe_prepaint_segments (R___(_plotter) prev_num_segments);
  198. /* If the path is getting too long (and it doesn't have to be filled),
  199. flush it out by invoking endpath(), and begin a new one. `Too long'
  200. is Plotter-dependent; some don't do this flushing at all. */
  201. if ((_plotter->drawstate->path->num_segments
  202. >= _plotter->data->max_unfilled_path_length)
  203. && (_plotter->drawstate->fill_type == 0)
  204. && _plotter->path_is_flushable (S___(_plotter)))
  205. _API_endpath (S___(_plotter));
  206. return 0;
  207. }