commit 292b70002ca334148affe12a9ad5e523011dcec3
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Thu Apr 9 07:36:43 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Thu Apr 9 07:36:43 2020 -0600
Add circ implementation and fix helpers
---
source/fakecart.c | 2 ++
source/graphics.c | 64 ++++++++++++++++++++++++++++++++++++++++++++-----------
source/graphics.h | 3 +++
3 files changed, 57 insertions(+), 12 deletions(-)
diff --git a/source/fakecart.c b/source/fakecart.c
index 03af485..bb6edf1 100644
--- a/source/fakecart.c
+++ b/source/fakecart.c
@@ -44,6 +44,8 @@ void _draw() {
line(20, 30, 0, 20, 12);
line(0, 30, 20, 20, 12);
+
+ circ(90, 90, 30, 8);
}
void _update() {
diff --git a/source/graphics.c b/source/graphics.c
index f0622a1..9dc9269 100644
--- a/source/graphics.c
+++ b/source/graphics.c
@@ -61,6 +61,23 @@ void swap(short *x, short *y) {
return;
}
+void sortPointsLtoR(short *x1, short *y1, short *x2, short *y2){
+ if (*x1 > *x2) {
+ swap(x1, x2);
+ swap(y1, y2);
+ }
+}
+
+void sortCoordsForRect(short *x1, short *y1, short *x2, short *y2){
+ if (*x1 > *x2) {
+ swap(x1, x2);
+ }
+
+ if (*y1 > *y2) {
+ swap(y1, y2);
+ }
+}
+
bool isOnScreen(short *x, short* y) {
return *x >= 0 && *x < 127 && *y >= 0 && *y < 127;
}
@@ -89,10 +106,7 @@ void color(uint16_t col){
}
void line (short x1, short y1, short x2, short y2, uint16_t col) {
- if (x1 > x2) {
- swap(&x1, &x2);
- swap(&y1, &y2);
- }
+ sortPointsLtoR(&x1, &y1, &x2, &y2);
float run = x2 - x1;
float rise = y2 - y1;
@@ -117,12 +131,41 @@ void line (short x1, short y1, short x2, short y2, uint16_t col) {
}
}
-void rect(short x1, short y1, short x2, short y2, uint16_t col) {
- if (x1 > x2) {
- swap(&x1, &x2);
- swap(&y1, &y2);
+void circ(short ox, short oy, short r, uint16_t col){
+ short x = r;
+ short y = 0;
+ short decisionOver2 = 1-x;
+
+ while (y <= x) {
+ pset(ox + x, oy + y, col);
+ pset(ox + y, oy + x, col);
+ pset(ox - x, oy + y, col);
+ pset(ox - y, oy + x, col);
+
+ pset(ox - x, oy - y, col);
+ pset(ox - y, oy - x, col);
+ pset(ox + x, oy - y, col);
+ pset(ox + y, oy - x, col);
+
+ y += 1;
+ if (decisionOver2 < 0) {
+ decisionOver2=decisionOver2+2*y+1;
+ }
+ else {
+ x = x-1;
+ decisionOver2 = decisionOver2 + 2 * (y - x) + 1;
+ }
}
+}
+
+void circfill(short ox, short oy, short r, uint16_t col){
+
+}
+
+void rect(short x1, short y1, short x2, short y2, uint16_t col) {
+ sortCoordsForRect(&x1, &y1, &x2, &y2);
+
for (short i = x1; i <= x2; i++) {
for (short j = y1; j <= y2; j++) {
if ((i == x1 || i == x2 || j == y1 || j == y2) ) {
@@ -134,10 +177,7 @@ void rect(short x1, short y1, short x2, short y2, uint16_t col) {
}
void rectfill(short x1, short y1, short x2, short y2, uint16_t col) {
- if (x1 > x2) {
- swap(&x1, &x2);
- swap(&y1, &y2);
- }
+ sortCoordsForRect(&x1, &y1, &x2, &y2);
for (short i = x1; i <= x2; i++) {
for (short j = y1; j <= y2; j++) {
diff --git a/source/graphics.h b/source/graphics.h
index 5123cc6..92e2d88 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -43,6 +43,9 @@ void color(uint16_t c);
void line (short x1, short y1, short x2, short y2, uint16_t col);
+void circ(short ox, short oy, short r, uint16_t col);
+void circfill(short ox, short oy, short r, uint16_t col);
+
void rect(short x1, short y1, short x2, short y2, uint16_t col);
void rectfill(short x1, short y1, short x2, short y2, uint16_t col);