2002-08-14 00:39:43 +00:00
|
|
|
|
/*
|
|
|
|
|
|
* $XFree86: $
|
|
|
|
|
|
*
|
|
|
|
|
|
* Copyright <EFBFBD> 2002 Carl D. Worth
|
|
|
|
|
|
*
|
|
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software
|
|
|
|
|
|
* and its documentation for any purpose is hereby granted without
|
|
|
|
|
|
* fee, provided that the above copyright notice appear in all copies
|
|
|
|
|
|
* and that both that copyright notice and this permission notice
|
|
|
|
|
|
* appear in supporting documentation, and that the name of Carl
|
|
|
|
|
|
* D. Worth not be used in advertising or publicity pertaining to
|
|
|
|
|
|
* distribution of the software without specific, written prior
|
|
|
|
|
|
* permission. Carl D. Worth makes no representations about the
|
|
|
|
|
|
* suitability of this software for any purpose. It is provided "as
|
|
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
|
|
*
|
|
|
|
|
|
* CARL D. WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|
|
|
|
|
|
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
|
* FITNESS, IN NO EVENT SHALL CARL D. WORTH BE LIABLE FOR ANY SPECIAL,
|
|
|
|
|
|
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
|
|
|
|
|
|
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
|
|
|
|
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#include "xrint.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define XR_POLYGON_GROWTH_INC 10
|
|
|
|
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
|
|
2002-08-15 05:22:59 +00:00
|
|
|
|
static XrError
|
2002-08-15 17:29:26 +00:00
|
|
|
|
_XrPolygonGrowBy(XrPolygon *polygon, int additional);
|
2002-08-14 00:39:43 +00:00
|
|
|
|
|
2002-09-03 08:42:25 +00:00
|
|
|
|
static void
|
|
|
|
|
|
_XrPolygonSetLastPoint(XrPolygon *polygon, XPointFixed *pt);
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-14 00:39:43 +00:00
|
|
|
|
void
|
2002-08-15 17:29:26 +00:00
|
|
|
|
XrPolygonInit(XrPolygon *polygon)
|
2002-08-14 00:39:43 +00:00
|
|
|
|
{
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->num_edges = 0;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->edges_size = 0;
|
|
|
|
|
|
polygon->edges = NULL;
|
2002-09-03 08:42:25 +00:00
|
|
|
|
|
|
|
|
|
|
polygon->first_pt_defined = 0;
|
|
|
|
|
|
polygon->last_pt_defined = 0;
|
|
|
|
|
|
|
|
|
|
|
|
polygon->closed = 0;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2002-08-15 17:29:26 +00:00
|
|
|
|
XrPolygonDeinit(XrPolygon *polygon)
|
2002-08-14 00:39:43 +00:00
|
|
|
|
{
|
2002-08-15 17:29:26 +00:00
|
|
|
|
if (polygon->edges_size) {
|
|
|
|
|
|
free(polygon->edges);
|
2002-09-10 08:01:00 +00:00
|
|
|
|
polygon->edges = NULL;
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->edges_size = 0;
|
|
|
|
|
|
polygon->num_edges = 0;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
2002-09-03 08:42:25 +00:00
|
|
|
|
|
|
|
|
|
|
polygon->first_pt_defined = 0;
|
|
|
|
|
|
polygon->last_pt_defined = 0;
|
|
|
|
|
|
|
|
|
|
|
|
polygon->closed = 0;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 05:22:59 +00:00
|
|
|
|
static XrError
|
2002-08-15 17:29:26 +00:00
|
|
|
|
_XrPolygonGrowBy(XrPolygon *polygon, int additional)
|
2002-08-14 00:39:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
XrEdge *new_edges;
|
2002-08-15 17:29:26 +00:00
|
|
|
|
int old_size = polygon->edges_size;
|
|
|
|
|
|
int new_size = polygon->num_edges + additional;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
if (new_size <= polygon->edges_size) {
|
2002-08-15 05:22:59 +00:00
|
|
|
|
return XrErrorSuccess;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->edges_size = new_size;
|
|
|
|
|
|
new_edges = realloc(polygon->edges, polygon->edges_size * sizeof(XrEdge));
|
2002-08-14 00:39:43 +00:00
|
|
|
|
|
2002-08-15 05:22:59 +00:00
|
|
|
|
if (new_edges == NULL) {
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->edges_size = old_size;
|
2002-08-15 05:22:59 +00:00
|
|
|
|
return XrErrorNoMemory;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
2002-08-15 05:22:59 +00:00
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->edges = new_edges;
|
2002-08-15 05:22:59 +00:00
|
|
|
|
|
|
|
|
|
|
return XrErrorSuccess;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-09-03 08:42:25 +00:00
|
|
|
|
static void
|
|
|
|
|
|
_XrPolygonSetLastPoint(XrPolygon *polygon, XPointFixed *pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
polygon->last_pt = *pt;
|
|
|
|
|
|
polygon->last_pt_defined = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 05:22:59 +00:00
|
|
|
|
XrError
|
2002-08-15 17:29:26 +00:00
|
|
|
|
XrPolygonAddEdge(XrPolygon *polygon, XPointFixed *p1, XPointFixed *p2)
|
2002-08-14 00:39:43 +00:00
|
|
|
|
{
|
2002-08-15 05:22:59 +00:00
|
|
|
|
XrError err;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
XrEdge *edge;
|
|
|
|
|
|
|
2002-09-03 08:42:25 +00:00
|
|
|
|
if (! polygon->first_pt_defined) {
|
|
|
|
|
|
polygon->first_pt = *p1;
|
|
|
|
|
|
polygon->first_pt_defined = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-14 00:39:43 +00:00
|
|
|
|
/* drop horizontal edges */
|
|
|
|
|
|
if (p1->y == p2->y) {
|
2002-09-05 13:12:23 +00:00
|
|
|
|
goto DONE;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
if (polygon->num_edges >= polygon->edges_size) {
|
|
|
|
|
|
err = _XrPolygonGrowBy(polygon, XR_POLYGON_GROWTH_INC);
|
2002-08-15 05:22:59 +00:00
|
|
|
|
if (err) {
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
edge = &polygon->edges[polygon->num_edges];
|
2002-08-14 00:39:43 +00:00
|
|
|
|
if (p1->y < p2->y) {
|
|
|
|
|
|
edge->edge.p1 = *p1;
|
|
|
|
|
|
edge->edge.p2 = *p2;
|
|
|
|
|
|
edge->clockWise = True;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
edge->edge.p1 = *p2;
|
|
|
|
|
|
edge->edge.p2 = *p1;
|
|
|
|
|
|
edge->clockWise = False;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 17:29:26 +00:00
|
|
|
|
polygon->num_edges++;
|
2002-08-15 05:22:59 +00:00
|
|
|
|
|
2002-09-05 13:12:23 +00:00
|
|
|
|
DONE:
|
2002-09-03 08:42:25 +00:00
|
|
|
|
_XrPolygonSetLastPoint(polygon, p2);
|
|
|
|
|
|
|
|
|
|
|
|
return XrErrorSuccess;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XrError
|
|
|
|
|
|
XrPolygonAddPoint(XrPolygon *polygon, XPointFixed *pt)
|
|
|
|
|
|
{
|
|
|
|
|
|
XrError err = XrErrorSuccess;
|
|
|
|
|
|
|
|
|
|
|
|
if (polygon->last_pt_defined) {
|
|
|
|
|
|
err = XrPolygonAddEdge(polygon, &polygon->last_pt, pt);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
_XrPolygonSetLastPoint(polygon, pt);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XrError
|
|
|
|
|
|
XrPolygonClose(XrPolygon *polygon)
|
|
|
|
|
|
{
|
|
|
|
|
|
XrError err;
|
|
|
|
|
|
|
|
|
|
|
|
if (polygon->closed == 0 && polygon->last_pt_defined) {
|
|
|
|
|
|
err = XrPolygonAddEdge(polygon, &polygon->last_pt, &polygon->first_pt);
|
|
|
|
|
|
if (err)
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
|
|
polygon->closed = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-15 05:22:59 +00:00
|
|
|
|
return XrErrorSuccess;
|
2002-08-14 00:39:43 +00:00
|
|
|
|
}
|