]> cvs.zerfleddert.de Git - micropolis/blob - src/tk/tktrig.c
Fixes for compilation with gcc 15
[micropolis] / src / tk / tktrig.c
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 1992 Regents of the University of California.
10 * Permission to use, copy, modify, and distribute this
11 * software and its documentation for any purpose and without
12 * fee is hereby granted, provided that the above copyright
13 * notice appear in all copies. The University of California
14 * makes no representations about the suitability of this
15 * software for any purpose. It is provided "as is" without
16 * express or implied warranty.
17 */
18
19 #ifndef lint
20 static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkTrig.c,v 1.8 92/08/24 09:24:14 ouster Exp $ SPRITE (Berkeley)";
21 #endif
22
23 #include <stdio.h>
24 #include <math.h>
25 #include "tkconfig.h"
26 #include "tkcanvas.h"
27
28 #undef MIN
29 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
30 #undef MAX
31 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
32 #define PI 3.14159265358979323846
33 \f
34 /*
35 *--------------------------------------------------------------
36 *
37 * TkLineToPoint --
38 *
39 * Compute the distance from a point to a finite line segment.
40 *
41 * Results:
42 * The return value is the distance from the line segment
43 * whose end-points are *end1Ptr and *end2Ptr to the point
44 * given by *pointPtr.
45 *
46 * Side effects:
47 * None.
48 *
49 *--------------------------------------------------------------
50 */
51
52 double
53 TkLineToPoint (
54 double end1Ptr[2], /* Coordinates of first end-point of line. */
55 double end2Ptr[2], /* Coordinates of second end-point of line. */
56 double pointPtr[2] /* Points to coords for point. */
57 )
58 {
59 double x, y;
60
61 /*
62 * Compute the point on the line that is closest to the
63 * point. This must be done separately for vertical edges,
64 * horizontal edges, and other edges.
65 */
66
67 if (end1Ptr[0] == end2Ptr[0]) {
68
69 /*
70 * Vertical edge.
71 */
72
73 x = end1Ptr[0];
74 if (end1Ptr[1] >= end2Ptr[1]) {
75 y = MIN(end1Ptr[1], pointPtr[1]);
76 y = MAX(y, end2Ptr[1]);
77 } else {
78 y = MIN(end2Ptr[1], pointPtr[1]);
79 y = MAX(y, end1Ptr[1]);
80 }
81 } else if (end1Ptr[1] == end2Ptr[1]) {
82
83 /*
84 * Horizontal edge.
85 */
86
87 y = end1Ptr[1];
88 if (end1Ptr[0] >= end2Ptr[0]) {
89 x = MIN(end1Ptr[0], pointPtr[0]);
90 x = MAX(x, end2Ptr[0]);
91 } else {
92 x = MIN(end2Ptr[0], pointPtr[0]);
93 x = MAX(x, end1Ptr[0]);
94 }
95 } else {
96 double m1, b1, m2, b2;
97
98 /*
99 * The edge is neither horizontal nor vertical. Convert the
100 * edge to a line equation of the form y = m1*x + b1. Then
101 * compute a line perpendicular to this edge but passing
102 * through the point, also in the form y = m2*x + b2.
103 */
104
105 m1 = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]);
106 b1 = end1Ptr[1] - m1*end1Ptr[0];
107 m2 = -1.0/m1;
108 b2 = pointPtr[1] - m2*pointPtr[0];
109 x = (b2 - b1)/(m1 - m2);
110 y = m1*x + b1;
111 if (end1Ptr[0] > end2Ptr[0]) {
112 if (x > end1Ptr[0]) {
113 x = end1Ptr[0];
114 y = end1Ptr[1];
115 } else if (x < end2Ptr[0]) {
116 x = end2Ptr[0];
117 y = end2Ptr[1];
118 }
119 } else {
120 if (x > end2Ptr[0]) {
121 x = end2Ptr[0];
122 y = end2Ptr[1];
123 } else if (x < end1Ptr[0]) {
124 x = end1Ptr[0];
125 y = end1Ptr[1];
126 }
127 }
128 }
129
130 /*
131 * Compute the distance to the closest point.
132 */
133
134 return hypot(pointPtr[0] - x, pointPtr[1] - y);
135 }
136 \f
137 /*
138 *--------------------------------------------------------------
139 *
140 * TkLineToArea --
141 *
142 * Determine whether a line lies entirely inside, entirely
143 * outside, or overlapping a given rectangular area.
144 *
145 * Results:
146 * -1 is returned if the line given by end1Ptr and end2Ptr
147 * is entirely outside the rectangle given by rectPtr. 0 is
148 * returned if the polygon overlaps the rectangle, and 1 is
149 * returned if the polygon is entirely inside the rectangle.
150 *
151 * Side effects:
152 * None.
153 *
154 *--------------------------------------------------------------
155 */
156
157 int
158 TkLineToArea (
159 double end1Ptr[2], /* X and y coordinates for one endpoint
160 * of line. */
161 double end2Ptr[2], /* X and y coordinates for other endpoint
162 * of line. */
163 double rectPtr[4] /* Points to coords for rectangle, in the
164 * order x1, y1, x2, y2. X1 must be no
165 * larger than x2, and y1 no larger than y2. */
166 )
167 {
168 int inside1, inside2;
169
170 /*
171 * First check the two points individually to see whether they
172 * are inside the rectangle or not.
173 */
174
175 inside1 = (end1Ptr[0] >= rectPtr[0]) && (end1Ptr[0] <= rectPtr[2])
176 && (end1Ptr[1] >= rectPtr[1]) && (end1Ptr[1] <= rectPtr[3]);
177 inside2 = (end2Ptr[0] >= rectPtr[0]) && (end2Ptr[0] <= rectPtr[2])
178 && (end2Ptr[1] >= rectPtr[1]) && (end2Ptr[1] <= rectPtr[3]);
179 if (inside1 != inside2) {
180 return 0;
181 }
182 if (inside1 & inside2) {
183 return 1;
184 }
185
186 /*
187 * Both points are outside the rectangle, but still need to check
188 * for intersections between the line and the rectangle. Horizontal
189 * and vertical lines are particularly easy, so handle them
190 * separately.
191 */
192
193 if (end1Ptr[0] == end2Ptr[0]) {
194 /*
195 * Vertical line.
196 */
197
198 if (((end1Ptr[1] >= rectPtr[1]) ^ (end2Ptr[1] >= rectPtr[1]))
199 && (end1Ptr[0] >= rectPtr[0])
200 && (end1Ptr[0] <= rectPtr[2])) {
201 return 0;
202 }
203 } else if (end1Ptr[1] == end2Ptr[1]) {
204 /*
205 * Horizontal line.
206 */
207
208 if (((end1Ptr[0] >= rectPtr[0]) ^ (end2Ptr[0] >= rectPtr[0]))
209 && (end1Ptr[1] >= rectPtr[1])
210 && (end1Ptr[1] <= rectPtr[3])) {
211 return 0;
212 }
213 } else {
214 double m, x, y, low, high;
215
216 /*
217 * Diagonal line. Compute slope of line and use
218 * for intersection checks against each of the
219 * sides of the rectangle: left, right, bottom, top.
220 */
221
222 m = (end2Ptr[1] - end1Ptr[1])/(end2Ptr[0] - end1Ptr[0]);
223 if (end1Ptr[0] < end2Ptr[0]) {
224 low = end1Ptr[0]; high = end2Ptr[0];
225 } else {
226 low = end2Ptr[0]; high = end1Ptr[0];
227 }
228
229 /*
230 * Left edge.
231 */
232
233 y = end1Ptr[1] + (rectPtr[0] - end1Ptr[0])*m;
234 if ((rectPtr[0] >= low) && (rectPtr[0] <= high)
235 && (y >= rectPtr[1]) && (y <= rectPtr[3])) {
236 return 0;
237 }
238
239 /*
240 * Right edge.
241 */
242
243 y += (rectPtr[2] - rectPtr[0])*m;
244 if ((y >= rectPtr[1]) && (y <= rectPtr[3])
245 && (rectPtr[2] >= low) && (rectPtr[2] <= high)) {
246 return 0;
247 }
248
249 /*
250 * Bottom edge.
251 */
252
253 if (end1Ptr[1] < end2Ptr[1]) {
254 low = end1Ptr[1]; high = end2Ptr[1];
255 } else {
256 low = end2Ptr[1]; high = end1Ptr[1];
257 }
258 x = end1Ptr[0] + (rectPtr[1] - end1Ptr[1])/m;
259 if ((x >= rectPtr[0]) && (x <= rectPtr[2])
260 && (rectPtr[1] >= low) && (rectPtr[1] <= high)) {
261 return 0;
262 }
263
264 /*
265 * Top edge.
266 */
267
268 x += (rectPtr[3] - rectPtr[1])/m;
269 if ((x >= rectPtr[0]) && (x <= rectPtr[2])
270 && (rectPtr[3] >= low) && (rectPtr[3] <= high)) {
271 return 0;
272 }
273 }
274 return -1;
275 }
276 \f
277 /*
278 *--------------------------------------------------------------
279 *
280 * TkPolygonToPoint --
281 *
282 * Compute the distance from a point to a polygon.
283 *
284 * Results:
285 * The return value is 0.0 if the point referred to by
286 * pointPtr is within the polygon referred to by polyPtr
287 * and numPoints. Otherwise the return value is the
288 * distance of the point from the polygon.
289 *
290 * Side effects:
291 * None.
292 *
293 *--------------------------------------------------------------
294 */
295
296 double
297 TkPolygonToPoint (
298 double *polyPtr, /* Points to an array coordinates for
299 * closed polygon: x0, y0, x1, y1, ...
300 * The polygon may be self-intersecting. */
301 int numPoints, /* Total number of points at *polyPtr. */
302 double *pointPtr /* Points to coords for point. */
303 )
304 {
305 double bestDist; /* Closest distance between point and
306 * any edge in polygon. */
307 int intersections; /* Number of edges in the polygon that
308 * intersect a ray extending vertically
309 * upwards from the point to infinity. */
310 int count;
311 register double *pPtr;
312
313 /*
314 * Iterate through all of the edges in the polygon, updating
315 * bestDist and intersections.
316 *
317 * TRICKY POINT: when computing intersections, include left
318 * x-coordinate of line within its range, but not y-coordinate.
319 * Otherwise if the point lies exactly below a vertex we'll
320 * count it as two intersections.
321 */
322
323 bestDist = 1.0e40;
324 intersections = 0;
325
326 for (count = numPoints, pPtr = polyPtr; count > 1; count--, pPtr += 2) {
327 double x, y, dist;
328
329 /*
330 * Compute the point on the current edge closest to the point
331 * and update the intersection count. This must be done
332 * separately for vertical edges, horizontal edges, and
333 * other edges.
334 */
335
336 if (pPtr[2] == pPtr[0]) {
337
338 /*
339 * Vertical edge.
340 */
341
342 x = pPtr[0];
343 if (pPtr[1] >= pPtr[3]) {
344 y = MIN(pPtr[1], pointPtr[1]);
345 y = MAX(y, pPtr[3]);
346 } else {
347 y = MIN(pPtr[3], pointPtr[1]);
348 y = MAX(y, pPtr[1]);
349 }
350 } else if (pPtr[3] == pPtr[1]) {
351
352 /*
353 * Horizontal edge.
354 */
355
356 y = pPtr[1];
357 if (pPtr[0] >= pPtr[2]) {
358 x = MIN(pPtr[0], pointPtr[0]);
359 x = MAX(x, pPtr[2]);
360 if ((pointPtr[1] < y) && (pointPtr[0] < pPtr[0])
361 && (pointPtr[0] >= pPtr[2])) {
362 intersections++;
363 }
364 } else {
365 x = MIN(pPtr[2], pointPtr[0]);
366 x = MAX(x, pPtr[0]);
367 if ((pointPtr[1] < y) && (pointPtr[0] < pPtr[2])
368 && (pointPtr[0] >= pPtr[0])) {
369 intersections++;
370 }
371 }
372 } else {
373 double m1, b1, m2, b2;
374 int lower; /* Non-zero means point below line. */
375
376 /*
377 * The edge is neither horizontal nor vertical. Convert the
378 * edge to a line equation of the form y = m1*x + b1. Then
379 * compute a line perpendicular to this edge but passing
380 * through the point, also in the form y = m2*x + b2.
381 */
382
383 m1 = (pPtr[3] - pPtr[1])/(pPtr[2] - pPtr[0]);
384 b1 = pPtr[1] - m1*pPtr[0];
385 m2 = -1.0/m1;
386 b2 = pointPtr[1] - m2*pointPtr[0];
387 x = (b2 - b1)/(m1 - m2);
388 y = m1*x + b1;
389 if (pPtr[0] > pPtr[2]) {
390 if (x > pPtr[0]) {
391 x = pPtr[0];
392 y = pPtr[1];
393 } else if (x < pPtr[2]) {
394 x = pPtr[2];
395 y = pPtr[3];
396 }
397 } else {
398 if (x > pPtr[2]) {
399 x = pPtr[2];
400 y = pPtr[3];
401 } else if (x < pPtr[0]) {
402 x = pPtr[0];
403 y = pPtr[1];
404 }
405 }
406 lower = (m1*pointPtr[0] + b1) > pointPtr[1];
407 if (lower && (pointPtr[0] >= MIN(pPtr[0], pPtr[2]))
408 && (pointPtr[0] < MAX(pPtr[0], pPtr[2]))) {
409 intersections++;
410 }
411 }
412
413 /*
414 * Compute the distance to the closest point, and see if that
415 * is the best distance seen so far.
416 */
417
418 dist = hypot(pointPtr[0] - x, pointPtr[1] - y);
419 if (dist < bestDist) {
420 bestDist = dist;
421 }
422 }
423
424 /*
425 * We've processed all of the points. If the number of intersections
426 * is odd, the point is inside the polygon.
427 */
428
429 if (intersections & 0x1) {
430 return 0.0;
431 }
432 return bestDist;
433 }
434 \f
435 /*
436 *--------------------------------------------------------------
437 *
438 * TkPolygonToArea --
439 *
440 * Determine whether a polygon lies entirely inside, entirely
441 * outside, or overlapping a given rectangular area.
442 *
443 * Results:
444 * -1 is returned if the polygon given by polyPtr and numPoints
445 * is entirely outside the rectangle given by rectPtr. 0 is
446 * returned if the polygon overlaps the rectangle, and 1 is
447 * returned if the polygon is entirely inside the rectangle.
448 *
449 * Side effects:
450 * None.
451 *
452 *--------------------------------------------------------------
453 */
454
455 int
456 TkPolygonToArea (
457 double *polyPtr, /* Points to an array coordinates for
458 * closed polygon: x0, y0, x1, y1, ...
459 * The polygon may be self-intersecting. */
460 int numPoints, /* Total number of points at *polyPtr. */
461 register double *rectPtr /* Points to coords for rectangle, in the
462 * order x1, y1, x2, y2. X1 and y1 must
463 * be lower-left corner. */
464 )
465 {
466 int state; /* State of all edges seen so far (-1 means
467 * outside, 1 means inside, won't ever be
468 * 0). */
469 int count;
470 register double *pPtr;
471
472 /*
473 * Iterate over all of the edges of the polygon and test them
474 * against the rectangle. Can quit as soon as the state becomes
475 * "intersecting".
476 */
477
478 state = TkLineToArea(polyPtr, polyPtr+2, rectPtr);
479 if (state == 0) {
480 return 0;
481 }
482 for (pPtr = polyPtr+2, count = numPoints-1; count >= 2;
483 pPtr += 2, count--) {
484 if (TkLineToArea(pPtr, pPtr+2, rectPtr) != state) {
485 return 0;
486 }
487 }
488
489 /*
490 * If all of the edges were inside the rectangle we're done.
491 * If all of the edges were outside, then the rectangle could
492 * still intersect the polygon (if it's entirely enclosed).
493 * Call TkPolygonToPoint to figure this out.
494 */
495
496 if (state == 1) {
497 return 1;
498 }
499 if (TkPolygonToPoint(polyPtr, numPoints, rectPtr) == 0.0) {
500 return 0;
501 }
502 return -1;
503 }
504 \f
505 /*
506 *--------------------------------------------------------------
507 *
508 * TkOvalToPoint --
509 *
510 * Computes the distance from a given point to a given
511 * oval, in canvas units.
512 *
513 * Results:
514 * The return value is 0 if the point given by *pointPtr is
515 * inside the oval. If the point isn't inside the
516 * oval then the return value is approximately the distance
517 * from the point to the oval. If the oval is filled, then
518 * anywhere in the interior is considered "inside"; if
519 * the oval isn't filled, then "inside" means only the area
520 * occupied by the outline.
521 *
522 * Side effects:
523 * None.
524 *
525 *--------------------------------------------------------------
526 */
527
528 /* ARGSUSED */
529 double
530 TkOvalToPoint (
531 double ovalPtr[4], /* Pointer to array of four coordinates
532 * (x1, y1, x2, y2) defining oval's bounding
533 * box. */
534 double width, /* Width of outline for oval. */
535 int filled, /* Non-zero means oval should be treated as
536 * filled; zero means only consider outline. */
537 double pointPtr[2] /* Coordinates of point. */
538 )
539 {
540 double xDelta, yDelta, scaledDistance, distToOutline, distToCenter;
541
542 /*
543 * Compute the distance between the center of the oval and the
544 * point in question, using a coordinate system where the oval
545 * has been transformed to a circle with unit radius.
546 */
547
548 xDelta = (pointPtr[0] - (ovalPtr[0] + ovalPtr[2])/2.0);
549 yDelta = (pointPtr[1] - (ovalPtr[1] + ovalPtr[3])/2.0);
550 distToCenter = hypot(xDelta, yDelta);
551 scaledDistance = hypot(xDelta / ((ovalPtr[2] + width - ovalPtr[0])/2.0),
552 yDelta / ((ovalPtr[3] + width - ovalPtr[1])/2.0));
553
554
555 /*
556 * If the scaled distance is greater than 1 then it means no
557 * hit. Compute the distance from the point to the edge of
558 * the circle, then scale this distance back to the original
559 * coordinate system.
560 *
561 * Note: this distance isn't completely accurate. It's only
562 * an approximation, and it can overestimate the correct
563 * distance when the oval is eccentric.
564 */
565
566 if (scaledDistance > 1.0) {
567 return (distToCenter/scaledDistance) * (scaledDistance - 1.0);
568 }
569
570 /*
571 * Scaled distance less than 1 means the point is inside the
572 * outer edge of the oval. If this is a filled oval, then we
573 * have a hit. Otherwise, do the same computation as above
574 * (scale back to original coordinate system), but also check
575 * to see if the point is within the width of the outline.
576 */
577
578 if (filled) {
579 return 0.0;
580 }
581 distToOutline = (distToCenter/scaledDistance) * (1.0 - scaledDistance)
582 - width;
583 if (distToOutline < 0.0) {
584 return 0.0;
585 }
586 return distToOutline;
587 }
588 \f
589 /*
590 *--------------------------------------------------------------
591 *
592 * TkOvalToArea --
593 *
594 * Determine whether an oval lies entirely inside, entirely
595 * outside, or overlapping a given rectangular area.
596 *
597 * Results:
598 * -1 is returned if the oval described by ovalPtr is entirely
599 * outside the rectangle given by rectPtr. 0 is returned if the
600 * oval overlaps the rectangle, and 1 is returned if the oval
601 * is entirely inside the rectangle.
602 *
603 * Side effects:
604 * None.
605 *
606 *--------------------------------------------------------------
607 */
608
609 int
610 TkOvalToArea (
611 register double *ovalPtr, /* Points to coordinates definining the
612 * bounding rectangle for the oval: x1, y1,
613 * x2, y2. X1 must be less than x2 and y1
614 * less than y2. */
615 register double *rectPtr /* Points to coords for rectangle, in the
616 * order x1, y1, x2, y2. X1 and y1 must
617 * be lower-left corner. */
618 )
619 {
620 double centerX, centerY, radX, radY, deltaX, deltaY;
621
622 /*
623 * First, see if oval is entirely inside rectangle or entirely
624 * outside rectangle.
625 */
626
627 if ((rectPtr[0] <= ovalPtr[0]) && (rectPtr[2] >= ovalPtr[2])
628 && (rectPtr[1] <= ovalPtr[1]) && (rectPtr[3] >= ovalPtr[3])) {
629 return 1;
630 }
631 if ((rectPtr[2] < ovalPtr[0]) || (rectPtr[0] > ovalPtr[2])
632 || (rectPtr[3] < ovalPtr[1]) || (rectPtr[1] > ovalPtr[3])) {
633 return -1;
634 }
635
636 /*
637 * Next, go through the rectangle side by side. For each side
638 * of the rectangle, find the point on the side that is closest
639 * to the oval's center, and see if that point is inside the
640 * oval. If at least one such point is inside the oval, then
641 * the rectangle intersects the oval.
642 */
643
644 centerX = (ovalPtr[0] + ovalPtr[2])/2;
645 centerY = (ovalPtr[1] + ovalPtr[3])/2;
646 radX = (ovalPtr[2] - ovalPtr[0])/2;
647 radY = (ovalPtr[3] - ovalPtr[1])/2;
648
649 deltaY = rectPtr[1] - centerY;
650 if (deltaY < 0.0) {
651 deltaY = centerY - rectPtr[3];
652 if (deltaY < 0.0) {
653 deltaY = 0;
654 }
655 }
656 deltaY /= radY;
657 deltaY *= deltaY;
658
659 /*
660 * Left side:
661 */
662
663 deltaX = (rectPtr[0] - centerX)/radX;
664 deltaX *= deltaX;
665 if ((deltaX + deltaY) <= 1.0) {
666 return 0;
667 }
668
669 /*
670 * Right side:
671 */
672
673 deltaX = (rectPtr[2] - centerX)/radX;
674 deltaX *= deltaX;
675 if ((deltaX + deltaY) <= 1.0) {
676 return 0;
677 }
678
679 deltaX = rectPtr[0] - centerX;
680 if (deltaX < 0.0) {
681 deltaX = centerX - rectPtr[2];
682 if (deltaX < 0.0) {
683 deltaX = 0;
684 }
685 }
686 deltaX /= radX;
687 deltaX *= deltaX;
688
689 /*
690 * Bottom side:
691 */
692
693 deltaY = (rectPtr[1] - centerY)/radY;
694 deltaY *= deltaY;
695 if ((deltaX + deltaY) < 1.0) {
696 return 0;
697 }
698
699 /*
700 * Top side:
701 */
702
703 deltaY = (rectPtr[3] - centerY)/radY;
704 deltaY *= deltaY;
705 if ((deltaX + deltaY) < 1.0) {
706 return 0;
707 }
708
709 return -1;
710 }
711 \f
712 /*
713 *--------------------------------------------------------------
714 *
715 * TkIncludePoint --
716 *
717 * Given a point and a generic canvas item header, expand
718 * the item's bounding box if needed to include the point.
719 *
720 * Results:
721 * None.
722 *
723 * Side effects:
724 * The boudn.
725 *
726 *--------------------------------------------------------------
727 */
728
729 /* ARGSUSED */
730 void
731 TkIncludePoint (
732 Tk_Canvas *canvasPtr, /* Canvas containing item. */
733 register Tk_Item *itemPtr, /* Item whose bounding box is
734 * being calculated. */
735 double *pointPtr /* Address of two doubles giving
736 * x and y coordinates of point. */
737 )
738 {
739 int tmp;
740
741 tmp = pointPtr[0] + 0.5;
742 if (tmp < itemPtr->x1) {
743 itemPtr->x1 = tmp;
744 }
745 if (tmp > itemPtr->x2) {
746 itemPtr->x2 = tmp;
747 }
748 tmp = pointPtr[1] + 0.5;
749 if (tmp < itemPtr->y1) {
750 itemPtr->y1 = tmp;
751 }
752 if (tmp > itemPtr->y2) {
753 itemPtr->y2 = tmp;
754 }
755 }
756 \f
757 /*
758 *--------------------------------------------------------------
759 *
760 * TkBezierScreenPoints --
761 *
762 * Given four control points, create a larger set of XPoints
763 * for a Bezier spline based on the points.
764 *
765 * Results:
766 * The array at *xPointPtr gets filled in with numSteps XPoints
767 * corresponding to the Bezier spline defined by the four
768 * control points. Note: no output point is generated for the
769 * first input point, but an output point *is* generated for
770 * the last input point.
771 *
772 * Side effects:
773 * None.
774 *
775 *--------------------------------------------------------------
776 */
777
778 void
779 TkBezierScreenPoints (
780 Tk_Canvas *canvasPtr, /* Canvas in which curve is to be
781 * drawn. */
782 double control[], /* Array of coordinates for four
783 * control points: x0, y0, x1, y1,
784 * ... x3 y3. */
785 int numSteps, /* Number of curve points to
786 * generate. */
787 register XPoint *xPointPtr /* Where to put new points. */
788 )
789 {
790 int i;
791 double u, u2, u3, t, t2, t3;
792
793 for (i = 1; i <= numSteps; i++, xPointPtr++) {
794 t = ((double) i)/((double) numSteps);
795 t2 = t*t;
796 t3 = t2*t;
797 u = 1.0 - t;
798 u2 = u*u;
799 u3 = u2*u;
800 xPointPtr->x = SCREEN_X(canvasPtr, (control[0]*u3
801 + 3.0 * (control[2]*t*u2 + control[4]*t2*u) + control[6]*t3));
802 xPointPtr->y = SCREEN_Y(canvasPtr, (control[1]*u3
803 + 3.0 * (control[3]*t*u2 + control[5]*t2*u) + control[7]*t3));
804 }
805 }
806 \f
807 /*
808 *--------------------------------------------------------------
809 *
810 * TkBezierPoints --
811 *
812 * Given four control points, create a larger set of points
813 * for a Bezier spline based on the points.
814 *
815 * Results:
816 * The array at *coordPtr gets filled in with 2*numSteps
817 * coordinates, which correspond to the Bezier spline defined
818 * by the four control points. Note: no output point is
819 * generated for the first input point, but an output point
820 * *is* generated for the last input point.
821 *
822 * Side effects:
823 * None.
824 *
825 *--------------------------------------------------------------
826 */
827
828 void
829 TkBezierPoints (
830 double control[], /* Array of coordinates for four
831 * control points: x0, y0, x1, y1,
832 * ... x3 y3. */
833 int numSteps, /* Number of curve points to
834 * generate. */
835 register double *coordPtr /* Where to put new points. */
836 )
837 {
838 int i;
839 double u, u2, u3, t, t2, t3;
840
841 for (i = 1; i <= numSteps; i++, coordPtr += 2) {
842 t = ((double) i)/((double) numSteps);
843 t2 = t*t;
844 t3 = t2*t;
845 u = 1.0 - t;
846 u2 = u*u;
847 u3 = u2*u;
848 coordPtr[0] = control[0]*u3
849 + 3.0 * (control[2]*t*u2 + control[4]*t2*u) + control[6]*t3;
850 coordPtr[1] = control[1]*u3
851 + 3.0 * (control[3]*t*u2 + control[5]*t2*u) + control[7]*t3;
852 }
853 }
854 \f
855 /*
856 *--------------------------------------------------------------
857 *
858 * TkMakeBezierCurve --
859 *
860 * Given a set of points, create a new set of points that
861 * fit Bezier splines to the line segments connecting the
862 * original points. Produces output points in either of two
863 * forms.
864 *
865 * Results:
866 * Either or both of the xPoints or dblPoints arrays are filled
867 * in. The return value is the number of points placed in the
868 * arrays. Note: if the first and last points are the same, then
869 * a closed curve is generated.
870 *
871 * Side effects:
872 * None.
873 *
874 *--------------------------------------------------------------
875 */
876
877 int
878 TkMakeBezierCurve (
879 Tk_Canvas *canvasPtr, /* Canvas in which curve is to be
880 * drawn. */
881 double *pointPtr, /* Array of input coordinates: x0,
882 * y0, x1, y1, etc.. */
883 int numPoints, /* Number of points at pointPtr. */
884 int numSteps, /* Number of steps to use for each
885 * spline segments (determines
886 * smoothness of curve). */
887 XPoint xPoints[], /* Array of XPoints to fill in (e.g.
888 * for display. NULL means don't
889 * fill in any XPoints. */
890 double dblPoints[] /* Array of points to fill in as
891 * doubles, in the form x0, y0,
892 * x1, y1, .... NULL means don't
893 * fill in anything in this form.
894 * Caller must make sure that this
895 * array has enough space. */
896 )
897 {
898 int closed, outputPoints, i;
899 int numCoords = numPoints*2;
900 double control[8];
901
902 /*
903 * If the curve is a closed one then generate a special spline
904 * that spans the last points and the first ones. Otherwise
905 * just put the first point into the output.
906 */
907
908 outputPoints = 0;
909 if ((pointPtr[0] == pointPtr[numCoords-2])
910 && (pointPtr[1] == pointPtr[numCoords-1])) {
911 closed = 1;
912 control[0] = 0.5*pointPtr[numCoords-4] + 0.5*pointPtr[0];
913 control[1] = 0.5*pointPtr[numCoords-3] + 0.5*pointPtr[1];
914 control[2] = 0.167*pointPtr[numCoords-4] + 0.833*pointPtr[0];
915 control[3] = 0.167*pointPtr[numCoords-3] + 0.833*pointPtr[1];
916 control[4] = 0.833*pointPtr[0] + 0.167*pointPtr[2];
917 control[5] = 0.833*pointPtr[1] + 0.167*pointPtr[3];
918 control[6] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
919 control[7] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
920 if (xPoints != NULL) {
921 xPoints->x = SCREEN_X(canvasPtr, control[0]);
922 xPoints->y = SCREEN_Y(canvasPtr, control[1]);
923 TkBezierScreenPoints(canvasPtr, control, numSteps, xPoints+1);
924 xPoints += numSteps+1;
925 }
926 if (dblPoints != NULL) {
927 dblPoints[0] = control[0];
928 dblPoints[1] = control[1];
929 TkBezierPoints(control, numSteps, dblPoints+2);
930 dblPoints += 2*(numSteps+1);
931 }
932 outputPoints += numSteps+1;
933 } else {
934 closed = 0;
935 if (xPoints != NULL) {
936 xPoints->x = SCREEN_X(canvasPtr, pointPtr[0]);
937 xPoints->y = SCREEN_Y(canvasPtr, pointPtr[1]);
938 xPoints += 1;
939 }
940 if (dblPoints != NULL) {
941 dblPoints[0] = pointPtr[0];
942 dblPoints[1] = pointPtr[1];
943 dblPoints += 2;
944 }
945 outputPoints += 1;
946 }
947
948 for (i = 2; i < numPoints; i++, pointPtr += 2) {
949 /*
950 * Set up the first two control points. This is done
951 * differently for the first spline of an open curve
952 * than for other cases.
953 */
954
955 if ((i == 2) && !closed) {
956 control[0] = pointPtr[0];
957 control[1] = pointPtr[1];
958 control[2] = 0.333*pointPtr[0] + 0.667*pointPtr[2];
959 control[3] = 0.333*pointPtr[1] + 0.667*pointPtr[3];
960 } else {
961 control[0] = 0.5*pointPtr[0] + 0.5*pointPtr[2];
962 control[1] = 0.5*pointPtr[1] + 0.5*pointPtr[3];
963 control[2] = 0.167*pointPtr[0] + 0.833*pointPtr[2];
964 control[3] = 0.167*pointPtr[1] + 0.833*pointPtr[3];
965 }
966
967 /*
968 * Set up the last two control points. This is done
969 * differently for the last spline of an open curve
970 * than for other cases.
971 */
972
973 if ((i == (numPoints-1)) && !closed) {
974 control[4] = .667*pointPtr[2] + .333*pointPtr[4];
975 control[5] = .667*pointPtr[3] + .333*pointPtr[5];
976 control[6] = pointPtr[4];
977 control[7] = pointPtr[5];
978 } else {
979 control[4] = .833*pointPtr[2] + .167*pointPtr[4];
980 control[5] = .833*pointPtr[3] + .167*pointPtr[5];
981 control[6] = 0.5*pointPtr[2] + 0.5*pointPtr[4];
982 control[7] = 0.5*pointPtr[3] + 0.5*pointPtr[5];
983 }
984
985 /*
986 * If the first two points coincide, or if the last
987 * two points coincide, then generate a single
988 * straight-line segment by outputting the last control
989 * point.
990 */
991
992 if (((pointPtr[0] == pointPtr[2]) && (pointPtr[1] == pointPtr[3]))
993 || ((pointPtr[2] == pointPtr[4])
994 && (pointPtr[3] == pointPtr[5]))) {
995 if (xPoints != NULL) {
996 xPoints[0].x = SCREEN_X(canvasPtr, control[6]);
997 xPoints[0].y = SCREEN_Y(canvasPtr, control[7]);
998 xPoints++;
999 }
1000 if (dblPoints != NULL) {
1001 dblPoints[0] = control[6];
1002 dblPoints[1] = control[7];
1003 dblPoints += 2;
1004 }
1005 outputPoints += 1;
1006 continue;
1007 }
1008
1009 /*
1010 * Generate a Bezier spline using the control points.
1011 */
1012
1013
1014 if (xPoints != NULL) {
1015 TkBezierScreenPoints(canvasPtr, control, numSteps, xPoints);
1016 xPoints += numSteps;
1017 }
1018 if (dblPoints != NULL) {
1019 TkBezierPoints(control, numSteps, dblPoints);
1020 dblPoints += 2*numSteps;
1021 }
1022 outputPoints += numSteps;
1023 }
1024 return outputPoints;
1025 }
1026 \f
1027 /*
1028 *--------------------------------------------------------------
1029 *
1030 * TkGetMiterPoints --
1031 *
1032 * Given three points forming an angle, compute the
1033 * coordinates of the inside and outside points of
1034 * the mitered corner formed by a line of a given
1035 * width at that angle.
1036 *
1037 * Results:
1038 * If the angle formed by the three points is less than
1039 * 11 degrees then 0 is returned and m1 and m2 aren't
1040 * modified. Otherwise 1 is returned and the points at
1041 * m1 and m2 are filled in with the positions of the points
1042 * of the mitered corner.
1043 *
1044 * Side effects:
1045 * None.
1046 *
1047 *--------------------------------------------------------------
1048 */
1049
1050 int
1051 TkGetMiterPoints (
1052 double p1[], /* Points to x- and y-coordinates of point
1053 * before vertex. */
1054 double p2[], /* Points to x- and y-coordinates of vertex
1055 * for mitered joint. */
1056 double p3[], /* Points to x- and y-coordinates of point
1057 * after vertex. */
1058 double width, /* Width of line. */
1059 double m1[], /* Points to place to put "left" vertex
1060 * point (see as you face from p1 to p2). */
1061 double m2[] /* Points to place to put "right" vertex
1062 * point. */
1063 )
1064 {
1065 double theta1; /* Angle of segment p2-p1. */
1066 double theta2; /* Angle of segment p2-p3. */
1067 double theta; /* Angle between line segments (angle
1068 * of joint). */
1069 double theta3; /* Angle that bisects theta1 and
1070 * theta2 and points to m1. */
1071 double dist; /* Distance of miter points from p2. */
1072 double deltaX, deltaY; /* X and y offsets cooresponding to
1073 * dist (fudge factors for bounding
1074 * box). */
1075 static float elevenDegrees = (11.0*2.0*PI)/360.0;
1076
1077 if (p2[1] == p1[1]) {
1078 theta1 = (p2[0] < p1[0]) ? 0 : PI;
1079 } else if (p2[0] == p1[0]) {
1080 theta1 = (p2[1] < p1[1]) ? PI/2.0 : -PI/2.0;
1081 } else {
1082 theta1 = atan2(p1[1] - p2[1], p1[0] - p2[0]);
1083 }
1084 if (p3[1] == p2[1]) {
1085 theta2 = (p3[0] > p2[0]) ? 0 : PI;
1086 } else if (p3[0] == p2[0]) {
1087 theta2 = (p3[1] > p2[1]) ? PI/2.0 : -PI/2.0;
1088 } else {
1089 theta2 = atan2(p3[1] - p2[1], p3[0] - p2[0]);
1090 }
1091 theta = theta1 - theta2;
1092 if (theta > PI) {
1093 theta -= 2*PI;
1094 } else if (theta < -PI) {
1095 theta += 2*PI;
1096 }
1097 if ((theta < elevenDegrees) && (theta > -elevenDegrees)) {
1098 return 0;
1099 }
1100 dist = 0.5*width/sin(0.5*theta);
1101 if (dist < 0.0) {
1102 dist = -dist;
1103 }
1104
1105 /*
1106 * Compute theta3 (make sure that it points to the left when
1107 * looking from p1 to p2).
1108 */
1109
1110 theta3 = (theta1 + theta2)/2.0;
1111 if (sin(theta3 - (theta1 + PI)) < 0.0) {
1112 theta3 += PI;
1113 }
1114 deltaX = dist*cos(theta3);
1115 m1[0] = p2[0] + deltaX;
1116 m2[0] = p2[0] - deltaX;
1117 deltaY = dist*sin(theta3);
1118 m1[1] = p2[1] + deltaY;
1119 m2[1] = p2[1] - deltaY;
1120 return 1;
1121 }
1122 \f
1123 /*
1124 *--------------------------------------------------------------
1125 *
1126 * TkGetButtPoints --
1127 *
1128 * Given two points forming a line segment, compute the
1129 * coordinates of two endpoints of a rectangle formed by
1130 * bloating the line segment until it is width units wide.
1131 *
1132 * Results:
1133 * There is no return value. M1 and m2 are filled in to
1134 * correspond to m1 and m2 in the diagram below:
1135 *
1136 * ----------------* m1
1137 * |
1138 * p1 *---------------* p2
1139 * |
1140 * ----------------* m2
1141 *
1142 * M1 and m2 will be W units apart, with p2 centered between
1143 * them and m1-m2 perpendicular to p1-p2. However, if
1144 * "project" is true then m1 and m2 will be as follows:
1145 *
1146 * -------------------* m1
1147 * p2 |
1148 * p1 *---------------* |
1149 * |
1150 * -------------------* m2
1151 *
1152 * In this case p2 will be width/2 units from the segment m1-m2.
1153 *
1154 * Side effects:
1155 * None.
1156 *
1157 *--------------------------------------------------------------
1158 */
1159
1160 void
1161 TkGetButtPoints (
1162 double p1[], /* Points to x- and y-coordinates of point
1163 * before vertex. */
1164 double p2[], /* Points to x- and y-coordinates of vertex
1165 * for mitered joint. */
1166 double width, /* Width of line. */
1167 int project, /* Non-zero means project p2 by an additional
1168 * width/2 before computing m1 and m2. */
1169 double m1[], /* Points to place to put "left" result
1170 * point, as you face from p1 to p2. */
1171 double m2[] /* Points to place to put "right" result
1172 * point. */
1173 )
1174 {
1175 double length; /* Length of p1-p2 segment. */
1176 double deltaX, deltaY; /* Increments in coords. */
1177
1178 width *= 0.5;
1179 length = hypot(p2[0] - p1[0], p2[1] - p1[1]);
1180 if (length == 0.0) {
1181 m1[0] = m2[0] = p2[0];
1182 m1[1] = m2[1] = p2[1];
1183 } else {
1184 deltaX = -width * (p2[1] - p1[1]) / length;
1185 deltaY = width * (p2[0] - p1[0]) / length;
1186 m1[0] = p2[0] + deltaX;
1187 m2[0] = p2[0] - deltaX;
1188 m1[1] = p2[1] + deltaY;
1189 m2[1] = p2[1] - deltaY;
1190 if (project) {
1191 m1[0] += deltaY;
1192 m2[0] += deltaY;
1193 m1[1] -= deltaX;
1194 m2[1] -= deltaX;
1195 }
1196 }
1197 }
Impressum, Datenschutz