typedefstruct cairo_hull {
cairo_point_t point;
cairo_slope_t slope; int discard; int id;
} cairo_hull_t;
staticvoid
_cairo_hull_init (cairo_hull_t *hull,
cairo_pen_vertex_t *vertices, int num_vertices)
{
cairo_point_t *p, *extremum, tmp; int i;
extremum = &vertices[0].point; for (i = 1; i < num_vertices; i++) {
p = &vertices[i].point; if (p->y < extremum->y || (p->y == extremum->y && p->x < extremum->x))
extremum = p;
} /* Put the extremal point at the beginning of the array */
tmp = *extremum;
*extremum = vertices[0].point;
vertices[0].point = tmp;
for (i = 0; i < num_vertices; i++) {
hull[i].point = vertices[i].point;
_cairo_slope_init (&hull[i].slope, &hull[0].point, &hull[i].point);
/* give each point a unique id for later comparison */
hull[i].id = i;
/* Don't discard by default */
hull[i].discard = 0;
/* Discard all points coincident with the extremal point */ if (i != 0 && hull[i].slope.dx == 0 && hull[i].slope.dy == 0)
hull[i].discard = 1;
}
}
/* Some libraries are reported to actually compare identical *pointersandrequiretheresulttobe0.Thisisthecrazyworldwe *havetolivein.
*/ if (a == b) return0;
ret = _cairo_slope_compare (&a->slope, &b->slope);
/* *Inthecaseoftwoverticeswithidenticalslopefromthe *extremalpointdiscardthenearerpoint.
*/ if (ret == 0) { int cmp;
staticint
_cairo_hull_prev_valid (cairo_hull_t *hull, int num_hull, int index)
{ /* hull[0] is always valid, and we never need to wraparound, (if *wearepassedanindexof0here,thenthecallingloopisjust
* about to terminate). */ if (index == 0) return0;
do {
index--;
} while (hull[index].discard);
return index;
}
staticint
_cairo_hull_next_valid (cairo_hull_t *hull, int num_hull, int index)
{ do {
index = (index + 1) % num_hull;
} while (hull[index].discard);
return index;
}
staticvoid
_cairo_hull_eliminate_concave (cairo_hull_t *hull, int num_hull)
{ int i, j, k;
cairo_slope_t slope_ij, slope_jk;
i = 0;
j = _cairo_hull_next_valid (hull, num_hull, i);
k = _cairo_hull_next_valid (hull, num_hull, j);
do {
_cairo_slope_init (&slope_ij, &hull[i].point, &hull[j].point);
_cairo_slope_init (&slope_jk, &hull[j].point, &hull[k].point);
/* Is the angle formed by ij and jk concave? */ if (_cairo_slope_compare (&slope_ij, &slope_jk) >= 0) { if (i == k) return;
hull[j].discard = 1;
j = i;
i = _cairo_hull_prev_valid (hull, num_hull, j);
} else {
i = j;
j = k;
k = _cairo_hull_next_valid (hull, num_hull, j);
}
} while (j != 0);
}
staticvoid
_cairo_hull_to_pen (cairo_hull_t *hull, cairo_pen_vertex_t *vertices, int *num_vertices)
{ int i, j = 0;
for (i = 0; i < *num_vertices; i++) { if (hull[i].discard) continue;
vertices[j++].point = hull[i].point;
}
*num_vertices = j;
}
/* Given a set of vertices, compute the convex hull using the Graham
scan algorithm. */
cairo_status_t
_cairo_hull_compute (cairo_pen_vertex_t *vertices, int *num_vertices)
{
cairo_hull_t hull_stack[CAIRO_STACK_ARRAY_LENGTH (cairo_hull_t)];
cairo_hull_t *hull; int num_hull = *num_vertices;
if (CAIRO_INJECT_FAULT ()) return _cairo_error (CAIRO_STATUS_NO_MEMORY);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.