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
|
#include <math.h>
#include <grass/display.h>
#include <grass/gis.h>
char percent[] = "%";
int draw_slice(struct Colors *colors, int fill_flag, DCELL fill_color1,
DCELL fill_color2, int txt_color, double cx, double cy,
double r, /* in normalized coords. */
double a1, double a2 /* in degrees */
)
{
double tt, tb, tr, tl;
int height, width;
double yoffset, xoffset;
double x[1000], y[1000];
int lx, ly;
int i = 1;
char txt[512];
double arc, arc_incr = 0.01;
DCELL fill_color;
D_get_src(&tt, &tb, &tl, &tr);
height = tb - tt;
width = tr - tl;
yoffset = tb;
xoffset = tl;
while (a2 / arc_incr > 998)
arc_incr *= 2;
x[0] = (xoffset + cx * width);
y[0] = (yoffset - cy * height);
arc = a1;
if (fill_flag && fill_color1 != fill_color2) {
i = 1;
while (arc <= (a1 + a2)) {
fill_color =
fill_color1 + (arc - a1) * (fill_color2 - fill_color1) / a2;
x[i] = x[0] + r * (width)*cos(arc / 57.296);
y[i] = y[0] - r * (height)*sin(arc / 57.296);
if (i == 2) {
D_d_color(fill_color, colors);
D_polygon_abs(x + i - 2, y + i - 2, 3);
x[i - 1] = x[i];
y[i - 1] = y[i];
}
else
i++;
arc = arc + arc_incr;
}
}
else {
i = 1;
while (arc <= (a1 + a2)) {
x[i] = x[0] + r * (width)*cos(arc / 57.296);
y[i] = y[0] - r * (height)*sin(arc / 57.296);
i++;
arc = arc + arc_incr;
}
if (!fill_flag) {
D_use_color(txt_color);
D_polyline_abs(x, y, i);
}
else {
D_d_color(fill_color1, colors);
D_polygon_abs(x, y, i);
}
}
if (a2 > 15.0) {
/* draw a label */
arc = a1 + a2 / 2;
sprintf(txt, "%2.0f%s", (a2 / 360.0) * 100.0, percent);
D_get_text_box(txt, &tt, &tb, &tl, &tr);
lx = x[0] + (r + 0.03) * (width)*cos(arc / 57.296) - (tr - tl) / 2;
ly = y[0] - (r + 0.03) * (height)*sin(arc / 57.296) + (tb - tt) / 2;
D_pos_abs(lx, ly);
D_use_color(txt_color);
D_text(txt);
}
return 0;
}
int draw_slice_unfilled(struct Colors *colors, int tc, double cx,
double cy, /* circle center, in normalized coords. */
double r, /* circle radius, in normalized units */
double a1, /* arc start position, in degrees */
double a2 /* arc size, in degrees */
)
{
draw_slice(colors, 0, 0., 0., tc, cx, cy, r, a1, a2);
return 0;
}
int draw_slice_filled(struct Colors *colors, DCELL fc, /* fill color */
int tc, /* text color */
double cx,
double cy, /* circle center, in normalized coords. */
double r, /* circle radius, in normalized units */
double a1, /* arc start position, in degrees */
double a2 /* arc size, in degrees */
)
{
draw_slice(colors, 1, fc, fc, tc, cx, cy, r, a1, a2);
return 0;
}
|