4 * This file contains a collection of trigonometry utility
5 * routines that are used by Tk and in particular by the
6 * canvas code. It also has miscellaneous geometry functions
9 * Copyright (c) 1992-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
24 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
26 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
28 # define PI 3.14159265358979323846
32 *--------------------------------------------------------------
36 * Compute the distance from a point to a finite line segment.
39 * The return value is the distance from the line segment
40 * whose end-points are *end1Ptr and *end2Ptr to the point
46 *--------------------------------------------------------------
50 TkLineToPoint(end1Ptr, end2Ptr, pointPtr)
51 double end1Ptr[2]; /* Coordinates of first end-point of line. */
52 double end2Ptr[2]; /* Coordinates of second end-point of line. */
53 double pointPtr[2]; /* Points to coords for point. */
58 * Compute the point on the line that is closest to the
59 * point. This must be done separately for vertical edges,
60 * horizontal edges, and other edges.
63 if (end1Ptr[0] == end2Ptr[0]) {
70 if (end1Ptr[1] >= end2Ptr[1]) {
71 y = MIN(end1Ptr[1], pointPtr[1]);
72 y = MAX(y, end2Ptr[1]);
74 y = MIN(end2Ptr[1], pointPtr[1]);
75 y = MAX(y, end1Ptr[1]);
77 } else if (end1Ptr[1] == end2Ptr[1]) {
84 if (end1Ptr[0] >= end2Ptr[0]) {
85 x = MIN(end1Ptr[0], pointPtr[0]);
86 x = MAX(x, end2Ptr[0]);
88 x = MIN(end2Ptr[0], pointPtr[0]);
89 x = MAX(x, end1Ptr[0]);
92 double m1, b1, m2, b2;
95 * The edge is neither horizontal nor vertical. Convert the
96 * edge to a line equation of the form y = m1*x + b1. Then
97 * compute a line perpendicular to this edge but passing
98 * through the point, also in the form y = m2*x + b2.
101 m1 = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]);
102 b1 = end1Ptr[1] - m1*end1Ptr[0];
104 b2 = pointPtr[1] - m2*pointPtr[0];
105 x = (b2 - b1)/(m1 - m2);
107 if (end1Ptr[0] > end2Ptr[0]) {
108 if (x > end1Ptr[0]) {
111 } else if (x < end2Ptr[0]) {
116 if (x > end2Ptr[0]) {
119 } else if (x < end1Ptr[0]) {
127 * Compute the distance to the closest point.
130 return hypot(pointPtr[0] - x, pointPtr[1] - y);
134 *--------------------------------------------------------------
138 * Determine whether a line lies entirely inside, entirely
139 * outside, or overlapping a given rectangular area.
142 * -1 is returned if the line given by end1Ptr and end2Ptr
143 * is entirely outside the rectangle given by rectPtr. 0 is
144 * returned if the polygon overlaps the rectangle, and 1 is
145 * returned if the polygon is entirely inside the rectangle.
150 *--------------------------------------------------------------
154 TkLineToArea(end1Ptr, end2Ptr, rectPtr)
155 double end1Ptr[2]; /* X and y coordinates for one endpoint
157 double end2Ptr[2]; /* X and y coordinates for other endpoint
159 double rectPtr[4]; /* Points to coords for rectangle, in the
160 * order x1, y1, x2, y2. X1 must be no
161 * larger than x2, and y1 no larger than y2. */
163 int inside1, inside2;
166 * First check the two points individually to see whether they
167 * are inside the rectangle or not.
170 inside1 = (end1Ptr[0] >= rectPtr[0]) && (end1Ptr[0] <= rectPtr[2])
171 && (end1Ptr[1] >= rectPtr[1]) && (end1Ptr[1] <= rectPtr[3]);
172 inside2 = (end2Ptr[0] >= rectPtr[0]) && (end2Ptr[0] <= rectPtr[2])
173 && (end2Ptr[1] >= rectPtr[1]) && (end2Ptr[1] <= rectPtr[3]);
174 if (inside1 != inside2) {
177 if (inside1 & inside2) {
182 * Both points are outside the rectangle, but still need to check
183 * for intersections between the line and the rectangle. Horizontal
184 * and vertical lines are particularly easy, so handle them
188 if (end1Ptr[0] == end2Ptr[0]) {
193 if (((end1Ptr[1] >= rectPtr[1]) ^ (end2Ptr[1] >= rectPtr[1]))
194 && (end1Ptr[0] >= rectPtr[0])
195 && (end1Ptr[0] <= rectPtr[2])) {
198 } else if (end1Ptr[1] == end2Ptr[1]) {
203 if (((end1Ptr[0] >= rectPtr[0]) ^ (end2Ptr[0] >= rectPtr[0]))
204 && (end1Ptr[1] >= rectPtr[1])
205 && (end1Ptr[1] <= rectPtr[3])) {
209 double m, x, y, low, high;
212 * Diagonal line. Compute slope of line and use
213 * for intersection checks against each of the
214 * sides of the rectangle: left, right, bottom, top.
217 m = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]);
218 if (end1Ptr[0] < end2Ptr[0]) {
219 low = end1Ptr[0]; high = end2Ptr[0];
221 low = end2Ptr[0]; high = end1Ptr[0];
228 y = end1Ptr[1] + (rectPtr[0] - end1Ptr[0])*m;
229 if ((rectPtr[0] >= low) && (rectPtr[0] <= high)
230 && (y >= rectPtr[1]) && (y <= rectPtr[3])) {
238 y += (rectPtr[2] - rectPtr[0])*m;
239 if ((y >= rectPtr[1]) && (y <= rectPtr[3])
240 && (rectPtr[2] >= low) && (rectPtr[2] <= high)) {
248 if (end1Ptr[1] < end2Ptr[1]) {
249 low = end1Ptr[1]; high = end2Ptr[1];
251 low = end2Ptr[1]; high = end1Ptr[1];
253 x = end1Ptr[0] + (rectPtr[1] - end1Ptr[1])/m;
254 if ((x >= rectPtr[0]) && (x <= rectPtr[2])
255 && (rectPtr[1] >= low) && (rectPtr[1] <= high)) {
263 x += (rectPtr[3] - rectPtr[1])/m;
264 if ((x >= rectPtr[0]) && (x <= rectPtr[2])
265 && (rectPtr[3] >= low) && (rectPtr[3] <= high)) {
273 *--------------------------------------------------------------
275 * TkThickPolyLineToArea --
277 * This procedure is called to determine whether a connected
278 * series of line segments lies entirely inside, entirely
279 * outside, or overlapping a given rectangular area.
282 * -1 is returned if the lines are entirely outside the area,
283 * 0 if they overlap, and 1 if they are entirely inside the
289 *--------------------------------------------------------------
294 TkThickPolyLineToArea(coordPtr, numPoints, width, capStyle, joinStyle, rectPtr)
295 double *coordPtr; /* Points to an array of coordinates for
296 * the polyline: x0, y0, x1, y1, ... */
297 int numPoints; /* Total number of points at *coordPtr. */
298 double width; /* Width of each line segment. */
299 int capStyle; /* How are end-points of polyline drawn?
300 * CapRound, CapButt, or CapProjecting. */
301 int joinStyle; /* How are joints in polyline drawn?
302 * JoinMiter, JoinRound, or JoinBevel. */
303 double *rectPtr; /* Rectangular area to check against. */
305 double radius, poly[10];
307 int changedMiterToBevel; /* Non-zero means that a mitered corner
308 * had to be treated as beveled after all
309 * because the angle was < 11 degrees. */
310 int inside; /* Tentative guess about what to return,
311 * based on all points seen so far: one
312 * means everything seen so far was
313 * inside the area; -1 means everything
314 * was outside the area. 0 means overlap
320 if ((coordPtr[0] >= rectPtr[0]) && (coordPtr[0] <= rectPtr[2])
321 && (coordPtr[1] >= rectPtr[1]) && (coordPtr[1] <= rectPtr[3])) {
326 * Iterate through all of the edges of the line, computing a polygon
327 * for each edge and testing the area against that polygon. In
328 * addition, there are additional tests to deal with rounded joints
332 changedMiterToBevel = 0;
333 for (count = numPoints; count >= 2; count--, coordPtr += 2) {
336 * If rounding is done around the first point of the edge
337 * then test a circular region around the point with the
341 if (((capStyle == CapRound) && (count == numPoints))
342 || ((joinStyle == JoinRound) && (count != numPoints))) {
343 poly[0] = coordPtr[0] - radius;
344 poly[1] = coordPtr[1] - radius;
345 poly[2] = coordPtr[0] + radius;
346 poly[3] = coordPtr[1] + radius;
347 if (TkOvalToArea(poly, rectPtr) != inside) {
353 * Compute the polygonal shape corresponding to this edge,
354 * consisting of two points for the first point of the edge
355 * and two points for the last point of the edge.
358 if (count == numPoints) {
359 TkGetButtPoints(coordPtr+2, coordPtr, width,
360 capStyle == CapProjecting, poly, poly+2);
361 } else if ((joinStyle == JoinMiter) && !changedMiterToBevel) {
367 TkGetButtPoints(coordPtr+2, coordPtr, width, 0, poly, poly+2);
370 * If the last joint was beveled, then also check a
371 * polygon comprising the last two points of the previous
372 * polygon and the first two from this polygon; this checks
373 * the wedges that fill the beveled joint.
376 if ((joinStyle == JoinBevel) || changedMiterToBevel) {
379 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
382 changedMiterToBevel = 0;
386 TkGetButtPoints(coordPtr, coordPtr+2, width,
387 capStyle == CapProjecting, poly+4, poly+6);
388 } else if (joinStyle == JoinMiter) {
389 if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
390 (double) width, poly+4, poly+6) == 0) {
391 changedMiterToBevel = 1;
392 TkGetButtPoints(coordPtr, coordPtr+2, width, 0, poly+4,
396 TkGetButtPoints(coordPtr, coordPtr+2, width, 0, poly+4, poly+6);
400 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
406 * If caps are rounded, check the cap around the final point
410 if (capStyle == CapRound) {
411 poly[0] = coordPtr[0] - radius;
412 poly[1] = coordPtr[1] - radius;
413 poly[2] = coordPtr[0] + radius;
414 poly[3] = coordPtr[1] + radius;
415 if (TkOvalToArea(poly, rectPtr) != inside) {
424 *--------------------------------------------------------------
426 * TkPolygonToPoint --
428 * Compute the distance from a point to a polygon.
431 * The return value is 0.0 if the point referred to by
432 * pointPtr is within the polygon referred to by polyPtr
433 * and numPoints. Otherwise the return value is the
434 * distance of the point from the polygon.
439 *--------------------------------------------------------------
443 TkPolygonToPoint(polyPtr, numPoints, pointPtr)
444 double *polyPtr; /* Points to an array coordinates for
445 * closed polygon: x0, y0, x1, y1, ...
446 * The polygon may be self-intersecting. */
447 int numPoints; /* Total number of points at *polyPtr. */
448 double *pointPtr; /* Points to coords for point. */
450 double bestDist; /* Closest distance between point and
451 * any edge in polygon. */
452 int intersections; /* Number of edges in the polygon that
453 * intersect a ray extending vertically
454 * upwards from the point to infinity. */
456 register double *pPtr;
459 * Iterate through all of the edges in the polygon, updating
460 * bestDist and intersections.
462 * TRICKY POINT: when computing intersections, include left
463 * x-coordinate of line within its range, but not y-coordinate.
464 * Otherwise if the point lies exactly below a vertex we'll
465 * count it as two intersections.
471 for (count = numPoints, pPtr = polyPtr; count > 1; count--, pPtr += 2) {
475 * Compute the point on the current edge closest to the point
476 * and update the intersection count. This must be done
477 * separately for vertical edges, horizontal edges, and
481 if (pPtr[2] == pPtr[0]) {
488 if (pPtr[1] >= pPtr[3]) {
489 y = MIN(pPtr[1], pointPtr[1]);
492 y = MIN(pPtr[3], pointPtr[1]);
495 } else if (pPtr[3] == pPtr[1]) {
502 if (pPtr[0] >= pPtr[2]) {
503 x = MIN(pPtr[0], pointPtr[0]);
505 if ((pointPtr[1] < y) && (pointPtr[0] < pPtr[0])
506 && (pointPtr[0] >= pPtr[2])) {
510 x = MIN(pPtr[2], pointPtr[0]);
512 if ((pointPtr[1] < y) && (pointPtr[0] < pPtr[2])
513 && (pointPtr[0] >= pPtr[0])) {
518 double m1, b1, m2, b2;
519 int lower; /* Non-zero means point below line. */
522 * The edge is neither horizontal nor vertical. Convert the
523 * edge to a line equation of the form y = m1*x + b1. Then
524 * compute a line perpendicular to this edge but passing
525 * through the point, also in the form y = m2*x + b2.
528 m1 = (pPtr[3] - pPtr[1])/(pPtr[2] - pPtr[0]);
529 b1 = pPtr[1] - m1*pPtr[0];
531 b2 = pointPtr[1] - m2*pointPtr[0];
532 x = (b2 - b1)/(m1 - m2);
534 if (pPtr[0] > pPtr[2]) {
538 } else if (x < pPtr[2]) {
546 } else if (x < pPtr[0]) {
551 lower = (m1*pointPtr[0] + b1) > pointPtr[1];
552 if (lower && (pointPtr[0] >= MIN(pPtr[0], pPtr[2]))
553 && (pointPtr[0] < MAX(pPtr[0], pPtr[2]))) {
559 * Compute the distance to the closest point, and see if that
560 * is the best distance seen so far.
563 dist = hypot(pointPtr[0] - x, pointPtr[1] - y);
564 if (dist < bestDist) {
570 * We've processed all of the points. If the number of intersections
571 * is odd, the point is inside the polygon.
574 if (intersections & 0x1) {
581 *--------------------------------------------------------------
585 * Determine whether a polygon lies entirely inside, entirely
586 * outside, or overlapping a given rectangular area.
589 * -1 is returned if the polygon given by polyPtr and numPoints
590 * is entirely outside the rectangle given by rectPtr. 0 is
591 * returned if the polygon overlaps the rectangle, and 1 is
592 * returned if the polygon is entirely inside the rectangle.
597 *--------------------------------------------------------------
601 TkPolygonToArea(polyPtr, numPoints, rectPtr)
602 double *polyPtr; /* Points to an array coordinates for
603 * closed polygon: x0, y0, x1, y1, ...
604 * The polygon may be self-intersecting. */
605 int numPoints; /* Total number of points at *polyPtr. */
606 register double *rectPtr; /* Points to coords for rectangle, in the
607 * order x1, y1, x2, y2. X1 and y1 must
608 * be lower-left corner. */
610 int state; /* State of all edges seen so far (-1 means
611 * outside, 1 means inside, won't ever be
614 register double *pPtr;
617 * Iterate over all of the edges of the polygon and test them
618 * against the rectangle. Can quit as soon as the state becomes
622 state = TkLineToArea(polyPtr, polyPtr+2, rectPtr);
626 for (pPtr = polyPtr+2, count = numPoints-1; count >= 2;
627 pPtr += 2, count--) {
628 if (TkLineToArea(pPtr, pPtr+2, rectPtr) != state) {
634 * If all of the edges were inside the rectangle we're done.
635 * If all of the edges were outside, then the rectangle could
636 * still intersect the polygon (if it's entirely enclosed).
637 * Call TkPolygonToPoint to figure this out.
643 if (TkPolygonToPoint(polyPtr, numPoints, rectPtr) == 0.0) {
650 *--------------------------------------------------------------
654 * Computes the distance from a given point to a given
655 * oval, in canvas units.
658 * The return value is 0 if the point given by *pointPtr is
659 * inside the oval. If the point isn't inside the
660 * oval then the return value is approximately the distance
661 * from the point to the oval. If the oval is filled, then
662 * anywhere in the interior is considered "inside"; if
663 * the oval isn't filled, then "inside" means only the area
664 * occupied by the outline.
669 *--------------------------------------------------------------
674 TkOvalToPoint(ovalPtr, width, filled, pointPtr)
675 double ovalPtr[4]; /* Pointer to array of four coordinates
676 * (x1, y1, x2, y2) defining oval's bounding
678 double width; /* Width of outline for oval. */
679 int filled; /* Non-zero means oval should be treated as
680 * filled; zero means only consider outline. */
681 double pointPtr[2]; /* Coordinates of point. */
683 double xDelta, yDelta, scaledDistance, distToOutline, distToCenter;
687 * Compute the distance between the center of the oval and the
688 * point in question, using a coordinate system where the oval
689 * has been transformed to a circle with unit radius.
692 xDelta = (pointPtr[0] - (ovalPtr[0] + ovalPtr[2])/2.0);
693 yDelta = (pointPtr[1] - (ovalPtr[1] + ovalPtr[3])/2.0);
694 distToCenter = hypot(xDelta, yDelta);
695 scaledDistance = hypot(xDelta / ((ovalPtr[2] + width - ovalPtr[0])/2.0),
696 yDelta / ((ovalPtr[3] + width - ovalPtr[1])/2.0));
700 * If the scaled distance is greater than 1 then it means no
701 * hit. Compute the distance from the point to the edge of
702 * the circle, then scale this distance back to the original
705 * Note: this distance isn't completely accurate. It's only
706 * an approximation, and it can overestimate the correct
707 * distance when the oval is eccentric.
710 if (scaledDistance > 1.0) {
711 return (distToCenter/scaledDistance) * (scaledDistance - 1.0);
715 * Scaled distance less than 1 means the point is inside the
716 * outer edge of the oval. If this is a filled oval, then we
717 * have a hit. Otherwise, do the same computation as above
718 * (scale back to original coordinate system), but also check
719 * to see if the point is within the width of the outline.
725 if (scaledDistance > 1E-10) {
726 distToOutline = (distToCenter/scaledDistance) * (1.0 - scaledDistance)
730 * Avoid dividing by a very small number (it could cause an
731 * arithmetic overflow). This problem occurs if the point is
732 * very close to the center of the oval.
735 xDiam = ovalPtr[2] - ovalPtr[0];
736 yDiam = ovalPtr[3] - ovalPtr[1];
738 distToOutline = (xDiam - width)/2;
740 distToOutline = (yDiam - width)/2;
744 if (distToOutline < 0.0) {
747 return distToOutline;
751 *--------------------------------------------------------------
755 * Determine whether an oval lies entirely inside, entirely
756 * outside, or overlapping a given rectangular area.
759 * -1 is returned if the oval described by ovalPtr is entirely
760 * outside the rectangle given by rectPtr. 0 is returned if the
761 * oval overlaps the rectangle, and 1 is returned if the oval
762 * is entirely inside the rectangle.
767 *--------------------------------------------------------------
771 TkOvalToArea(ovalPtr, rectPtr)
772 register double *ovalPtr; /* Points to coordinates definining the
773 * bounding rectangle for the oval: x1, y1,
774 * x2, y2. X1 must be less than x2 and y1
776 register double *rectPtr; /* Points to coords for rectangle, in the
777 * order x1, y1, x2, y2. X1 and y1 must
778 * be lower-left corner. */
780 double centerX, centerY, radX, radY, deltaX, deltaY;
783 * First, see if oval is entirely inside rectangle or entirely
787 if ((rectPtr[0] <= ovalPtr[0]) && (rectPtr[2] >= ovalPtr[2])
788 && (rectPtr[1] <= ovalPtr[1]) && (rectPtr[3] >= ovalPtr[3])) {
791 if ((rectPtr[2] < ovalPtr[0]) || (rectPtr[0] > ovalPtr[2])
792 || (rectPtr[3] < ovalPtr[1]) || (rectPtr[1] > ovalPtr[3])) {
797 * Next, go through the rectangle side by side. For each side
798 * of the rectangle, find the point on the side that is closest
799 * to the oval's center, and see if that point is inside the
800 * oval. If at least one such point is inside the oval, then
801 * the rectangle intersects the oval.
804 centerX = (ovalPtr[0] + ovalPtr[2])/2;
805 centerY = (ovalPtr[1] + ovalPtr[3])/2;
806 radX = (ovalPtr[2] - ovalPtr[0])/2;
807 radY = (ovalPtr[3] - ovalPtr[1])/2;
809 deltaY = rectPtr[1] - centerY;
811 deltaY = centerY - rectPtr[3];
823 deltaX = (rectPtr[0] - centerX)/radX;
825 if ((deltaX + deltaY) <= 1.0) {
833 deltaX = (rectPtr[2] - centerX)/radX;
835 if ((deltaX + deltaY) <= 1.0) {
839 deltaX = rectPtr[0] - centerX;
841 deltaX = centerX - rectPtr[2];
853 deltaY = (rectPtr[1] - centerY)/radY;
855 if ((deltaX + deltaY) < 1.0) {
863 deltaY = (rectPtr[3] - centerY)/radY;
865 if ((deltaX + deltaY) < 1.0) {
873 *--------------------------------------------------------------
877 * Given a point and a generic canvas item header, expand
878 * the item's bounding box if needed to include the point.
886 *--------------------------------------------------------------
891 TkIncludePoint(itemPtr, pointPtr)
892 register Tk_Item *itemPtr; /* Item whose bounding box is
893 * being calculated. */
894 double *pointPtr; /* Address of two doubles giving
895 * x and y coordinates of point. */
899 tmp = (int) (pointPtr[0] + 0.5);
900 if (tmp < itemPtr->x1) {
903 if (tmp > itemPtr->x2) {
906 tmp = (int) (pointPtr[1] + 0.5);
907 if (tmp < itemPtr->y1) {
910 if (tmp > itemPtr->y2) {
916 *--------------------------------------------------------------
918 * TkBezierScreenPoints --
920 * Given four control points, create a larger set of XPoints
921 * for a Bezier spline based on the points.
924 * The array at *xPointPtr gets filled in with numSteps XPoints
925 * corresponding to the Bezier spline defined by the four
926 * control points. Note: no output point is generated for the
927 * first input point, but an output point *is* generated for
928 * the last input point.
933 *--------------------------------------------------------------
937 TkBezierScreenPoints(canvas, control, numSteps, xPointPtr)
938 Tk_Canvas canvas; /* Canvas in which curve is to be
940 double control[]; /* Array of coordinates for four
941 * control points: x0, y0, x1, y1,
943 int numSteps; /* Number of curve points to
945 register XPoint *xPointPtr; /* Where to put new points. */
948 double u, u2, u3, t, t2, t3;
950 for (i = 1; i <= numSteps; i++, xPointPtr++) {
951 t = ((double) i)/((double) numSteps);
957 Tk_CanvasDrawableCoords(canvas,
958 (control[0]*u3 + 3.0 * (control[2]*t*u2 + control[4]*t2*u)
960 (control[1]*u3 + 3.0 * (control[3]*t*u2 + control[5]*t2*u)
962 &xPointPtr->x, &xPointPtr->y);
967 *--------------------------------------------------------------
971 * Given four control points, create a larger set of points
972 * for a Bezier spline based on the points.
975 * The array at *coordPtr gets filled in with 2*numSteps
976 * coordinates, which correspond to the Bezier spline defined
977 * by the four control points. Note: no output point is
978 * generated for the first input point, but an output point
979 * *is* generated for the last input point.
984 *--------------------------------------------------------------
988 TkBezierPoints(control, numSteps, coordPtr)
989 double control[]; /* Array of coordinates for four
990 * control points: x0, y0, x1, y1,
992 int numSteps; /* Number of curve points to
994 register double *coordPtr; /* Where to put new points. */
997 double u, u2, u3, t, t2, t3;
999 for (i = 1; i <= numSteps; i++, coordPtr += 2) {
1000 t = ((double) i)/((double) numSteps);
1006 coordPtr[0] = control[0]*u3
1007 + 3.0 * (control[2]*t*u2 + control[4]*t2*u) + control[6]*t3;
1008 coordPtr[1] = control[1]*u3
1009 + 3.0 * (control[3]*t*u2 + control[5]*t2*u) + control[7]*t3;
1014 *--------------------------------------------------------------
1016 * TkMakeBezierCurve --
1018 * Given a set of points, create a new set of points that fit
1019 * parabolic splines to the line segments connecting the original
1020 * points. Produces output points in either of two forms.
1022 * Note: in spite of this procedure's name, it does *not* generate
1023 * Bezier curves. Since only three control points are used for
1024 * each curve segment, not four, the curves are actually just
1028 * Either or both of the xPoints or dblPoints arrays are filled
1029 * in. The return value is the number of points placed in the
1030 * arrays. Note: if the first and last points are the same, then
1031 * a closed curve is generated.
1036 *--------------------------------------------------------------
1040 TkMakeBezierCurve(canvas, pointPtr, numPoints, numSteps, xPoints, dblPoints)
1041 Tk_Canvas canvas; /* Canvas in which curve is to be
1043 double *pointPtr; /* Array of input coordinates: x0,
1044 * y0, x1, y1, etc.. */
1045 int numPoints; /* Number of points at pointPtr. */
1046 int numSteps; /* Number of steps to use for each
1047 * spline segments (determines
1048 * smoothness of curve). */
1049 XPoint xPoints[]; /* Array of XPoints to fill in (e.g.
1050 * for display. NULL means don't
1051 * fill in any XPoints. */
1052 double dblPoints[]; /* Array of points to fill in as
1053 * doubles, in the form x0, y0,
1054 * x1, y1, .... NULL means don't
1055 * fill in anything in this form.
1056 * Caller must make sure that this
1057 * array has enough space. */
1059 int closed, outputPoints, i;
1060 int numCoords = numPoints*2;
1064 * If the curve is a closed one then generate a special spline
1065 * that spans the last points and the first ones. Otherwise
1066 * just put the first point into the output.
1070 /* Of pointPtr == NULL, this function returns an upper limit.
1071 * of the array size to store the coordinates. This can be
1072 * used to allocate storage, before the actual coordinates
1073 * are calculated. */
1074 return 1 + numPoints * numSteps;
1078 if ((pointPtr[0] == pointPtr[numCoords-2])
1079 && (pointPtr[1] == pointPtr[numCoords-1])) {
1081 control[0] = 0.5*pointPtr[numCoords-4] + 0.5*pointPtr[0];
1082 control[1] = 0.5*pointPtr[numCoords-3] + 0.5*pointPtr[1];
1083 control[2] = 0.167*pointPtr[numCoords-4] + 0.833*pointPtr[0];
1084 control[3] = 0.167*pointPtr[numCoords-3] + 0.833*pointPtr[1];
1085 control[4] = 0.833*pointPtr[0] + 0.167*pointPtr[2];
1086 control[5] = 0.833*pointPtr[1] + 0.167*pointPtr[3];
1087 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1088 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1089 if (xPoints != NULL) {
1090 Tk_CanvasDrawableCoords(canvas, control[0], control[1],
1091 &xPoints->x, &xPoints->y);
1092 TkBezierScreenPoints(canvas, control, numSteps, xPoints+1);
1093 xPoints += numSteps+1;
1095 if (dblPoints != NULL) {
1096 dblPoints[0] = control[0];
1097 dblPoints[1] = control[1];
1098 TkBezierPoints(control, numSteps, dblPoints+2);
1099 dblPoints += 2*(numSteps+1);
1101 outputPoints += numSteps+1;
1104 if (xPoints != NULL) {
1105 Tk_CanvasDrawableCoords(canvas, pointPtr[0], pointPtr[1],
1106 &xPoints->x, &xPoints->y);
1109 if (dblPoints != NULL) {
1110 dblPoints[0] = pointPtr[0];
1111 dblPoints[1] = pointPtr[1];
1117 for (i = 2; i < numPoints; i++, pointPtr += 2) {
1119 * Set up the first two control points. This is done
1120 * differently for the first spline of an open curve
1121 * than for other cases.
1124 if ((i == 2) && !closed) {
1125 control[0] = pointPtr[0];
1126 control[1] = pointPtr[1];
1127 control[2] = 0.333*pointPtr[0] + 0.667*pointPtr[2];
1128 control[3] = 0.333*pointPtr[1] + 0.667*pointPtr[3];
1130 control[0] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1131 control[1] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1132 control[2] = 0.167*pointPtr[0] + 0.833*pointPtr[2];
1133 control[3] = 0.167*pointPtr[1] + 0.833*pointPtr[3];
1137 * Set up the last two control points. This is done
1138 * differently for the last spline of an open curve
1139 * than for other cases.
1142 if ((i == (numPoints-1)) && !closed) {
1143 control[4] = .667*pointPtr[2] + .333*pointPtr[4];
1144 control[5] = .667*pointPtr[3] + .333*pointPtr[5];
1145 control[6] = pointPtr[4];
1146 control[7] = pointPtr[5];
1148 control[4] = .833*pointPtr[2] + .167*pointPtr[4];
1149 control[5] = .833*pointPtr[3] + .167*pointPtr[5];
1150 control[6] = 0.5*pointPtr[2] + 0.5*pointPtr[4];
1151 control[7] = 0.5*pointPtr[3] + 0.5*pointPtr[5];
1155 * If the first two points coincide, or if the last
1156 * two points coincide, then generate a single
1157 * straight-line segment by outputting the last control
1161 if (((pointPtr[0] == pointPtr[2]) && (pointPtr[1] == pointPtr[3]))
1162 || ((pointPtr[2] == pointPtr[4])
1163 && (pointPtr[3] == pointPtr[5]))) {
1164 if (xPoints != NULL) {
1165 Tk_CanvasDrawableCoords(canvas, control[6], control[7],
1166 &xPoints[0].x, &xPoints[0].y);
1169 if (dblPoints != NULL) {
1170 dblPoints[0] = control[6];
1171 dblPoints[1] = control[7];
1179 * Generate a Bezier spline using the control points.
1183 if (xPoints != NULL) {
1184 TkBezierScreenPoints(canvas, control, numSteps, xPoints);
1185 xPoints += numSteps;
1187 if (dblPoints != NULL) {
1188 TkBezierPoints(control, numSteps, dblPoints);
1189 dblPoints += 2*numSteps;
1191 outputPoints += numSteps;
1193 return outputPoints;
1197 *--------------------------------------------------------------
1199 * TkMakeBezierPostscript --
1201 * This procedure generates Postscript commands that create
1202 * a path corresponding to a given Bezier curve.
1205 * None. Postscript commands to generate the path are appended
1206 * to the interp's result.
1211 *--------------------------------------------------------------
1215 TkMakeBezierPostscript(interp, canvas, pointPtr, numPoints)
1216 Tcl_Interp *interp; /* Interpreter in whose result the
1217 * Postscript is to be stored. */
1218 Tk_Canvas canvas; /* Canvas widget for which the
1219 * Postscript is being generated. */
1220 double *pointPtr; /* Array of input coordinates: x0,
1221 * y0, x1, y1, etc.. */
1222 int numPoints; /* Number of points at pointPtr. */
1225 int numCoords = numPoints*2;
1230 * If the curve is a closed one then generate a special spline
1231 * that spans the last points and the first ones. Otherwise
1232 * just put the first point into the path.
1235 if ((pointPtr[0] == pointPtr[numCoords-2])
1236 && (pointPtr[1] == pointPtr[numCoords-1])) {
1238 control[0] = 0.5*pointPtr[numCoords-4] + 0.5*pointPtr[0];
1239 control[1] = 0.5*pointPtr[numCoords-3] + 0.5*pointPtr[1];
1240 control[2] = 0.167*pointPtr[numCoords-4] + 0.833*pointPtr[0];
1241 control[3] = 0.167*pointPtr[numCoords-3] + 0.833*pointPtr[1];
1242 control[4] = 0.833*pointPtr[0] + 0.167*pointPtr[2];
1243 control[5] = 0.833*pointPtr[1] + 0.167*pointPtr[3];
1244 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1245 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1246 sprintf(buffer, "%.15g %.15g moveto\n%.15g %.15g %.15g %.15g %.15g %.15g curveto\n",
1247 control[0], Tk_CanvasPsY(canvas, control[1]),
1248 control[2], Tk_CanvasPsY(canvas, control[3]),
1249 control[4], Tk_CanvasPsY(canvas, control[5]),
1250 control[6], Tk_CanvasPsY(canvas, control[7]));
1253 control[6] = pointPtr[0];
1254 control[7] = pointPtr[1];
1255 sprintf(buffer, "%.15g %.15g moveto\n",
1256 control[6], Tk_CanvasPsY(canvas, control[7]));
1258 Tcl_AppendResult(interp, buffer, (char *) NULL);
1261 * Cycle through all the remaining points in the curve, generating
1262 * a curve section for each vertex in the linear path.
1265 for (i = numPoints-2, pointPtr += 2; i > 0; i--, pointPtr += 2) {
1266 control[2] = 0.333*control[6] + 0.667*pointPtr[0];
1267 control[3] = 0.333*control[7] + 0.667*pointPtr[1];
1270 * Set up the last two control points. This is done
1271 * differently for the last spline of an open curve
1272 * than for other cases.
1275 if ((i == 1) && !closed) {
1276 control[6] = pointPtr[2];
1277 control[7] = pointPtr[3];
1279 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1280 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1282 control[4] = 0.333*control[6] + 0.667*pointPtr[0];
1283 control[5] = 0.333*control[7] + 0.667*pointPtr[1];
1285 sprintf(buffer, "%.15g %.15g %.15g %.15g %.15g %.15g curveto\n",
1286 control[2], Tk_CanvasPsY(canvas, control[3]),
1287 control[4], Tk_CanvasPsY(canvas, control[5]),
1288 control[6], Tk_CanvasPsY(canvas, control[7]));
1289 Tcl_AppendResult(interp, buffer, (char *) NULL);
1294 *--------------------------------------------------------------
1296 * TkGetMiterPoints --
1298 * Given three points forming an angle, compute the
1299 * coordinates of the inside and outside points of
1300 * the mitered corner formed by a line of a given
1301 * width at that angle.
1304 * If the angle formed by the three points is less than
1305 * 11 degrees then 0 is returned and m1 and m2 aren't
1306 * modified. Otherwise 1 is returned and the points at
1307 * m1 and m2 are filled in with the positions of the points
1308 * of the mitered corner.
1313 *--------------------------------------------------------------
1317 TkGetMiterPoints(p1, p2, p3, width, m1, m2)
1318 double p1[]; /* Points to x- and y-coordinates of point
1320 double p2[]; /* Points to x- and y-coordinates of vertex
1321 * for mitered joint. */
1322 double p3[]; /* Points to x- and y-coordinates of point
1324 double width; /* Width of line. */
1325 double m1[]; /* Points to place to put "left" vertex
1326 * point (see as you face from p1 to p2). */
1327 double m2[]; /* Points to place to put "right" vertex
1330 double theta1; /* Angle of segment p2-p1. */
1331 double theta2; /* Angle of segment p2-p3. */
1332 double theta; /* Angle between line segments (angle
1334 double theta3; /* Angle that bisects theta1 and
1335 * theta2 and points to m1. */
1336 double dist; /* Distance of miter points from p2. */
1337 double deltaX, deltaY; /* X and y offsets cooresponding to
1338 * dist (fudge factors for bounding
1340 double p1x, p1y, p2x, p2y, p3x, p3y;
1341 static double elevenDegrees = (11.0*2.0*PI)/360.0;
1344 * Round the coordinates to integers to mimic what happens when the
1345 * line segments are displayed; without this code, the bounding box
1346 * of a mitered line can be miscomputed greatly.
1349 p1x = floor(p1[0]+0.5);
1350 p1y = floor(p1[1]+0.5);
1351 p2x = floor(p2[0]+0.5);
1352 p2y = floor(p2[1]+0.5);
1353 p3x = floor(p3[0]+0.5);
1354 p3y = floor(p3[1]+0.5);
1357 theta1 = (p2x < p1x) ? 0 : PI;
1358 } else if (p2x == p1x) {
1359 theta1 = (p2y < p1y) ? PI/2.0 : -PI/2.0;
1361 theta1 = atan2(p1y - p2y, p1x - p2x);
1364 theta2 = (p3x > p2x) ? 0 : PI;
1365 } else if (p3x == p2x) {
1366 theta2 = (p3y > p2y) ? PI/2.0 : -PI/2.0;
1368 theta2 = atan2(p3y - p2y, p3x - p2x);
1370 theta = theta1 - theta2;
1373 } else if (theta < -PI) {
1376 if ((theta < elevenDegrees) && (theta > -elevenDegrees)) {
1379 dist = 0.5*width/sin(0.5*theta);
1385 * Compute theta3 (make sure that it points to the left when
1386 * looking from p1 to p2).
1389 theta3 = (theta1 + theta2)/2.0;
1390 if (sin(theta3 - (theta1 + PI)) < 0.0) {
1393 deltaX = dist*cos(theta3);
1394 m1[0] = p2x + deltaX;
1395 m2[0] = p2x - deltaX;
1396 deltaY = dist*sin(theta3);
1397 m1[1] = p2y + deltaY;
1398 m2[1] = p2y - deltaY;
1403 *--------------------------------------------------------------
1405 * TkGetButtPoints --
1407 * Given two points forming a line segment, compute the
1408 * coordinates of two endpoints of a rectangle formed by
1409 * bloating the line segment until it is width units wide.
1412 * There is no return value. M1 and m2 are filled in to
1413 * correspond to m1 and m2 in the diagram below:
1415 * ----------------* m1
1417 * p1 *---------------* p2
1419 * ----------------* m2
1421 * M1 and m2 will be W units apart, with p2 centered between
1422 * them and m1-m2 perpendicular to p1-p2. However, if
1423 * "project" is true then m1 and m2 will be as follows:
1425 * -------------------* m1
1427 * p1 *---------------* |
1429 * -------------------* m2
1431 * In this case p2 will be width/2 units from the segment m1-m2.
1436 *--------------------------------------------------------------
1440 TkGetButtPoints(p1, p2, width, project, m1, m2)
1441 double p1[]; /* Points to x- and y-coordinates of point
1443 double p2[]; /* Points to x- and y-coordinates of vertex
1444 * for mitered joint. */
1445 double width; /* Width of line. */
1446 int project; /* Non-zero means project p2 by an additional
1447 * width/2 before computing m1 and m2. */
1448 double m1[]; /* Points to place to put "left" result
1449 * point, as you face from p1 to p2. */
1450 double m2[]; /* Points to place to put "right" result
1453 double length; /* Length of p1-p2 segment. */
1454 double deltaX, deltaY; /* Increments in coords. */
1457 length = hypot(p2[0] - p1[0], p2[1] - p1[1]);
1458 if (length == 0.0) {
1459 m1[0] = m2[0] = p2[0];
1460 m1[1] = m2[1] = p2[1];
1462 deltaX = -width * (p2[1] - p1[1]) / length;
1463 deltaY = width * (p2[0] - p1[0]) / length;
1464 m1[0] = p2[0] + deltaX;
1465 m2[0] = p2[0] - deltaX;
1466 m1[1] = p2[1] + deltaY;
1467 m2[1] = p2[1] - deltaY;