created circle type

This commit is contained in:
Solomon W. 2020-05-31 21:57:47 -04:00
parent 7de795a48f
commit c65ae423f3
3 changed files with 74 additions and 2 deletions

View File

@ -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) ;

41
src/Circle.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "Circle.h"
#include <SDL2/SDL2_gfxPrimitives.h>
#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);
}

29
src/Circle.h Normal file
View File

@ -0,0 +1,29 @@
#include <SDL2/SDL.h>
#include <cmath>
#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);
};