0 Members and 2 Guests are viewing this topic.

void oled_bdraws(int16_t x, int16_t y, uint8_t width, uint8_t height, const uint8_t bitmap[]) { uint8_t ow = width; if(width + x > BUFFER_WIDTH) width -= width + x - BUFFER_WIDTH; if(height + y > BUFFER_HEIGHT) height -= height + y - BUFFER_HEIGHT; if(width + x <= 0 || height + y <= 0) return; for(int yi = y; yi < (y + height); yi++) { for(int xi = x; xi < (x + width); xi++) { if((yi - y) < 0 || (xi - x) < 0) continue; if(bitmap[(yi - y) * ow + (xi - x)] > 0) screen_buffer[yi * ow + xi] = bitmap[(yi - y) * ow + (xi - x)]; } }}

public void drawCircle(int x, int y, int r) { double angle, x1, y1; for (angle = 0; angle < 90; angle += 0.1) { x1 = r * Math.cos(angle * Math.PI / 180); y1 = r * Math.sin(angle * Math.PI / 180); setPixel((int) (x + x1), (int) (y + y1)); //bottom left setPixel((int) (x + x1), (int) (y - y1)); //top left setPixel((int) (x - x1), (int) (y + y1)); //nottom right setPixel((int) (x - x1), (int) (y - y1)); // top right } }

public void setPixel(int x, int y) { if (x >= 0 && x < width && y >= 0 && y < height) { pixels[x + width * y] = color; } }

public void drawFillCircle(int x, int y, int r) { double angle, x1, y1; for (angle = 0; angle < 90; angle += 0.1) { x1 = r * Math.cos(angle * Math.PI / 180); y1 = r * Math.sin(angle * Math.PI / 180); setPixel((int) (x + x1), (int) (y + y1)); //bottom left setPixel((int) (x - x1), (int) (y + y1)); //nottom right for (double i = x - x1; i <= x + x1; i++) { setPixel((int) i, (int) (y + y1)); setPixel((int) i, (int) (y - y1)); } } }

Yeah thats why I was saying 1-90 instead of 1-360 The 360 was 90*4(for num bytes in a float)=360

#ifdef _MSC_VER#define _CRT_SECURE_NO_WARNINGS#endif#include <math.h>#include <stdio.h>#include <stdlib.h>using namespace std;# define PI 3.14159265358979323846void writeSines() { FILE *myfile; if ((myfile = fopen("C:\\Users\\Andrew\\vs\\sines\\sinesBYTE.txt", "w")) == NULL) { printf("didnt open"); return; } unsigned char tempSin; for (double i = 1.0; i <= 90.0; i += 1.0) { tempSin = (unsigned char)(sin(i / 180.0*PI) * 256); fprintf(myfile, "%d, ", tempSin); double tempcheck = tempSin; } fclose(myfile);}//generated using writeSines(). doesnt include sin(90) because sin(90)*256=256>255, max for byte/unsigned char, so it wraps around to 0, which is pointlessunsigned char sinArray[90] = {0, 4, 8, 13, 17, 22, 26, 31, 35, 40, 44, 48, 53, 57, 61, 66, 70, 74, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 127, 131, 135, 139, 143, 146, 150, 154, 157, 161, 164, 167, 171, 174, 177, 181, 184, 187, 190, 193, 196, 198, 201, 204, 207, 209, 212, 214, 217, 219, 221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 240, 242, 243, 244, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255};void fastSinMult(int *val, int degree) { if (degree == 90) { return; } else if (degree < 90 && degree>=0) { *val = (*val)*sinArray[degree+1]; (*val) = (*val) >> 8; return; } else if (degree > 90 && degree <= 180) { fastSinMult(val, 180 - degree); return; } else if (degree > 180 && degree <= 360) { fastSinMult(val, degree - 180); (*val) *= -1; return; } else if (degree > 360)//not allowing negative degrees for now fastSinMult(val, degree % 360);}void fastCosMult(int *val, int degree) { fastSinMult(val, degree - 90);}void fastTanMult(int *val, int angle) { int val1 = *val; int val2 = *val; fastSinMult(&val1, angle); fastCosMult(&val2, angle); (*val) = val1 / val2;}

Yay! Triangles are awesome! Unfortunately many simple graphics apis don't let you use them, like ti nspire lua. It would be amazing if the microcat api had triangles.Also, I made a fast sin/cos/tan generating program. It doesnt give you the sin value, but forces you to input a value and have it multiplied by the sin, because otherwise it would be annoying to deal with. I have not tested my code yet and don't have time to right now, but here it is:Code: [Select]#ifdef _MSC_VER#define _CRT_SECURE_NO_WARNINGS#endif#include <math.h>#include <stdio.h>#include <stdlib.h>using namespace std;# define PI 3.14159265358979323846void writeSines() { FILE *myfile; if ((myfile = fopen("C:\\Users\\Andrew\\vs\\sines\\sinesBYTE.txt", "w")) == NULL) { printf("didnt open"); return; } unsigned char tempSin; for (double i = 1.0; i <= 90.0; i += 1.0) { tempSin = (unsigned char)(sin(i / 180.0*PI) * 256); fprintf(myfile, "%d, ", tempSin); double tempcheck = tempSin; } fclose(myfile);}//generated using writeSines(). doesnt include sin(90) because sin(90)*256=256>255, max for byte/unsigned char, so it wraps around to 0, which is pointlessunsigned char sinArray[90] = {0, 4, 8, 13, 17, 22, 26, 31, 35, 40, 44, 48, 53, 57, 61, 66, 70, 74, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 127, 131, 135, 139, 143, 146, 150, 154, 157, 161, 164, 167, 171, 174, 177, 181, 184, 187, 190, 193, 196, 198, 201, 204, 207, 209, 212, 214, 217, 219, 221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 240, 242, 243, 244, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255};void fastSinMult(int *val, int degree) { if (degree == 90) { return; } else if (degree < 90 && degree>=0) { *val = (*val)*sinArray[degree+1]; (*val) = (*val) >> 8; return; } else if (degree > 90 && degree <= 180) { fastSinMult(val, 180 - degree); return; } else if (degree > 180 && degree <= 360) { fastSinMult(val, degree - 180); (*val) *= -1; return; } else if (degree > 360)//not allowing negative degrees for now fastSinMult(val, degree % 360);}void fastCosMult(int *val, int degree) { fastSinMult(val, degree - 90);}void fastTanMult(int *val, int angle) { int val1 = *val; int val2 = *val; fastSinMult(&val1, angle); fastCosMult(&val2, angle); (*val) = val1 / val2;}

public void circle(int cx, int cy, int r) { int f = 1-r; int ddFx = 1; int ddFy = -2*r; int x = 0; int y = r; setPixel(cx, cy+r); setPixel(cx, cy-r); setPixel(cx+r, cy); setPixel(cx-r, cy); while (x < y) { if (f>=0) { y--; ddFy += 2; f += ddFy; } x++; ddFx += 2; f += ddFx; setPixel(cx + x, cy + y); setPixel(cx - x, cy + y); setPixel(cx + x, cy - y); setPixel(cx - x, cy - y); setPixel(cx + y, cy + x); setPixel(cx - y, cy + x); setPixel(cx + y, cy - x); setPixel(cx - y, cy - x); } }

public void fcircle(int cx, int cy, int r) { int f = 1-r; int ddFx = 1; int ddFy = -2*r; int x = 0; int y = r; for (int fr=r; fr>0; fr--) { setPixel(cx, cy+fr); setPixel(cx, cy-fr); setPixel(cx+fr, cy); setPixel(cx-fr, cy); } setPixel(cx, cy); while (x < y) { if (f >= 0) { y--; ddFy += 2; f += ddFy; } x++; ddFx += 2; f += ddFx; for (int fx=x; fx>0; fx--) { setPixel(cx + fx, cy + y); setPixel(cx - fx, cy + y); setPixel(cx + fx, cy - y); setPixel(cx - fx, cy - y); } for (int fy=y; fy>0; fy--) { setPixel(cx + fy, cy + x); setPixel(cx - fy, cy + x); setPixel(cx + fy, cy - x); setPixel(cx - fy, cy - x); } } }

Code: [Select] public void circle(int cx, int cy, int r) { int f = 1-r; int ddFx = 1; int ddFy = -2*r; int x = 0; int y = r; setPixel(cx, cy+r); setPixel(cx, cy-r); setPixel(cx+r, cy); setPixel(cx-r, cy); while (x < y) { if (f>=0) { y--; ddFy += 2; f += ddFy; } x++; ddFx += 2; f += ddFx; setPixel(cx + x, cy + y); setPixel(cx - x, cy + y); setPixel(cx + x, cy - y); setPixel(cx - x, cy - y); setPixel(cx + y, cy + x); setPixel(cx - y, cy + x); setPixel(cx + y, cy - x); setPixel(cx - y, cy - x); } }Here's a sine/cosineless circle algoritm (midpoint circle/bresenham). Also you might want to thin about adding translations, since they;re really usefull.

unsigned char sinArray[90] = {0, 4, 8, 13, 17, 22, 26, 31, 35, 40, 44, 48, 53, 57, 61, 66, 70, 74, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 127, 131, 135, 139, 143, 146, 150, 154, 157, 161, 164, 167, 171, 174, 177, 181, 184, 187, 190, 193, 196, 198, 201, 204, 207, 209, 212, 214, 217, 219, 221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 240, 242, 243, 244, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255};void fastSinMult(int *val, int degree) { if (degree == 90) { return; } else if (degree < 90 && degree>=0) { *val = (*val)*sinArray[degree-1]; (*val) = (*val) >> 8; return; } else if (degree > 90 && degree <= 180) { fastSinMult(val, 180 - degree); return; } else if (degree > 180 && degree <= 360) { fastSinMult(val, degree - 180); (*val) *= -1; return; } else if (degree > 360) fastSinMult(val, degree % 360); else { fastSinMult(val, -degree); (*val) = -(*val); }}void fastCosMult(int *val, int degree) { fastSinMult(val, 90-degree);}void fastTanMult(int *val, int degree) { int val1 = *val; int val2 = *val; fastSinMult(&val1, degree); fastCosMult(&val2, degree); (*val) = ((*val) * val1) / val2;}

FacebookandTwitterFollow @Codewalr_us