qp_draw_ellipse.c (3753B)
1 // Copyright 2021 Paul Cotter (@gr1mr3aver) 2 // Copyright 2021 Nick Brassel (@tzarc) 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 #include "qp_internal.h" 6 #include "qp_comms.h" 7 #include "qp_draw.h" 8 9 // Utilize 4-way symmetry to draw an ellipse 10 static bool qp_ellipse_helper_impl(painter_device_t device, uint16_t centerx, uint16_t centery, uint16_t offsetx, uint16_t offsety, bool filled) { 11 /* 12 Ellipses have the property of 4-way symmetry, so four pixels can be drawn 13 for each computed [offsetx,offsety] given the center coordinates 14 represented by [centerx,centery]. 15 16 For filled ellipses, we can draw horizontal lines between each pair of 17 pixels with the same final value of y. 18 19 When offsetx == 0 only two pixels can be drawn for filled or unfilled ellipses 20 */ 21 22 int16_t xpx = ((int16_t)centerx) + ((int16_t)offsetx); 23 int16_t xmx = ((int16_t)centerx) - ((int16_t)offsetx); 24 int16_t ypy = ((int16_t)centery) + ((int16_t)offsety); 25 int16_t ymy = ((int16_t)centery) - ((int16_t)offsety); 26 27 if (offsetx == 0) { 28 if (!qp_internal_setpixel_impl(device, xpx, ypy)) { 29 return false; 30 } 31 if (!qp_internal_setpixel_impl(device, xpx, ymy)) { 32 return false; 33 } 34 } else if (filled) { 35 if (!qp_internal_fillrect_helper_impl(device, xpx, ypy, xmx, ypy)) { 36 return false; 37 } 38 if (offsety > 0 && !qp_internal_fillrect_helper_impl(device, xpx, ymy, xmx, ymy)) { 39 return false; 40 } 41 } else { 42 if (!qp_internal_setpixel_impl(device, xpx, ypy)) { 43 return false; 44 } 45 if (!qp_internal_setpixel_impl(device, xpx, ymy)) { 46 return false; 47 } 48 if (!qp_internal_setpixel_impl(device, xmx, ypy)) { 49 return false; 50 } 51 if (!qp_internal_setpixel_impl(device, xmx, ymy)) { 52 return false; 53 } 54 } 55 56 return true; 57 } 58 59 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 60 // Quantum Painter External API: qp_ellipse 61 62 bool qp_ellipse(painter_device_t device, uint16_t x, uint16_t y, uint16_t sizex, uint16_t sizey, uint8_t hue, uint8_t sat, uint8_t val, bool filled) { 63 qp_dprintf("qp_ellipse: entry\n"); 64 painter_driver_t *driver = (painter_driver_t *)device; 65 if (!driver || !driver->validate_ok) { 66 qp_dprintf("qp_ellipse: fail (validation_ok == false)\n"); 67 return false; 68 } 69 70 int32_t aa = ((int32_t)sizex) * ((int32_t)sizex); 71 int32_t bb = ((int32_t)sizey) * ((int32_t)sizey); 72 int32_t fa = 4 * aa; 73 int32_t fb = 4 * bb; 74 75 int16_t dx = 0; 76 int16_t dy = ((int16_t)sizey); 77 78 qp_internal_fill_pixdata(device, MAX(sizex, sizey), hue, sat, val); 79 80 if (!qp_comms_start(device)) { 81 qp_dprintf("qp_ellipse: fail (could not start comms)\n"); 82 return false; 83 } 84 85 bool ret = true; 86 for (int32_t delta = (2 * bb) + (aa * (1 - (2 * sizey))); bb * dx <= aa * dy; dx++) { 87 if (!qp_ellipse_helper_impl(device, x, y, dx, dy, filled)) { 88 ret = false; 89 break; 90 } 91 if (delta >= 0) { 92 delta += fa * (1 - dy); 93 dy--; 94 } 95 delta += bb * (4 * dx + 6); 96 } 97 98 dx = sizex; 99 dy = 0; 100 101 for (int32_t delta = (2 * aa) + (bb * (1 - (2 * sizex))); aa * dy <= bb * dx; dy++) { 102 if (!qp_ellipse_helper_impl(device, x, y, dx, dy, filled)) { 103 ret = false; 104 break; 105 } 106 if (delta >= 0) { 107 delta += fb * (1 - dx); 108 dx--; 109 } 110 delta += aa * (4 * dy + 6); 111 } 112 113 qp_dprintf("qp_ellipse: %s\n", ret ? "ok" : "fail"); 114 qp_comms_stop(device); 115 return ret; 116 }