| 1 | /* |
| 2 | |
| 3 | u8g2_box.c |
| 4 | |
| 5 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 6 | |
| 7 | Copyright (c) 2016, olikraus@gmail.com |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without modification, |
| 11 | are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, this list |
| 14 | of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 17 | list of conditions and the following disclaimer in the documentation and/or other |
| 18 | materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 22 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 32 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | |
| 34 | */ |
| 35 | |
| 36 | #include "u8g2.h" |
| 37 | |
| 38 | |
| 39 | void u8g2_DrawLine(u8g2_t *u8g2, u8g2_uint_t x1, u8g2_uint_t y1, u8g2_uint_t x2, u8g2_uint_t y2) |
| 40 | { |
| 41 | u8g2_uint_t tmp; |
| 42 | u8g2_uint_t x,y; |
| 43 | u8g2_uint_t dx, dy; |
| 44 | u8g2_int_t err; |
| 45 | u8g2_int_t ystep; |
| 46 | |
| 47 | uint8_t swapxy = 0; |
| 48 | |
| 49 | /* no intersection check at the moment, should be added... */ |
| 50 | |
| 51 | if ( x1 > x2 ) dx = x1-x2; else dx = x2-x1; |
| 52 | if ( y1 > y2 ) dy = y1-y2; else dy = y2-y1; |
| 53 | |
| 54 | if ( dy > dx ) |
| 55 | { |
| 56 | swapxy = 1; |
| 57 | tmp = dx; dx =dy; dy = tmp; |
| 58 | tmp = x1; x1 =y1; y1 = tmp; |
| 59 | tmp = x2; x2 =y2; y2 = tmp; |
| 60 | } |
| 61 | if ( x1 > x2 ) |
| 62 | { |
| 63 | tmp = x1; x1 =x2; x2 = tmp; |
| 64 | tmp = y1; y1 =y2; y2 = tmp; |
| 65 | } |
| 66 | err = dx >> 1; |
| 67 | if ( y2 > y1 ) ystep = 1; else ystep = -1; |
| 68 | y = y1; |
| 69 | |
| 70 | #ifndef U8G2_16BIT |
| 71 | if ( x2 == 255 ) |
| 72 | x2--; |
| 73 | #else |
| 74 | if ( x2 == 0xffff ) |
| 75 | x2--; |
| 76 | #endif |
| 77 | |
| 78 | for( x = x1; x <= x2; x++ ) |
| 79 | { |
| 80 | if ( swapxy == 0 ) |
| 81 | u8g2_DrawPixel(u8g2, x, y); |
| 82 | else |
| 83 | u8g2_DrawPixel(u8g2, y, x); |
| 84 | err -= (u8g2_uint_t)dy; |
| 85 | if ( err < 0 ) |
| 86 | { |
| 87 | y += (u8g2_uint_t)ystep; |
| 88 | err += (u8g2_uint_t)dx; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |