From 6474b43217985701775a9abe9450067ee46c9273 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 3 Apr 2015 22:31:16 +0200 Subject: [PATCH] Added DrawCircle and DrawLine api updated for color --- include/graphics.h | 6 ++++-- lib/graphics.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/include/graphics.h b/include/graphics.h index 9e10f0d..7c48f47 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -65,8 +65,10 @@ private: // Returns how far we advance on the screen. int DrawText(Canvas *c, const Font &font, int x, int y, const Color &color, const char *utf8_text); - -// lines, circles and stuff. +// Draw a circle centered at "x0", "y0", with a radius of "radius" and with "color" +void DrawCircle(Canvas *c, int32_t x0, int32_t y0, int32_t radius, const Color &color); +// Draw a line from "x0", "y0" to "x1", "y1" and with "color" +void DrawLine(Canvas *c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, const Color &color); } // namespace rgb_matrix diff --git a/lib/graphics.cc b/lib/graphics.cc index 930cf63..25a098b 100644 --- a/lib/graphics.cc +++ b/lib/graphics.cc @@ -15,6 +15,8 @@ #include "graphics.h" #include "utf8-internal.h" +#include +#include namespace rgb_matrix { int DrawText(Canvas *c, const Font &font, @@ -27,4 +29,56 @@ int DrawText(Canvas *c, const Font &font, } return x - start_x; } + + +void DrawCircle(Canvas *c, int32_t x0, int32_t y0, int32_t radius, const Color &color){ +int32_t x = radius, y = 0; +int32_t radiusError = 1 - x; + + while (y <= x){ + c->SetPixel(x + x0, y + y0, color.r, color.g, color.b); + c->SetPixel(y + x0, x + y0, color.r, color.g, color.b); + c->SetPixel(-x + x0, y + y0, color.r, color.g, color.b); + c->SetPixel(-y + x0, x + y0, color.r, color.g, color.b); + c->SetPixel(-x + x0, -y + y0, color.r, color.g, color.b); + c->SetPixel(-y + x0, -x + y0, color.r, color.g, color.b); + c->SetPixel(x + x0, -y + y0, color.r, color.g, color.b); + c->SetPixel(y + x0, -x + y0, color.r, color.g, color.b); + y++; + if (radiusError<0){ + radiusError += 2 * y + 1; + } else { + x--; + radiusError+= 2 * (y - x + 1); + } + } } + +void DrawLine(Canvas *c, int32_t x0, int32_t y0, int32_t x1, int32_t y1, const Color &color){ + int32_t dy = y1 - y0, dx = x1 - x0, gradient, x, y, shift = 0x10; + + if (abs(dx) > abs(dy)) { + // x variation is bigger than y variation + if (x1 < x0) { + std::swap(x0, x1); + std::swap(y0, y1); + } + gradient = (dy << shift) / dx ; + + for (x = x0 , y = 0x8000 + (y0 << shift); x <= x1; ++x, y += gradient) { + c->SetPixel(x, y >> shift, color.r, color.g, color.b); + } + } else { + // y variation is bigger than x variation + if (y1 < y0) { + std::swap(x0, x1); + std::swap(y0, y1); + } + gradient = (dx << shift) / dy; + for (y = y0 , x = 0x8000 + (x0 << shift); y <= y1; ++y, x += gradient) { + c->SetPixel(x >> shift, y, color.r, color.g, color.b); + } + } +} + +}//namespace