| 1 | /* |
| 2 | |
| 3 | u8g2_arc.c |
| 4 | |
| 5 | Contributed... see here: https://github.com/olikraus/u8g2/issues/2243 |
| 6 | |
| 7 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 8 | |
| 9 | Copyright (c) 2023, olikraus@gmail.com |
| 10 | All rights reserved. |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without modification, |
| 13 | are permitted provided that the following conditions are met: |
| 14 | |
| 15 | * Redistributions of source code must retain the above copyright notice, this list |
| 16 | of conditions and the following disclaimer. |
| 17 | |
| 18 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 19 | list of conditions and the following disclaimer in the documentation and/or other |
| 20 | materials provided with the distribution. |
| 21 | |
| 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 23 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 24 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 34 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | |
| 36 | */ |
| 37 | |
| 38 | #include "u8g2.h" |
| 39 | |
| 40 | |
| 41 | static void u8g2_draw_arc(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rad, uint8_t start, uint8_t end) |
| 42 | { |
| 43 | // Manage angle inputs |
| 44 | uint8_t full = (start == end); |
| 45 | uint8_t inverted = (start > end); |
| 46 | uint8_t a_start = inverted ? end : start; |
| 47 | uint8_t a_end = inverted ? start : end; |
| 48 | |
| 49 | // Initialize variables |
| 50 | uint32_t ratio; |
| 51 | u8g2_int_t x = 0; |
| 52 | u8g2_int_t y = rad; |
| 53 | u8g2_int_t d = rad - 1; |
| 54 | |
| 55 | // Trace arc radius with the Andres circle algorithm (process each pixel of a 1/8th circle of radius rad) |
| 56 | while (y >= x) |
| 57 | { |
| 58 | // Get the percentage of 1/8th circle drawn with a fast approximation of arctan(x/y) |
| 59 | ratio = x * 255 / y; // x/y [0..255] |
| 60 | ratio = ratio * (770195 - (ratio - 255) * (ratio + 941)) / 6137491; // arctan(x/y) [0..32] |
| 61 | |
| 62 | // Fill the pixels of the 8 sections of the circle, but only on the arc defined by the angles (start and end) |
| 63 | if(full || ((ratio >= a_start && ratio < a_end) ^ inverted)) u8g2_DrawPixel(u8g2, x0 + y, y0 - x); |
| 64 | if(full || (((ratio + a_end) > 63 && (ratio + a_start) <= 63) ^ inverted)) u8g2_DrawPixel(u8g2, x0 + x, y0 - y); |
| 65 | if(full || (((ratio + 64) >= a_start && (ratio + 64) < a_end) ^ inverted)) u8g2_DrawPixel(u8g2, x0 - x, y0 - y); |
| 66 | if(full || (((ratio + a_end) > 127 && (ratio + a_start) <= 127) ^ inverted)) u8g2_DrawPixel(u8g2, x0 - y, y0 - x); |
| 67 | if(full || (((ratio + 128) >= a_start && (ratio + 128) < a_end) ^ inverted)) u8g2_DrawPixel(u8g2, x0 - y, y0 + x); |
| 68 | if(full || (((ratio + a_end) > 191 && (ratio + a_start) <= 191) ^ inverted)) u8g2_DrawPixel(u8g2, x0 - x, y0 + y); |
| 69 | if(full || (((ratio + 192) >= a_start && (ratio + 192) < a_end) ^ inverted)) u8g2_DrawPixel(u8g2, x0 + x, y0 + y); |
| 70 | if(full || (((ratio + a_end) > 255 && (ratio + a_start) <= 255) ^ inverted)) u8g2_DrawPixel(u8g2, x0 + y, y0 + x); |
| 71 | |
| 72 | // Run Andres circle algorithm to get to the next pixel |
| 73 | if (d >= 2 * x) |
| 74 | { |
| 75 | d = d - 2 * x - 1; |
| 76 | x = x + 1; |
| 77 | } |
| 78 | else if (d < 2 * ((u8g2_int_t)rad - y)) |
| 79 | { |
| 80 | d = d + 2 * y - 1; |
| 81 | y = y - 1; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | d = d + 2 * (y - x - 1); |
| 86 | y = y - 1; |
| 87 | x = x + 1; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void u8g2_DrawArc(u8g2_t *u8g2, u8g2_uint_t x0, u8g2_uint_t y0, u8g2_uint_t rad, uint8_t start, uint8_t end) |
| 93 | { |
| 94 | /* check for bounding box */ |
| 95 | #ifdef U8G2_WITH_INTERSECTION |
| 96 | { |
| 97 | if ( u8g2_IsIntersection(u8g2, x0-rad, y0-rad, x0+rad+1, y0+rad+1) == 0 ) |
| 98 | return; |
| 99 | } |
| 100 | #endif /* U8G2_WITH_INTERSECTION */ |
| 101 | |
| 102 | |
| 103 | /* draw arc */ |
| 104 | u8g2_draw_arc(u8g2, x0, y0, rad, start, end); |
| 105 | } |
| 106 | |
| 107 | |