Skip to content

Commit 04227a0

Browse files
author
Martin Klang
committed
added draw/fillCircle
1 parent cbccbe2 commit 04227a0

1 file changed

Lines changed: 52 additions & 26 deletions

File tree

LibSource/ScreenBuffer.h

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,59 @@ ScreenBuffer(uint16_t w, uint16_t h) :
196196
}
197197
}
198198

199-
// void invert();
200-
// void invert(int x, int y, int width, int height);
201-
// void draw(int x, int y, ScreenBuffer& pixels);
202-
203-
// // lines and rectangles
204-
// void drawLine(int fromX, int fromY, int toX, int toY, Colour c);
205-
// void drawVerticalLine(int x, int y, int length, Colour c);
206-
// void drawHorizontalLine(int x, int y, int length, Colour c);
207-
// void drawRectangle(int x, int y, int width, int height, Colour c);
208-
// void fillRectangle(int x, int y, int width, int height, Colour c);
209-
210-
// void setCursor(uint16_t x, uint16_t y);
211-
// void setTextColour(Colour c);
212-
// void setTextColour(Colour fg, Colour bg);
213-
// void setTextSize(uint8_t s);
214-
// void setTextWrap(bool w);
215-
// // void setFont(int font, int size);
216-
// void write(uint8_t c);
217-
// void print(const char* str);
218-
// void print(int num);
219-
// void print(float num);
220-
// static ScreenBuffer* create(uint16_t width, uint16_t height);
199+
void drawCircle(uint16_t x, uint16_t y, uint16_t r, Colour c){
200+
/*
201+
* Bresenhams midpoint circle algorithm AKA "Make turbo C great again!"
202+
* We don't use floating point or any slow maths to find circle points.
203+
* But since we draw it around center, circles with even radius become
204+
* slightly asymmetric.
205+
*/
206+
int16_t tx = r;
207+
int16_t ty = 0;
208+
int16_t err = 0;
209+
while (tx >= ty) {
210+
setPixel(x + tx, y + ty, c); // p1
211+
setPixel(x + ty, y + tx, c); // p2
212+
setPixel(x - ty, y + tx, c); // p3
213+
setPixel(x - tx, y + ty, c); // p4
214+
setPixel(x - tx, y - ty, c); // p5
215+
setPixel(x - ty, y - tx, c); // p6
216+
setPixel(x + ty, y - tx, c); // p7
217+
setPixel(x + tx, y - ty, c); // p8
218+
if (err <= 0){
219+
ty += 1;
220+
err += 2 * ty + 1;
221+
}
222+
if (err >= 0){
223+
tx -= 1;
224+
err -= 2 * tx + 1;
225+
}
226+
}
227+
}
221228

222-
223-
// // todo: load font as resources et c
224-
// void drawChar(uint16_t x, uint16_t y, unsigned char c, Colour fg, Colour bg, uint8_t size);
225-
// void drawRotatedChar(uint16_t x, uint16_t y, unsigned char c, Colour fg, Colour bg, uint8_t size);
229+
void fillCircle(uint16_t x, uint16_t y, uint16_t r, Colour c){
230+
/*
231+
* This is based of code from drawCircle, but we connect circle's points
232+
* with horizontal lines
233+
*/
234+
int16_t tx = r;
235+
int16_t ty = 0;
236+
int16_t err = 0;
237+
while (tx >= ty) {
238+
drawHorizontalLine(x - tx, y + ty, tx * 2 + 1, c); // p4 -> p1
239+
drawHorizontalLine(x - ty, y + tx, ty * 2 + 1, c); // p3 -> p2
240+
drawHorizontalLine(x - tx, y - ty, tx * 2 + 1, c); // p5 -> p8
241+
drawHorizontalLine(x - ty, y - tx, ty * 2 + 1, c); // p6 -> p7
242+
if (err <= 0){
243+
ty += 1;
244+
err += 2 * ty + 1;
245+
}
246+
if (err >= 0){
247+
tx -= 1;
248+
err -= 2 * tx + 1;
249+
}
250+
}
251+
}
226252

227253
// Draw a character
228254
void drawChar(uint16_t x, uint16_t y, unsigned char ch,

0 commit comments

Comments
 (0)