Debian Codegraph @

pTk/mTk/generic/tkTrig.c

raw symbols SCIP
1 /*
2 * tkTrig.c --
3 *
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
7 * used by canvases.
8 *
9 * Copyright (c) 1992-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 *
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 *
15 * RCS: @(#) $Id: tkTrig.c,v 1.4 1999/12/14 06:52:33 hobbs Exp $
16 */
17
18 #include "tkInt.h"
19 #include "tkPort.h"
20 #include "tkCanvases.h"
21
22 #undef MIN
23 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
24 #undef MAX
25 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
26 #ifndef PI
27 # define PI 3.14159265358979323846
28 #endif /* PI */
29
30 /*
31 *--------------------------------------------------------------
32 *
33 * TkLineToPoint --
34 *
35 * Compute the distance from a point to a finite line segment.
36 *
37 * Results:
38 * The return value is the distance from the line segment
39 * whose end-points are *end1Ptr and *end2Ptr to the point
40 * given by *pointPtr.
41 *
42 * Side effects:
43 * None.
44 *
45 *--------------------------------------------------------------
46 */
47
48 double
49 TkLineToPoint(end1Ptr, end2Ptr, pointPtr)
50 double end1Ptr[2]; /* Coordinates of first end-point of line. */
51 double end2Ptr[2]; /* Coordinates of second end-point of line. */
52 double pointPtr[2]; /* Points to coords for point. */
53 {
54 double x, y;
55
56 /*
57 * Compute the point on the line that is closest to the
58 * point. This must be done separately for vertical edges,
59 * horizontal edges, and other edges.
60 */
61
62 if (end1Ptr[0] == end2Ptr[0]) {
63
64 /*
65 * Vertical edge.
66 */
67
68 x = end1Ptr[0];
69 if (end1Ptr[1] >= end2Ptr[1]) {
70 y = MIN(end1Ptr[1], pointPtr[1]);
71 y = MAX(y, end2Ptr[1]);
72 } else {
73 y = MIN(end2Ptr[1], pointPtr[1]);
74 y = MAX(y, end1Ptr[1]);
75 }
76 } else if (end1Ptr[1] == end2Ptr[1]) {
77
78 /*
79 * Horizontal edge.
80 */
81
82 y = end1Ptr[1];
83 if (end1Ptr[0] >= end2Ptr[0]) {
84 x = MIN(end1Ptr[0], pointPtr[0]);
85 x = MAX(x, end2Ptr[0]);
86 } else {
87 x = MIN(end2Ptr[0], pointPtr[0]);
88 x = MAX(x, end1Ptr[0]);
89 }
90 } else {
91 double m1, b1, m2, b2;
92
93 /*
94 * The edge is neither horizontal nor vertical. Convert the
95 * edge to a line equation of the form y = m1*x + b1. Then
96 * compute a line perpendicular to this edge but passing
97 * through the point, also in the form y = m2*x + b2.
98 */
99
100 m1 = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]);
101 b1 = end1Ptr[1] - m1*end1Ptr[0];
102 m2 = -1.0/m1;
103 b2 = pointPtr[1] - m2*pointPtr[0];
104 x = (b2 - b1)/(m1 - m2);
105 y = m1*x + b1;
106 if (end1Ptr[0] > end2Ptr[0]) {
107 if (x > end1Ptr[0]) {
108 x = end1Ptr[0];
109 y = end1Ptr[1];
110 } else if (x < end2Ptr[0]) {
111 x = end2Ptr[0];
112 y = end2Ptr[1];
113 }
114 } else {
115 if (x > end2Ptr[0]) {
116 x = end2Ptr[0];
117 y = end2Ptr[1];
118 } else if (x < end1Ptr[0]) {
119 x = end1Ptr[0];
120 y = end1Ptr[1];
121 }
122 }
123 }
124
125 /*
126 * Compute the distance to the closest point.
127 */
128
129 return hypot(pointPtr[0] - x, pointPtr[1] - y);
130 }
131
132 /*
133 *--------------------------------------------------------------
134 *
135 * TkLineToArea --
136 *
137 * Determine whether a line lies entirely inside, entirely
138 * outside, or overlapping a given rectangular area.
139 *
140 * Results:
141 * -1 is returned if the line given by end1Ptr and end2Ptr
142 * is entirely outside the rectangle given by rectPtr. 0 is
143 * returned if the polygon overlaps the rectangle, and 1 is
144 * returned if the polygon is entirely inside the rectangle.
145 *
146 * Side effects:
147 * None.
148 *
149 *--------------------------------------------------------------
150 */
151
152 int
153 TkLineToArea(end1Ptr, end2Ptr, rectPtr)
154 double end1Ptr[2]; /* X and y coordinates for one endpoint
155 * of line. */
156 double end2Ptr[2]; /* X and y coordinates for other endpoint
157 * of line. */
158 double rectPtr[4]; /* Points to coords for rectangle, in the
159 * order x1, y1, x2, y2. X1 must be no
160 * larger than x2, and y1 no larger than y2. */
161 {
162 int inside1, inside2;
163
164 /*
165 * First check the two points individually to see whether they
166 * are inside the rectangle or not.
167 */
168
169 inside1 = (end1Ptr[0] >= rectPtr[0]) && (end1Ptr[0] <= rectPtr[2])
170 && (end1Ptr[1] >= rectPtr[1]) && (end1Ptr[1] <= rectPtr[3]);
171 inside2 = (end2Ptr[0] >= rectPtr[0]) && (end2Ptr[0] <= rectPtr[2])
172 && (end2Ptr[1] >= rectPtr[1]) && (end2Ptr[1] <= rectPtr[3]);
173 if (inside1 != inside2) {
174 return 0;
175 }
176 if (inside1 & inside2) {
177 return 1;
178 }
179
180 /*
181 * Both points are outside the rectangle, but still need to check
182 * for intersections between the line and the rectangle. Horizontal
183 * and vertical lines are particularly easy, so handle them
184 * separately.
185 */
186
187 if (end1Ptr[0] == end2Ptr[0]) {
188 /*
189 * Vertical line.
190 */
191
192 if (((end1Ptr[1] >= rectPtr[1]) ^ (end2Ptr[1] >= rectPtr[1]))
193 && (end1Ptr[0] >= rectPtr[0])
194 && (end1Ptr[0] <= rectPtr[2])) {
195 return 0;
196 }
197 } else if (end1Ptr[1] == end2Ptr[1]) {
198 /*
199 * Horizontal line.
200 */
201
202 if (((end1Ptr[0] >= rectPtr[0]) ^ (end2Ptr[0] >= rectPtr[0]))
203 && (end1Ptr[1] >= rectPtr[1])
204 && (end1Ptr[1] <= rectPtr[3])) {
205 return 0;
206 }
207 } else {
208 double m, x, y, low, high;
209
210 /*
211 * Diagonal line. Compute slope of line and use
212 * for intersection checks against each of the
213 * sides of the rectangle: left, right, bottom, top.
214 */
215
216 m = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]);
217 if (end1Ptr[0] < end2Ptr[0]) {
218 low = end1Ptr[0]; high = end2Ptr[0];
219 } else {
220 low = end2Ptr[0]; high = end1Ptr[0];
221 }
222
223 /*
224 * Left edge.
225 */
226
227 y = end1Ptr[1] + (rectPtr[0] - end1Ptr[0])*m;
228 if ((rectPtr[0] >= low) && (rectPtr[0] <= high)
229 && (y >= rectPtr[1]) && (y <= rectPtr[3])) {
230 return 0;
231 }
232
233 /*
234 * Right edge.
235 */
236
237 y += (rectPtr[2] - rectPtr[0])*m;
238 if ((y >= rectPtr[1]) && (y <= rectPtr[3])
239 && (rectPtr[2] >= low) && (rectPtr[2] <= high)) {
240 return 0;
241 }
242
243 /*
244 * Bottom edge.
245 */
246
247 if (end1Ptr[1] < end2Ptr[1]) {
248 low = end1Ptr[1]; high = end2Ptr[1];
249 } else {
250 low = end2Ptr[1]; high = end1Ptr[1];
251 }
252 x = end1Ptr[0] + (rectPtr[1] - end1Ptr[1])/m;
253 if ((x >= rectPtr[0]) && (x <= rectPtr[2])
254 && (rectPtr[1] >= low) && (rectPtr[1] <= high)) {
255 return 0;
256 }
257
258 /*
259 * Top edge.
260 */
261
262 x += (rectPtr[3] - rectPtr[1])/m;
263 if ((x >= rectPtr[0]) && (x <= rectPtr[2])
264 && (rectPtr[3] >= low) && (rectPtr[3] <= high)) {
265 return 0;
266 }
267 }
268 return -1;
269 }
270
271 /*
272 *--------------------------------------------------------------
273 *
274 * TkThickPolyLineToArea --
275 *
276 * This procedure is called to determine whether a connected
277 * series of line segments lies entirely inside, entirely
278 * outside, or overlapping a given rectangular area.
279 *
280 * Results:
281 * -1 is returned if the lines are entirely outside the area,
282 * 0 if they overlap, and 1 if they are entirely inside the
283 * given area.
284 *
285 * Side effects:
286 * None.
287 *
288 *--------------------------------------------------------------
289 */
290
291 /* ARGSUSED */
292 int
293 TkThickPolyLineToArea(coordPtr, numPoints, width, capStyle, joinStyle, rectPtr)
294 double *coordPtr; /* Points to an array of coordinates for
295 * the polyline: x0, y0, x1, y1, ... */
296 int numPoints; /* Total number of points at *coordPtr. */
297 double width; /* Width of each line segment. */
298 int capStyle; /* How are end-points of polyline drawn?
299 * CapRound, CapButt, or CapProjecting. */
300 int joinStyle; /* How are joints in polyline drawn?
301 * JoinMiter, JoinRound, or JoinBevel. */
302 double *rectPtr; /* Rectangular area to check against. */
303 {
304 double radius, poly[10];
305 int count;
306 int changedMiterToBevel; /* Non-zero means that a mitered corner
307 * had to be treated as beveled after all
308 * because the angle was < 11 degrees. */
309 int inside; /* Tentative guess about what to return,
310 * based on all points seen so far: one
311 * means everything seen so far was
312 * inside the area; -1 means everything
313 * was outside the area. 0 means overlap
314 * has been found. */
315
316 radius = width/2.0;
317 inside = -1;
318
319 if ((coordPtr[0] >= rectPtr[0]) && (coordPtr[0] <= rectPtr[2])
320 && (coordPtr[1] >= rectPtr[1]) && (coordPtr[1] <= rectPtr[3])) {
321 inside = 1;
322 }
323
324 /*
325 * Iterate through all of the edges of the line, computing a polygon
326 * for each edge and testing the area against that polygon. In
327 * addition, there are additional tests to deal with rounded joints
328 * and caps.
329 */
330
331 changedMiterToBevel = 0;
332 for (count = numPoints; count >= 2; count--, coordPtr += 2) {
333
334 /*
335 * If rounding is done around the first point of the edge
336 * then test a circular region around the point with the
337 * area.
338 */
339
340 if (((capStyle == CapRound) && (count == numPoints))
341 || ((joinStyle == JoinRound) && (count != numPoints))) {
342 poly[0] = coordPtr[0] - radius;
343 poly[1] = coordPtr[1] - radius;
344 poly[2] = coordPtr[0] + radius;
345 poly[3] = coordPtr[1] + radius;
346 if (TkOvalToArea(poly, rectPtr) != inside) {
347 return 0;
348 }
349 }
350
351 /*
352 * Compute the polygonal shape corresponding to this edge,
353 * consisting of two points for the first point of the edge
354 * and two points for the last point of the edge.
355 */
356
357 if (count == numPoints) {
358 TkGetButtPoints(coordPtr+2, coordPtr, width,
359 capStyle == CapProjecting, poly, poly+2);
360 } else if ((joinStyle == JoinMiter) && !changedMiterToBevel) {
361 poly[0] = poly[6];
362 poly[1] = poly[7];
363 poly[2] = poly[4];
364 poly[3] = poly[5];
365 } else {
366 TkGetButtPoints(coordPtr+2, coordPtr, width, 0, poly, poly+2);
367
368 /*
369 * If the last joint was beveled, then also check a
370 * polygon comprising the last two points of the previous
371 * polygon and the first two from this polygon; this checks
372 * the wedges that fill the beveled joint.
373 */
374
375 if ((joinStyle == JoinBevel) || changedMiterToBevel) {
376 poly[8] = poly[0];
377 poly[9] = poly[1];
378 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
379 return 0;
380 }
381 changedMiterToBevel = 0;
382 }
383 }
384 if (count == 2) {
385 TkGetButtPoints(coordPtr, coordPtr+2, width,
386 capStyle == CapProjecting, poly+4, poly+6);
387 } else if (joinStyle == JoinMiter) {
388 if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
389 (double) width, poly+4, poly+6) == 0) {
390 changedMiterToBevel = 1;
391 TkGetButtPoints(coordPtr, coordPtr+2, width, 0, poly+4,
392 poly+6);
393 }
394 } else {
395 TkGetButtPoints(coordPtr, coordPtr+2, width, 0, poly+4, poly+6);
396 }
397 poly[8] = poly[0];
398 poly[9] = poly[1];
399 if (TkPolygonToArea(poly, 5, rectPtr) != inside) {
400 return 0;
401 }
402 }
403
404 /*
405 * If caps are rounded, check the cap around the final point
406 * of the line.
407 */
408
409 if (capStyle == CapRound) {
410 poly[0] = coordPtr[0] - radius;
411 poly[1] = coordPtr[1] - radius;
412 poly[2] = coordPtr[0] + radius;
413 poly[3] = coordPtr[1] + radius;
414 if (TkOvalToArea(poly, rectPtr) != inside) {
415 return 0;
416 }
417 }
418
419 return inside;
420 }
421
422 /*
423 *--------------------------------------------------------------
424 *
425 * TkPolygonToPoint --
426 *
427 * Compute the distance from a point to a polygon.
428 *
429 * Results:
430 * The return value is 0.0 if the point referred to by
431 * pointPtr is within the polygon referred to by polyPtr
432 * and numPoints. Otherwise the return value is the
433 * distance of the point from the polygon.
434 *
435 * Side effects:
436 * None.
437 *
438 *--------------------------------------------------------------
439 */
440
441 double
442 TkPolygonToPoint(polyPtr, numPoints, pointPtr)
443 double *polyPtr; /* Points to an array coordinates for
444 * closed polygon: x0, y0, x1, y1, ...
445 * The polygon may be self-intersecting. */
446 int numPoints; /* Total number of points at *polyPtr. */
447 double *pointPtr; /* Points to coords for point. */
448 {
449 double bestDist; /* Closest distance between point and
450 * any edge in polygon. */
451 int intersections; /* Number of edges in the polygon that
452 * intersect a ray extending vertically
453 * upwards from the point to infinity. */
454 int count;
455 register double *pPtr;
456
457 /*
458 * Iterate through all of the edges in the polygon, updating
459 * bestDist and intersections.
460 *
461 * TRICKY POINT: when computing intersections, include left
462 * x-coordinate of line within its range, but not y-coordinate.
463 * Otherwise if the point lies exactly below a vertex we'll
464 * count it as two intersections.
465 */
466
467 bestDist = 1.0e36;
468 intersections = 0;
469
470 for (count = numPoints, pPtr = polyPtr; count > 1; count--, pPtr += 2) {
471 double x, y, dist;
472
473 /*
474 * Compute the point on the current edge closest to the point
475 * and update the intersection count. This must be done
476 * separately for vertical edges, horizontal edges, and
477 * other edges.
478 */
479
480 if (pPtr[2] == pPtr[0]) {
481
482 /*
483 * Vertical edge.
484 */
485
486 x = pPtr[0];
487 if (pPtr[1] >= pPtr[3]) {
488 y = MIN(pPtr[1], pointPtr[1]);
489 y = MAX(y, pPtr[3]);
490 } else {
491 y = MIN(pPtr[3], pointPtr[1]);
492 y = MAX(y, pPtr[1]);
493 }
494 } else if (pPtr[3] == pPtr[1]) {
495
496 /*
497 * Horizontal edge.
498 */
499
500 y = pPtr[1];
501 if (pPtr[0] >= pPtr[2]) {
502 x = MIN(pPtr[0], pointPtr[0]);
503 x = MAX(x, pPtr[2]);
504 if ((pointPtr[1] < y) && (pointPtr[0] < pPtr[0])
505 && (pointPtr[0] >= pPtr[2])) {
506 intersections++;
507 }
508 } else {
509 x = MIN(pPtr[2], pointPtr[0]);
510 x = MAX(x, pPtr[0]);
511 if ((pointPtr[1] < y) && (pointPtr[0] < pPtr[2])
512 && (pointPtr[0] >= pPtr[0])) {
513 intersections++;
514 }
515 }
516 } else {
517 double m1, b1, m2, b2;
518 int lower; /* Non-zero means point below line. */
519
520 /*
521 * The edge is neither horizontal nor vertical. Convert the
522 * edge to a line equation of the form y = m1*x + b1. Then
523 * compute a line perpendicular to this edge but passing
524 * through the point, also in the form y = m2*x + b2.
525 */
526
527 m1 = (pPtr[3] - pPtr[1])/(pPtr[2] - pPtr[0]);
528 b1 = pPtr[1] - m1*pPtr[0];
529 m2 = -1.0/m1;
530 b2 = pointPtr[1] - m2*pointPtr[0];
531 x = (b2 - b1)/(m1 - m2);
532 y = m1*x + b1;
533 if (pPtr[0] > pPtr[2]) {
534 if (x > pPtr[0]) {
535 x = pPtr[0];
536 y = pPtr[1];
537 } else if (x < pPtr[2]) {
538 x = pPtr[2];
539 y = pPtr[3];
540 }
541 } else {
542 if (x > pPtr[2]) {
543 x = pPtr[2];
544 y = pPtr[3];
545 } else if (x < pPtr[0]) {
546 x = pPtr[0];
547 y = pPtr[1];
548 }
549 }
550 lower = (m1*pointPtr[0] + b1) > pointPtr[1];
551 if (lower && (pointPtr[0] >= MIN(pPtr[0], pPtr[2]))
552 && (pointPtr[0] < MAX(pPtr[0], pPtr[2]))) {
553 intersections++;
554 }
555 }
556
557 /*
558 * Compute the distance to the closest point, and see if that
559 * is the best distance seen so far.
560 */
561
562 dist = hypot(pointPtr[0] - x, pointPtr[1] - y);
563 if (dist < bestDist) {
564 bestDist = dist;
565 }
566 }
567
568 /*
569 * We've processed all of the points. If the number of intersections
570 * is odd, the point is inside the polygon.
571 */
572
573 if (intersections & 0x1) {
574 return 0.0;
575 }
576 return bestDist;
577 }
578
579 /*
580 *--------------------------------------------------------------
581 *
582 * TkPolygonToArea --
583 *
584 * Determine whether a polygon lies entirely inside, entirely
585 * outside, or overlapping a given rectangular area.
586 *
587 * Results:
588 * -1 is returned if the polygon given by polyPtr and numPoints
589 * is entirely outside the rectangle given by rectPtr. 0 is
590 * returned if the polygon overlaps the rectangle, and 1 is
591 * returned if the polygon is entirely inside the rectangle.
592 *
593 * Side effects:
594 * None.
595 *
596 *--------------------------------------------------------------
597 */
598
599 int
600 TkPolygonToArea(polyPtr, numPoints, rectPtr)
601 double *polyPtr; /* Points to an array coordinates for
602 * closed polygon: x0, y0, x1, y1, ...
603 * The polygon may be self-intersecting. */
604 int numPoints; /* Total number of points at *polyPtr. */
605 register double *rectPtr; /* Points to coords for rectangle, in the
606 * order x1, y1, x2, y2. X1 and y1 must
607 * be lower-left corner. */
608 {
609 int state; /* State of all edges seen so far (-1 means
610 * outside, 1 means inside, won't ever be
611 * 0). */
612 int count;
613 register double *pPtr;
614
615 /*
616 * Iterate over all of the edges of the polygon and test them
617 * against the rectangle. Can quit as soon as the state becomes
618 * "intersecting".
619 */
620
621 state = TkLineToArea(polyPtr, polyPtr+2, rectPtr);
622 if (state == 0) {
623 return 0;
624 }
625 for (pPtr = polyPtr+2, count = numPoints-1; count >= 2;
626 pPtr += 2, count--) {
627 if (TkLineToArea(pPtr, pPtr+2, rectPtr) != state) {
628 return 0;
629 }
630 }
631
632 /*
633 * If all of the edges were inside the rectangle we're done.
634 * If all of the edges were outside, then the rectangle could
635 * still intersect the polygon (if it's entirely enclosed).
636 * Call TkPolygonToPoint to figure this out.
637 */
638
639 if (state == 1) {
640 return 1;
641 }
642 if (TkPolygonToPoint(polyPtr, numPoints, rectPtr) == 0.0) {
643 return 0;
644 }
645 return -1;
646 }
647
648 /*
649 *--------------------------------------------------------------
650 *
651 * TkOvalToPoint --
652 *
653 * Computes the distance from a given point to a given
654 * oval, in canvas units.
655 *
656 * Results:
657 * The return value is 0 if the point given by *pointPtr is
658 * inside the oval. If the point isn't inside the
659 * oval then the return value is approximately the distance
660 * from the point to the oval. If the oval is filled, then
661 * anywhere in the interior is considered "inside"; if
662 * the oval isn't filled, then "inside" means only the area
663 * occupied by the outline.
664 *
665 * Side effects:
666 * None.
667 *
668 *--------------------------------------------------------------
669 */
670
671 /* ARGSUSED */
672 double
673 TkOvalToPoint(ovalPtr, width, filled, pointPtr)
674 double ovalPtr[4]; /* Pointer to array of four coordinates
675 * (x1, y1, x2, y2) defining oval's bounding
676 * box. */
677 double width; /* Width of outline for oval. */
678 int filled; /* Non-zero means oval should be treated as
679 * filled; zero means only consider outline. */
680 double pointPtr[2]; /* Coordinates of point. */
681 {
682 double xDelta, yDelta, scaledDistance, distToOutline, distToCenter;
683 double xDiam, yDiam;
684
685 /*
686 * Compute the distance between the center of the oval and the
687 * point in question, using a coordinate system where the oval
688 * has been transformed to a circle with unit radius.
689 */
690
691 xDelta = (pointPtr[0] - (ovalPtr[0] + ovalPtr[2])/2.0);
692 yDelta = (pointPtr[1] - (ovalPtr[1] + ovalPtr[3])/2.0);
693 distToCenter = hypot(xDelta, yDelta);
694 scaledDistance = hypot(xDelta / ((ovalPtr[2] + width - ovalPtr[0])/2.0),
695 yDelta / ((ovalPtr[3] + width - ovalPtr[1])/2.0));
696
697
698 /*
699 * If the scaled distance is greater than 1 then it means no
700 * hit. Compute the distance from the point to the edge of
701 * the circle, then scale this distance back to the original
702 * coordinate system.
703 *
704 * Note: this distance isn't completely accurate. It's only
705 * an approximation, and it can overestimate the correct
706 * distance when the oval is eccentric.
707 */
708
709 if (scaledDistance > 1.0) {
710 return (distToCenter/scaledDistance) * (scaledDistance - 1.0);
711 }
712
713 /*
714 * Scaled distance less than 1 means the point is inside the
715 * outer edge of the oval. If this is a filled oval, then we
716 * have a hit. Otherwise, do the same computation as above
717 * (scale back to original coordinate system), but also check
718 * to see if the point is within the width of the outline.
719 */
720
721 if (filled) {
722 return 0.0;
723 }
724 if (scaledDistance > 1E-10) {
725 distToOutline = (distToCenter/scaledDistance) * (1.0 - scaledDistance)
726 - width;
727 } else {
728 /*
729 * Avoid dividing by a very small number (it could cause an
730 * arithmetic overflow). This problem occurs if the point is
731 * very close to the center of the oval.
732 */
733
734 xDiam = ovalPtr[2] - ovalPtr[0];
735 yDiam = ovalPtr[3] - ovalPtr[1];
736 if (xDiam < yDiam) {
737 distToOutline = (xDiam - width)/2;
738 } else {
739 distToOutline = (yDiam - width)/2;
740 }
741 }
742
743 if (distToOutline < 0.0) {
744 return 0.0;
745 }
746 return distToOutline;
747 }
748
749 /*
750 *--------------------------------------------------------------
751 *
752 * TkOvalToArea --
753 *
754 * Determine whether an oval lies entirely inside, entirely
755 * outside, or overlapping a given rectangular area.
756 *
757 * Results:
758 * -1 is returned if the oval described by ovalPtr is entirely
759 * outside the rectangle given by rectPtr. 0 is returned if the
760 * oval overlaps the rectangle, and 1 is returned if the oval
761 * is entirely inside the rectangle.
762 *
763 * Side effects:
764 * None.
765 *
766 *--------------------------------------------------------------
767 */
768
769 int
770 TkOvalToArea(ovalPtr, rectPtr)
771 register double *ovalPtr; /* Points to coordinates definining the
772 * bounding rectangle for the oval: x1, y1,
773 * x2, y2. X1 must be less than x2 and y1
774 * less than y2. */
775 register double *rectPtr; /* Points to coords for rectangle, in the
776 * order x1, y1, x2, y2. X1 and y1 must
777 * be lower-left corner. */
778 {
779 double centerX, centerY, radX, radY, deltaX, deltaY;
780
781 /*
782 * First, see if oval is entirely inside rectangle or entirely
783 * outside rectangle.
784 */
785
786 if ((rectPtr[0] <= ovalPtr[0]) && (rectPtr[2] >= ovalPtr[2])
787 && (rectPtr[1] <= ovalPtr[1]) && (rectPtr[3] >= ovalPtr[3])) {
788 return 1;
789 }
790 if ((rectPtr[2] < ovalPtr[0]) || (rectPtr[0] > ovalPtr[2])
791 || (rectPtr[3] < ovalPtr[1]) || (rectPtr[1] > ovalPtr[3])) {
792 return -1;
793 }
794
795 /*
796 * Next, go through the rectangle side by side. For each side
797 * of the rectangle, find the point on the side that is closest
798 * to the oval's center, and see if that point is inside the
799 * oval. If at least one such point is inside the oval, then
800 * the rectangle intersects the oval.
801 */
802
803 centerX = (ovalPtr[0] + ovalPtr[2])/2;
804 centerY = (ovalPtr[1] + ovalPtr[3])/2;
805 radX = (ovalPtr[2] - ovalPtr[0])/2;
806 radY = (ovalPtr[3] - ovalPtr[1])/2;
807
808 deltaY = rectPtr[1] - centerY;
809 if (deltaY < 0.0) {
810 deltaY = centerY - rectPtr[3];
811 if (deltaY < 0.0) {
812 deltaY = 0;
813 }
814 }
815 deltaY /= radY;
816 deltaY *= deltaY;
817
818 /*
819 * Left side:
820 */
821
822 deltaX = (rectPtr[0] - centerX)/radX;
823 deltaX *= deltaX;
824 if ((deltaX + deltaY) <= 1.0) {
825 return 0;
826 }
827
828 /*
829 * Right side:
830 */
831
832 deltaX = (rectPtr[2] - centerX)/radX;
833 deltaX *= deltaX;
834 if ((deltaX + deltaY) <= 1.0) {
835 return 0;
836 }
837
838 deltaX = rectPtr[0] - centerX;
839 if (deltaX < 0.0) {
840 deltaX = centerX - rectPtr[2];
841 if (deltaX < 0.0) {
842 deltaX = 0;
843 }
844 }
845 deltaX /= radX;
846 deltaX *= deltaX;
847
848 /*
849 * Bottom side:
850 */
851
852 deltaY = (rectPtr[1] - centerY)/radY;
853 deltaY *= deltaY;
854 if ((deltaX + deltaY) < 1.0) {
855 return 0;
856 }
857
858 /*
859 * Top side:
860 */
861
862 deltaY = (rectPtr[3] - centerY)/radY;
863 deltaY *= deltaY;
864 if ((deltaX + deltaY) < 1.0) {
865 return 0;
866 }
867
868 return -1;
869 }
870
871 /*
872 *--------------------------------------------------------------
873 *
874 * TkIncludePoint --
875 *
876 * Given a point and a generic canvas item header, expand
877 * the item's bounding box if needed to include the point.
878 *
879 * Results:
880 * None.
881 *
882 * Side effects:
883 * The boudn.
884 *
885 *--------------------------------------------------------------
886 */
887
888 /* ARGSUSED */
889 void
890 TkIncludePoint(itemPtr, pointPtr)
891 register Tk_Item *itemPtr; /* Item whose bounding box is
892 * being calculated. */
893 double *pointPtr; /* Address of two doubles giving
894 * x and y coordinates of point. */
895 {
896 int tmp;
897
898 tmp = (int) (pointPtr[0] + 0.5);
899 if (tmp < itemPtr->x1) {
900 itemPtr->x1 = tmp;
901 }
902 if (tmp > itemPtr->x2) {
903 itemPtr->x2 = tmp;
904 }
905 tmp = (int) (pointPtr[1] + 0.5);
906 if (tmp < itemPtr->y1) {
907 itemPtr->y1 = tmp;
908 }
909 if (tmp > itemPtr->y2) {
910 itemPtr->y2 = tmp;
911 }
912 }
913
914 /*
915 *--------------------------------------------------------------
916 *
917 * TkBezierScreenPoints --
918 *
919 * Given four control points, create a larger set of XPoints
920 * for a Bezier spline based on the points.
921 *
922 * Results:
923 * The array at *xPointPtr gets filled in with numSteps XPoints
924 * corresponding to the Bezier spline defined by the four
925 * control points. Note: no output point is generated for the
926 * first input point, but an output point *is* generated for
927 * the last input point.
928 *
929 * Side effects:
930 * None.
931 *
932 *--------------------------------------------------------------
933 */
934
935 void
936 TkBezierScreenPoints(canvas, control, numSteps, xPointPtr)
937 Tk_Canvas canvas; /* Canvas in which curve is to be
938 * drawn. */
939 double control[]; /* Array of coordinates for four
940 * control points: x0, y0, x1, y1,
941 * ... x3 y3. */
942 int numSteps; /* Number of curve points to
943 * generate. */
944 register XPoint *xPointPtr; /* Where to put new points. */
945 {
946 int i;
947 double u, u2, u3, t, t2, t3;
948
949 for (i = 1; i <= numSteps; i++, xPointPtr++) {
950 t = ((double) i)/((double) numSteps);
951 t2 = t*t;
952 t3 = t2*t;
953 u = 1.0 - t;
954 u2 = u*u;
955 u3 = u2*u;
956 Tk_CanvasDrawableCoords(canvas,
957 (control[0]*u3 + 3.0 * (control[2]*t*u2 + control[4]*t2*u)
958 + control[6]*t3),
959 (control[1]*u3 + 3.0 * (control[3]*t*u2 + control[5]*t2*u)
960 + control[7]*t3),
961 &xPointPtr->x, &xPointPtr->y);
962 }
963 }
964
965 /*
966 *--------------------------------------------------------------
967 *
968 * TkBezierPoints --
969 *
970 * Given four control points, create a larger set of points
971 * for a Bezier spline based on the points.
972 *
973 * Results:
974 * The array at *coordPtr gets filled in with 2*numSteps
975 * coordinates, which correspond to the Bezier spline defined
976 * by the four control points. Note: no output point is
977 * generated for the first input point, but an output point
978 * *is* generated for the last input point.
979 *
980 * Side effects:
981 * None.
982 *
983 *--------------------------------------------------------------
984 */
985
986 void
987 TkBezierPoints(control, numSteps, coordPtr)
988 double control[]; /* Array of coordinates for four
989 * control points: x0, y0, x1, y1,
990 * ... x3 y3. */
991 int numSteps; /* Number of curve points to
992 * generate. */
993 register double *coordPtr; /* Where to put new points. */
994 {
995 int i;
996 double u, u2, u3, t, t2, t3;
997
998 for (i = 1; i <= numSteps; i++, coordPtr += 2) {
999 t = ((double) i)/((double) numSteps);
1000 t2 = t*t;
1001 t3 = t2*t;
1002 u = 1.0 - t;
1003 u2 = u*u;
1004 u3 = u2*u;
1005 coordPtr[0] = control[0]*u3
1006 + 3.0 * (control[2]*t*u2 + control[4]*t2*u) + control[6]*t3;
1007 coordPtr[1] = control[1]*u3
1008 + 3.0 * (control[3]*t*u2 + control[5]*t2*u) + control[7]*t3;
1009 }
1010 }
1011
1012 /*
1013 *--------------------------------------------------------------
1014 *
1015 * TkMakeBezierCurve --
1016 *
1017 * Given a set of points, create a new set of points that fit
1018 * parabolic splines to the line segments connecting the original
1019 * points. Produces output points in either of two forms.
1020 *
1021 * Note: in spite of this procedure's name, it does *not* generate
1022 * Bezier curves. Since only three control points are used for
1023 * each curve segment, not four, the curves are actually just
1024 * parabolic.
1025 *
1026 * Results:
1027 * Either or both of the xPoints or dblPoints arrays are filled
1028 * in. The return value is the number of points placed in the
1029 * arrays. Note: if the first and last points are the same, then
1030 * a closed curve is generated.
1031 *
1032 * Side effects:
1033 * None.
1034 *
1035 *--------------------------------------------------------------
1036 */
1037
1038 int
1039 TkMakeBezierCurve(canvas, pointPtr, numPoints, numSteps, xPoints, dblPoints)
1040 Tk_Canvas canvas; /* Canvas in which curve is to be
1041 * drawn. */
1042 double *pointPtr; /* Array of input coordinates: x0,
1043 * y0, x1, y1, etc.. */
1044 int numPoints; /* Number of points at pointPtr. */
1045 int numSteps; /* Number of steps to use for each
1046 * spline segments (determines
1047 * smoothness of curve). */
1048 XPoint xPoints[]; /* Array of XPoints to fill in (e.g.
1049 * for display. NULL means don't
1050 * fill in any XPoints. */
1051 double dblPoints[]; /* Array of points to fill in as
1052 * doubles, in the form x0, y0,
1053 * x1, y1, .... NULL means don't
1054 * fill in anything in this form.
1055 * Caller must make sure that this
1056 * array has enough space. */
1057 {
1058 int closed, outputPoints, i;
1059 int numCoords = numPoints*2;
1060 double control[8];
1061
1062 /*
1063 * If the curve is a closed one then generate a special spline
1064 * that spans the last points and the first ones. Otherwise
1065 * just put the first point into the output.
1066 */
1067
1068 if (!pointPtr) {
1069 /* Of pointPtr == NULL, this function returns an upper limit.
1070 * of the array size to store the coordinates. This can be
1071 * used to allocate storage, before the actual coordinates
1072 * are calculated. */
1073 return 1 + numPoints * numSteps;
1074 }
1075
1076 outputPoints = 0;
1077 if ((pointPtr[0] == pointPtr[numCoords-2])
1078 && (pointPtr[1] == pointPtr[numCoords-1])) {
1079 closed = 1;
1080 control[0] = 0.5*pointPtr[numCoords-4] + 0.5*pointPtr[0];
1081 control[1] = 0.5*pointPtr[numCoords-3] + 0.5*pointPtr[1];
1082 control[2] = 0.167*pointPtr[numCoords-4] + 0.833*pointPtr[0];
1083 control[3] = 0.167*pointPtr[numCoords-3] + 0.833*pointPtr[1];
1084 control[4] = 0.833*pointPtr[0] + 0.167*pointPtr[2];
1085 control[5] = 0.833*pointPtr[1] + 0.167*pointPtr[3];
1086 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1087 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1088 if (xPoints != NULL) {
1089 Tk_CanvasDrawableCoords(canvas, control[0], control[1],
1090 &xPoints->x, &xPoints->y);
1091 TkBezierScreenPoints(canvas, control, numSteps, xPoints+1);
1092 xPoints += numSteps+1;
1093 }
1094 if (dblPoints != NULL) {
1095 dblPoints[0] = control[0];
1096 dblPoints[1] = control[1];
1097 TkBezierPoints(control, numSteps, dblPoints+2);
1098 dblPoints += 2*(numSteps+1);
1099 }
1100 outputPoints += numSteps+1;
1101 } else {
1102 closed = 0;
1103 if (xPoints != NULL) {
1104 Tk_CanvasDrawableCoords(canvas, pointPtr[0], pointPtr[1],
1105 &xPoints->x, &xPoints->y);
1106 xPoints += 1;
1107 }
1108 if (dblPoints != NULL) {
1109 dblPoints[0] = pointPtr[0];
1110 dblPoints[1] = pointPtr[1];
1111 dblPoints += 2;
1112 }
1113 outputPoints += 1;
1114 }
1115
1116 for (i = 2; i < numPoints; i++, pointPtr += 2) {
1117 /*
1118 * Set up the first two control points. This is done
1119 * differently for the first spline of an open curve
1120 * than for other cases.
1121 */
1122
1123 if ((i == 2) && !closed) {
1124 control[0] = pointPtr[0];
1125 control[1] = pointPtr[1];
1126 control[2] = 0.333*pointPtr[0] + 0.667*pointPtr[2];
1127 control[3] = 0.333*pointPtr[1] + 0.667*pointPtr[3];
1128 } else {
1129 control[0] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1130 control[1] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1131 control[2] = 0.167*pointPtr[0] + 0.833*pointPtr[2];
1132 control[3] = 0.167*pointPtr[1] + 0.833*pointPtr[3];
1133 }
1134
1135 /*
1136 * Set up the last two control points. This is done
1137 * differently for the last spline of an open curve
1138 * than for other cases.
1139 */
1140
1141 if ((i == (numPoints-1)) && !closed) {
1142 control[4] = .667*pointPtr[2] + .333*pointPtr[4];
1143 control[5] = .667*pointPtr[3] + .333*pointPtr[5];
1144 control[6] = pointPtr[4];
1145 control[7] = pointPtr[5];
1146 } else {
1147 control[4] = .833*pointPtr[2] + .167*pointPtr[4];
1148 control[5] = .833*pointPtr[3] + .167*pointPtr[5];
1149 control[6] = 0.5*pointPtr[2] + 0.5*pointPtr[4];
1150 control[7] = 0.5*pointPtr[3] + 0.5*pointPtr[5];
1151 }
1152
1153 /*
1154 * If the first two points coincide, or if the last
1155 * two points coincide, then generate a single
1156 * straight-line segment by outputting the last control
1157 * point.
1158 */
1159
1160 if (((pointPtr[0] == pointPtr[2]) && (pointPtr[1] == pointPtr[3]))
1161 || ((pointPtr[2] == pointPtr[4])
1162 && (pointPtr[3] == pointPtr[5]))) {
1163 if (xPoints != NULL) {
1164 Tk_CanvasDrawableCoords(canvas, control[6], control[7],
1165 &xPoints[0].x, &xPoints[0].y);
1166 xPoints++;
1167 }
1168 if (dblPoints != NULL) {
1169 dblPoints[0] = control[6];
1170 dblPoints[1] = control[7];
1171 dblPoints += 2;
1172 }
1173 outputPoints += 1;
1174 continue;
1175 }
1176
1177 /*
1178 * Generate a Bezier spline using the control points.
1179 */
1180
1181
1182 if (xPoints != NULL) {
1183 TkBezierScreenPoints(canvas, control, numSteps, xPoints);
1184 xPoints += numSteps;
1185 }
1186 if (dblPoints != NULL) {
1187 TkBezierPoints(control, numSteps, dblPoints);
1188 dblPoints += 2*numSteps;
1189 }
1190 outputPoints += numSteps;
1191 }
1192 return outputPoints;
1193 }
1194
1195 /*
1196 *--------------------------------------------------------------
1197 *
1198 * TkMakeBezierPostscript --
1199 *
1200 * This procedure generates Postscript commands that create
1201 * a path corresponding to a given Bezier curve.
1202 *
1203 * Results:
1204 * None. Postscript commands to generate the path are appended
1205 * to the interp's result.
1206 *
1207 * Side effects:
1208 * None.
1209 *
1210 *--------------------------------------------------------------
1211 */
1212
1213 void
1214 TkMakeBezierPostscript(interp, canvas, pointPtr, numPoints)
1215 Tcl_Interp *interp; /* Interpreter in whose result the
1216 * Postscript is to be stored. */
1217 Tk_Canvas canvas; /* Canvas widget for which the
1218 * Postscript is being generated. */
1219 double *pointPtr; /* Array of input coordinates: x0,
1220 * y0, x1, y1, etc.. */
1221 int numPoints; /* Number of points at pointPtr. */
1222 {
1223 int closed, i;
1224 int numCoords = numPoints*2;
1225 double control[8];
1226 char buffer[200];
1227
1228 /*
1229 * If the curve is a closed one then generate a special spline
1230 * that spans the last points and the first ones. Otherwise
1231 * just put the first point into the path.
1232 */
1233
1234 if ((pointPtr[0] == pointPtr[numCoords-2])
1235 && (pointPtr[1] == pointPtr[numCoords-1])) {
1236 closed = 1;
1237 control[0] = 0.5*pointPtr[numCoords-4] + 0.5*pointPtr[0];
1238 control[1] = 0.5*pointPtr[numCoords-3] + 0.5*pointPtr[1];
1239 control[2] = 0.167*pointPtr[numCoords-4] + 0.833*pointPtr[0];
1240 control[3] = 0.167*pointPtr[numCoords-3] + 0.833*pointPtr[1];
1241 control[4] = 0.833*pointPtr[0] + 0.167*pointPtr[2];
1242 control[5] = 0.833*pointPtr[1] + 0.167*pointPtr[3];
1243 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1244 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1245 sprintf(buffer, "%.15g %.15g moveto\n%.15g %.15g %.15g %.15g %.15g %.15g curveto\n",
1246 control[0], Tk_CanvasPsY(canvas, control[1]),
1247 control[2], Tk_CanvasPsY(canvas, control[3]),
1248 control[4], Tk_CanvasPsY(canvas, control[5]),
1249 control[6], Tk_CanvasPsY(canvas, control[7]));
1250 } else {
1251 closed = 0;
1252 control[6] = pointPtr[0];
1253 control[7] = pointPtr[1];
1254 sprintf(buffer, "%.15g %.15g moveto\n",
1255 control[6], Tk_CanvasPsY(canvas, control[7]));
1256 }
1257 Tcl_AppendResult(interp, buffer, (char *) NULL);
1258
1259 /*
1260 * Cycle through all the remaining points in the curve, generating
1261 * a curve section for each vertex in the linear path.
1262 */
1263
1264 for (i = numPoints-2, pointPtr += 2; i > 0; i--, pointPtr += 2) {
1265 control[2] = 0.333*control[6] + 0.667*pointPtr[0];
1266 control[3] = 0.333*control[7] + 0.667*pointPtr[1];
1267
1268 /*
1269 * Set up the last two control points. This is done
1270 * differently for the last spline of an open curve
1271 * than for other cases.
1272 */
1273
1274 if ((i == 1) && !closed) {
1275 control[6] = pointPtr[2];
1276 control[7] = pointPtr[3];
1277 } else {
1278 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
1279 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
1280 }
1281 control[4] = 0.333*control[6] + 0.667*pointPtr[0];
1282 control[5] = 0.333*control[7] + 0.667*pointPtr[1];
1283
1284 sprintf(buffer, "%.15g %.15g %.15g %.15g %.15g %.15g curveto\n",
1285 control[2], Tk_CanvasPsY(canvas, control[3]),
1286 control[4], Tk_CanvasPsY(canvas, control[5]),
1287 control[6], Tk_CanvasPsY(canvas, control[7]));
1288 Tcl_AppendResult(interp, buffer, (char *) NULL);
1289 }
1290 }
1291
1292 /*
1293 *--------------------------------------------------------------
1294 *
1295 * TkGetMiterPoints --
1296 *
1297 * Given three points forming an angle, compute the
1298 * coordinates of the inside and outside points of
1299 * the mitered corner formed by a line of a given
1300 * width at that angle.
1301 *
1302 * Results:
1303 * If the angle formed by the three points is less than
1304 * 11 degrees then 0 is returned and m1 and m2 aren't
1305 * modified. Otherwise 1 is returned and the points at
1306 * m1 and m2 are filled in with the positions of the points
1307 * of the mitered corner.
1308 *
1309 * Side effects:
1310 * None.
1311 *
1312 *--------------------------------------------------------------
1313 */
1314
1315 int
1316 TkGetMiterPoints(p1, p2, p3, width, m1, m2)
1317 double p1[]; /* Points to x- and y-coordinates of point
1318 * before vertex. */
1319 double p2[]; /* Points to x- and y-coordinates of vertex
1320 * for mitered joint. */
1321 double p3[]; /* Points to x- and y-coordinates of point
1322 * after vertex. */
1323 double width; /* Width of line. */
1324 double m1[]; /* Points to place to put "left" vertex
1325 * point (see as you face from p1 to p2). */
1326 double m2[]; /* Points to place to put "right" vertex
1327 * point. */
1328 {
1329 double theta1; /* Angle of segment p2-p1. */
1330 double theta2; /* Angle of segment p2-p3. */
1331 double theta; /* Angle between line segments (angle
1332 * of joint). */
1333 double theta3; /* Angle that bisects theta1 and
1334 * theta2 and points to m1. */
1335 double dist; /* Distance of miter points from p2. */
1336 double deltaX, deltaY; /* X and y offsets cooresponding to
1337 * dist (fudge factors for bounding
1338 * box). */
1339 double p1x, p1y, p2x, p2y, p3x, p3y;
1340 static double elevenDegrees = (11.0*2.0*PI)/360.0;
1341
1342 /*
1343 * Round the coordinates to integers to mimic what happens when the
1344 * line segments are displayed; without this code, the bounding box
1345 * of a mitered line can be miscomputed greatly.
1346 */
1347
1348 p1x = floor(p1[0]+0.5);
1349 p1y = floor(p1[1]+0.5);
1350 p2x = floor(p2[0]+0.5);
1351 p2y = floor(p2[1]+0.5);
1352 p3x = floor(p3[0]+0.5);
1353 p3y = floor(p3[1]+0.5);
1354
1355 if (p2y == p1y) {
1356 theta1 = (p2x < p1x) ? 0 : PI;
1357 } else if (p2x == p1x) {
1358 theta1 = (p2y < p1y) ? PI/2.0 : -PI/2.0;
1359 } else {
1360 theta1 = atan2(p1y - p2y, p1x - p2x);
1361 }
1362 if (p3y == p2y) {
1363 theta2 = (p3x > p2x) ? 0 : PI;
1364 } else if (p3x == p2x) {
1365 theta2 = (p3y > p2y) ? PI/2.0 : -PI/2.0;
1366 } else {
1367 theta2 = atan2(p3y - p2y, p3x - p2x);
1368 }
1369 theta = theta1 - theta2;
1370 if (theta > PI) {
1371 theta -= 2*PI;
1372 } else if (theta < -PI) {
1373 theta += 2*PI;
1374 }
1375 if ((theta < elevenDegrees) && (theta > -elevenDegrees)) {
1376 return 0;
1377 }
1378 dist = 0.5*width/sin(0.5*theta);
1379 if (dist < 0.0) {
1380 dist = -dist;
1381 }
1382
1383 /*
1384 * Compute theta3 (make sure that it points to the left when
1385 * looking from p1 to p2).
1386 */
1387
1388 theta3 = (theta1 + theta2)/2.0;
1389 if (sin(theta3 - (theta1 + PI)) < 0.0) {
1390 theta3 += PI;
1391 }
1392 deltaX = dist*cos(theta3);
1393 m1[0] = p2x + deltaX;
1394 m2[0] = p2x - deltaX;
1395 deltaY = dist*sin(theta3);
1396 m1[1] = p2y + deltaY;
1397 m2[1] = p2y - deltaY;
1398 return 1;
1399 }
1400
1401 /*
1402 *--------------------------------------------------------------
1403 *
1404 * TkGetButtPoints --
1405 *
1406 * Given two points forming a line segment, compute the
1407 * coordinates of two endpoints of a rectangle formed by
1408 * bloating the line segment until it is width units wide.
1409 *
1410 * Results:
1411 * There is no return value. M1 and m2 are filled in to
1412 * correspond to m1 and m2 in the diagram below:
1413 *
1414 * ----------------* m1
1415 * |
1416 * p1 *---------------* p2
1417 * |
1418 * ----------------* m2
1419 *
1420 * M1 and m2 will be W units apart, with p2 centered between
1421 * them and m1-m2 perpendicular to p1-p2. However, if
1422 * "project" is true then m1 and m2 will be as follows:
1423 *
1424 * -------------------* m1
1425 * p2 |
1426 * p1 *---------------* |
1427 * |
1428 * -------------------* m2
1429 *
1430 * In this case p2 will be width/2 units from the segment m1-m2.
1431 *
1432 * Side effects:
1433 * None.
1434 *
1435 *--------------------------------------------------------------
1436 */
1437
1438 void
1439 TkGetButtPoints(p1, p2, width, project, m1, m2)
1440 double p1[]; /* Points to x- and y-coordinates of point
1441 * before vertex. */
1442 double p2[]; /* Points to x- and y-coordinates of vertex
1443 * for mitered joint. */
1444 double width; /* Width of line. */
1445 int project; /* Non-zero means project p2 by an additional
1446 * width/2 before computing m1 and m2. */
1447 double m1[]; /* Points to place to put "left" result
1448 * point, as you face from p1 to p2. */
1449 double m2[]; /* Points to place to put "right" result
1450 * point. */
1451 {
1452 double length; /* Length of p1-p2 segment. */
1453 double deltaX, deltaY; /* Increments in coords. */
1454
1455 width *= 0.5;
1456 length = hypot(p2[0] - p1[0], p2[1] - p1[1]);
1457 if (length == 0.0) {
1458 m1[0] = m2[0] = p2[0];
1459 m1[1] = m2[1] = p2[1];
1460 } else {
1461 deltaX = -width * (p2[1] - p1[1]) / length;
1462 deltaY = width * (p2[0] - p1[0]) / length;
1463 m1[0] = p2[0] + deltaX;
1464 m2[0] = p2[0] - deltaX;
1465 m1[1] = p2[1] + deltaY;
1466 m2[1] = p2[1] - deltaY;
1467 if (project) {
1468 m1[0] += deltaY;
1469 m2[0] += deltaY;
1470 m1[1] -= deltaX;
1471 m2[1] -= deltaX;
1472 }
1473 }
1474 }