drawLine(): sort inputs on horizontal/vertical lines

C'mon guys, this is Graphics 101 stuff
This commit is contained in:
Phillip Burgess 2017-04-05 09:03:34 -07:00
parent 7be06d3b3b
commit cb95e0307b
2 changed files with 13 additions and 5 deletions

View File

@ -134,6 +134,8 @@ void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){
drawPixel(x, y, color); drawPixel(x, y, color);
} }
// (x,y) is topmost point; if unsure, calling function
// should sort endpoints or call writeLine() instead
void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y, void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) { int16_t h, uint16_t color) {
// Overwrite in subclasses if startWrite is defined! // Overwrite in subclasses if startWrite is defined!
@ -142,6 +144,8 @@ void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
drawFastVLine(x, y, h, color); drawFastVLine(x, y, h, color);
} }
// (x,y) is leftmost point; if unsure, calling function
// should sort endpoints or call writeLine() instead
void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y, void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) { int16_t w, uint16_t color) {
// Overwrite in subclasses if startWrite is defined! // Overwrite in subclasses if startWrite is defined!
@ -160,6 +164,8 @@ void Adafruit_GFX::endWrite(){
// Overwrite in subclasses if startWrite is defined! // Overwrite in subclasses if startWrite is defined!
} }
// (x,y) is topmost point; if unsure, calling function
// should sort endpoints or call drawLine() instead
void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) { int16_t h, uint16_t color) {
// Update in subclasses if desired! // Update in subclasses if desired!
@ -168,6 +174,8 @@ void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
endWrite(); endWrite();
} }
// (x,y) is leftmost point; if unsure, calling function
// should sort endpoints or call drawLine() instead
void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) { int16_t w, uint16_t color) {
// Update in subclasses if desired! // Update in subclasses if desired!
@ -191,15 +199,15 @@ void Adafruit_GFX::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color); fillRect(0, 0, _width, _height, color);
} }
#define distDiff(a,b) ((max(a,b) - min(a,b))+1)
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) { uint16_t color) {
// Update in subclasses if desired! // Update in subclasses if desired!
if(x0 == x1){ if(x0 == x1){
drawFastVLine(x0, y0, distDiff(y0,y1), color); if(y0 > y1) _swap_int16_t(y0, y1);
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else if(y0 == y1){ } else if(y0 == y1){
drawFastHLine(x0, y0, distDiff(x0,x1), color); if(x0 > x1) _swap_int16_t(x0, x1);
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else { } else {
startWrite(); startWrite();
writeLine(x0, y0, x1, y1, color); writeLine(x0, y0, x1, y1, color);

View File

@ -1,5 +1,5 @@
name=Adafruit GFX Library name=Adafruit GFX Library
version=1.1.8 version=1.1.9
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from. sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from.