Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkCanvArc.c
1810 views
1
/*
2
* tkCanvArc.c --
3
*
4
* This file implements arc items for canvas widgets.
5
*
6
* Copyright (c) 1992-1994 The Regents of the University of California.
7
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
8
*
9
* See the file "license.terms" for information on usage and redistribution
10
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
*
12
* SCCS: @(#) tkCanvArc.c 1.32 96/02/17 16:59:09
13
*/
14
15
#include "tkInt.h"
16
17
/*
18
* The structure below defines the record for each arc item.
19
*/
20
21
typedef struct ArcItem {
22
Tk_Item header; /* Generic stuff that's the same for all
23
* types. MUST BE FIRST IN STRUCTURE. */
24
double bbox[4]; /* Coordinates (x1, y1, x2, y2) of bounding
25
* box for oval of which arc is a piece. */
26
double start; /* Angle at which arc begins, in degrees
27
* between 0 and 360. */
28
double extent; /* Extent of arc (angular distance from
29
* start to end of arc) in degrees between
30
* -360 and 360. */
31
double *outlinePtr; /* Points to (x,y) coordinates for points
32
* that define one or two closed polygons
33
* representing the portion of the outline
34
* that isn't part of the arc (the V-shape
35
* for a pie slice or a line-like segment
36
* for a chord). Malloc'ed. */
37
int numOutlinePoints; /* Number of points at outlinePtr. Zero
38
* means no space allocated. */
39
int width; /* Width of outline (in pixels). */
40
XColor *outlineColor; /* Color for outline. NULL means don't
41
* draw outline. */
42
XColor *fillColor; /* Color for filling arc (used for drawing
43
* outline too when style is "arc"). NULL
44
* means don't fill arc. */
45
Pixmap fillStipple; /* Stipple bitmap for filling item. */
46
Pixmap outlineStipple; /* Stipple bitmap for outline. */
47
Tk_Uid style; /* How to draw arc: arc, chord, or pieslice. */
48
GC outlineGC; /* Graphics context for outline. */
49
GC fillGC; /* Graphics context for filling item. */
50
double center1[2]; /* Coordinates of center of arc outline at
51
* start (see ComputeArcOutline). */
52
double center2[2]; /* Coordinates of center of arc outline at
53
* start+extent (see ComputeArcOutline). */
54
} ArcItem;
55
56
/*
57
* The definitions below define the sizes of the polygons used to
58
* display outline information for various styles of arcs:
59
*/
60
61
#define CHORD_OUTLINE_PTS 7
62
#define PIE_OUTLINE1_PTS 6
63
#define PIE_OUTLINE2_PTS 7
64
65
/*
66
* Information used for parsing configuration specs:
67
*/
68
69
static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
70
Tk_CanvasTagsPrintProc, (ClientData) NULL
71
};
72
73
static Tk_ConfigSpec configSpecs[] = {
74
{TK_CONFIG_DOUBLE, "-extent", (char *) NULL, (char *) NULL,
75
"90", Tk_Offset(ArcItem, extent), TK_CONFIG_DONT_SET_DEFAULT},
76
{TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
77
(char *) NULL, Tk_Offset(ArcItem, fillColor), TK_CONFIG_NULL_OK},
78
{TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL,
79
"black", Tk_Offset(ArcItem, outlineColor), TK_CONFIG_NULL_OK},
80
{TK_CONFIG_BITMAP, "-outlinestipple", (char *) NULL, (char *) NULL,
81
(char *) NULL, Tk_Offset(ArcItem, outlineStipple), TK_CONFIG_NULL_OK},
82
{TK_CONFIG_DOUBLE, "-start", (char *) NULL, (char *) NULL,
83
"0", Tk_Offset(ArcItem, start), TK_CONFIG_DONT_SET_DEFAULT},
84
{TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
85
(char *) NULL, Tk_Offset(ArcItem, fillStipple), TK_CONFIG_NULL_OK},
86
{TK_CONFIG_UID, "-style", (char *) NULL, (char *) NULL,
87
"pieslice", Tk_Offset(ArcItem, style), TK_CONFIG_DONT_SET_DEFAULT},
88
{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
89
(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
90
{TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
91
"1", Tk_Offset(ArcItem, width), TK_CONFIG_DONT_SET_DEFAULT},
92
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
93
(char *) NULL, 0, 0}
94
};
95
96
/*
97
* Prototypes for procedures defined in this file:
98
*/
99
100
static void ComputeArcBbox _ANSI_ARGS_((Tk_Canvas canvas,
101
ArcItem *arcPtr));
102
static int ConfigureArc _ANSI_ARGS_((Tcl_Interp *interp,
103
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
104
char **argv, int flags));
105
static int CreateArc _ANSI_ARGS_((Tcl_Interp *interp,
106
Tk_Canvas canvas, struct Tk_Item *itemPtr,
107
int argc, char **argv));
108
static void DeleteArc _ANSI_ARGS_((Tk_Canvas canvas,
109
Tk_Item *itemPtr, Display *display));
110
static void DisplayArc _ANSI_ARGS_((Tk_Canvas canvas,
111
Tk_Item *itemPtr, Display *display, Drawable dst,
112
int x, int y, int width, int height));
113
static int ArcCoords _ANSI_ARGS_((Tcl_Interp *interp,
114
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
115
char **argv));
116
static int ArcToArea _ANSI_ARGS_((Tk_Canvas canvas,
117
Tk_Item *itemPtr, double *rectPtr));
118
static double ArcToPoint _ANSI_ARGS_((Tk_Canvas canvas,
119
Tk_Item *itemPtr, double *coordPtr));
120
static int ArcToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
121
Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
122
static void ScaleArc _ANSI_ARGS_((Tk_Canvas canvas,
123
Tk_Item *itemPtr, double originX, double originY,
124
double scaleX, double scaleY));
125
static void TranslateArc _ANSI_ARGS_((Tk_Canvas canvas,
126
Tk_Item *itemPtr, double deltaX, double deltaY));
127
static int AngleInRange _ANSI_ARGS_((double x, double y,
128
double start, double extent));
129
static void ComputeArcOutline _ANSI_ARGS_((ArcItem *arcPtr));
130
static int HorizLineToArc _ANSI_ARGS_((double x1, double x2,
131
double y, double rx, double ry,
132
double start, double extent));
133
static int VertLineToArc _ANSI_ARGS_((double x, double y1,
134
double y2, double rx, double ry,
135
double start, double extent));
136
137
/*
138
* The structures below defines the arc item types by means of procedures
139
* that can be invoked by generic item code.
140
*/
141
142
Tk_ItemType tkArcType = {
143
"arc", /* name */
144
sizeof(ArcItem), /* itemSize */
145
CreateArc, /* createProc */
146
configSpecs, /* configSpecs */
147
ConfigureArc, /* configureProc */
148
ArcCoords, /* coordProc */
149
DeleteArc, /* deleteProc */
150
DisplayArc, /* displayProc */
151
0, /* alwaysRedraw */
152
ArcToPoint, /* pointProc */
153
ArcToArea, /* areaProc */
154
ArcToPostscript, /* postscriptProc */
155
ScaleArc, /* scaleProc */
156
TranslateArc, /* translateProc */
157
(Tk_ItemIndexProc *) NULL, /* indexProc */
158
(Tk_ItemCursorProc *) NULL, /* icursorProc */
159
(Tk_ItemSelectionProc *) NULL, /* selectionProc */
160
(Tk_ItemInsertProc *) NULL, /* insertProc */
161
(Tk_ItemDCharsProc *) NULL, /* dTextProc */
162
(Tk_ItemType *) NULL /* nextPtr */
163
};
164
165
#ifndef PI
166
# define PI 3.14159265358979323846
167
#endif
168
169
/*
170
* The uid's below comprise the legal values for the "-style"
171
* option for arcs.
172
*/
173
174
static Tk_Uid arcUid = NULL;
175
static Tk_Uid chordUid = NULL;
176
static Tk_Uid pieSliceUid = NULL;
177
178
/*
179
*--------------------------------------------------------------
180
*
181
* CreateArc --
182
*
183
* This procedure is invoked to create a new arc item in
184
* a canvas.
185
*
186
* Results:
187
* A standard Tcl return value. If an error occurred in
188
* creating the item, then an error message is left in
189
* interp->result; in this case itemPtr is
190
* left uninitialized, so it can be safely freed by the
191
* caller.
192
*
193
* Side effects:
194
* A new arc item is created.
195
*
196
*--------------------------------------------------------------
197
*/
198
199
static int
200
CreateArc(interp, canvas, itemPtr, argc, argv)
201
Tcl_Interp *interp; /* Interpreter for error reporting. */
202
Tk_Canvas canvas; /* Canvas to hold new item. */
203
Tk_Item *itemPtr; /* Record to hold new item; header
204
* has been initialized by caller. */
205
int argc; /* Number of arguments in argv. */
206
char **argv; /* Arguments describing arc. */
207
{
208
ArcItem *arcPtr = (ArcItem *) itemPtr;
209
210
if (argc < 4) {
211
Tcl_AppendResult(interp, "wrong # args: should be \"",
212
Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
213
itemPtr->typePtr->name, " x1 y1 x2 y2 ?options?\"",
214
(char *) NULL);
215
return TCL_ERROR;
216
}
217
218
/*
219
* Carry out once-only initialization.
220
*/
221
222
if (arcUid == NULL) {
223
arcUid = Tk_GetUid("arc");
224
chordUid = Tk_GetUid("chord");
225
pieSliceUid = Tk_GetUid("pieslice");
226
}
227
228
/*
229
* Carry out initialization that is needed in order to clean
230
* up after errors during the the remainder of this procedure.
231
*/
232
233
arcPtr->start = 0;
234
arcPtr->extent = 90;
235
arcPtr->outlinePtr = NULL;
236
arcPtr->numOutlinePoints = 0;
237
arcPtr->width = 1;
238
arcPtr->outlineColor = NULL;
239
arcPtr->fillColor = NULL;
240
arcPtr->fillStipple = None;
241
arcPtr->outlineStipple = None;
242
arcPtr->style = pieSliceUid;
243
arcPtr->outlineGC = None;
244
arcPtr->fillGC = None;
245
246
/*
247
* Process the arguments to fill in the item record.
248
*/
249
250
if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &arcPtr->bbox[0]) != TCL_OK)
251
|| (Tk_CanvasGetCoord(interp, canvas, argv[1],
252
&arcPtr->bbox[1]) != TCL_OK)
253
|| (Tk_CanvasGetCoord(interp, canvas, argv[2],
254
&arcPtr->bbox[2]) != TCL_OK)
255
|| (Tk_CanvasGetCoord(interp, canvas, argv[3],
256
&arcPtr->bbox[3]) != TCL_OK)) {
257
return TCL_ERROR;
258
}
259
260
if (ConfigureArc(interp, canvas, itemPtr, argc-4, argv+4, 0) != TCL_OK) {
261
DeleteArc(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
262
return TCL_ERROR;
263
}
264
return TCL_OK;
265
}
266
267
/*
268
*--------------------------------------------------------------
269
*
270
* ArcCoords --
271
*
272
* This procedure is invoked to process the "coords" widget
273
* command on arcs. See the user documentation for details
274
* on what it does.
275
*
276
* Results:
277
* Returns TCL_OK or TCL_ERROR, and sets interp->result.
278
*
279
* Side effects:
280
* The coordinates for the given item may be changed.
281
*
282
*--------------------------------------------------------------
283
*/
284
285
static int
286
ArcCoords(interp, canvas, itemPtr, argc, argv)
287
Tcl_Interp *interp; /* Used for error reporting. */
288
Tk_Canvas canvas; /* Canvas containing item. */
289
Tk_Item *itemPtr; /* Item whose coordinates are to be
290
* read or modified. */
291
int argc; /* Number of coordinates supplied in
292
* argv. */
293
char **argv; /* Array of coordinates: x1, y1,
294
* x2, y2, ... */
295
{
296
ArcItem *arcPtr = (ArcItem *) itemPtr;
297
char c0[TCL_DOUBLE_SPACE], c1[TCL_DOUBLE_SPACE];
298
char c2[TCL_DOUBLE_SPACE], c3[TCL_DOUBLE_SPACE];
299
300
if (argc == 0) {
301
Tcl_PrintDouble(interp, arcPtr->bbox[0], c0);
302
Tcl_PrintDouble(interp, arcPtr->bbox[1], c1);
303
Tcl_PrintDouble(interp, arcPtr->bbox[2], c2);
304
Tcl_PrintDouble(interp, arcPtr->bbox[3], c3);
305
Tcl_AppendResult(interp, c0, " ", c1, " ", c2, " ", c3,
306
(char *) NULL);
307
} else if (argc == 4) {
308
if ((Tk_CanvasGetCoord(interp, canvas, argv[0],
309
&arcPtr->bbox[0]) != TCL_OK)
310
|| (Tk_CanvasGetCoord(interp, canvas, argv[1],
311
&arcPtr->bbox[1]) != TCL_OK)
312
|| (Tk_CanvasGetCoord(interp, canvas, argv[2],
313
&arcPtr->bbox[2]) != TCL_OK)
314
|| (Tk_CanvasGetCoord(interp, canvas, argv[3],
315
&arcPtr->bbox[3]) != TCL_OK)) {
316
return TCL_ERROR;
317
}
318
ComputeArcBbox(canvas, arcPtr);
319
} else {
320
sprintf(interp->result,
321
"wrong # coordinates: expected 0 or 4, got %d",
322
argc);
323
return TCL_ERROR;
324
}
325
return TCL_OK;
326
}
327
328
/*
329
*--------------------------------------------------------------
330
*
331
* ConfigureArc --
332
*
333
* This procedure is invoked to configure various aspects
334
* of a arc item, such as its outline and fill colors.
335
*
336
* Results:
337
* A standard Tcl result code. If an error occurs, then
338
* an error message is left in interp->result.
339
*
340
* Side effects:
341
* Configuration information, such as colors and stipple
342
* patterns, may be set for itemPtr.
343
*
344
*--------------------------------------------------------------
345
*/
346
347
static int
348
ConfigureArc(interp, canvas, itemPtr, argc, argv, flags)
349
Tcl_Interp *interp; /* Used for error reporting. */
350
Tk_Canvas canvas; /* Canvas containing itemPtr. */
351
Tk_Item *itemPtr; /* Arc item to reconfigure. */
352
int argc; /* Number of elements in argv. */
353
char **argv; /* Arguments describing things to configure. */
354
int flags; /* Flags to pass to Tk_ConfigureWidget. */
355
{
356
ArcItem *arcPtr = (ArcItem *) itemPtr;
357
XGCValues gcValues;
358
GC newGC;
359
unsigned long mask;
360
int i;
361
Tk_Window tkwin;
362
363
tkwin = Tk_CanvasTkwin(canvas);
364
if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,
365
(char *) arcPtr, flags) != TCL_OK) {
366
return TCL_ERROR;
367
}
368
369
/*
370
* A few of the options require additional processing, such as
371
* style and graphics contexts.
372
*/
373
374
i = arcPtr->start/360.0;
375
arcPtr->start -= i*360.0;
376
if (arcPtr->start < 0) {
377
arcPtr->start += 360.0;
378
}
379
i = arcPtr->extent/360.0;
380
arcPtr->extent -= i*360.0;
381
382
if ((arcPtr->style != arcUid) && (arcPtr->style != chordUid)
383
&& (arcPtr->style != pieSliceUid)) {
384
Tcl_AppendResult(interp, "bad -style option \"",
385
arcPtr->style, "\": must be arc, chord, or pieslice",
386
(char *) NULL);
387
arcPtr->style = pieSliceUid;
388
return TCL_ERROR;
389
}
390
391
if (arcPtr->width < 0) {
392
arcPtr->width = 1;
393
}
394
if (arcPtr->outlineColor == NULL) {
395
newGC = None;
396
} else {
397
gcValues.foreground = arcPtr->outlineColor->pixel;
398
gcValues.cap_style = CapButt;
399
gcValues.line_width = arcPtr->width;
400
mask = GCForeground|GCCapStyle|GCLineWidth;
401
if (arcPtr->outlineStipple != None) {
402
gcValues.stipple = arcPtr->outlineStipple;
403
gcValues.fill_style = FillStippled;
404
mask |= GCStipple|GCFillStyle;
405
}
406
newGC = Tk_GetGC(tkwin, mask, &gcValues);
407
}
408
if (arcPtr->outlineGC != None) {
409
Tk_FreeGC(Tk_Display(tkwin), arcPtr->outlineGC);
410
}
411
arcPtr->outlineGC = newGC;
412
413
if ((arcPtr->fillColor == NULL) || (arcPtr->style == arcUid)) {
414
newGC = None;
415
} else {
416
gcValues.foreground = arcPtr->fillColor->pixel;
417
if (arcPtr->style == chordUid) {
418
gcValues.arc_mode = ArcChord;
419
} else {
420
gcValues.arc_mode = ArcPieSlice;
421
}
422
mask = GCForeground|GCArcMode;
423
if (arcPtr->fillStipple != None) {
424
gcValues.stipple = arcPtr->fillStipple;
425
gcValues.fill_style = FillStippled;
426
mask |= GCStipple|GCFillStyle;
427
}
428
newGC = Tk_GetGC(tkwin, mask, &gcValues);
429
}
430
if (arcPtr->fillGC != None) {
431
Tk_FreeGC(Tk_Display(tkwin), arcPtr->fillGC);
432
}
433
arcPtr->fillGC = newGC;
434
435
ComputeArcBbox(canvas, arcPtr);
436
return TCL_OK;
437
}
438
439
/*
440
*--------------------------------------------------------------
441
*
442
* DeleteArc --
443
*
444
* This procedure is called to clean up the data structure
445
* associated with a arc item.
446
*
447
* Results:
448
* None.
449
*
450
* Side effects:
451
* Resources associated with itemPtr are released.
452
*
453
*--------------------------------------------------------------
454
*/
455
456
static void
457
DeleteArc(canvas, itemPtr, display)
458
Tk_Canvas canvas; /* Info about overall canvas. */
459
Tk_Item *itemPtr; /* Item that is being deleted. */
460
Display *display; /* Display containing window for
461
* canvas. */
462
{
463
ArcItem *arcPtr = (ArcItem *) itemPtr;
464
465
if (arcPtr->numOutlinePoints != 0) {
466
ckfree((char *) arcPtr->outlinePtr);
467
}
468
if (arcPtr->outlineColor != NULL) {
469
Tk_FreeColor(arcPtr->outlineColor);
470
}
471
if (arcPtr->fillColor != NULL) {
472
Tk_FreeColor(arcPtr->fillColor);
473
}
474
if (arcPtr->fillStipple != None) {
475
Tk_FreeBitmap(display, arcPtr->fillStipple);
476
}
477
if (arcPtr->outlineStipple != None) {
478
Tk_FreeBitmap(display, arcPtr->outlineStipple);
479
}
480
if (arcPtr->outlineGC != None) {
481
Tk_FreeGC(display, arcPtr->outlineGC);
482
}
483
if (arcPtr->fillGC != None) {
484
Tk_FreeGC(display, arcPtr->fillGC);
485
}
486
}
487
488
/*
489
*--------------------------------------------------------------
490
*
491
* ComputeArcBbox --
492
*
493
* This procedure is invoked to compute the bounding box of
494
* all the pixels that may be drawn as part of an arc.
495
*
496
* Results:
497
* None.
498
*
499
* Side effects:
500
* The fields x1, y1, x2, and y2 are updated in the header
501
* for itemPtr.
502
*
503
*--------------------------------------------------------------
504
*/
505
506
/* ARGSUSED */
507
static void
508
ComputeArcBbox(canvas, arcPtr)
509
Tk_Canvas canvas; /* Canvas that contains item. */
510
ArcItem *arcPtr; /* Item whose bbox is to be
511
* recomputed. */
512
{
513
double tmp, center[2], point[2];
514
515
/*
516
* Make sure that the first coordinates are the lowest ones.
517
*/
518
519
if (arcPtr->bbox[1] > arcPtr->bbox[3]) {
520
double tmp;
521
tmp = arcPtr->bbox[3];
522
arcPtr->bbox[3] = arcPtr->bbox[1];
523
arcPtr->bbox[1] = tmp;
524
}
525
if (arcPtr->bbox[0] > arcPtr->bbox[2]) {
526
double tmp;
527
tmp = arcPtr->bbox[2];
528
arcPtr->bbox[2] = arcPtr->bbox[0];
529
arcPtr->bbox[0] = tmp;
530
}
531
532
ComputeArcOutline(arcPtr);
533
534
/*
535
* To compute the bounding box, start with the the bbox formed
536
* by the two endpoints of the arc. Then add in the center of
537
* the arc's oval (if relevant) and the 3-o'clock, 6-o'clock,
538
* 9-o'clock, and 12-o'clock positions, if they are relevant.
539
*/
540
541
arcPtr->header.x1 = arcPtr->header.x2 = arcPtr->center1[0];
542
arcPtr->header.y1 = arcPtr->header.y2 = arcPtr->center1[1];
543
TkIncludePoint((Tk_Item *) arcPtr, arcPtr->center2);
544
center[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2;
545
center[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2;
546
if (arcPtr->style != arcUid) {
547
TkIncludePoint((Tk_Item *) arcPtr, center);
548
}
549
550
tmp = -arcPtr->start;
551
if (tmp < 0) {
552
tmp += 360.0;
553
}
554
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
555
point[0] = arcPtr->bbox[2];
556
point[1] = center[1];
557
TkIncludePoint((Tk_Item *) arcPtr, point);
558
}
559
tmp = 90.0 - arcPtr->start;
560
if (tmp < 0) {
561
tmp += 360.0;
562
}
563
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
564
point[0] = center[0];
565
point[1] = arcPtr->bbox[1];
566
TkIncludePoint((Tk_Item *) arcPtr, point);
567
}
568
tmp = 180.0 - arcPtr->start;
569
if (tmp < 0) {
570
tmp += 360.0;
571
}
572
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
573
point[0] = arcPtr->bbox[0];
574
point[1] = center[1];
575
TkIncludePoint((Tk_Item *) arcPtr, point);
576
}
577
tmp = 270.0 - arcPtr->start;
578
if (tmp < 0) {
579
tmp += 360.0;
580
}
581
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
582
point[0] = center[0];
583
point[1] = arcPtr->bbox[3];
584
TkIncludePoint((Tk_Item *) arcPtr, point);
585
}
586
587
/*
588
* Lastly, expand by the width of the arc (if the arc's outline is
589
* being drawn) and add one extra pixel just for safety.
590
*/
591
592
if (arcPtr->outlineColor == NULL) {
593
tmp = 1;
594
} else {
595
tmp = (arcPtr->width + 1)/2 + 1;
596
}
597
arcPtr->header.x1 -= tmp;
598
arcPtr->header.y1 -= tmp;
599
arcPtr->header.x2 += tmp;
600
arcPtr->header.y2 += tmp;
601
}
602
603
/*
604
*--------------------------------------------------------------
605
*
606
* DisplayArc --
607
*
608
* This procedure is invoked to draw an arc item in a given
609
* drawable.
610
*
611
* Results:
612
* None.
613
*
614
* Side effects:
615
* ItemPtr is drawn in drawable using the transformation
616
* information in canvas.
617
*
618
*--------------------------------------------------------------
619
*/
620
621
static void
622
DisplayArc(canvas, itemPtr, display, drawable, x, y, width, height)
623
Tk_Canvas canvas; /* Canvas that contains item. */
624
Tk_Item *itemPtr; /* Item to be displayed. */
625
Display *display; /* Display on which to draw item. */
626
Drawable drawable; /* Pixmap or window in which to draw
627
* item. */
628
int x, y, width, height; /* Describes region of canvas that
629
* must be redisplayed (not used). */
630
{
631
ArcItem *arcPtr = (ArcItem *) itemPtr;
632
short x1, y1, x2, y2;
633
int start, extent;
634
635
/*
636
* Compute the screen coordinates of the bounding box for the item,
637
* plus integer values for the angles.
638
*/
639
640
Tk_CanvasDrawableCoords(canvas, arcPtr->bbox[0], arcPtr->bbox[1],
641
&x1, &y1);
642
Tk_CanvasDrawableCoords(canvas, arcPtr->bbox[2], arcPtr->bbox[3],
643
&x2, &y2);
644
if (x2 <= x1) {
645
x2 = x1+1;
646
}
647
if (y2 <= y1) {
648
y2 = y1+1;
649
}
650
start = (64*arcPtr->start) + 0.5;
651
extent = (64*arcPtr->extent) + 0.5;
652
653
/*
654
* Display filled arc first (if wanted), then outline. If the extent
655
* is zero then don't invoke XFillArc or XDrawArc, since this causes
656
* some window servers to crash and should be a no-op anyway.
657
*/
658
659
if ((arcPtr->fillGC != None) && (extent != 0)) {
660
if (arcPtr->fillStipple != None) {
661
Tk_CanvasSetStippleOrigin(canvas, arcPtr->fillGC);
662
}
663
XFillArc(display, drawable, arcPtr->fillGC, x1, y1, (unsigned) (x2-x1),
664
(unsigned) (y2-y1), start, extent);
665
if (arcPtr->fillStipple != None) {
666
XSetTSOrigin(display, arcPtr->fillGC, 0, 0);
667
}
668
}
669
if (arcPtr->outlineGC != None) {
670
if (arcPtr->outlineStipple != None) {
671
Tk_CanvasSetStippleOrigin(canvas, arcPtr->outlineGC);
672
}
673
if (extent != 0) {
674
XDrawArc(display, drawable, arcPtr->outlineGC, x1, y1,
675
(unsigned) (x2-x1), (unsigned) (y2-y1), start, extent);
676
}
677
678
/*
679
* If the outline width is very thin, don't use polygons to draw
680
* the linear parts of the outline (this often results in nothing
681
* being displayed); just draw lines instead.
682
*/
683
684
if (arcPtr->width <= 2) {
685
Tk_CanvasDrawableCoords(canvas, arcPtr->center1[0],
686
arcPtr->center1[1], &x1, &y1);
687
Tk_CanvasDrawableCoords(canvas, arcPtr->center2[0],
688
arcPtr->center2[1], &x2, &y2);
689
690
if (arcPtr->style == chordUid) {
691
XDrawLine(display, drawable, arcPtr->outlineGC,
692
x1, y1, x2, y2);
693
} else if (arcPtr->style == pieSliceUid) {
694
short cx, cy;
695
696
Tk_CanvasDrawableCoords(canvas,
697
(arcPtr->bbox[0] + arcPtr->bbox[2])/2.0,
698
(arcPtr->bbox[1] + arcPtr->bbox[3])/2.0, &cx, &cy);
699
XDrawLine(display, drawable, arcPtr->outlineGC,
700
cx, cy, x1, y1);
701
XDrawLine(display, drawable, arcPtr->outlineGC,
702
cx, cy, x2, y2);
703
}
704
} else {
705
if (arcPtr->style == chordUid) {
706
TkFillPolygon(canvas, arcPtr->outlinePtr, CHORD_OUTLINE_PTS,
707
display, drawable, arcPtr->outlineGC, None);
708
} else if (arcPtr->style == pieSliceUid) {
709
TkFillPolygon(canvas, arcPtr->outlinePtr, PIE_OUTLINE1_PTS,
710
display, drawable, arcPtr->outlineGC, None);
711
TkFillPolygon(canvas, arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
712
PIE_OUTLINE2_PTS, display, drawable, arcPtr->outlineGC,
713
None);
714
}
715
}
716
if (arcPtr->outlineStipple != None) {
717
XSetTSOrigin(display, arcPtr->outlineGC, 0, 0);
718
}
719
}
720
}
721
722
/*
723
*--------------------------------------------------------------
724
*
725
* ArcToPoint --
726
*
727
* Computes the distance from a given point to a given
728
* arc, in canvas units.
729
*
730
* Results:
731
* The return value is 0 if the point whose x and y coordinates
732
* are coordPtr[0] and coordPtr[1] is inside the arc. If the
733
* point isn't inside the arc then the return value is the
734
* distance from the point to the arc. If itemPtr is filled,
735
* then anywhere in the interior is considered "inside"; if
736
* itemPtr isn't filled, then "inside" means only the area
737
* occupied by the outline.
738
*
739
* Side effects:
740
* None.
741
*
742
*--------------------------------------------------------------
743
*/
744
745
/* ARGSUSED */
746
static double
747
ArcToPoint(canvas, itemPtr, pointPtr)
748
Tk_Canvas canvas; /* Canvas containing item. */
749
Tk_Item *itemPtr; /* Item to check against point. */
750
double *pointPtr; /* Pointer to x and y coordinates. */
751
{
752
ArcItem *arcPtr = (ArcItem *) itemPtr;
753
double vertex[2], pointAngle, diff, dist, newDist;
754
double poly[8], polyDist, width, t1, t2;
755
int filled, angleInRange;
756
757
if ((arcPtr->fillGC != None) || (arcPtr->outlineGC == None)) {
758
filled = 1;
759
} else {
760
filled = 0;
761
}
762
763
/*
764
* See if the point is within the angular range of the arc.
765
* Remember, X angles are backwards from the way we'd normally
766
* think of them. Also, compensate for any eccentricity of
767
* the oval.
768
*/
769
770
vertex[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0;
771
vertex[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0;
772
t1 = (pointPtr[1] - vertex[1])/(arcPtr->bbox[3] - arcPtr->bbox[1]);
773
t2 = (pointPtr[0] - vertex[0])/(arcPtr->bbox[2] - arcPtr->bbox[0]);
774
if ((t1 == 0.0) && (t2 == 0.0)) {
775
pointAngle = 0;
776
} else {
777
pointAngle = -atan2(t1, t2)*180/PI;
778
}
779
diff = pointAngle - arcPtr->start;
780
diff -= ((int) (diff/360.0) * 360.0);
781
if (diff < 0) {
782
diff += 360.0;
783
}
784
angleInRange = (diff <= arcPtr->extent) ||
785
((arcPtr->extent < 0) && ((diff - 360.0) >= arcPtr->extent));
786
787
/*
788
* Now perform different tests depending on what kind of arc
789
* we're dealing with.
790
*/
791
792
if (arcPtr->style == arcUid) {
793
if (angleInRange) {
794
return TkOvalToPoint(arcPtr->bbox, (double) arcPtr->width,
795
0, pointPtr);
796
}
797
dist = hypot(pointPtr[0] - arcPtr->center1[0],
798
pointPtr[1] - arcPtr->center1[1]);
799
newDist = hypot(pointPtr[0] - arcPtr->center2[0],
800
pointPtr[1] - arcPtr->center2[1]);
801
if (newDist < dist) {
802
return newDist;
803
}
804
return dist;
805
}
806
807
if ((arcPtr->fillGC != None) || (arcPtr->outlineGC == None)) {
808
filled = 1;
809
} else {
810
filled = 0;
811
}
812
if (arcPtr->outlineGC == None) {
813
width = 0.0;
814
} else {
815
width = arcPtr->width;
816
}
817
818
if (arcPtr->style == pieSliceUid) {
819
if (width > 1.0) {
820
dist = TkPolygonToPoint(arcPtr->outlinePtr, PIE_OUTLINE1_PTS,
821
pointPtr);
822
newDist = TkPolygonToPoint(arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
823
PIE_OUTLINE2_PTS, pointPtr);
824
} else {
825
dist = TkLineToPoint(vertex, arcPtr->center1, pointPtr);
826
newDist = TkLineToPoint(vertex, arcPtr->center2, pointPtr);
827
}
828
if (newDist < dist) {
829
dist = newDist;
830
}
831
if (angleInRange) {
832
newDist = TkOvalToPoint(arcPtr->bbox, width, filled, pointPtr);
833
if (newDist < dist) {
834
dist = newDist;
835
}
836
}
837
return dist;
838
}
839
840
/*
841
* This is a chord-style arc. We have to deal specially with the
842
* triangular piece that represents the difference between a
843
* chord-style arc and a pie-slice arc (for small angles this piece
844
* is excluded here where it would be included for pie slices;
845
* for large angles the piece is included here but would be
846
* excluded for pie slices).
847
*/
848
849
if (width > 1.0) {
850
dist = TkPolygonToPoint(arcPtr->outlinePtr, CHORD_OUTLINE_PTS,
851
pointPtr);
852
} else {
853
dist = TkLineToPoint(arcPtr->center1, arcPtr->center2, pointPtr);
854
}
855
poly[0] = poly[6] = vertex[0];
856
poly[1] = poly[7] = vertex[1];
857
poly[2] = arcPtr->center1[0];
858
poly[3] = arcPtr->center1[1];
859
poly[4] = arcPtr->center2[0];
860
poly[5] = arcPtr->center2[1];
861
polyDist = TkPolygonToPoint(poly, 4, pointPtr);
862
if (angleInRange) {
863
if ((arcPtr->extent < -180.0) || (arcPtr->extent > 180.0)
864
|| (polyDist > 0.0)) {
865
newDist = TkOvalToPoint(arcPtr->bbox, width, filled, pointPtr);
866
if (newDist < dist) {
867
dist = newDist;
868
}
869
}
870
} else {
871
if ((arcPtr->extent < -180.0) || (arcPtr->extent > 180.0)) {
872
if (filled && (polyDist < dist)) {
873
dist = polyDist;
874
}
875
}
876
}
877
return dist;
878
}
879
880
/*
881
*--------------------------------------------------------------
882
*
883
* ArcToArea --
884
*
885
* This procedure is called to determine whether an item
886
* lies entirely inside, entirely outside, or overlapping
887
* a given area.
888
*
889
* Results:
890
* -1 is returned if the item is entirely outside the area
891
* given by rectPtr, 0 if it overlaps, and 1 if it is entirely
892
* inside the given area.
893
*
894
* Side effects:
895
* None.
896
*
897
*--------------------------------------------------------------
898
*/
899
900
/* ARGSUSED */
901
static int
902
ArcToArea(canvas, itemPtr, rectPtr)
903
Tk_Canvas canvas; /* Canvas containing item. */
904
Tk_Item *itemPtr; /* Item to check against arc. */
905
double *rectPtr; /* Pointer to array of four coordinates
906
* (x1, y1, x2, y2) describing rectangular
907
* area. */
908
{
909
ArcItem *arcPtr = (ArcItem *) itemPtr;
910
double rx, ry; /* Radii for transformed oval: these define
911
* an oval centered at the origin. */
912
double tRect[4]; /* Transformed version of x1, y1, x2, y2,
913
* for coord. system where arc is centered
914
* on the origin. */
915
double center[2], width, angle, tmp;
916
double points[20], *pointPtr;
917
int numPoints, filled;
918
int inside; /* Non-zero means every test so far suggests
919
* that arc is inside rectangle. 0 means
920
* every test so far shows arc to be outside
921
* of rectangle. */
922
int newInside;
923
924
if ((arcPtr->fillGC != None) || (arcPtr->outlineGC == None)) {
925
filled = 1;
926
} else {
927
filled = 0;
928
}
929
if (arcPtr->outlineGC == None) {
930
width = 0.0;
931
} else {
932
width = arcPtr->width;
933
}
934
935
/*
936
* Transform both the arc and the rectangle so that the arc's oval
937
* is centered on the origin.
938
*/
939
940
center[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0;
941
center[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0;
942
tRect[0] = rectPtr[0] - center[0];
943
tRect[1] = rectPtr[1] - center[1];
944
tRect[2] = rectPtr[2] - center[0];
945
tRect[3] = rectPtr[3] - center[1];
946
rx = arcPtr->bbox[2] - center[0] + width/2.0;
947
ry = arcPtr->bbox[3] - center[1] + width/2.0;
948
949
/*
950
* Find the extreme points of the arc and see whether these are all
951
* inside the rectangle (in which case we're done), partly in and
952
* partly out (in which case we're done), or all outside (in which
953
* case we have more work to do). The extreme points include the
954
* following, which are checked in order:
955
*
956
* 1. The outside points of the arc, corresponding to start and
957
* extent.
958
* 2. The center of the arc (but only in pie-slice mode).
959
* 3. The 12, 3, 6, and 9-o'clock positions (but only if the arc
960
* includes those angles).
961
*/
962
963
pointPtr = points;
964
numPoints = 0;
965
angle = -arcPtr->start*(PI/180.0);
966
pointPtr[0] = rx*cos(angle);
967
pointPtr[1] = ry*sin(angle);
968
angle += -arcPtr->extent*(PI/180.0);
969
pointPtr[2] = rx*cos(angle);
970
pointPtr[3] = ry*sin(angle);
971
numPoints = 2;
972
pointPtr += 4;
973
974
if ((arcPtr->style == pieSliceUid) && (arcPtr->extent < 180.0)) {
975
pointPtr[0] = 0.0;
976
pointPtr[1] = 0.0;
977
numPoints++;
978
pointPtr += 2;
979
}
980
981
tmp = -arcPtr->start;
982
if (tmp < 0) {
983
tmp += 360.0;
984
}
985
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
986
pointPtr[0] = rx;
987
pointPtr[1] = 0.0;
988
numPoints++;
989
pointPtr += 2;
990
}
991
tmp = 90.0 - arcPtr->start;
992
if (tmp < 0) {
993
tmp += 360.0;
994
}
995
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
996
pointPtr[0] = 0.0;
997
pointPtr[1] = -ry;
998
numPoints++;
999
pointPtr += 2;
1000
}
1001
tmp = 180.0 - arcPtr->start;
1002
if (tmp < 0) {
1003
tmp += 360.0;
1004
}
1005
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
1006
pointPtr[0] = -rx;
1007
pointPtr[1] = 0.0;
1008
numPoints++;
1009
pointPtr += 2;
1010
}
1011
tmp = 270.0 - arcPtr->start;
1012
if (tmp < 0) {
1013
tmp += 360.0;
1014
}
1015
if ((tmp < arcPtr->extent) || ((tmp-360) > arcPtr->extent)) {
1016
pointPtr[0] = 0.0;
1017
pointPtr[1] = ry;
1018
numPoints++;
1019
pointPtr += 2;
1020
}
1021
1022
/*
1023
* Now that we've located the extreme points, loop through them all
1024
* to see which are inside the rectangle.
1025
*/
1026
1027
inside = (points[0] > tRect[0]) && (points[0] < tRect[2])
1028
&& (points[1] > tRect[1]) && (points[1] < tRect[3]);
1029
for (pointPtr = points+2; numPoints > 1; pointPtr += 2, numPoints--) {
1030
newInside = (pointPtr[0] > tRect[0]) && (pointPtr[0] < tRect[2])
1031
&& (pointPtr[1] > tRect[1]) && (pointPtr[1] < tRect[3]);
1032
if (newInside != inside) {
1033
return 0;
1034
}
1035
}
1036
1037
if (inside) {
1038
return 1;
1039
}
1040
1041
/*
1042
* So far, oval appears to be outside rectangle, but can't yet tell
1043
* for sure. Next, test each of the four sides of the rectangle
1044
* against the bounding region for the arc. If any intersections
1045
* are found, then return "overlapping". First, test against the
1046
* polygon(s) forming the sides of a chord or pie-slice.
1047
*/
1048
1049
if (arcPtr->style == pieSliceUid) {
1050
if (width >= 1.0) {
1051
if (TkPolygonToArea(arcPtr->outlinePtr, PIE_OUTLINE1_PTS,
1052
rectPtr) != -1) {
1053
return 0;
1054
}
1055
if (TkPolygonToArea(arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
1056
PIE_OUTLINE2_PTS, rectPtr) != -1) {
1057
return 0;
1058
}
1059
} else {
1060
if ((TkLineToArea(center, arcPtr->center1, rectPtr) != -1) ||
1061
(TkLineToArea(center, arcPtr->center2, rectPtr) != -1)) {
1062
return 0;
1063
}
1064
}
1065
} else if (arcPtr->style == chordUid) {
1066
if (width >= 1.0) {
1067
if (TkPolygonToArea(arcPtr->outlinePtr, CHORD_OUTLINE_PTS,
1068
rectPtr) != -1) {
1069
return 0;
1070
}
1071
} else {
1072
if (TkLineToArea(arcPtr->center1, arcPtr->center2,
1073
rectPtr) != -1) {
1074
return 0;
1075
}
1076
}
1077
}
1078
1079
/*
1080
* Next check for overlap between each of the four sides and the
1081
* outer perimiter of the arc. If the arc isn't filled, then also
1082
* check the inner perimeter of the arc.
1083
*/
1084
1085
if (HorizLineToArc(tRect[0], tRect[2], tRect[1], rx, ry, arcPtr->start,
1086
arcPtr->extent)
1087
|| HorizLineToArc(tRect[0], tRect[2], tRect[3], rx, ry,
1088
arcPtr->start, arcPtr->extent)
1089
|| VertLineToArc(tRect[0], tRect[1], tRect[3], rx, ry,
1090
arcPtr->start, arcPtr->extent)
1091
|| VertLineToArc(tRect[2], tRect[1], tRect[3], rx, ry,
1092
arcPtr->start, arcPtr->extent)) {
1093
return 0;
1094
}
1095
if ((width > 1.0) && !filled) {
1096
rx -= width;
1097
ry -= width;
1098
if (HorizLineToArc(tRect[0], tRect[2], tRect[1], rx, ry, arcPtr->start,
1099
arcPtr->extent)
1100
|| HorizLineToArc(tRect[0], tRect[2], tRect[3], rx, ry,
1101
arcPtr->start, arcPtr->extent)
1102
|| VertLineToArc(tRect[0], tRect[1], tRect[3], rx, ry,
1103
arcPtr->start, arcPtr->extent)
1104
|| VertLineToArc(tRect[2], tRect[1], tRect[3], rx, ry,
1105
arcPtr->start, arcPtr->extent)) {
1106
return 0;
1107
}
1108
}
1109
1110
/*
1111
* The arc still appears to be totally disjoint from the rectangle,
1112
* but it's also possible that the rectangle is totally inside the arc.
1113
* Do one last check, which is to check one point of the rectangle
1114
* to see if it's inside the arc. If it is, we've got overlap. If
1115
* it isn't, the arc's really outside the rectangle.
1116
*/
1117
1118
if (ArcToPoint(canvas, itemPtr, rectPtr) == 0.0) {
1119
return 0;
1120
}
1121
return -1;
1122
}
1123
1124
/*
1125
*--------------------------------------------------------------
1126
*
1127
* ScaleArc --
1128
*
1129
* This procedure is invoked to rescale an arc item.
1130
*
1131
* Results:
1132
* None.
1133
*
1134
* Side effects:
1135
* The arc referred to by itemPtr is rescaled so that the
1136
* following transformation is applied to all point
1137
* coordinates:
1138
* x' = originX + scaleX*(x-originX)
1139
* y' = originY + scaleY*(y-originY)
1140
*
1141
*--------------------------------------------------------------
1142
*/
1143
1144
static void
1145
ScaleArc(canvas, itemPtr, originX, originY, scaleX, scaleY)
1146
Tk_Canvas canvas; /* Canvas containing arc. */
1147
Tk_Item *itemPtr; /* Arc to be scaled. */
1148
double originX, originY; /* Origin about which to scale rect. */
1149
double scaleX; /* Amount to scale in X direction. */
1150
double scaleY; /* Amount to scale in Y direction. */
1151
{
1152
ArcItem *arcPtr = (ArcItem *) itemPtr;
1153
1154
arcPtr->bbox[0] = originX + scaleX*(arcPtr->bbox[0] - originX);
1155
arcPtr->bbox[1] = originY + scaleY*(arcPtr->bbox[1] - originY);
1156
arcPtr->bbox[2] = originX + scaleX*(arcPtr->bbox[2] - originX);
1157
arcPtr->bbox[3] = originY + scaleY*(arcPtr->bbox[3] - originY);
1158
ComputeArcBbox(canvas, arcPtr);
1159
}
1160
1161
/*
1162
*--------------------------------------------------------------
1163
*
1164
* TranslateArc --
1165
*
1166
* This procedure is called to move an arc by a given amount.
1167
*
1168
* Results:
1169
* None.
1170
*
1171
* Side effects:
1172
* The position of the arc is offset by (xDelta, yDelta), and
1173
* the bounding box is updated in the generic part of the item
1174
* structure.
1175
*
1176
*--------------------------------------------------------------
1177
*/
1178
1179
static void
1180
TranslateArc(canvas, itemPtr, deltaX, deltaY)
1181
Tk_Canvas canvas; /* Canvas containing item. */
1182
Tk_Item *itemPtr; /* Item that is being moved. */
1183
double deltaX, deltaY; /* Amount by which item is to be
1184
* moved. */
1185
{
1186
ArcItem *arcPtr = (ArcItem *) itemPtr;
1187
1188
arcPtr->bbox[0] += deltaX;
1189
arcPtr->bbox[1] += deltaY;
1190
arcPtr->bbox[2] += deltaX;
1191
arcPtr->bbox[3] += deltaY;
1192
ComputeArcBbox(canvas, arcPtr);
1193
}
1194
1195
/*
1196
*--------------------------------------------------------------
1197
*
1198
* ComputeArcOutline --
1199
*
1200
* This procedure creates a polygon describing everything in
1201
* the outline for an arc except what's in the curved part.
1202
* For a "pie slice" arc this is a V-shaped chunk, and for
1203
* a "chord" arc this is a linear chunk (with cutaway corners).
1204
* For "arc" arcs, this stuff isn't relevant.
1205
*
1206
* Results:
1207
* None.
1208
*
1209
* Side effects:
1210
* The information at arcPtr->outlinePtr gets modified, and
1211
* storage for arcPtr->outlinePtr may be allocated or freed.
1212
*
1213
*--------------------------------------------------------------
1214
*/
1215
1216
static void
1217
ComputeArcOutline(arcPtr)
1218
ArcItem *arcPtr; /* Information about arc. */
1219
{
1220
double sin1, cos1, sin2, cos2, angle, halfWidth;
1221
double boxWidth, boxHeight;
1222
double vertex[2], corner1[2], corner2[2];
1223
double *outlinePtr;
1224
1225
/*
1226
* Make sure that the outlinePtr array is large enough to hold
1227
* either a chord or pie-slice outline.
1228
*/
1229
1230
if (arcPtr->numOutlinePoints == 0) {
1231
arcPtr->outlinePtr = (double *) ckalloc((unsigned)
1232
(26 * sizeof(double)));
1233
arcPtr->numOutlinePoints = 22;
1234
}
1235
outlinePtr = arcPtr->outlinePtr;
1236
1237
/*
1238
* First compute the two points that lie at the centers of
1239
* the ends of the curved arc segment, which are marked with
1240
* X's in the figure below:
1241
*
1242
*
1243
* * * *
1244
* * *
1245
* * * * *
1246
* * * * *
1247
* * * * *
1248
* X * * X
1249
*
1250
* The code is tricky because the arc can be ovular in shape.
1251
* It computes the position for a unit circle, and then
1252
* scales to fit the shape of the arc's bounding box.
1253
*
1254
* Also, watch out because angles go counter-clockwise like you
1255
* might expect, but the y-coordinate system is inverted. To
1256
* handle this, just negate the angles in all the computations.
1257
*/
1258
1259
boxWidth = arcPtr->bbox[2] - arcPtr->bbox[0];
1260
boxHeight = arcPtr->bbox[3] - arcPtr->bbox[1];
1261
angle = -arcPtr->start*PI/180.0;
1262
sin1 = sin(angle);
1263
cos1 = cos(angle);
1264
angle -= arcPtr->extent*PI/180.0;
1265
sin2 = sin(angle);
1266
cos2 = cos(angle);
1267
vertex[0] = (arcPtr->bbox[0] + arcPtr->bbox[2])/2.0;
1268
vertex[1] = (arcPtr->bbox[1] + arcPtr->bbox[3])/2.0;
1269
arcPtr->center1[0] = vertex[0] + cos1*boxWidth/2.0;
1270
arcPtr->center1[1] = vertex[1] + sin1*boxHeight/2.0;
1271
arcPtr->center2[0] = vertex[0] + cos2*boxWidth/2.0;
1272
arcPtr->center2[1] = vertex[1] + sin2*boxHeight/2.0;
1273
1274
/*
1275
* Next compute the "outermost corners" of the arc, which are
1276
* marked with X's in the figure below:
1277
*
1278
* * * *
1279
* * *
1280
* * * * *
1281
* * * * *
1282
* X * * X
1283
* * *
1284
*
1285
* The code below is tricky because it has to handle eccentricity
1286
* in the shape of the oval. The key in the code below is to
1287
* realize that the slope of the line from arcPtr->center1 to corner1
1288
* is (boxWidth*sin1)/(boxHeight*cos1), and similarly for arcPtr->center2
1289
* and corner2. These formulas can be computed from the formula for
1290
* the oval.
1291
*/
1292
1293
halfWidth = arcPtr->width/2.0;
1294
if (((boxWidth*sin1) == 0.0) && ((boxHeight*cos1) == 0.0)) {
1295
angle = 0.0;
1296
} else {
1297
angle = atan2(boxWidth*sin1, boxHeight*cos1);
1298
}
1299
corner1[0] = arcPtr->center1[0] + cos(angle)*halfWidth;
1300
corner1[1] = arcPtr->center1[1] + sin(angle)*halfWidth;
1301
if (((boxWidth*sin2) == 0.0) && ((boxHeight*cos2) == 0.0)) {
1302
angle = 0.0;
1303
} else {
1304
angle = atan2(boxWidth*sin2, boxHeight*cos2);
1305
}
1306
corner2[0] = arcPtr->center2[0] + cos(angle)*halfWidth;
1307
corner2[1] = arcPtr->center2[1] + sin(angle)*halfWidth;
1308
1309
/*
1310
* For a chord outline, generate a six-sided polygon with three
1311
* points for each end of the chord. The first and third points
1312
* for each end are butt points generated on either side of the
1313
* center point. The second point is the corner point.
1314
*/
1315
1316
if (arcPtr->style == chordUid) {
1317
outlinePtr[0] = outlinePtr[12] = corner1[0];
1318
outlinePtr[1] = outlinePtr[13] = corner1[1];
1319
TkGetButtPoints(arcPtr->center2, arcPtr->center1,
1320
(double) arcPtr->width, 0, outlinePtr+10, outlinePtr+2);
1321
outlinePtr[4] = arcPtr->center2[0] + outlinePtr[2]
1322
- arcPtr->center1[0];
1323
outlinePtr[5] = arcPtr->center2[1] + outlinePtr[3]
1324
- arcPtr->center1[1];
1325
outlinePtr[6] = corner2[0];
1326
outlinePtr[7] = corner2[1];
1327
outlinePtr[8] = arcPtr->center2[0] + outlinePtr[10]
1328
- arcPtr->center1[0];
1329
outlinePtr[9] = arcPtr->center2[1] + outlinePtr[11]
1330
- arcPtr->center1[1];
1331
} else if (arcPtr->style == pieSliceUid) {
1332
/*
1333
* For pie slices, generate two polygons, one for each side
1334
* of the pie slice. The first arm has a shape like this,
1335
* where the center of the oval is X, arcPtr->center1 is at Y, and
1336
* corner1 is at Z:
1337
*
1338
* _____________________
1339
* | \
1340
* | \
1341
* X Y Z
1342
* | /
1343
* |_____________________/
1344
*
1345
*/
1346
1347
TkGetButtPoints(arcPtr->center1, vertex, (double) arcPtr->width, 0,
1348
outlinePtr, outlinePtr+2);
1349
outlinePtr[4] = arcPtr->center1[0] + outlinePtr[2] - vertex[0];
1350
outlinePtr[5] = arcPtr->center1[1] + outlinePtr[3] - vertex[1];
1351
outlinePtr[6] = corner1[0];
1352
outlinePtr[7] = corner1[1];
1353
outlinePtr[8] = arcPtr->center1[0] + outlinePtr[0] - vertex[0];
1354
outlinePtr[9] = arcPtr->center1[1] + outlinePtr[1] - vertex[1];
1355
outlinePtr[10] = outlinePtr[0];
1356
outlinePtr[11] = outlinePtr[1];
1357
1358
/*
1359
* The second arm has a shape like this:
1360
*
1361
*
1362
* ______________________
1363
* / \
1364
* / \
1365
* Z Y X /
1366
* \ /
1367
* \______________________/
1368
*
1369
* Similar to above X is the center of the oval/circle, Y is
1370
* arcPtr->center2, and Z is corner2. The extra jog out to the left
1371
* of X is needed in or to produce a butted joint with the
1372
* first arm; the corner to the right of X is one of the
1373
* first two points of the first arm, depending on extent.
1374
*/
1375
1376
TkGetButtPoints(arcPtr->center2, vertex, (double) arcPtr->width, 0,
1377
outlinePtr+12, outlinePtr+16);
1378
if ((arcPtr->extent > 180) ||
1379
((arcPtr->extent < 0) && (arcPtr->extent > -180))) {
1380
outlinePtr[14] = outlinePtr[0];
1381
outlinePtr[15] = outlinePtr[1];
1382
} else {
1383
outlinePtr[14] = outlinePtr[2];
1384
outlinePtr[15] = outlinePtr[3];
1385
}
1386
outlinePtr[18] = arcPtr->center2[0] + outlinePtr[16] - vertex[0];
1387
outlinePtr[19] = arcPtr->center2[1] + outlinePtr[17] - vertex[1];
1388
outlinePtr[20] = corner2[0];
1389
outlinePtr[21] = corner2[1];
1390
outlinePtr[22] = arcPtr->center2[0] + outlinePtr[12] - vertex[0];
1391
outlinePtr[23] = arcPtr->center2[1] + outlinePtr[13] - vertex[1];
1392
outlinePtr[24] = outlinePtr[12];
1393
outlinePtr[25] = outlinePtr[13];
1394
}
1395
}
1396
1397
/*
1398
*--------------------------------------------------------------
1399
*
1400
* HorizLineToArc --
1401
*
1402
* Determines whether a horizontal line segment intersects
1403
* a given arc.
1404
*
1405
* Results:
1406
* The return value is 1 if the given line intersects the
1407
* infinitely-thin arc section defined by rx, ry, start,
1408
* and extent, and 0 otherwise. Only the perimeter of the
1409
* arc is checked: interior areas (e.g. pie-slice or chord)
1410
* are not checked.
1411
*
1412
* Side effects:
1413
* None.
1414
*
1415
*--------------------------------------------------------------
1416
*/
1417
1418
static int
1419
HorizLineToArc(x1, x2, y, rx, ry, start, extent)
1420
double x1, x2; /* X-coords of endpoints of line segment.
1421
* X1 must be <= x2. */
1422
double y; /* Y-coordinate of line segment. */
1423
double rx, ry; /* These x- and y-radii define an oval
1424
* centered at the origin. */
1425
double start, extent; /* Angles that define extent of arc, in
1426
* the standard fashion for this module. */
1427
{
1428
double tmp;
1429
double tx, ty; /* Coordinates of intersection point in
1430
* transformed coordinate system. */
1431
double x;
1432
1433
/*
1434
* Compute the x-coordinate of one possible intersection point
1435
* between the arc and the line. Use a transformed coordinate
1436
* system where the oval is a unit circle centered at the origin.
1437
* Then scale back to get actual x-coordinate.
1438
*/
1439
1440
ty = y/ry;
1441
tmp = 1 - ty*ty;
1442
if (tmp < 0) {
1443
return 0;
1444
}
1445
tx = sqrt(tmp);
1446
x = tx*rx;
1447
1448
/*
1449
* Test both intersection points.
1450
*/
1451
1452
if ((x >= x1) && (x <= x2) && AngleInRange(tx, ty, start, extent)) {
1453
return 1;
1454
}
1455
if ((-x >= x1) && (-x <= x2) && AngleInRange(-tx, ty, start, extent)) {
1456
return 1;
1457
}
1458
return 0;
1459
}
1460
1461
/*
1462
*--------------------------------------------------------------
1463
*
1464
* VertLineToArc --
1465
*
1466
* Determines whether a vertical line segment intersects
1467
* a given arc.
1468
*
1469
* Results:
1470
* The return value is 1 if the given line intersects the
1471
* infinitely-thin arc section defined by rx, ry, start,
1472
* and extent, and 0 otherwise. Only the perimeter of the
1473
* arc is checked: interior areas (e.g. pie-slice or chord)
1474
* are not checked.
1475
*
1476
* Side effects:
1477
* None.
1478
*
1479
*--------------------------------------------------------------
1480
*/
1481
1482
static int
1483
VertLineToArc(x, y1, y2, rx, ry, start, extent)
1484
double x; /* X-coordinate of line segment. */
1485
double y1, y2; /* Y-coords of endpoints of line segment.
1486
* Y1 must be <= y2. */
1487
double rx, ry; /* These x- and y-radii define an oval
1488
* centered at the origin. */
1489
double start, extent; /* Angles that define extent of arc, in
1490
* the standard fashion for this module. */
1491
{
1492
double tmp;
1493
double tx, ty; /* Coordinates of intersection point in
1494
* transformed coordinate system. */
1495
double y;
1496
1497
/*
1498
* Compute the y-coordinate of one possible intersection point
1499
* between the arc and the line. Use a transformed coordinate
1500
* system where the oval is a unit circle centered at the origin.
1501
* Then scale back to get actual y-coordinate.
1502
*/
1503
1504
tx = x/rx;
1505
tmp = 1 - tx*tx;
1506
if (tmp < 0) {
1507
return 0;
1508
}
1509
ty = sqrt(tmp);
1510
y = ty*ry;
1511
1512
/*
1513
* Test both intersection points.
1514
*/
1515
1516
if ((y > y1) && (y < y2) && AngleInRange(tx, ty, start, extent)) {
1517
return 1;
1518
}
1519
if ((-y > y1) && (-y < y2) && AngleInRange(tx, -ty, start, extent)) {
1520
return 1;
1521
}
1522
return 0;
1523
}
1524
1525
/*
1526
*--------------------------------------------------------------
1527
*
1528
* AngleInRange --
1529
*
1530
* Determine whether the angle from the origin to a given
1531
* point is within a given range.
1532
*
1533
* Results:
1534
* The return value is 1 if the angle from (0,0) to (x,y)
1535
* is in the range given by start and extent, where angles
1536
* are interpreted in the standard way for ovals (meaning
1537
* backwards from normal interpretation). Otherwise the
1538
* return value is 0.
1539
*
1540
* Side effects:
1541
* None.
1542
*
1543
*--------------------------------------------------------------
1544
*/
1545
1546
static int
1547
AngleInRange(x, y, start, extent)
1548
double x, y; /* Coordinate of point; angle measured
1549
* from origin to here, relative to x-axis. */
1550
double start; /* First angle, degrees, >=0, <=360. */
1551
double extent; /* Size of arc in degrees >=-360, <=360. */
1552
{
1553
double diff;
1554
1555
if ((x == 0.0) && (y == 0.0)) {
1556
return 1;
1557
}
1558
diff = -atan2(y, x);
1559
diff = diff*(180.0/PI) - start;
1560
while (diff > 360.0) {
1561
diff -= 360.0;
1562
}
1563
while (diff < 0.0) {
1564
diff += 360.0;
1565
}
1566
if (extent >= 0) {
1567
return diff <= extent;
1568
}
1569
return (diff-360.0) >= extent;
1570
}
1571
1572
/*
1573
*--------------------------------------------------------------
1574
*
1575
* ArcToPostscript --
1576
*
1577
* This procedure is called to generate Postscript for
1578
* arc items.
1579
*
1580
* Results:
1581
* The return value is a standard Tcl result. If an error
1582
* occurs in generating Postscript then an error message is
1583
* left in interp->result, replacing whatever used
1584
* to be there. If no error occurs, then Postscript for the
1585
* item is appended to the result.
1586
*
1587
* Side effects:
1588
* None.
1589
*
1590
*--------------------------------------------------------------
1591
*/
1592
1593
static int
1594
ArcToPostscript(interp, canvas, itemPtr, prepass)
1595
Tcl_Interp *interp; /* Leave Postscript or error message
1596
* here. */
1597
Tk_Canvas canvas; /* Information about overall canvas. */
1598
Tk_Item *itemPtr; /* Item for which Postscript is
1599
* wanted. */
1600
int prepass; /* 1 means this is a prepass to
1601
* collect font information; 0 means
1602
* final Postscript is being created. */
1603
{
1604
ArcItem *arcPtr = (ArcItem *) itemPtr;
1605
char buffer[400];
1606
double y1, y2, ang1, ang2;
1607
1608
y1 = Tk_CanvasPsY(canvas, arcPtr->bbox[1]);
1609
y2 = Tk_CanvasPsY(canvas, arcPtr->bbox[3]);
1610
ang1 = arcPtr->start;
1611
ang2 = ang1 + arcPtr->extent;
1612
if (ang2 < ang1) {
1613
ang1 = ang2;
1614
ang2 = arcPtr->start;
1615
}
1616
1617
/*
1618
* If the arc is filled, output Postscript for the interior region
1619
* of the arc.
1620
*/
1621
1622
if (arcPtr->fillGC != None) {
1623
sprintf(buffer, "matrix currentmatrix\n%.15g %.15g translate %.15g %.15g scale\n",
1624
(arcPtr->bbox[0] + arcPtr->bbox[2])/2, (y1 + y2)/2,
1625
(arcPtr->bbox[2] - arcPtr->bbox[0])/2, (y1 - y2)/2);
1626
Tcl_AppendResult(interp, buffer, (char *) NULL);
1627
if (arcPtr->style == chordUid) {
1628
sprintf(buffer, "0 0 1 %.15g %.15g arc closepath\nsetmatrix\n",
1629
ang1, ang2);
1630
} else {
1631
sprintf(buffer,
1632
"0 0 moveto 0 0 1 %.15g %.15g arc closepath\nsetmatrix\n",
1633
ang1, ang2);
1634
}
1635
Tcl_AppendResult(interp, buffer, (char *) NULL);
1636
if (Tk_CanvasPsColor(interp, canvas, arcPtr->fillColor) != TCL_OK) {
1637
return TCL_ERROR;
1638
};
1639
if (arcPtr->fillStipple != None) {
1640
Tcl_AppendResult(interp, "clip ", (char *) NULL);
1641
if (Tk_CanvasPsStipple(interp, canvas, arcPtr->fillStipple)
1642
!= TCL_OK) {
1643
return TCL_ERROR;
1644
}
1645
if (arcPtr->outlineGC != None) {
1646
Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1647
}
1648
} else {
1649
Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1650
}
1651
}
1652
1653
/*
1654
* If there's an outline for the arc, draw it.
1655
*/
1656
1657
if (arcPtr->outlineGC != None) {
1658
sprintf(buffer, "matrix currentmatrix\n%.15g %.15g translate %.15g %.15g scale\n",
1659
(arcPtr->bbox[0] + arcPtr->bbox[2])/2, (y1 + y2)/2,
1660
(arcPtr->bbox[2] - arcPtr->bbox[0])/2, (y1 - y2)/2);
1661
Tcl_AppendResult(interp, buffer, (char *) NULL);
1662
sprintf(buffer, "0 0 1 %.15g %.15g arc\nsetmatrix\n", ang1, ang2);
1663
Tcl_AppendResult(interp, buffer, (char *) NULL);
1664
sprintf(buffer, "%d setlinewidth\n0 setlinecap\n", arcPtr->width);
1665
Tcl_AppendResult(interp, buffer, (char *) NULL);
1666
if (Tk_CanvasPsColor(interp, canvas, arcPtr->outlineColor)
1667
!= TCL_OK) {
1668
return TCL_ERROR;
1669
}
1670
if (arcPtr->outlineStipple != None) {
1671
Tcl_AppendResult(interp, "StrokeClip ", (char *) NULL);
1672
if (Tk_CanvasPsStipple(interp, canvas,
1673
arcPtr->outlineStipple) != TCL_OK) {
1674
return TCL_ERROR;
1675
}
1676
} else {
1677
Tcl_AppendResult(interp, "stroke\n", (char *) NULL);
1678
}
1679
if (arcPtr->style != arcUid) {
1680
Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1681
if (arcPtr->style == chordUid) {
1682
Tk_CanvasPsPath(interp, canvas, arcPtr->outlinePtr,
1683
CHORD_OUTLINE_PTS);
1684
} else {
1685
Tk_CanvasPsPath(interp, canvas, arcPtr->outlinePtr,
1686
PIE_OUTLINE1_PTS);
1687
if (Tk_CanvasPsColor(interp, canvas, arcPtr->outlineColor)
1688
!= TCL_OK) {
1689
return TCL_ERROR;
1690
}
1691
if (arcPtr->outlineStipple != None) {
1692
Tcl_AppendResult(interp, "clip ", (char *) NULL);
1693
if (Tk_CanvasPsStipple(interp, canvas,
1694
arcPtr->outlineStipple) != TCL_OK) {
1695
return TCL_ERROR;
1696
}
1697
} else {
1698
Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1699
}
1700
Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
1701
Tk_CanvasPsPath(interp, canvas,
1702
arcPtr->outlinePtr + 2*PIE_OUTLINE1_PTS,
1703
PIE_OUTLINE2_PTS);
1704
}
1705
if (Tk_CanvasPsColor(interp, canvas, arcPtr->outlineColor)
1706
!= TCL_OK) {
1707
return TCL_ERROR;
1708
}
1709
if (arcPtr->outlineStipple != None) {
1710
Tcl_AppendResult(interp, "clip ", (char *) NULL);
1711
if (Tk_CanvasPsStipple(interp, canvas,
1712
arcPtr->outlineStipple) != TCL_OK) {
1713
return TCL_ERROR;
1714
}
1715
} else {
1716
Tcl_AppendResult(interp, "fill\n", (char *) NULL);
1717
}
1718
}
1719
}
1720
1721
return TCL_OK;
1722
}
1723
1724