u8g_line.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. u8g_line.h
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2012, 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 "u8g.h"
  28. void u8g_DrawLine(u8g_t *u8g, u8g_uint_t x1, u8g_uint_t y1, u8g_uint_t x2, u8g_uint_t y2)
  29. {
  30. u8g_uint_t tmp;
  31. u8g_uint_t x,y;
  32. u8g_uint_t dx, dy;
  33. u8g_int_t err;
  34. u8g_int_t ystep;
  35. uint8_t swapxy = 0;
  36. /* no BBX 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. for( x = x1; x <= x2; x++ )
  55. {
  56. if ( swapxy == 0 )
  57. u8g_DrawPixel(u8g, x, y);
  58. else
  59. u8g_DrawPixel(u8g, y, x);
  60. err -= (uint8_t)dy;
  61. if ( err < 0 )
  62. {
  63. y += (u8g_uint_t)ystep;
  64. err += (u8g_uint_t)dx;
  65. }
  66. }
  67. }