📄 generators.c
字号:
} }}/* Circle generator */void GUIAPI CircleGenerator (void* context, int x, int y, int r, CB_CIRCLE cb){ int cx = 0; int cy = r; int df = 1 - r; int d_e = 3; int d_se = -2 * r + 5; do { cb (context, x-cx, x+cx, y+cy); if (cy) cb (context, x-cx, x+cx, y-cy); if (cx != cy) { cb (context, x-cy, x+cy, y+cx); if (cx) cb (context, x-cy, x+cy, y-cx); } if (df < 0) { df += d_e; d_e += 2; d_se += 2; } else { df += d_se; d_e += 2; d_se += 4; cy--; } cx++; } while (cx <= cy);}/* Ellipse generator */void GUIAPI EllipseGenerator (void* context, int x, int y, int rx, int ry, CB_ELLIPSE cb){ int ix, iy; int h, i, j, k; int oh, oi, oj, ok; if (rx < 1) rx = 1; if (ry < 1) ry = 1; h = i = j = k = 0xFFFF; if (rx > ry) { ix = 0; iy = rx * 64; do { oh = h; oi = i; oj = j; ok = k; h = (ix + 32) >> 6; i = (iy + 32) >> 6; j = (h * ry) / rx; k = (i * ry) / rx; if (((h != oh) || (k != ok)) && (h < oi)) { cb (context, x-h, x+h, y+k); if (k) { cb (context, x-h, x+h, y-k); } } if (((i != oi) || (j != oj)) && (h < i)) { cb (context, x-i, x+i, y+j); if (j) { cb (context, x-i, x+i, y-j); } } ix = ix + iy / rx; iy = iy - ix / rx; } while (i > h); } else { ix = 0; iy = ry * 64; do { oh = h; oi = i; oj = j; ok = k; h = (ix + 32) >> 6; i = (iy + 32) >> 6; j = (h * rx) / ry; k = (i * rx) / ry; if (((j != oj) || (i != oi)) && (h < i)) { cb (context, x-j, x+j, y+i); if (i) { cb (context, x-j, x+j, y-i); } } if (((k != ok) || (h != oh)) && (h < oi)) { cb (context, x-k, x+k, y+h); if (h) { cb (context, x-k, x+k, y-h); } } ix = ix + iy / ry; iy = iy - ix / ry; } while(i > h); }}/* Arc generator */void GUIAPI ArcGenerator (void* context, int x, int y, int r, fixed ang1, fixed ang2, CB_ARC cb){ unsigned long rr = r*r; unsigned long rr1, rr2, rr3; int px, py; int ex, ey; int px1, px2, px3; int py1, py2, py3; int d1, d2, d3; int ax, ay; int q, qe; long tg_cur, tg_end; int done = FALSE; rr1 = r; rr2 = itofix(x); rr3 = itofix(y); /* evaluate the start point and the end point */ px = fixtoi(rr2 + rr1 * fcos(ang1)); py = fixtoi(rr3 - rr1 * fsin(ang1)); ex = fixtoi(rr2 + rr1 * fcos(ang2)); ey = fixtoi(rr3 - rr1 * fsin(ang2)); /* start quadrant */ if (px >= x) { if (py <= y) q = 1; /* quadrant 1 */ else q = 4; /* quadrant 4 */ } else { if (py < y) q = 2; /* quadrant 2 */ else q = 3; /* quadrant 3 */ } /* end quadrant */ if (ex >= x) { if (ey <= y) qe = 1; /* quadrant 1 */ else qe = 4; /* quadrant 4 */ } else { if (ey < y) qe = 2; /* quadrant 2 */ else qe = 3; /* quadrant 3 */ } #define loc_tg(_y, _x) (_x-x) ? itofix(_y-y)/(_x-x) : itofix(_y-y) tg_end = loc_tg(ey, ex); while (!done) { cb (context, px, py); /* from here, we have only 3 possible direction of movement, eg. * for the first quadrant: * * OOOOOOOOO * OOOOOOOOO * OOOOOO21O * OOOOOO3*O */ /* evaluate the 3 possible points */ switch (q) { case 1: px1 = px; py1 = py-1; px2 = px-1; py2 = py-1; px3 = px-1; py3 = py; /* 2nd quadrant check */ if (px != x) { break; } else { /* we were in the end quadrant, changing is illegal. Exit. */ if (qe == q) done = TRUE; q++; } /* fall through */ case 2: px1 = px-1; py1 = py; px2 = px-1; py2 = py+1; px3 = px; py3 = py+1; /* 3rd quadrant check */ if (py != y) { break; } else { /* we were in the end quadrant, changing is illegal. Exit. */ if (qe == q) done = TRUE; q++; } /* fall through */ case 3: px1 = px; py1 = py+1; px2 = px+1; py2 = py+1; px3 = px+1; py3 = py; /* 4th quadrant check */ if (px != x) { break; } else { /* we were in the end quadrant, changing is illegal. Exit. */ if (qe == q) done = TRUE; q++; } /* fall through */ case 4: px1 = px+1; py1 = py; px2 = px+1; py2 = py-1; px3 = px; py3 = py-1; /* 1st quadrant check */ if (py == y) { /* we were in the end quadrant, changing is illegal. Exit. */ if (qe == q) done = TRUE; q = 1; px1 = px; py1 = py-1; px2 = px-1; py2 = py-1; px3 = px-1; py3 = py; } break; default: return; } /* now, we must decide which of the 3 points is the right point. * We evaluate the distance from center and, then, choose the * nearest point. */ ax = x-px1; ay = y-py1; rr1 = ax*ax + ay*ay; ax = x-px2; ay = y-py2; rr2 = ax*ax + ay*ay; ax = x-px3; ay = y-py3; rr3 = ax*ax + ay*ay; /* difference from the main radius */ if (rr1 > rr) d1 = rr1-rr; else d1 = rr-rr1; if (rr2 > rr) d2 = rr2-rr; else d2 = rr-rr2; if (rr3 > rr) d3 = rr3-rr; else d3 = rr-rr3; /* what is the minimum? */ if (d1 <= d2) { px = px1; py = py1; } else if (d2 <= d3) { px = px2; py = py2; } else { px = px3; py = py3; } /* are we in the final quadrant? */ if (qe == q) { tg_cur = loc_tg(py, px); /* is the arc finished? */ switch (q) { case 1: /* end quadrant = 1? */ if (tg_cur <= tg_end) done = TRUE; break; case 2: /* end quadrant = 2? */ if (tg_cur <= tg_end) done = TRUE; break; case 3: /* end quadrant = 3? */ if (tg_cur <= tg_end) done = TRUE; break; case 4: /* end quadrant = 4? */ if (tg_cur <= tg_end) done = TRUE; break; } } } /* draw the last evaluated point */ cb (context, px, py);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -