File: g_line.c

package info (click to toggle)
plotutils 2.4.1-15
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,072 kB
  • ctags: 6,952
  • sloc: ansic: 76,305; cpp: 12,402; sh: 8,475; yacc: 2,604; makefile: 894; lex: 144
file content (220 lines) | stat: -rw-r--r-- 8,138 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* This file contains the line method, which is a standard part of libplot.
   It draws an object: a line segment extending from the point x0,y0 to the
   point x1,y1.  By repeatedly invoking cont(), the user may extend this
   line object to a polyline object.  As implemented, the line method is a
   wrapper around the cont method. */

/* This file also contains the cont method, which is a standard part of
   libplot.  It continues a line from the current position of the graphics
   cursor to the point specified by x and y.

   This method is used in the construction of paths.  By repeatedly
   invoking cont(), the user may construct a polyline of arbitrary length.
   arc() and ellarc() may also be invoked, to add circular or elliptic arc
   elements to the path.  The path will terminate when the user either

     (1) explicitly invokes the endpath() method, or 
     (2) changes the value of one of the relevant drawing attributes, 
          e.g. by invoking move(), linemod(), linewidth(), pencolor(), 
	  fillcolor(), or filltype(), or 
     (3) draws some non-path object, by invoking box(), 
           circle(), point(), label(), alabel(), etc., or 
     (4) invokes restorestate() to restore an earlier drawing state. */

#include "sys-defines.h"
#include "extern.h"

int
#ifdef _HAVE_PROTOS
_API_fline (R___(Plotter *_plotter) double x0, double y0, double x1, double y1)
#else
_API_fline (R___(_plotter) x0, y0, x1, y1)
     S___(Plotter *_plotter;) 
     double x0, y0, x1, y1;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "fline: invalid operation");
      return -1;
    }

  if (_plotter->drawstate->path != (plPath *)NULL
      && (_plotter->drawstate->path->type != PATH_SEGMENT_LIST
	  || 
	  (_plotter->drawstate->path->type == PATH_SEGMENT_LIST
	   && _plotter->drawstate->path->primitive)))
    /* There's a simple path under construction (so that endsubpath() must
       not have been invoked), and it contains a closed primitive
       (box/circle/ellipse).  So flush out the whole compound path.  (It
       may include other, previously drawn simple paths.) */
    _API_endpath (S___(_plotter));

  /* if new segment not contiguous, move to its starting point (first
     flushing out the compound path under construction, if any) */
  if (x0 != _plotter->drawstate->pos.x
      || y0 != _plotter->drawstate->pos.y)
    {
      if (_plotter->drawstate->path)
	_API_endpath (S___(_plotter));
      _plotter->drawstate->pos.x = x0;
      _plotter->drawstate->pos.y = y0;
    }

  return _API_fcont (R___(_plotter) x1, y1);
}

int
#ifdef _HAVE_PROTOS
_API_fcont (R___(Plotter *_plotter) double x, double y)
#else
_API_fcont (R___(_plotter) x, y)
     S___(Plotter *_plotter;) 
     double x, y;
#endif
{
  int prev_num_segments;
  plPoint p0, p1;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "fcont: invalid operation");
      return -1;
    }

  if (_plotter->drawstate->path != (plPath *)NULL
      && (_plotter->drawstate->path->type != PATH_SEGMENT_LIST
	  || 
	  (_plotter->drawstate->path->type == PATH_SEGMENT_LIST
	   && _plotter->drawstate->path->primitive)))
    /* There's a simple path under construction (so that endsubpath() must
       not have been invoked), and it contains a closed primitive
       (box/circle/ellipse).  So flush out the whole compound path.  (It
       may include other, previously drawn simple paths.) */
    _API_endpath (S___(_plotter));

  p0 = _plotter->drawstate->pos;
  p1.x = x; p1.y = y;      

  if (_plotter->drawstate->path == (plPath *)NULL)
    /* begin a new path, of segment list type */
    {
      _plotter->drawstate->path = _new_plPath ();
      prev_num_segments = 0;
      _add_moveto (_plotter->drawstate->path, p0);
    }
  else
    prev_num_segments = _plotter->drawstate->path->num_segments;

  /* if segment buffer is occupied by a single arc, replace arc by a
     polyline if that's called for (Plotter-dependent) */
  if (_plotter->data->have_mixed_paths == false
      && _plotter->drawstate->path->num_segments == 2)
    {
      _maybe_replace_arc (S___(_plotter));
      if (_plotter->drawstate->path->num_segments > 2)
	prev_num_segments = 0;	
    }

  /* add new line segment to the path buffer */
  _add_line (_plotter->drawstate->path, p1);

  /* move to endpoint */
  _plotter->drawstate->pos = p1;

  /* pass all the newly added segments to the Plotter-specific function
     maybe_paint_segments(), since some Plotters plot paths in real time,
     i.e., prepaint them, rather than waiting until endpath() is called */
  _plotter->maybe_prepaint_segments (R___(_plotter) prev_num_segments);

  /* If the path is getting too long (and it doesn't have to be filled),
     flush it out by invoking endpath(), and begin a new one.  `Too long'
     is Plotter-dependent; some don't do this flushing at all. */
  if ((_plotter->drawstate->path->num_segments 
       >= _plotter->data->max_unfilled_path_length)
      && (_plotter->drawstate->fill_type == 0)
      && _plotter->path_is_flushable (S___(_plotter)))
    _API_endpath (S___(_plotter));
  
  return 0;
}

/* Some Plotters, such as FigPlotters, support the drawing of single arc
   segments as primitives, but they don't allow mixed segment lists to
   appear in the path storage buffer, because they don't know how to handle
   them.  A `mixed segment list' is a sequence of arc segments interspersed
   with line segments, or a path consisting of more than a single
   contiguous arc.  I.e., a mixed path is defined by a segment list that is
   not a pure polyline and has length >1.
   
   For such Plotters, this function may be invoked by fcont() or farc() or
   fellarc() or fbezier2() or fbezier3(), to delete a single arc from the
   buffer and replace it by its polygonal approximation, i.e. by a
   polyline.  I.e., it is invoked to keep the stored path from becoming
   mixed. */

void
#ifdef _HAVE_PROTOS
_maybe_replace_arc (S___(Plotter *_plotter))
#else
_maybe_replace_arc (S___(_plotter))
     S___(Plotter *_plotter;) 
#endif
{
  /* sanity check */
  if (!(_plotter->data->have_mixed_paths == false
	&& _plotter->drawstate->path->num_segments == 2))
    return;

  switch (_plotter->drawstate->path->segments[1].type)
    {
      plPoint pc, pd, p1;
      
    case S_ARC:
      /* segment buffer contains a single circular arc segment, so remove it */
      pc = _plotter->drawstate->path->segments[1].pc;
      p1 = _plotter->drawstate->path->segments[1].p;
      _plotter->drawstate->path->num_segments = 1;

      /* add polygonal approximation to circular arc to the segment buffer */
      _add_arc_as_lines (_plotter->drawstate->path, pc, p1);
      break;
      
    case S_ELLARC:
      /* segment buffer contains a single elliptic arc segment, so remove it */
      pc = _plotter->drawstate->path->segments[1].pc;
      p1 = _plotter->drawstate->path->segments[1].p;
      _plotter->drawstate->path->num_segments = 1;

      /* add polygonal approximation to elliptic arc to the segment buffer */
      _add_ellarc_as_lines (_plotter->drawstate->path, pc, p1);
      break;

    case S_QUAD:
      /* segment buffer contains a single quad. Bezier segment, so remove it */
      pc = _plotter->drawstate->path->segments[1].pc;
      p1 = _plotter->drawstate->path->segments[1].p;
      _plotter->drawstate->path->num_segments = 1;

      /* add polygonal approximation to quad. Bezier to the segment buffer */
      _add_bezier2_as_lines (_plotter->drawstate->path, pc, p1);
      break;

    case S_CUBIC:
      /* segment buffer contains a single cubic Bezier segment, so remove it */
      pc = _plotter->drawstate->path->segments[1].pc;
      pd = _plotter->drawstate->path->segments[1].pd;
      p1 = _plotter->drawstate->path->segments[1].p;
      _plotter->drawstate->path->num_segments = 1;

      /* add polygonal approximation to cubic Bezier to the segment buffer */
      _add_bezier3_as_lines (_plotter->drawstate->path, pc, pd, p1);
      break;

      default:
	/* other segment type (presumably a line segment, see above); OK */
	break;
    }
}