Add drawLine function
This commit is contained in:
@@ -126,6 +126,57 @@ void BitmapBuffer::drawVerticalLine(coord_t x, coord_t y, coord_t h, uint8_t pat
|
||||
}
|
||||
}
|
||||
|
||||
void BitmapBuffer::drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t pat, LcdFlags att)
|
||||
{
|
||||
// Offsets
|
||||
x1 += this->offsetX;
|
||||
y1 += this->offsetY;
|
||||
x2 += this->offsetX;
|
||||
y2 += this->offsetY;
|
||||
|
||||
pixel_t color = lcdColorTable[COLOR_IDX(att)];
|
||||
|
||||
int dx = x2 - x1; /* the horizontal distance of the line */
|
||||
int dy = y2 - y1; /* the vertical distance of the line */
|
||||
int dxabs = abs(dx);
|
||||
int dyabs = abs(dy);
|
||||
int sdx = sgn(dx);
|
||||
int sdy = sgn(dy);
|
||||
int x = dyabs >> 1;
|
||||
int y = dxabs >> 1;
|
||||
int px = x1;
|
||||
int py = y1;
|
||||
|
||||
if (dxabs >= dyabs) {
|
||||
/* the line is more horizontal than vertical */
|
||||
for (int i = 0; i <= dxabs; i++) {
|
||||
if ((1 << (px % 8)) & pat) {
|
||||
drawPixel(px, py, color);
|
||||
}
|
||||
y += dyabs;
|
||||
if (y >= dxabs) {
|
||||
y -= dxabs;
|
||||
py += sdy;
|
||||
}
|
||||
px += sdx;
|
||||
}
|
||||
} else {
|
||||
/* the line is more vertical than horizontal */
|
||||
for (int i = 0; i <= dyabs; i++) {
|
||||
if ((1 << (py % 8)) & pat) {
|
||||
drawPixel(px, py, color);
|
||||
}
|
||||
x += dxabs;
|
||||
if (x >= dyabs) {
|
||||
x -= dyabs;
|
||||
px += sdx;
|
||||
}
|
||||
py += sdy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BitmapBuffer::drawRect(coord_t x, coord_t y, coord_t w, coord_t h, uint8_t thickness, uint8_t pat, LcdFlags att)
|
||||
{
|
||||
for (int i=0; i<thickness; i++) {
|
||||
@@ -627,6 +678,8 @@ coord_t BitmapBuffer::drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags fla
|
||||
return drawText(x, y, s, flags);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void drawSolidRect(BitmapBuffer * dc, coord_t x, coord_t y, coord_t w, coord_t h, uint8_t thickness, LcdFlags att)
|
||||
{
|
||||
dc->drawSolidFilledRect(x, y, thickness, h, att);
|
||||
|
||||
@@ -327,6 +327,8 @@ class BitmapBuffer: public BitmapBufferBase<pixel_t>
|
||||
|
||||
void drawVerticalLine(coord_t x, coord_t y, coord_t h, uint8_t pat = SOLID, LcdFlags flags = 0);
|
||||
|
||||
void drawLine(coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t pat, LcdFlags att);
|
||||
|
||||
inline void drawSolidHorizontalLine(coord_t x, coord_t y, coord_t w, LcdFlags flags)
|
||||
{
|
||||
drawSolidFilledRect(x, y, w, 1, flags);
|
||||
|
||||
@@ -53,6 +53,12 @@ inline int divRoundClosest(const int n, const int d)
|
||||
return ((n < 0) ^ (d < 0)) ? ((n - d/2)/d) : ((n + d/2)/d);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T sgn(T a)
|
||||
{
|
||||
return a > 0 ? 1 : (a < 0 ? -1 : 0);
|
||||
}
|
||||
|
||||
// TODO duplicated code
|
||||
inline std::string TEXT_AT_INDEX(const char * val, uint8_t idx)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user