Menu

#1 error while drawing a line that has slope < 0

open
nobody
None
5
2014-09-27
2004-03-22
No

2 lines creep in when a line whose slope < 0 is drawn. i
noticed in the examples there are only lines with slopes
> 0 or = 0 being drawn this is my solution to the problem
since i did not find any here.

static void
vc_graph_data (GtkStripChartChart *chart,
GtkStripChartStrip *strip, int new_y_max)
{
GList *stubble;
gdouble *stub;
gdouble delta;
GtkPlot *plot;
GList *node;
int i;
int istart, iend;
GtkStripChartLimit *limit, *limit_last=NULL;
gboolean fill_area = 0;

if (0 >= strip->npts) return;

plot = GTK_PLOT(strip);
stubble = strip->stubs;

/* Free old plot data */
/* hack alert -- fixme -- we could save some CPU by
recyling
* the old plotdata structs */
for (node=strip->plot_data_list; node; node=node-
>next)
{
GtkPlotData *pld = (GtkPlotData *)node->data;
gtk_plot_remove_data (plot, pld);
gtk_object_destroy (GTK_OBJECT (pld));
}
g_list_free (strip->plot_data_list);
strip->plot_data_list = NULL;

if (0 >= strip->npts) return;

/* loop over points until the range changes */
istart = 1; /* thats right, istart=1 */
for (i=0; i<strip->npts; i++)
{
GList * lnode;
GtkPlotData *pld;
GdkColor *color = NULL;

iend = i;
color = &chart->default_color;
fill_area = 0;

limit = NULL;
for (lnode = strip->limit_list; lnode; lnode=lnode-
>next)
{
GtkStripChartLimit * next_limit = (GtkStripChartLimit
*)lnode->data;
if (next_limit->range_max < strip->ypts[i]) break;
limit = next_limit;
}

if (0 == i) {limit_last = limit; continue; }

/* if no crossings, continue */
if ((limit_last == limit) && (i < strip->npts-1))
continue;

/* ------------------------------------------------
*/
/* If we are here, then we have a limit-ine crossing */
if (limit_last)
{
color = &limit_last->range_color;
fill_area = limit_last->range_fill;
}
else
{
color = &chart->default_color;
fill_area = 0;
}

/* create a dataset; set dataset attributes -- this
* for the main set of data points before the crossing
*/

LINE_NEW(strip->symbol.symbol_type);
LINE_ATTR();

gtk_plot_data_set_points (pld, &strip->xpts[istart-1],
&strip->ypts[istart-1],
NULL, NULL, iend - istart+1);

if (iend < strip->npts-1)
{
/* And now do the stub */
/* The stubs are two short lines, each of a different
color,
* drawn from the last point before a limit line, to the
* limit line (in the color of the preceeding segments),
and
* then from the limit line to the next 'true' datapoint,
* (in the color of the limit line). We do this fancy
footwork
* so that the line segment crossing the limit line
changes
* color as it crosses the limit line. Wow !
*/
g_return_if_fail (limit != NULL);

if (stubble)
{
stub = (double *)stubble->data;
stubble = stubble->next;
}
else
{
stub = g_new (gdouble, 6);
strip->stubs = g_list_prepend (strip->stubs, stub);
}

stub[0] = strip->xpts[iend-1];
stub[2] = strip->xpts[iend];
stub[3] = strip->ypts[iend-1];
stub[4] = new_y_max;//limit->range_max;
stub[5] = strip->ypts[iend];
stub[6] = new_y_max;

/* linear interp to get x-intersection */
delta = (strip->ypts[iend-1] - strip->ypts[iend]) ;
if (delta > 0.0)
{
stub[1] = (strip->xpts[iend-1] - strip->xpts
[iend]) ;
stub[1] /= delta;
stub[1] *= (limit->range_max - strip->ypts[iend]) ;
stub[1] += strip->xpts[iend];
/* the line stub above the limit */
LINE_NEW
(GTK_PLOT_SYMBOL_NONE);
LINE_ATTR();

      gtk\_plot\_data\_set\_points \(pld, &stub

[0],

             &stub\[3\],

        NULL, NULL, 2\);

      /\* the line stub below the limit \*/
      color = &limit-&gt;range\_color;
      fill\_area = limit-&gt;range\_fill;
      LINE\_NEW

(GTK_PLOT_SYMBOL_NONE);
LINE_ATTR();

      gtk\_plot\_data\_set\_points \(pld, &stub

[1],

             &stub\[4\],

        NULL, NULL, 2\);

}
else if(delta == 0.0)
{
stub[1] = stub[0];
/* the line stub above the limit */
LINE_NEW
(GTK_PLOT_SYMBOL_NONE);
LINE_ATTR();

      gtk\_plot\_data\_set\_points \(pld, &stub

[0],

             &stub\[3\],

        NULL, NULL, 2\);

      /\* the line stub below the limit \*/
      color = &limit-&gt;range\_color;
      fill\_area = limit-&gt;range\_fill;
      LINE\_NEW

(GTK_PLOT_SYMBOL_NONE);
LINE_ATTR();

      gtk\_plot\_data\_set\_points \(pld, &stub

[1],

             &stub\[4\],

        NULL, NULL, 2\);

}
else if(delta < 0.0) {
double slope = (strip->ypts[iend] -
strip->ypts[iend - 1]) / (strip->xpts[iend] - strip->xpts
[iend - 1]); // m = y2 - y1 / x2 - x1
stub[1] = ((new_y_max - strip-
>ypts[iend - 1]) / slope) + strip->xpts[iend - 1];

      // the line stub above the limit 
      LINE\_NEW

(GTK_PLOT_SYMBOL_NONE);
LINE_ATTR();

/\*   gtk\_plot\_data\_set\_points \(pld, &stub

[0],

             &stub\[3\],

        NULL, NULL, 2\);

\*/

      gtk\_plot\_data\_set\_points \(pld, &stub

[0],

             &stub\[3\],

        NULL, NULL, 2\);

      // the line stub below the limit 
      color = &limit-&gt;range\_color;
      fill\_area = limit-&gt;range\_fill;
      LINE\_NEW

(GTK_PLOT_SYMBOL_NONE);
LINE_ATTR();

      gtk\_plot\_data\_set\_points \(pld, &stub

[1],

             &stub\[4\],

        NULL, NULL, 2\);
  \}

}
istart = iend+1;
limit_last = limit;
}
}

thanks again

Discussion


Log in to post a comment.

MongoDB Logo MongoDB