1 //
2 // "$Id$"
3 //
4 // Arc (integer) drawing functions for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18
19 /**
20 \file fl_arci.cxx
21 \brief Utility functions for drawing circles using integers
22 */
23
24 // "integer" circle drawing functions. These draw the limited
25 // circle types provided by X and NT graphics. The advantage of
26 // these is that small ones draw quite nicely (probably due to stored
27 // hand-drawn bitmaps of small circles!) and may be implemented by
28 // hardware and thus are fast.
29
30 // Probably should add fl_chord.
31
32 // 3/10/98: created
33
34 #include <FL/fl_draw.H>
35 #include <FL/x.H>
36 #ifdef WIN32
37 # include <FL/math.h>
38 #endif
39 #include <config.h>
40
arc(int x,int y,int w,int h,double a1,double a2)41 void Fl_Graphics_Driver::arc(int x,int y,int w,int h,double a1,double a2) {
42 if (w <= 0 || h <= 0) return;
43
44 #if defined(USE_X11)
45 XDrawArc(fl_display, fl_window, fl_gc, x,y,w-1,h-1, int(a1*64),int((a2-a1)*64));
46 #elif defined(WIN32)
47 int xa = x+w/2+int(w*cos(a1/180.0*M_PI));
48 int ya = y+h/2-int(h*sin(a1/180.0*M_PI));
49 int xb = x+w/2+int(w*cos(a2/180.0*M_PI));
50 int yb = y+h/2-int(h*sin(a2/180.0*M_PI));
51 if (fabs(a1 - a2) < 90) {
52 if (xa == xb && ya == yb) SetPixel(fl_gc, xa, ya, fl_RGB());
53 else Arc(fl_gc, x, y, x+w, y+h, xa, ya, xb, yb);
54 } else Arc(fl_gc, x, y, x+w, y+h, xa, ya, xb, yb);
55 #elif defined(__APPLE_QUARTZ__)
56 a1 = (-a1)/180.0f*M_PI; a2 = (-a2)/180.0f*M_PI;
57 float cx = x + 0.5f*w - 0.5f, cy = y + 0.5f*h - 0.5f;
58 CGContextSetShouldAntialias(fl_gc, true);
59 if (w!=h) {
60 CGContextSaveGState(fl_gc);
61 CGContextTranslateCTM(fl_gc, cx, cy);
62 CGContextScaleCTM(fl_gc, w-1.0f, h-1.0f);
63 CGContextAddArc(fl_gc, 0, 0, 0.5, a1, a2, 1);
64 CGContextRestoreGState(fl_gc);
65 } else {
66 float r = (w+h)*0.25f-0.5f;
67 CGContextAddArc(fl_gc, cx, cy, r, a1, a2, 1);
68 }
69 CGContextStrokePath(fl_gc);
70 CGContextSetShouldAntialias(fl_gc, false);
71 #else
72 # error unsupported platform
73 #endif
74 }
75
pie(int x,int y,int w,int h,double a1,double a2)76 void Fl_Graphics_Driver::pie(int x,int y,int w,int h,double a1,double a2) {
77 if (w <= 0 || h <= 0) return;
78
79 #if defined(USE_X11)
80 XDrawArc(fl_display, fl_window, fl_gc, x,y,w-1,h-1, int(a1*64),int((a2-a1)*64));
81 XFillArc(fl_display, fl_window, fl_gc, x,y,w-1,h-1, int(a1*64),int((a2-a1)*64));
82 #elif defined(WIN32)
83 if (a1 == a2) return;
84 int xa = x+w/2+int(w*cos(a1/180.0*M_PI));
85 int ya = y+h/2-int(h*sin(a1/180.0*M_PI));
86 int xb = x+w/2+int(w*cos(a2/180.0*M_PI));
87 int yb = y+h/2-int(h*sin(a2/180.0*M_PI));
88 SelectObject(fl_gc, fl_brush());
89 if (fabs(a1 - a2) < 90) {
90 if (xa == xb && ya == yb) {
91 MoveToEx(fl_gc, x+w/2, y+h/2, 0L);
92 LineTo(fl_gc, xa, ya);
93 SetPixel(fl_gc, xa, ya, fl_RGB());
94 } else Pie(fl_gc, x, y, x+w, y+h, xa, ya, xb, yb);
95 } else Pie(fl_gc, x, y, x+w, y+h, xa, ya, xb, yb);
96 #elif defined(__APPLE_QUARTZ__)
97 a1 = (-a1)/180.0f*M_PI; a2 = (-a2)/180.0f*M_PI;
98 float cx = x + 0.5f*w - 0.5f, cy = y + 0.5f*h - 0.5f;
99 CGContextSetShouldAntialias(fl_gc, true);
100 if (w!=h) {
101 CGContextSaveGState(fl_gc);
102 CGContextTranslateCTM(fl_gc, cx, cy);
103 CGContextScaleCTM(fl_gc, w, h);
104 CGContextAddArc(fl_gc, 0, 0, 0.5, a1, a2, 1);
105 CGContextAddLineToPoint(fl_gc, 0, 0);
106 CGContextClosePath(fl_gc);
107 CGContextRestoreGState(fl_gc);
108 } else {
109 float r = (w+h)*0.25f;
110 CGContextAddArc(fl_gc, cx, cy, r, a1, a2, 1);
111 CGContextAddLineToPoint(fl_gc, cx, cy);
112 CGContextClosePath(fl_gc);
113 }
114 CGContextFillPath(fl_gc);
115 CGContextSetShouldAntialias(fl_gc, false);
116 #else
117 # error unsupported platform
118 #endif
119 }
120
121 //
122 // End of "$Id$".
123 //
124