u8g2_line.c 2.5 KB

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