void SharpMemory::drawPixel(int16_t x, int16_t y, uint16_t color) { // Do nothing if outside of the bounds if((x < 0) || (x >= DISPLAY_WIDTH) || (y < 0) || (y >= DISPLAY_HEIGHT)) return; switch(rotation) { case 1: _swap_int16_t(x, y); x = WIDTH - 1 - x; break; case 2: x = WIDTH - 1 - x; y = HEIGHT - 1 - y; break; case 3: _swap_int16_t(x, y); y = HEIGHT - 1 - y; break; } hasChanges = true; lineHasChanges[y] = true; // If writing a black pixel if(color == BLACK) { // Clear the selected bit frameBuffer[y][x/8] &= clr[x % 8]; } else { // Set the selected bit frameBuffer[y][x/8] |= set[x % 8]; } }
// Bresenham's algorithm - thx wikpedia void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) { int16_t steep = abs(y1 - y0) > abs(x1 - x0); if (steep) { _swap_int16_t(x0, y0); _swap_int16_t(x1, y1); } if (x0 > x1) { _swap_int16_t(x0, x1); _swap_int16_t(y0, y1); } int16_t dx, dy; dx = x1 - x0; dy = abs(y1 - y0); int16_t err = dx / 2; int16_t ystep; if (y0 < y1) { ystep = 1; } else { ystep = -1; } for (; x0<=x1; x0++) { if (steep) { drawPixel(y0, x0, color); } else { drawPixel(x0, y0, color); } err -= dy; if (err < 0) { y0 += ystep; err += dx; } } }
void Adafruit_IS31FL3731_Wing::drawPixel(int16_t x, int16_t y, uint16_t color) { // check rotation, move pixel around if necessary switch (getRotation()) { case 1: _swap_int16_t(x, y); x = 15 - x - 1; break; case 2: x = 15 - x - 1; y = 7 - y - 1; break; case 3: _swap_int16_t(x, y); y = 9 - y - 1; break; } // charlie wing is smaller //if (x > 15) return; //if (y > 7) return; if (x > 7) { x=15-x; y += 8; } else { y = 7-y; } _swap_int16_t(x, y); if ((x < 0) || (x >= 16)) return; if ((y < 0) || (y >= 9)) return; if (color > 255) color = 255; // PWM 8bit max setLEDPWM(x + y*16, color, _frame); return; }
void Adafruit_IS31FL3731::drawPixel(int16_t x, int16_t y, uint16_t color) { // check rotation, move pixel around if necessary switch (getRotation()) { case 1: _swap_int16_t(x, y); x = 16 - x - 1; break; case 2: x = 16 - x - 1; y = 9 - y - 1; break; case 3: _swap_int16_t(x, y); y = 9 - y - 1; break; } if ((x < 0) || (x >= 16)) return; if ((y < 0) || (y >= 9)) return; if (color > 255) color = 255; // PWM 8bit max setLEDPWM(x + y*16, color, _frame); return; }
void Adafruit_SharpMem::drawPixel(int16_t x, int16_t y, uint16_t color) { if((x < 0) || (x >= _width) || (y < 0) || (y >= _height)) return; switch(rotation) { case 1: _swap_int16_t(x, y); x = WIDTH - 1 - x; break; case 2: x = WIDTH - 1 - x; y = HEIGHT - 1 - y; break; case 3: _swap_int16_t(x, y); y = HEIGHT - 1 - y; break; default: break; } #if defined __AVR__ if(color) { sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) / 8] |= pgm_read_byte(&set[x & 7]); } else { sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) / 8] &= pgm_read_byte(&clr[x & 7]); } #else if (color) sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) /8] |= (1 << x % 8); else sharpmem_buffer[(y*SHARPMEM_LCDWIDTH + x) /8] &= ~(1 << x % 8); #endif }
// Fill a triangle void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) { int16_t a, b, y, last; // Sort coordinates by Y order (y2 >= y1 >= y0) if (y0 > y1) { _swap_int16_t(y0, y1); _swap_int16_t(x0, x1); } if (y1 > y2) { _swap_int16_t(y2, y1); _swap_int16_t(x2, x1); } if (y0 > y1) { _swap_int16_t(y0, y1); _swap_int16_t(x0, x1); } if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing a = b = x0; if(x1 < a) a = x1; else if(x1 > b) b = x1; if(x2 < a) a = x2; else if(x2 > b) b = x2; drawFastHLine(a, y0, b-a+1, color); return; } int16_t dx01 = x1 - x0, dy01 = y1 - y0, dx02 = x2 - x0, dy02 = y2 - y0, dx12 = x2 - x1, dy12 = y2 - y1; int32_t sa = 0, sb = 0; // For upper part of triangle, find scanline crossings for segments // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 // is included here (and second loop will be skipped, avoiding a /0 // error there), otherwise scanline y1 is skipped here and handled // in the second loop...which also avoids a /0 error here if y0=y1 // (flat-topped triangle). if(y1 == y2) last = y1; // Include y1 scanline else last = y1-1; // Skip it for(y=y0; y<=last; y++) { a = x0 + sa / dy01; b = x0 + sb / dy02; sa += dx01; sb += dx02; /* longhand: a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); */ if(a > b) _swap_int16_t(a,b); drawFastHLine(a, y, b-a+1, color); } // For lower part of triangle, find scanline crossings for segments // 0-2 and 1-2. This loop is skipped if y1=y2. sa = dx12 * (y - y1); sb = dx02 * (y - y0); for(; y<=y2; y++) { a = x1 + sa / dy12; b = x0 + sb / dy02; sa += dx12; sb += dx02; /* longhand: a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); */ if(a > b) _swap_int16_t(a,b); drawFastHLine(a, y, b-a+1, color); } }