g_line.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 line method, which is a standard part of libplot.
  16. It draws an object: a line segment extending from the point x0,y0 to the
  17. point x1,y1. By repeatedly invoking cont(), the user may extend this
  18. line object to a polyline object. As implemented, the line method is a
  19. wrapper around the cont method. */
  20. /* This file also contains the cont method, which is a standard part of
  21. libplot. It continues a line from the current position of the graphics
  22. cursor to the point specified by x and y.
  23. This method is used in the construction of paths. By repeatedly
  24. invoking cont(), the user may construct a polyline of arbitrary length.
  25. arc() and ellarc() may also be invoked, to add circular or elliptic arc
  26. elements to the path. The path will terminate when the user either
  27. (1) explicitly invokes the endpath() method, or
  28. (2) changes the value of one of the relevant drawing attributes,
  29. e.g. by invoking move(), linemod(), linewidth(), pencolor(),
  30. fillcolor(), or filltype(), or
  31. (3) draws some non-path object, by invoking box(),
  32. circle(), point(), label(), alabel(), etc., or
  33. (4) invokes restorestate() to restore an earlier drawing state. */
  34. #include "sys-defines.h"
  35. #include "extern.h"
  36. int
  37. _API_fline (R___(Plotter *_plotter) double x0, double y0, double x1, double y1)
  38. {
  39. if (!_plotter->data->open)
  40. {
  41. _plotter->error (R___(_plotter)
  42. "fline: invalid operation");
  43. return -1;
  44. }
  45. if (_plotter->drawstate->path != (plPath *)NULL
  46. && (_plotter->drawstate->path->type != PATH_SEGMENT_LIST
  47. ||
  48. (_plotter->drawstate->path->type == PATH_SEGMENT_LIST
  49. && _plotter->drawstate->path->primitive)))
  50. /* There's a simple path under construction (so that endsubpath() must
  51. not have been invoked), and it contains a closed primitive
  52. (box/circle/ellipse). So flush out the whole compound path. (It
  53. may include other, previously drawn simple paths.) */
  54. _API_endpath (S___(_plotter));
  55. /* if new segment not contiguous, move to its starting point (first
  56. flushing out the compound path under construction, if any) */
  57. if (x0 != _plotter->drawstate->pos.x
  58. || y0 != _plotter->drawstate->pos.y)
  59. {
  60. if (_plotter->drawstate->path)
  61. _API_endpath (S___(_plotter));
  62. _plotter->drawstate->pos.x = x0;
  63. _plotter->drawstate->pos.y = y0;
  64. }
  65. return _API_fcont (R___(_plotter) x1, y1);
  66. }
  67. int
  68. _API_fcont (R___(Plotter *_plotter) double x, double y)
  69. {
  70. int prev_num_segments;
  71. plPoint p0, p1;
  72. if (!_plotter->data->open)
  73. {
  74. _plotter->error (R___(_plotter)
  75. "fcont: invalid operation");
  76. return -1;
  77. }
  78. if (_plotter->drawstate->path != (plPath *)NULL
  79. && (_plotter->drawstate->path->type != PATH_SEGMENT_LIST
  80. ||
  81. (_plotter->drawstate->path->type == PATH_SEGMENT_LIST
  82. && _plotter->drawstate->path->primitive)))
  83. /* There's a simple path under construction (so that endsubpath() must
  84. not have been invoked), and it contains a closed primitive
  85. (box/circle/ellipse). So flush out the whole compound path. (It
  86. may include other, previously drawn simple paths.) */
  87. _API_endpath (S___(_plotter));
  88. p0 = _plotter->drawstate->pos;
  89. p1.x = x; p1.y = y;
  90. if (_plotter->drawstate->path == (plPath *)NULL)
  91. /* begin a new path, of segment list type */
  92. {
  93. _plotter->drawstate->path = _new_plPath ();
  94. prev_num_segments = 0;
  95. _add_moveto (_plotter->drawstate->path, p0);
  96. }
  97. else
  98. prev_num_segments = _plotter->drawstate->path->num_segments;
  99. /* if segment buffer is occupied by a single arc, replace arc by a
  100. polyline if that's called for (Plotter-dependent) */
  101. if (_plotter->data->have_mixed_paths == false
  102. && _plotter->drawstate->path->num_segments == 2)
  103. {
  104. _pl_g_maybe_replace_arc (S___(_plotter));
  105. if (_plotter->drawstate->path->num_segments > 2)
  106. prev_num_segments = 0;
  107. }
  108. /* add new line segment to the path buffer */
  109. _add_line (_plotter->drawstate->path, p1);
  110. /* move to endpoint */
  111. _plotter->drawstate->pos = p1;
  112. /* pass all the newly added segments to the Plotter-specific function
  113. maybe_paint_segments(), since some Plotters plot paths in real time,
  114. i.e., prepaint them, rather than waiting until endpath() is called */
  115. _plotter->maybe_prepaint_segments (R___(_plotter) prev_num_segments);
  116. /* If the path is getting too long (and it doesn't have to be filled),
  117. flush it out by invoking endpath(), and begin a new one. `Too long'
  118. is Plotter-dependent; some don't do this flushing at all. */
  119. if ((_plotter->drawstate->path->num_segments
  120. >= _plotter->data->max_unfilled_path_length)
  121. && (_plotter->drawstate->fill_type == 0)
  122. && _plotter->path_is_flushable (S___(_plotter)))
  123. _API_endpath (S___(_plotter));
  124. return 0;
  125. }
  126. /* Some Plotters, such as FigPlotters, support the drawing of single arc
  127. segments as primitives, but they don't allow mixed segment lists to
  128. appear in the path storage buffer, because they don't know how to handle
  129. them. A `mixed segment list' is a sequence of arc segments interspersed
  130. with line segments, or a path consisting of more than a single
  131. contiguous arc. I.e., a mixed path is defined by a segment list that is
  132. not a pure polyline and has length >1.
  133. For such Plotters, this function may be invoked by fcont() or farc() or
  134. fellarc() or fbezier2() or fbezier3(), to delete a single arc from the
  135. buffer and replace it by its polygonal approximation, i.e. by a
  136. polyline. I.e., it is invoked to keep the stored path from becoming
  137. mixed. */
  138. void
  139. _pl_g_maybe_replace_arc (S___(Plotter *_plotter))
  140. {
  141. /* sanity check */
  142. if (!(_plotter->data->have_mixed_paths == false
  143. && _plotter->drawstate->path->num_segments == 2))
  144. return;
  145. switch (_plotter->drawstate->path->segments[1].type)
  146. {
  147. plPoint pc, pd, p1;
  148. case S_ARC:
  149. /* segment buffer contains a single circular arc segment, so remove it */
  150. pc = _plotter->drawstate->path->segments[1].pc;
  151. p1 = _plotter->drawstate->path->segments[1].p;
  152. _plotter->drawstate->path->num_segments = 1;
  153. /* add polygonal approximation to circular arc to the segment buffer */
  154. _add_arc_as_lines (_plotter->drawstate->path, pc, p1);
  155. break;
  156. case S_ELLARC:
  157. /* segment buffer contains a single elliptic arc segment, so remove it */
  158. pc = _plotter->drawstate->path->segments[1].pc;
  159. p1 = _plotter->drawstate->path->segments[1].p;
  160. _plotter->drawstate->path->num_segments = 1;
  161. /* add polygonal approximation to elliptic arc to the segment buffer */
  162. _add_ellarc_as_lines (_plotter->drawstate->path, pc, p1);
  163. break;
  164. case S_QUAD:
  165. /* segment buffer contains a single quad. Bezier segment, so remove it */
  166. pc = _plotter->drawstate->path->segments[1].pc;
  167. p1 = _plotter->drawstate->path->segments[1].p;
  168. _plotter->drawstate->path->num_segments = 1;
  169. /* add polygonal approximation to quad. Bezier to the segment buffer */
  170. _add_bezier2_as_lines (_plotter->drawstate->path, pc, p1);
  171. break;
  172. case S_CUBIC:
  173. /* segment buffer contains a single cubic Bezier segment, so remove it */
  174. pc = _plotter->drawstate->path->segments[1].pc;
  175. pd = _plotter->drawstate->path->segments[1].pd;
  176. p1 = _plotter->drawstate->path->segments[1].p;
  177. _plotter->drawstate->path->num_segments = 1;
  178. /* add polygonal approximation to cubic Bezier to the segment buffer */
  179. _add_bezier3_as_lines (_plotter->drawstate->path, pc, pd, p1);
  180. break;
  181. default:
  182. /* other segment type (presumably a line segment, see above); OK */
  183. break;
  184. }
  185. }