Skip to content

Fix to Issue #6 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Font KEYWORD1
Image KEYWORD1

##########################################
# Methods and Functions
##########################################
# Methods and Functions
##########################################

begin KEYWORD2
end KEYWORD2
Expand All @@ -33,6 +33,9 @@ line KEYWORD2
point KEYWORD2
quad KEYWORD2
rect KEYWORD2
ellipse KEYWORD2
circle KEYWORD2
arc KEYWORD2

text KEYWORD2
textFont KEYWORD2
Expand Down
93 changes: 91 additions & 2 deletions src/ArduinoGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,95 @@ void ArduinoGraphics::rect(int x, int y, int width, int height)
}
}

void ArduinoGraphics::ellipse(int x, int y, int width, int height)
{
if (!_stroke && !_fill) {
return;
}

int x1 = x;
int y1 = y;
int r1 = (int)(width/2);
int r2 = (int)(height/2);
int x2 = x1 + r1 - 1;

for (x = x1; x <= x2; x++) {
y2 = (int) (y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2);
for (y = y1; y <= y2; y++) {
if ((y == y2) && _stroke) {
// stroke
set(x, y, _strokeR, _strokeG, _strokeB); // current point
set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection
set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection
set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection
} else if (_fill) {
// fill
set(x, y, _fillR, _fillG, _fillB); // current point
set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection
set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection
set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection
}
}
}
}

void ArduinoGraphics::circle(int x, int y, int radius)
{
if (!_stroke && !_fill) {
return;
}

int x1 = x;
int y1 = y;
int x2 = x1 + radius;

for (x = x1; x <= x2; x++) {
y2 = (int) y1 + sqrt((radius*radius)-(x*x)) ;
for (y = y1; y <= y2; y++) {
if ((y == y2) && _stroke) {
// stroke
set(x, y, _strokeR, _strokeG, _strokeB); // current point
set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection
set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection
set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection
} else if (_fill) {
// fill
set(x, y, _fillR, _fillG, _fillB); // current point
set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection
set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection
set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection
}
}
}
}

void ArduinoGraphics::arc(int x, int y, int radiusX, int radiusY, int start, int stop);
{
if (!_stroke && !_fill) {
return;
}

int x1 = x;
int y1 = y;

for(int a = start; a <= stop; a++) {

int x2 = (int)(x1 + (radiusX * cos(a)));
int y2 = (int)(y1 + (radiusY * sin(a)));

if (_stroke) {
// stroke
set(x2, y2, _strokeR, _strokeG, _strokeB);
}

if (_fill) {
for (int r = 0; r < a; r++) {
set((int)(x1 + (radiusX * cos(r))), (int)(y1 + (radiusY * sin(r))), _fillR, _fillG, _fillB);
}
}
}
}

void ArduinoGraphics::text(const char* str, int x, int y)
{
if (!_font || !_stroke) {
Expand Down Expand Up @@ -365,7 +454,7 @@ void ArduinoGraphics::beginText(int x, int y, uint8_t r, uint8_t g, uint8_t b)

_textR = r;
_textG = g;
_textB = b;
_textB = b;
}

void ArduinoGraphics::beginText(int x, int y, uint32_t color)
Expand Down Expand Up @@ -482,7 +571,7 @@ void ArduinoGraphics::lineHigh(int x1, int y1, int x2, int y2)
xi = -1;
dx = -dx;
}

int D = 2 * dx - dy;
int x = x1;

Expand Down
7 changes: 5 additions & 2 deletions src/ArduinoGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <Arduino.h>

#include <math.h> // added for sqrt, cos and sin functions

#include "Font.h"
#include "Image.h"

Expand Down Expand Up @@ -57,13 +59,14 @@ class ArduinoGraphics : public Print {
void stroke(uint32_t color);
void noStroke();

//virtual void arc(int x, int y, int width, int height, int start, int stop);
//virtual void ellipse(int x, int y, int width, int height);
virtual void line(int x1, int y1, int x2, int y2);
virtual void point(int x, int y);
//virtual void quad(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
//virtual void triangle(int x1, int y1, int x2, int y2, int x3, int y3);
virtual void rect(int x, int y, int width, int height);
virtual void ellipse(int x, int y, int width, int height);
virtual void circle(int x, int y, int radius);
virtual void arc(int x, int y, int radiusX, int radiusY, int start, int stop);

virtual void text(const char* str, int x = 0, int y = 0);
virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); }
Expand Down