MCPcopy Create free account
hub / github.com/NetHack/NetHack / selection_do_ellipse

Function selection_do_ellipse

src/selvar.c:455–538  ·  view source on GitHub ↗

McIlroy's Ellipse Algorithm */

Source from the content-addressed store, hash-verified

453
454/* McIlroy's Ellipse Algorithm */
455void
456selection_do_ellipse(
457 struct selectionvar *ov,
458 int xc, int yc,
459 int a, int b,
460 int filled)
461{ /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */
462 int x = 0, y = b;
463 long a2 = (long) a * a, b2 = (long) b * b;
464 long crit1 = -(a2 / 4 + a % 2 + b2);
465 long crit2 = -(b2 / 4 + b % 2 + a2);
466 long crit3 = -(b2 / 4 + b % 2);
467 long t = -a2 * y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */
468 long dxt = 2 * b2 * x, dyt = -2 * a2 * y;
469 long d2xt = 2 * b2, d2yt = 2 * a2;
470 long width = 1;
471 long i;
472
473 if (!ov)
474 return;
475
476 filled = !filled;
477
478 if (!filled) {
479 while (y >= 0 && x <= a) {
480 selection_setpoint(xc + x, yc + y, ov, 1);
481 if (x != 0 || y != 0)
482 selection_setpoint(xc - x, yc - y, ov, 1);
483 if (x != 0 && y != 0) {
484 selection_setpoint(xc + x, yc - y, ov, 1);
485 selection_setpoint(xc - x, yc + y, ov, 1);
486 }
487 if (t + b2 * x <= crit1 /* e(x+1,y-1/2) <= 0 */
488 || t + a2 * y <= crit3) { /* e(x+1/2,y) <= 0 */
489 x++;
490 dxt += d2xt;
491 t += dxt;
492 } else if (t - a2 * y > crit2) { /* e(x+1/2,y-1) > 0 */
493 y--;
494 dyt += d2yt;
495 t += dyt;
496 } else {
497 x++;
498 dxt += d2xt;
499 t += dxt;
500 y--;
501 dyt += d2yt;
502 t += dyt;
503 }
504 }
505 } else {
506 while (y >= 0 && x <= a) {
507 if (t + b2 * x <= crit1 /* e(x+1,y-1/2) <= 0 */
508 || t + a2 * y <= crit3) { /* e(x+1/2,y) <= 0 */
509 x++;
510 dxt += d2xt;
511 t += dxt;
512 width += 2;

Callers 2

l_selection_circleFunction · 0.85
l_selection_ellipseFunction · 0.85

Calls 1

selection_setpointFunction · 0.85

Tested by

no test coverage detected