diff --git a/Makefile b/Makefile index d8fde77..f35d0af 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ CC = clang -LINK = -lstdc++ -lSDL2 +LINK = -lstdc++ -lSDL2 -lm -ldl -lSDL_gfx DBG = -Wall -O3 CC += $(DBG) all: build ; -goals = bin/main.o bin/Color.o bin/Quad.o bin/Vector.o bin/phys_obj.o bin/BoundingBox.o +goals = bin/main.o bin/Color.o bin/Quad.o bin/Vector.o bin/phys_obj.o bin/BoundingBox.o bin/Circle.o define pro = $(CC) -c $^ -o $@ @@ -23,6 +23,8 @@ bin/Vector.o: src/Vector.cpp $(pro) bin/phys_obj.o: src/phys_obj.cpp $(pro) +bin/Circle.o: src/Circle.cpp + $(pro) #targets = $(patsubst bin/%.o,src/%.cpp,$(goals)) #$(goals): $(targets) ; diff --git a/src/Circle.cpp b/src/Circle.cpp new file mode 100644 index 0000000..0bb0b33 --- /dev/null +++ b/src/Circle.cpp @@ -0,0 +1,41 @@ +#include "Circle.h" +#include + +#ifndef PI +#define PI 3.14159265359 +#endif + +#define toradians(a) a*PI/180 + +Circle::Circle(Vector c, int r) { + center.x = c.x; + center.y = c.y; + radius = r; +} + +void Circle::translate(Vector vec) { + if(vec.x != 0) { + center.x += vec.x; + } + if(vec.y != 0) { + center.y += vec.y; + } +} + +void Circle::setColor(Color c) { + this->color = c; +} + +void Circle::render(SDL_Renderer *renderer) { + uint8_t r,g,b,a; + SDL_GetRenderDrawColor(renderer,&r,&g,&b,&a); + SDL_SetRenderDrawColor(renderer,color.r,color.g,color.b,color.a); + for(int x=center.x-radius; x<=center.x+radius; x++) { + for(int y=center.y-radius; y<=center.y+radius; y++) { + if((pow(center.y-y,2)+pow(center.x-x,2)) <= pow(radius,2)) { + SDL_RenderDrawPoint(renderer,x,y); + } + } + } + SDL_SetRenderDrawColor(renderer,r,g,b,a); +} \ No newline at end of file diff --git a/src/Circle.h b/src/Circle.h new file mode 100644 index 0000000..1cfede5 --- /dev/null +++ b/src/Circle.h @@ -0,0 +1,29 @@ +#include +#include +#include "Vector.h" +#include "Color.h" + +class Circle { + private: + int testpoint(int x,int y) { + return pow(x,2)+pow(y,2)-pow(radius,2); + } + bool incircle(int x,int y) { + return testpoint(x,y) < 0; + } + bool outcircle(int x,int y) { + return testpoint(x,y) > 0; + } + bool oncircle(int x,int y) { + return testpoint(x,y) == 0; + } + public: + Color color; + int x,y,radius; + Vector center; + Circle(Vector center, int radius); + Circle(); + void render(SDL_Renderer *renderer); + void translate(Vector vec); + void setColor(Color c); +}; \ No newline at end of file