aboutsummaryrefslogtreecommitdiff
path: root/generic/Arc.c
blob: 77e655cfb85d6beb0df851e617f08fb388ad827c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
/*
 * Arc.c -- Implementation of Arc item.
 *
 * Authors		: Patrick Lecoanet.
 * Creation date	: Wed Mar 30 16:24:09 1994
 *
 * $Id$
 */

/*
 *  Copyright (c) 1993 - 1999 CENA, Patrick Lecoanet --
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this code; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include <malloc.h>

#include "Item.h"
#include "Geo.h"
#include "Draw.h"
#include "Types.h"
#include "WidgetInfo.h"
#include "Image.h"


static const char rcsid[] = "$Id$";
static const char compile_id[]="$Compile: " __FILE__ " " __DATE__ " " __TIME__ " $";


/*
 * Bit offset of flags.
 */
#define FILLED_BIT	1	/* If the arc is filled with color/pattern */
#define CLOSED_BIT	2	/* If the arc outline is closed */
#define PIE_SLICE_BIT	4	/* If the arc is closed as a pie slice or a chord */


#define PTS_IN_SHAPE	64
static RadarPoint oval_shape[PTS_IN_SHAPE] = {
  {1.0, 0.0},				/* 0 */
  {0.99518472653, 0.0980171417729},
  {0.980785279837, 0.195090324861},
  {0.956940334469, 0.290284681418},
  {0.923879530291, 0.382683437725},
  {0.88192126093, 0.471396743221},
  {0.831469607468, 0.555570240255},
  {0.773010446922, 0.634393292011},
  {0.707106772982, 0.707106789391},
  {0.634393274074, 0.773010461643},
  {0.555570220961, 0.83146962036},
  {0.471396722756, 0.881921271869},
  {0.382683416286, 0.923879539171},
  {0.290284659212, 0.956940341205},
  {0.195090302102, 0.980785284364},
  {0.0980171186795, 0.995184728805},
  {0.0, 1.0},				/* PI/2 */
  {-0.0980171648663, 0.995184724256},
  {-0.19509034762, 0.98078527531},
  {-0.290284703624, 0.956940327733},
  {-0.382683459163, 0.923879521411},
  {-0.471396763686, 0.881921249991},
  {-0.555570259549, 0.831469594576},
  {-0.634393309949, 0.773010432201},
  {-0.707106805799, 0.707106756574},
  {-0.773010476365, 0.634393256136},
  {-0.831469633252, 0.555570201666},
  {-0.881921282808, 0.471396702291},
  {-0.923879548052, 0.382683394847},
  {-0.956940347941, 0.290284637006},
  {-0.980785288892, 0.195090279343},
  {-0.995184731079, 0.0980170955862},
  {-1.0, 0.0},				/* PI */
  {-0.995184721981, -0.0980171879596},
  {-0.980785270783, -0.195090370379},
  {-0.956940320997, -0.29028472583},
  {-0.923879512531, -0.382683480602},
  {-0.881921239052, -0.471396784151},
  {-0.831469581684, -0.555570278844},
  {-0.77301041748, -0.634393327887},
  {-0.707106740165, -0.707106822208},
  {-0.634393238198, -0.773010491086},
  {-0.555570182372, -0.831469646144},
  {-0.471396681826, -0.881921293746},
  {-0.382683373409, -0.923879556932},
  {-0.2902846148, -0.956940354677},
  {-0.195090256583, -0.980785293419},
  {-0.0980170724928, -0.995184733354},
  {0.0, -1.0},				/* 3*PI/2 */
  {0.098017211053, -0.995184719707},
  {0.195090393139, -0.980785266256},
  {0.290284748036, -0.956940314261},
  {0.382683502041, -0.923879503651},
  {0.471396804617, -0.881921228114},
  {0.555570298138, -0.831469568792},
  {0.634393345825, -0.773010402759},
  {0.707106838616, -0.707106723757},
  {0.773010505807, -0.63439322026},
  {0.831469659036, -0.555570163078},
  {0.881921304685, -0.471396661361},
  {0.923879565812, -0.38268335197},
  {0.956940361414, -0.290284592594},
  {0.980785297946, -0.195090233824},
  {0.995184735628, -0.0980170493994}
};


/*
 **********************************************************************************
 *
 * Specific Arc item record.
 *
 **********************************************************************************
 */
typedef struct _ArcItemStruct {
  ItemStruct	header;
  
  /* Public data */
  RadarPoint	coords[2];
  int		start_angle;
  int		angle_extent;
  Pixmap	line_pattern;
  Pixmap	fill_pattern;
  RadarColor	fill_color;
  RadarColor	line_color;
  int		line_width;
  LineStyle	line_style;
  char		*tile_name;
  unsigned char flags;

  /* Private data */
  RadarPoint	orig;
  RadarPoint	corner;
  RadarPoint	center1;
  RadarPoint	center2;
  RadarImage	tile;
  RadarList	render_shape;
} ArcItemStruct, *ArcItem;


static RadarAttrConfig	arc_attrs[] = {
  { RADAR_CONFIG_BOOL, "-closed", NULL,
    Tk_Offset(ArcItemStruct, flags), CLOSED_BIT, RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_BOOL, "-composerotation", NULL,
    Tk_Offset(ArcItemStruct, header.flags), COMPOSE_ROTATION_BIT,
    RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_BOOL, "-composescale", NULL,
    Tk_Offset(ArcItemStruct, header.flags), COMPOSE_SCALE_BIT,
    RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_ANGLE, "-extent", NULL,
    Tk_Offset(ArcItemStruct, angle_extent), 0, RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_COLOR, "-fillcolor", NULL,
    Tk_Offset(ArcItemStruct, fill_color), 0, RADAR_DRAW_FLAG, False },
  { RADAR_CONFIG_BOOL, "-filled", NULL,
    Tk_Offset(ArcItemStruct, flags), FILLED_BIT, RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_PATTERN, "-fillpattern", NULL,
    Tk_Offset(ArcItemStruct, fill_pattern), 0, RADAR_DRAW_FLAG, False },
  { RADAR_CONFIG_COLOR, "-linecolor", NULL,
    Tk_Offset(ArcItemStruct, line_color), 0,
    RADAR_DRAW_FLAG|RADAR_BORDER_FLAG, False },
  { RADAR_CONFIG_PATTERN, "-linepattern", NULL,
    Tk_Offset(ArcItemStruct, line_pattern), 0, RADAR_DRAW_FLAG, False },
  { RADAR_CONFIG_LINE_STYLE, "-linestyle", NULL,
    Tk_Offset(ArcItemStruct, line_style), 0, RADAR_DRAW_FLAG, False },
  { RADAR_CONFIG_DIM, "-linewidth", NULL,
    Tk_Offset(ArcItemStruct, line_width), 0, RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_BOOL, "-pieslice", NULL,
    Tk_Offset(ArcItemStruct, flags), PIE_SLICE_BIT, RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_PRI, "-priority", NULL,
    Tk_Offset(ArcItemStruct, header.priority), 0,
    RADAR_DRAW_FLAG|RADAR_REPICK_FLAG, False },
  { RADAR_CONFIG_BOOL, "-sensitive", NULL,
    Tk_Offset(ArcItemStruct, header.flags), SENSITIVE_BIT,
    RADAR_REPICK_FLAG, False },
  { RADAR_CONFIG_ANGLE, "-startangle", NULL,
    Tk_Offset(ArcItemStruct, start_angle), 0, RADAR_COORDS_FLAG, False },
  { RADAR_CONFIG_TAGS, "-tags", NULL,
    Tk_Offset(ArcItemStruct, header.tags), 0, 0, False },
  { RADAR_CONFIG_IMAGE, "-tile", NULL,
    Tk_Offset(ArcItemStruct, tile_name), 0,
    RADAR_DRAW_FLAG|RADAR_TILE_FLAG, False },
  { RADAR_CONFIG_BOOL, "-visible", NULL,
    Tk_Offset(ArcItemStruct, header.flags), VISIBLE_BIT,
    RADAR_DRAW_FLAG|RADAR_REPICK_FLAG|RADAR_VIS_FLAG, False },
  
  { RADAR_CONFIG_END, NULL, NULL, 0, 0, 0 }
};


/*
 **********************************************************************************
 *
 * ArcTileChange --
 *
 **********************************************************************************
 */
static void
ArcTileChange(ClientData	client_data,
	      int		x,
	      int		y,
	      int		width,
	      int		height,
	      int		image_width,
	      int		image_height)
{
  ArcItem	arc = (ArcItem) client_data;

  InvalidateImage(arc->tile);
  ITEM.Invalidate((Item) arc, RADAR_COORDS_FLAG);
}


/*
 **********************************************************************************
 *
 * Init --
 *
 **********************************************************************************
 */
static int
Init(Item	item,
     int	*argc,
     Arg	**args)
{
  WidgetInfo	*wi = item->wi;
  ArcItem	arc = (ArcItem) item;
  Arg		*elems;
  int		result, num_elems;
#ifdef PTK
  LangFreeProc *freeProc = NULL;
#endif

  /* Init attributes */
  SET(item->flags, VISIBLE_BIT);
  SET(item->flags, SENSITIVE_BIT);
  SET(item->flags, COMPOSE_ROTATION_BIT);
  SET(item->flags, COMPOSE_SCALE_BIT);
  item->priority = DEFAULT_ARC_PRIORITY;

  arc->start_angle = 0;
  arc->angle_extent = 360;
  CLEAR(arc->flags, FILLED_BIT);
  CLEAR(arc->flags, CLOSED_BIT);
  CLEAR(arc->flags, PIE_SLICE_BIT);
  arc->line_pattern = RadarUnspecifiedPattern;
  arc->fill_pattern = RadarUnspecifiedPattern;
  arc->line_style = LINE_SIMPLE;
  arc->line_width = 1;
  arc->tile_name = "";
  arc->tile = RadarUnspecifiedImage;
  arc->render_shape = NULL;
  
  if (*argc < 1) {
    Tcl_AppendResult(wi->interp, " arc coords expected", NULL);
    return RADAR_ERROR;
  }
  result = Lang_SplitList(wi->interp, (*args)[0], &num_elems, &elems, &freeProc);
  if ((result == RADAR_ERROR) || (num_elems != 4)) {
  arc_error:
#ifdef PTK
    if (elems != NULL && freeProc) {
      (*freeProc)(num_elems, elems);
    }
#endif
    Tcl_AppendResult(wi->interp, " malformed arc coords", NULL);
    return RADAR_ERROR;
  }
  if (Tcl_GetDouble(wi->interp, elems[0], &arc->coords[0].x) == RADAR_ERROR) {
  arc_error2:
#ifndef PTK
    Tcl_Free((char *) elems);
#endif
    goto arc_error;
  };
  if (Tcl_GetDouble(wi->interp, elems[1], &arc->coords[0].y) == RADAR_ERROR) {
    goto arc_error2;
  };
  if (Tcl_GetDouble(wi->interp, elems[2], &arc->coords[1].x) == RADAR_ERROR) {
    goto arc_error2;
  };
  if (Tcl_GetDouble(wi->interp, elems[3], &arc->coords[1].y) == RADAR_ERROR) {
    goto arc_error2;
  };
  (*args)++;
  (*argc)--;
#ifndef PTK
  Tcl_Free((char *) elems);
#else
  if (freeProc) {
    (*freeProc)(num_elems, elems);
  }
#endif

  arc->fill_color = RadarGetColorByValue(wi->win, wi->fore_color);
  arc->line_color = RadarGetColorByValue(wi->win, wi->fore_color);

  return RADAR_OK;
}


/*
 **********************************************************************************
 *
 * Clone --
 *
 **********************************************************************************
 */
static void
Clone(Item	item)
{
  ArcItem	arc = (ArcItem) item;
  WidgetInfo	*wi = item->wi;
  char		*text;

  if (strlen(arc->tile_name) != 0) {
    text = RadarMalloc((strlen(arc->tile_name) + 1) * sizeof(char));
    strcpy(text, arc->tile_name);
    arc->tile_name = text;
    arc->tile = Tk_GetImage(wi->interp, wi->win, arc->tile_name,
			    ArcTileChange, (ClientData) arc);
  }
  if (arc->line_pattern != RadarUnspecifiedPattern) {
    arc->line_pattern = Tk_GetBitmap(wi->interp, wi->win,
				     Tk_NameOfBitmap(wi->dpy, arc->line_pattern));
  }
  if (arc->fill_pattern != RadarUnspecifiedPattern) {
    arc->fill_pattern = Tk_GetBitmap(wi->interp, wi->win,
				     Tk_NameOfBitmap(wi->dpy, arc->fill_pattern));
  }
  arc->line_color = RadarGetColorByValue(wi->win, arc->line_color);
  arc->fill_color = RadarGetColorByValue(wi->win, arc->fill_color);
  arc->render_shape = NULL;
}


/*
 **********************************************************************************
 *
 * Destroy --
 *
 **********************************************************************************
 */
static void
Destroy(Item	item)
{
  WidgetInfo	*wi = item->wi;
  ArcItem	arc = (ArcItem) item;

  if (arc->render_shape) {
    RadarListFree(arc->render_shape);
  }
  if (strlen(arc->tile_name) != 0) {
    RadarFree(arc->tile_name);
  }
  if (arc->tile != RadarUnspecifiedImage) {
    Tk_FreeImage(arc->tile);
    arc->tile = RadarUnspecifiedImage;
  }
  if (arc->line_pattern != RadarUnspecifiedPattern) {
    Tk_FreeBitmap(wi->dpy, arc->line_pattern);
  }
  if (arc->fill_pattern != RadarUnspecifiedPattern) {
    Tk_FreeBitmap(wi->dpy, arc->fill_pattern);
  }
  RadarFreeColor(arc->fill_color);
  RadarFreeColor(arc->line_color);
}


/*
 **********************************************************************************
 *
 * Configure --
 *
 **********************************************************************************
 */
static int
Configure(Item		item,
	  int		argc,
	  RadarAttrList	argv,
	  int		*flags)
{
  WidgetInfo	*wi = item->wi;
  ArcItem	arc = (ArcItem) item;

  if (ITEM_P.ConfigureAttributes((char *) item, -1, argc, argv, flags) == RADAR_ERROR) {
    return RADAR_ERROR;
  }  

  if (ISSET(*flags, RADAR_TILE_FLAG)) {
    Tk_Image	tile;

    if (strcmp(arc->tile_name, "") != 0) {
      tile = Tk_GetImage(wi->interp, wi->win, arc->tile_name,
			  ArcTileChange, (ClientData) arc);
      if (tile == NULL) {
	/*
	 * The name will not be in sync with the image in
	 * this case.
	 */
	return RADAR_ERROR;
      }
    }
    else {
      tile = RadarUnspecifiedImage;
    }
    if (arc->tile != RadarUnspecifiedImage) {
      Tk_FreeImage(arc->tile);
    }
    arc->tile = tile;
  }

  return RADAR_OK;
}


/*
 **********************************************************************************
 *
 * Query --
 *
 **********************************************************************************
 */
static int
Query(Item		item,
      int		argc,
      RadarAttrList	argv)
{
  if (ITEM_P.QueryAttribute((char *) item, -1, argv[0]) == RADAR_ERROR) {
    return RADAR_ERROR;
  }  

  return RADAR_OK;
}


/*
 **********************************************************************************
 *
 * ComputeCoordinates --
 *
 **********************************************************************************
 */
static void
ComputeCoordinates(Item		item,
		   RadarBool	force)
{
  WidgetInfo	*wi = item->wi;
  ArcItem	arc = (ArcItem) item;
  RadarReal	angle, sin1, cos1, sin2, cos2;
  int		tmp, w_2, h_2, center_x, center_y,i;
  RadarPoint	*p_list, p;
  RadarReal	width_2, height_2, ox, oy;
  
  ResetBBox(&item->item_bounding_box);
  /*
   * If it is neither filled nor outlined, then nothing to show.
   */
  if (!arc->line_width && ISCLEAR(arc->flags, FILLED_BIT)) {
    return;
  }

  /*
   * Special casing for rotation. This should eventually be the
   * only code but it lacks the arc capability. 
   */
  RadarTransfoDecompose(wi->current_transfo, NULL, NULL, &angle, NULL);
  if (angle >= PRECISION_LIMIT) {
    if (!arc->render_shape) {
      arc->render_shape = RadarListNew(PTS_IN_SHAPE, sizeof(RadarPoint));
      RadarListAssertSize(arc->render_shape, PTS_IN_SHAPE);
    }
    p_list = (RadarPoint *) RadarListArray(arc->render_shape);
    width_2 = (arc->coords[1].x - arc->coords[0].x)/2.0;
    height_2 = (arc->coords[1].y - arc->coords[0].y)/2.0;
    ox = (arc->coords[1].x + arc->coords[0].x)/2.0;
    oy = (arc->coords[1].y + arc->coords[0].y)/2.0;
    for (i = 0; i < PTS_IN_SHAPE; i++, p_list++) {
      p.x = ox + oval_shape[i].x*width_2;
      p.y = oy + oval_shape[i].y*height_2;
      RadarTransformPoint(wi->current_transfo, &p, p_list);
      AddPointToBBox(&item->item_bounding_box, p_list->x, p_list->y);
    }

    tmp = (arc->line_width + 1) / 2 + 1;
    item->item_bounding_box.orig.x -= tmp;
    item->item_bounding_box.orig.y -= tmp;
    item->item_bounding_box.corner.x += tmp;
    item->item_bounding_box.corner.y += tmp;
    return;
  }

  RadarTransformPoint(wi->current_transfo, &arc->coords[0], &arc->orig);
  RadarTransformPoint(wi->current_transfo, &arc->coords[1], &arc->corner);

  if (arc->orig.x > arc->corner.x) {
    tmp = arc->orig.x;
    arc->orig.x = arc->corner.x;
    arc->corner.x = tmp;
  }
  if (arc->orig.y > arc->corner.y) {
    tmp = arc->orig.y;
    arc->orig.y = arc->corner.y;
    arc->corner.y = tmp;
  }

  /*
   * now compute the two points at the centers of the ends of the arc.
   * We first compute the position for a unit circle and then scale
   * to fit the shape.
   * Angles are running counter clockwise and y coordinates are inverted.
   * To handle these, the angles are negated.
   */
  angle = -(arc->start_angle * M_PI / 180.0);
  sin1 = sin(angle);
  cos1 = cos(angle);
  angle -= (arc->angle_extent * M_PI / 180.0);
  sin2 = sin(angle);
  cos2 = cos(angle);
  
  w_2 = (arc->corner.x - arc->orig.x) / 2;
  h_2 = (arc->corner.y - arc->orig.y) / 2;
  center_x = (arc->corner.x + arc->orig.x) / 2;
  center_y = (arc->corner.y + arc->orig.y) / 2;

  arc->center1.x = center_x + REAL_TO_INT(cos1*w_2);
  arc->center1.y = center_y + REAL_TO_INT(sin1*h_2);
  arc->center2.x = center_x + REAL_TO_INT(cos2*w_2);
  arc->center2.y = center_y + REAL_TO_INT(sin2*h_2);
  
  /*
   * Add the ends centers to the bbox.
   */
  AddPointToBBox(&item->item_bounding_box, arc->center1.x, arc->center1.y);
  AddPointToBBox(&item->item_bounding_box, arc->center2.x, arc->center2.y);
  
  /*
   * If the arc is filled or if the outline is closed in pie slice,
   * add the center of the arc.
   */
  if ((ISSET(arc->flags, FILLED_BIT) || ISSET(arc->flags, CLOSED_BIT)) &&
      ISSET(arc->flags, PIE_SLICE_BIT)) {
    AddPointToBBox(&item->item_bounding_box, center_x, center_y);
  }
  
  /*
   * Then add the 3-o'clock, 6-o'clock, 9-o'clock, 12-o'clock position
   * as required.
   */
  tmp = -arc->start_angle;
  if (tmp < 0) {
    tmp += 360;
  }
  if ((tmp < arc->angle_extent) || ((tmp - 360) > arc->angle_extent)) {
    AddPointToBBox(&item->item_bounding_box, arc->corner.x, center_y);
  }

  tmp = 90 - arc->start_angle;
  if (tmp < 0) {
    tmp += 360;
  }
  if ((tmp < arc->angle_extent) || ((tmp - 360) > arc->angle_extent)) {
    AddPointToBBox(&item->item_bounding_box, center_x, arc->orig.y);
  }
  
  tmp = 180 - arc->start_angle;
  if (tmp < 0) {
    tmp += 360;
  }
  if ((tmp < arc->angle_extent) || ((tmp - 360) > arc->angle_extent)) {
    AddPointToBBox(&item->item_bounding_box, arc->orig.x, center_y);
  }
  
  tmp = 270 - arc->start_angle;
  if (tmp < 0) {
    tmp += 360;
  }
  if ((tmp < arc->angle_extent) || ((tmp - 360) > arc->angle_extent)) {
    AddPointToBBox(&item->item_bounding_box, center_x, arc->corner.y);
  }

  /*
   * Now take care of the arc outline width plus one pixel of margin.
   */
  tmp = (arc->line_width + 1) / 2 + 1;
  item->item_bounding_box.orig.x -= tmp;
  item->item_bounding_box.orig.y -= tmp;
  item->item_bounding_box.corner.x += tmp;
  item->item_bounding_box.corner.y += tmp;
}


/*
 **********************************************************************************
 *
 * ToArea --
 *	Tell if the object is entirely outside (-1),
 *	entirely inside (1) or in between (0).
 *
 **********************************************************************************
 */
static int
ToArea(Item		item,
       RadarBBox	*area,
       Tk_Uid		tag_uid,
       int		enclosed,
       RadarBool	report)
{
  ArcItem	arc = (ArcItem) item;
  RadarPoint	center;
  RadarBBox	tran_rect;
  int		rx, ry;
  int		lw = arc->line_width;

  /*
   * Translate both the arc and the area so that they are
   * centered on the origin. rx,ry are the radiuses in x and y.
   */
  center.x = (arc->corner.x + arc->orig.x) / 2;
  center.y = (arc->corner.y + arc->orig.y) / 2;
  tran_rect.orig.x = area->orig.x - center.x;
  tran_rect.orig.y = area->orig.y - center.y;
  rx = arc->corner.x - center.x + lw/2;
  ry = arc->corner.y - center.y + lw/2;
  
  return -1;
}


/*
 **********************************************************************************
 *
 * Draw --
 *
 **********************************************************************************
 */
static void
Draw(Item	item)
{
  WidgetInfo	*wi = item->wi;
  ArcItem	arc = (ArcItem) item;
  XGCValues  	values;
  int		width, height;
  RadarPoint	*p;
  XPoint	xp[PTS_IN_SHAPE+1];
  int		num_points, i;
  
  width = arc->corner.x - arc->orig.x;
  height = arc->corner.y - arc->orig.y;

  if (arc->render_shape &&
      (ISSET(arc->flags, FILLED_BIT) || (arc->line_width))) {
    p = (RadarPoint *) RadarListArray(arc->render_shape);
    num_points = RadarListSize(arc->render_shape);
    for (i = 0; i < num_points; i++, p++) {
      xp[i].x = (short) p->x;
      xp[i].y = (short) p->y;
    }
    xp[i] = xp[0];
    num_points++;
  }
  
  /* Fill if requested */
  if (ISSET(arc->flags, FILLED_BIT)) {
    values.foreground = RadarPixel(arc->fill_color);
    values.arc_mode = ISSET(arc->flags, PIE_SLICE_BIT) ? ArcPieSlice : ArcChord;
    if (arc->tile != RadarUnspecifiedImage) { /* Fill Tiled */
      Pixmap pmap = GetImagePixmap(wi->win, arc->tile);
      values.fill_style = FillTiled;
      values.tile = pmap;
      values.ts_x_origin = (int) item->item_bounding_box.orig.x;
      values.ts_y_origin = (int) item->item_bounding_box.orig.y;
      XChangeGC(wi->dpy, wi->gc,
		GCTileStipXOrigin|GCTileStipYOrigin|GCFillStyle|GCTile|GCArcMode,
		&values);
    }
    else if (arc->fill_pattern != RadarUnspecifiedPattern) { /* Fill stippled */
      values.fill_style = FillStippled;
      values.stipple = arc->fill_pattern;
      values.ts_x_origin = (int) item->item_bounding_box.orig.x;
      values.ts_y_origin = (int) item->item_bounding_box.orig.y;
      XChangeGC(wi->dpy, wi->gc,
		GCTileStipXOrigin|GCTileStipYOrigin|GCFillStyle|GCStipple|GCForeground|GCArcMode,
		&values);
    }
    else { /* Fill solid */
      values.fill_style = FillSolid;
      XChangeGC(wi->dpy, wi->gc, GCForeground|GCFillStyle|GCArcMode, &values);
    }
    if (arc->render_shape) {
      XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc,
		   xp, num_points, Convex, CoordModeOrigin);
    }
    else {
      XFillArc(wi->dpy, wi->draw_buffer, wi->gc,
	       arc->orig.x, arc->orig.y, width, height,
	       arc->start_angle*64, arc->angle_extent*64);
    }
  }

  /*
   * Draw the arc.
   */
  if (arc->line_width) {
    SetLineStyle(wi->dpy, wi->gc, arc->line_style);
    values.foreground = RadarPixel(arc->line_color);
    values.line_width = (arc->line_width == 1) ? 0 : arc->line_width;
    values.cap_style = CapRound;
    values.join_style = JoinRound;
    if (arc->line_pattern == RadarUnspecifiedPattern) {
      values.fill_style = FillSolid;
      XChangeGC(wi->dpy, wi->gc,
		GCFillStyle|GCLineWidth|GCCapStyle|GCJoinStyle|GCForeground, &values);
    }
    else {
      values.fill_style = FillStippled;
      values.stipple = arc->line_pattern;
      XChangeGC(wi->dpy, wi->gc,
		GCFillStyle|GCStipple|GCLineWidth|GCCapStyle|GCJoinStyle|GCForeground,
		&values);
    }
    if (arc->render_shape) {
      XDrawLines(wi->dpy, wi->draw_buffer, wi->gc,
		 xp, num_points, CoordModeOrigin);
    }
    else {
      XDrawArc(wi->dpy, wi->draw_buffer, wi->gc,
	       arc->orig.x, arc->orig.y, width, height,
	       arc->start_angle*64, arc->angle_extent*64);
    }
  
    /*
     * If the outline is closed, draw the closure.
     */
    if (ISSET(arc->flags, CLOSED_BIT)) {
      if (ISSET(arc->flags, PIE_SLICE_BIT)) {
	XPoint	points[3];
	
	points[0].x = arc->center1.x;
	points[0].y = arc->center1.y;
	points[1].x = (arc->corner.x + arc->orig.x) / 2;
	points[1].y = (arc->corner.y + arc->orig.y) / 2;
	points[2].x = arc->center2.x;
	points[2].y = arc->center2.y;
	XDrawLines(wi->dpy, wi->draw_buffer, wi->gc, points, 3, CoordModeOrigin);
      }
      else {
	XDrawLine(wi->dpy, wi->draw_buffer, wi->gc,
		  arc->center1.x, arc->center1.y, arc->center2.x, arc->center2.y);
      }
    }
  }
}


/*
 **********************************************************************************
 *
 * IsSensitive --
 *
 **********************************************************************************
 */
static RadarBool
IsSensitive(Item	item,
	    int		item_part)
{
  return (ISSET(item->flags, SENSITIVE_BIT) &&
	  item->parent->class->IsSensitive(item->parent, RADAR_NO_PART));
}


/*
 **********************************************************************************
 *
 * Pick --
 *
 **********************************************************************************
 */
static double
Pick(Item	item,
     RadarPoint	*p,
     Item	start_item,
     int	aperture,
     Item	*a_item,
     int	*a_part)
{
  ArcItem	arc = (ArcItem) item;
  double	dist, new_dist;
  RadarBool	point_in_angle, filled, closed;
  RadarBool	in_triangle, acute_angle;
  RadarPoint	p1, center;
  int		width, height;
  RadarDim	lw = arc->line_width;

  center.x = (arc->corner.x + arc->orig.x) / 2;
  center.y = (arc->corner.y + arc->orig.y) / 2;
  width = arc->corner.x - arc->orig.x;
  height = arc->corner.y - arc->orig.y;
  
  /*
   * Let see if the point is in the angular range. First
   * transform the coordinates so that the oval is circular.
   * The 8192 factor avoids rounding errors.
   */
  p1.y = ((int)(p->y - center.y)*8192) / height;
  p1.x = ((int)(p->x - center.x)*8192) / width;
  point_in_angle = PointInAngle(arc->start_angle, arc->angle_extent, &p1);

  /*
   * Now try to compute the distance dealing with the
   * many possible configurations.
   */
  filled = !ISCLEAR(arc->flags, FILLED_BIT);
  closed = !ISCLEAR(arc->flags, CLOSED_BIT);

  /*
   * First the case of an arc not filled, not closed. We suppose
   * here that the outline is drawn since we cannot get here without
   * filling or outlining.
   */
  if (!filled && !closed) {
    if (point_in_angle) {
      dist = OvalToPointDist(&center, width, height, lw, p);
      if (dist < 0.0) {
	dist = -dist;
      }
      return dist;
    }
    dist = hypot((p->x - arc->center1.x), (p->y - arc->center1.y));
    new_dist = hypot((p->x - arc->center2.x), (p->y - arc->center2.y));
    if (new_dist < dist) {
      dist = new_dist;
    }
    /* Take into account CapRounded path. */
    if (lw > 1) {
      dist -= lw/2;
      if (dist < 0.0) {
	dist = 0.0;
      }
    }
    return dist;
  }
  
  /*
   * Try to deal with filled and/or outline-closed arcs (not having full
   * angular extent).
   */
  if (ISSET(arc->flags, PIE_SLICE_BIT)) {
    dist = LineToPointDist(&center, &arc->center1, p);
    new_dist = LineToPointDist(&center, &arc->center2, p);
    if (new_dist < dist) {
      dist = new_dist;
    }
    if (arc->line_width > 1) {
      if (closed) {
	dist -= arc->line_width/2;
      }
      /*
       * The arc outline is CapRounded so if it is not
       * full extent, includes the caps.
       */
      else {
	new_dist = hypot(p->x - arc->center1.x, p->y - arc->center1.y) - lw/2;
	if (new_dist < dist) {
	  dist = new_dist;
	}
	new_dist = hypot(p->x - arc->center2.x, p->y - arc->center2.y) - lw/2;
	if (new_dist < dist) {
	  dist = new_dist;
	}
      }
    }
    if (dist <= 0.0) {
      return 0.0;
    }
    if (point_in_angle) {
      new_dist = OvalToPointDist(&center, width, height, lw, p);
      if (new_dist < dist) {
	dist = new_dist;
      }
      if (dist < 0.0) {
	dist = filled ? 0.0 : -dist;
      }
    }
    return dist;
  }

  /*
   * This is a chord closed oval.
   */    
  dist = LineToPointDist(&arc->center1, &arc->center2, p);
  if (arc->line_width > 1) {
    if (closed) {
      dist -= arc->line_width/2;
    }
    /*
     * The arc outline is CapRounded so if it is not
     * full extent, includes the caps.
     */
    else {
      new_dist = hypot(p->x - arc->center1.x, p->y - arc->center1.y) - lw/2;
      if (new_dist < dist) {
	dist = new_dist;
      }
      new_dist = hypot(p->x - arc->center2.x, p->y - arc->center2.y) - lw/2;
      if (new_dist < dist) {
	dist = new_dist;
      }
    }
  }
  if (dist <= 0.0) {
    return 0.0;
  }
  
  /*
   * Need to check the point against the triangle formed
   * by the difference between Chord mode and PieSlice mode.
   * This triangle needs to be included if extend is more than
   * 180 degrees and excluded otherwise. We try to see if
   * the center of the arc and the point are both on the same
   * side of the chord. If they are, the point is in the triangle
   */
  if (arc->center1.x == arc->center2.x) {
    in_triangle = ((center.x <= arc->center1.x) && (p->x <= arc->center1.x)) ||
      ((center.x > arc->center1.x) && (p->x > arc->center1.x));
  }
  else {
    double	a, b;
    
    a = ((double) (arc->center2.y - arc->center1.y)) /
      ((double) (arc->center2.x - arc->center1.x));
    b = arc->center1.y - a*arc->center1.x;
    in_triangle = (((a*center.x + b - center.y) >= 0.0) ==
		   ((a*p->x + b - p->y) >= 0.0));
  }
  
  acute_angle = ((arc->angle_extent > -180) && (arc->angle_extent < 180));
  if (!point_in_angle && !acute_angle && filled && in_triangle) {
    return 0.0;
  }
  
  if (point_in_angle && (!acute_angle || !in_triangle)) {
    new_dist = OvalToPointDist(&center, width, height, lw, p);
    if (new_dist < dist) {
      dist = new_dist;
    }
    if (dist < 0.0) {
      dist = filled ? 0.0 : -dist;
    }
  }

  return dist;
}


/*
 **********************************************************************************
 *
 * GetClipVertices --
 *	Get the clipping shape.
 *
 **********************************************************************************
 */
static RadarBool
GetClipVertices(Item		item,
		RadarPoint	**points,
		int		*num_points)
{
  return False;
}


/*
 **********************************************************************************
 *
 * Coords --
 *	Return or edit the item vertices.
 *
 **********************************************************************************
 */
static int
Coords(Item		item,
       int		index,
       int		cmd,
       RadarPoint	**pts,
       int		*num_pts)
{
  ArcItem	arc = (ArcItem) item;

  if ((cmd == COORDS_ADD) || (cmd == COORDS_ADD_LAST) || (cmd == COORDS_REMOVE)) {
    Tcl_AppendResult(item->wi->interp,
		     " arcs can't add or remove vertices", NULL);
    return RADAR_ERROR;
  }
  else if (cmd == COORDS_REPLACE_ALL) {
    if (*num_pts != 2) {
      Tcl_AppendResult(item->wi->interp,
		       " coords command need 2 points on arcs", NULL);
      return RADAR_ERROR;
    }
    arc->coords[0] = (*pts)[0];
    arc->coords[1] = (*pts)[1];
    ITEM.Invalidate(item, RADAR_COORDS_FLAG);
  }
  else if (cmd == COORDS_REPLACE) {
    if (*num_pts < 1) {
      Tcl_AppendResult(item->wi->interp,
		       " coords command need at least 1 point", NULL);
      return RADAR_ERROR;
    }
    if (index < 0) {
      index += 2;
    }
    if ((index < 0) || (index > 1)) {
    range_err:
      Tcl_AppendResult(item->wi->interp,
		       " incorrect coord index, should be between -2 and 1", NULL);
      return RADAR_ERROR;
    }
    arc->coords[index] = (*pts)[0];
    ITEM.Invalidate(item, RADAR_COORDS_FLAG);
  }
  else if (cmd == COORDS_READ_ALL) {
    *num_pts = 2;
    *pts = arc->coords;
  }
  else if (cmd == COORDS_READ) {
    if (index < 0) {
      index += 2;
    }
    if ((index < 0) || (index > 1)) {
      goto range_err;
    }
    *num_pts = 1;
    *pts = &arc->coords[index];
  }

  return RADAR_OK;
}


/*
 **********************************************************************************
 *
 * PostScript --
 *
 **********************************************************************************
 */
static void
PostScript(Item			item,
	   PostScriptInfo	ps_info)
{
}


/*
 **********************************************************************************
 *
 * Exported functions struct --
 *
 **********************************************************************************
 */
static ItemClassStruct ARC_ITEM_CLASS = {
  sizeof(ArcItemStruct),
  False,
  False,
  False,
  "arc",
  arc_attrs,
  Init,
  Clone,
  Destroy,
  Configure,
  Query,
  NULL,
  NULL,
  GetClipVertices,
  Coords,
  ComputeCoordinates,
  ToArea,
  Draw,
  IsSensitive,
  Pick,
  PostScript
};

RadarItemClassId RadarArc = (RadarItemClassId) &ARC_ITEM_CLASS;