Skip to content

Commit 653020c

Browse files
added a drawCircle method
1 parent 62a4d3e commit 653020c

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111

1212
### Added
1313
- Added support for the Arduino Zero and Due boards (and related)
14+
- Added method to LEDImage for drawing circles
1415

1516
## [1.0.1] - 2017-12-24
1617
### Changed

src/LEDImage.h

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ class MutableLEDImage : public LEDImageBase<PixelType> {
195195

196196
using LEDImageBase<PixelType>::pixel;
197197
PixelType& pixel( int row, int column );
198-
198+
void setPixel( int row, int column, PixelType color );
199+
199200
void placeImageAt( const LEDImageBase<PixelType>& image, int row, int column );
200201
void paintColor( PixelType color );
201202
void drawLine(
@@ -214,6 +215,13 @@ class MutableLEDImage : public LEDImageBase<PixelType> {
214215
bool fill = true
215216
);
216217

218+
void drawCircle(
219+
int centerRow,
220+
int centerColumn,
221+
int radius,
222+
PixelType color,
223+
bool fill = true
224+
);
217225
};
218226

219227
template <class PixelType, PixelType LEDBlackColor, PixelType LEDTransparentColor>
@@ -282,6 +290,23 @@ PixelType& MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::pixel(
282290
return _data[row*this->columns() + column];
283291
}
284292

293+
template <class PixelType, PixelType LEDBlackColor, PixelType LEDTransparentColor>
294+
void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::setPixel(
295+
int row,
296+
int column,
297+
PixelType color
298+
) {
299+
if ( (row < 0)
300+
||(column < 0)
301+
||(row >= this->rows())
302+
||(column >= this->columns())
303+
) {
304+
return;
305+
}
306+
307+
this->pixel(row, column) = color;
308+
}
309+
285310
template <class PixelType, PixelType LEDBlackColor, PixelType LEDTransparentColor>
286311
void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::placeImageAt(
287312
const LEDImageBase<PixelType>& image,
@@ -345,6 +370,15 @@ void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::drawLine(
345370
PixelType color
346371
)
347372
{
373+
if (
374+
((startRow < 0)&&(stopRow < 0))
375+
||((startRow >= this->rows())&&(stopRow >= this->rows()))
376+
||((startColumn < 0)&&(stopColumn < 0))
377+
||((startColumn >= this->columns())&&(stopColumn >= this->columns()))
378+
) {
379+
// the line is assuredly completely off screen. do nothing
380+
return;
381+
}
348382
_dirty = true;
349383
if ( stopColumn != startColumn ) {
350384
float delta_col = stopColumn - startColumn;
@@ -356,7 +390,7 @@ void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::drawLine(
356390
delta_col < 0 ? col-- : col++ )
357391
{
358392
int row = round(startRow + delta_row*(col - startColumn)/delta_col);
359-
this->pixel(row,col) = color;
393+
this->setPixel(row, col, color);
360394
}
361395
}
362396
else {
@@ -365,7 +399,7 @@ void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::drawLine(
365399
delta_row < 0 ? row-- : row++ )
366400
{
367401
int col = round(startColumn + delta_col*(row - startRow)/delta_row);
368-
this->pixel(row,col) = color;
402+
this->setPixel(row, col, color);
369403
}
370404

371405
}
@@ -377,7 +411,7 @@ void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::drawLine(
377411
delta_row < 0 ? row >= stopRow : row <= stopRow;
378412
delta_row < 0 ? row-- : row++ )
379413
{
380-
this->pixel(row,startColumn) = color;
414+
this->setPixel(row, startColumn, color);
381415
}
382416
}
383417
}
@@ -418,4 +452,88 @@ void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::drawRectangle
418452
}
419453
}
420454

455+
template <class PixelType, PixelType LEDBlackColor, PixelType LEDTransparentColor>
456+
void MutableLEDImage<PixelType,LEDBlackColor,LEDTransparentColor>::drawCircle(
457+
int cRow,
458+
int cCol,
459+
int radius,
460+
PixelType color,
461+
bool fill
462+
)
463+
{
464+
if (radius == 0) {
465+
this->setPixel(cRow, cCol, color);
466+
return;
467+
}
468+
469+
470+
this->setPixel(cRow + radius, cCol, color);
471+
this->setPixel(cRow - radius, cCol, color);
472+
this->setPixel(cRow, cCol + radius, color);
473+
this->setPixel(cRow, cCol - radius, color);
474+
475+
int r2 = radius*radius;
476+
int x = 1;
477+
int y = (int)(sqrtf(r2 - 1) + 0.5);
478+
while (x < y){
479+
if (fill) {
480+
this->drawLine(
481+
cRow + y, cCol + x,
482+
cRow - y, cCol + x,
483+
color
484+
);
485+
this->drawLine(
486+
cRow + y, cCol - x,
487+
cRow - y, cCol - x,
488+
color
489+
);
490+
this->drawLine(
491+
cRow + x, cCol + y,
492+
cRow - x, cCol + y,
493+
color
494+
);
495+
this->drawLine(
496+
cRow + x, cCol - y,
497+
cRow - x, cCol - y,
498+
color
499+
);
500+
} else {
501+
this->setPixel(cRow + y, cCol + x, color);
502+
this->setPixel(cRow + y, cCol - x, color);
503+
this->setPixel(cRow - y, cCol + x, color);
504+
this->setPixel(cRow - y, cCol - x, color);
505+
this->setPixel(cRow + x, cCol + y, color);
506+
this->setPixel(cRow + x, cCol - y, color);
507+
this->setPixel(cRow - x, cCol + y, color);
508+
this->setPixel(cRow - x, cCol - y, color);
509+
}
510+
511+
x += 1;
512+
y = (int)(sqrtf(r2 - x*x) + 0.5);
513+
}
514+
515+
if (x == y) {
516+
if (fill) {
517+
this->drawLine(
518+
cRow + y, cCol + x,
519+
cRow - y, cCol + x,
520+
color
521+
);
522+
this->drawLine(
523+
cRow + y, cCol - x,
524+
cRow - y, cCol - x,
525+
color
526+
);
527+
} else {
528+
this->setPixel(cRow + y, cCol + x, color);
529+
this->setPixel(cRow + y, cCol - x, color);
530+
this->setPixel(cRow - y, cCol + x, color);
531+
this->setPixel(cRow - y, cCol - x, color);
532+
}
533+
}
534+
535+
if (fill) {
536+
this->drawCircle(cRow, cCol, radius-1, color, fill);
537+
}
538+
}
421539
#endif //__LEDIMAGE_H__

0 commit comments

Comments
 (0)