/*
* tkCanvPoly.c --
*
* This file implements polygon items for canvas widgets.
*
* Copyright (c) 1991-1994 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkCanvPoly.c 1.34 96/02/15 18:52:32
*/
#include "tkInt.h"
/*
* The structure below defines the record for each polygon item.
*/
typedef struct PolygonItem {
* types. MUST BE FIRST IN STRUCTURE. */
* Polygon is always closed. */
* allocated at *coordPtr. */
* x- and y-coords of all points in polygon.
* X-coords are even-valued indices, y-coords
* are corresponding odd-valued indices. */
* with Bezier splines). */
} PolygonItem;
/*
* Information used for parsing configuration specs:
*/
};
(char *) NULL, 0, 0}
};
/*
* Prototypes for procedures defined in this file:
*/
PolygonItem *polyPtr));
/*
* The structures below defines the polygon item type by means
* of procedures that can be invoked by generic item code.
*/
"polygon", /* name */
sizeof(PolygonItem), /* itemSize */
CreatePolygon, /* createProc */
configSpecs, /* configSpecs */
ConfigurePolygon, /* configureProc */
PolygonCoords, /* coordProc */
DeletePolygon, /* deleteProc */
DisplayPolygon, /* displayProc */
0, /* alwaysRedraw */
PolygonToPoint, /* pointProc */
PolygonToArea, /* areaProc */
PolygonToPostscript, /* postscriptProc */
ScalePolygon, /* scaleProc */
TranslatePolygon, /* translateProc */
};
/*
* The definition below determines how large are static arrays
* used to hold spline points (splines larger than this have to
* have their arrays malloc-ed).
*/
/*
*--------------------------------------------------------------
*
* CreatePolygon --
*
* This procedure is invoked to create a new polygon item in
* a canvas.
*
* Results:
* A standard Tcl return value. If an error occurred in
* creating the item, then an error message is left in
* interp->result; in this case itemPtr is
* left uninitialized, so it can be safely freed by the
* caller.
*
* Side effects:
* A new polygon item is created.
*
*--------------------------------------------------------------
*/
static int
* has been initialized by caller. */
int argc; /* Number of arguments in argv. */
char **argv; /* Arguments describing polygon. */
{
int i;
if (argc < 6) {
" x1 y1 x2 y2 x3 y3 ?x4 y4 ...? ?options?\"", (char *) NULL);
return TCL_ERROR;
}
/*
* Carry out initialization that is needed in order to clean
* up after errors during the the remainder of this procedure.
*/
polyPtr->pointsAllocated = 0;
/*
* Count the number of points and then parse them into a point
* array. Leading arguments are assumed to be points if they
* start with a digit or a minus sign followed by a digit.
*/
break;
}
}
goto error;
}
== TCL_OK) {
return TCL_OK;
}
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* PolygonCoords --
*
* This procedure is invoked to process the "coords" widget
* command on polygons. See the user documentation for details
* on what it does.
*
* Results:
* Returns TCL_OK or TCL_ERROR, and sets interp->result.
*
* Side effects:
* The coordinates for the given item may be changed.
*
*--------------------------------------------------------------
*/
static int
* read or modified. */
int argc; /* Number of coordinates supplied in
* argv. */
char **argv; /* Array of coordinates: x1, y1,
* x2, y2, ... */
{
int i, numPoints;
if (argc == 0) {
}
} else if (argc < 6) {
"too few coordinates for polygon: must have at least 6",
(char *) NULL);
return TCL_ERROR;
} else if (argc & 1) {
"odd number of coordinates specified for polygon",
(char *) NULL);
return TCL_ERROR;
} else {
}
/*
* One extra point gets allocated here, just in case we have
* to add another point to close the polygon.
*/
(sizeof(double) * (argc+2)));
}
for (i = argc-1; i >= 0; i--) {
return TCL_ERROR;
}
}
/*
* Close the polygon if it isn't already closed.
*/
}
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ConfigurePolygon --
*
* This procedure is invoked to configure various aspects
* of a polygon item such as its background color.
*
* Results:
* A standard Tcl result code. If an error occurs, then
* an error message is left in interp->result.
*
* Side effects:
* Configuration information, such as colors and stipple
* patterns, may be set for itemPtr.
*
*--------------------------------------------------------------
*/
static int
int argc; /* Number of elements in argv. */
char **argv; /* Arguments describing things to configure. */
int flags; /* Flags to pass to Tk_ConfigureWidget. */
{
unsigned long mask;
return TCL_ERROR;
}
/*
* A few of the options require additional processing, such as
* graphics contexts.
*/
}
} else {
}
}
} else {
mask = GCForeground;
}
}
}
/*
* Keep spline parameters within reasonable limits.
*/
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* DeletePolygon --
*
* This procedure is called to clean up the data structure
* associated with a polygon item.
*
* Results:
* None.
*
* Side effects:
* Resources associated with itemPtr are released.
*
*--------------------------------------------------------------
*/
static void
* canvas. */
{
}
}
}
}
}
}
}
/*
*--------------------------------------------------------------
*
* ComputePolygonBbox --
*
* This procedure is invoked to compute the bounding box of
* all the pixels that may be drawn as part of a polygon.
*
* Results:
* None.
*
* Side effects:
* The fields x1, y1, x2, and y2 are updated in the header
* for itemPtr.
*
*--------------------------------------------------------------
*/
static void
* recomputed. */
{
double *coordPtr;
int i;
i++, coordPtr += 2) {
}
/*
* Expand bounding box in all directions to account for the outline,
* which can stick out beyond the polygon. Add one extra pixel of
* fudge, just in case X rounds differently than we do.
*/
}
/*
*--------------------------------------------------------------
*
* TkFillPolygon --
*
* This procedure is invoked to convert a polygon to screen
* coordinates and display it using a particular GC.
*
* Results:
* None.
*
* Side effects:
* ItemPtr is drawn in drawable using the transformation
* information in canvas.
*
*--------------------------------------------------------------
*/
void
* is to be used for drawing. */
double *coordPtr; /* Array of coordinates for polygon:
* x1, y1, x2, y2, .... */
int numPoints; /* Twice this many coordinates are
* present at *coordPtr. */
* polygon. */
* outline around the polygon after
* filling it. */
{
int i;
/*
* Build up an array of points in screen coordinates. Use a
* static array unless the polygon has an enormous number of points;
* in this case, dynamically allocate an array.
*/
if (numPoints <= MAX_STATIC_POINTS) {
} else {
}
&pPtr->y);
}
/*
* Display polygon, then free up polygon storage if it was dynamically
* allocated.
*/
}
}
if (pointPtr != staticPoints) {
}
}
/*
*--------------------------------------------------------------
*
* DisplayPolygon --
*
* This procedure is invoked to draw a polygon item in a given
* drawable.
*
* Results:
* None.
*
* Side effects:
* ItemPtr is drawn in drawable using the transformation
* information in canvas.
*
*--------------------------------------------------------------
*/
static void
* item. */
* must be redisplayed (not used). */
{
return;
}
/*
* If we're stippling then modify the stipple offset in the GC. Be
* sure to reset the offset when done, since the GC is supposed to be
* read-only.
*/
}
} else {
int numPoints;
/*
* This is a smoothed polygon. Display using a set of generated
* spline points rather than the original points.
*/
if (numPoints <= MAX_STATIC_POINTS) {
} else {
}
(double *) NULL);
}
}
if (pointPtr != staticPoints) {
}
}
}
}
/*
*--------------------------------------------------------------
*
* PolygonToPoint --
*
* Computes the distance from a given point to a given
* polygon, in canvas units.
*
* Results:
* The return value is 0 if the point whose x and y coordinates
* are pointPtr[0] and pointPtr[1] is inside the polygon. If the
* point isn't inside the polygon then the return value is the
* distance from the point to the polygon.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static double
double *pointPtr; /* Pointer to x and y coordinates. */
{
int numPoints;
pointPtr);
} else {
/*
* Smoothed polygon. Generate a new set of points and use them
* for comparison.
*/
if (numPoints <= MAX_STATIC_POINTS) {
} else {
(2*numPoints*sizeof(double)));
}
coordPtr);
if (coordPtr != staticSpace) {
}
}
if (distance < 0) {
distance = 0;
}
}
return distance;
}
/*
*--------------------------------------------------------------
*
* PolygonToArea --
*
* This procedure is called to determine whether an item
* lies entirely inside, entirely outside, or overlapping
* a given rectangular area.
*
* Results:
* -1 is returned if the item is entirely outside the area
* given by rectPtr, 0 if it overlaps, and 1 if it is entirely
* inside the given area.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static int
double *rectPtr; /* Pointer to array of four coordinates
* (x1, y1, x2, y2) describing rectangular
* area. */
{
/*
* Handle smoothed polygons by generating an expanded set of points
* against which to do the check.
*/
if (numPoints <= MAX_STATIC_POINTS) {
} else {
(2*numPoints*sizeof(double)));
}
coordPtr);
} else {
}
/*
* The outline of the polygon doesn't stick out, so we can
* do a simple check.
*/
} else {
/*
* The polygon has a wide outline, so the check is more complicated.
* First, check the line segments to see if they overlap the area.
*/
if (result >= 0) {
goto done;
}
/*
* There is no overlap between the polygon's outline and the
* rectangle. This means either the rectangle is entirely outside
* the polygon or entirely inside. To tell the difference,
* see whether the polygon (with 0 outline width) overlaps the
* rectangle bloated by half the outline width.
*/
result = -1;
} else {
result = 0;
}
}
done:
}
return result;
}
/*
*--------------------------------------------------------------
*
* ScalePolygon --
*
* This procedure is invoked to rescale a polygon item.
*
* Results:
* None.
*
* Side effects:
* The polygon referred to by itemPtr is rescaled so that the
* following transformation is applied to all point
* coordinates:
* x' = originX + scaleX*(x-originX)
* y' = originY + scaleY*(y-originY)
*
*--------------------------------------------------------------
*/
static void
double scaleX; /* Amount to scale in X direction. */
double scaleY; /* Amount to scale in Y direction. */
{
double *coordPtr;
int i;
i++, coordPtr += 2) {
}
}
/*
*--------------------------------------------------------------
*
* TranslatePolygon --
*
* This procedure is called to move a polygon by a given
* amount.
*
* Results:
* None.
*
* Side effects:
* The position of the polygon is offset by (xDelta, yDelta),
* and the bounding box is updated in the generic part of the
* item structure.
*
*--------------------------------------------------------------
*/
static void
* moved. */
{
double *coordPtr;
int i;
i++, coordPtr += 2) {
}
}
/*
*--------------------------------------------------------------
*
* PolygonToPostscript --
*
* This procedure is called to generate Postscript for
* polygon items.
*
* Results:
* The return value is a standard Tcl result. If an error
* occurs in generating Postscript then an error message is
* left in interp->result, replacing whatever used
* to be there. If no error occurs, then Postscript for the
* item is appended to the result.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static int
* here. */
* wanted. */
int prepass; /* 1 means this is a prepass to
* collect font information; 0 means
* final Postscript is being created. */
{
/*
* Fill the area of the polygon.
*/
} else {
}
return TCL_ERROR;
}
!= TCL_OK) {
return TCL_ERROR;
}
}
} else {
}
}
/*
* Now draw the outline, if there is one.
*/
} else {
}
"1 setlinecap\n1 setlinejoin\n", (char *) NULL);
!= TCL_OK) {
return TCL_ERROR;
}
}
return TCL_OK;
}