diff --git a/C++/Bresenham/Makefile b/C++/Bresenham/Makefile new file mode 100644 index 0000000..0d406bc --- /dev/null +++ b/C++/Bresenham/Makefile @@ -0,0 +1,24 @@ +CXX = g++ +# Update these paths to match your installation +# You may also need to update the linker option rpath, which sets where to look for +# the SDL2 libraries at runtime to match your install +SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib +SDL_INCLUDE = -I/usr/local/include +CXXFLAGS = -Wall -c -std=c++14 $(SDL_INCLUDE) +LDFLAGS = $(SDL_LIB) +EXE = circle + +all: $(EXE) + + +$(EXE): circle.o + + $(CXX) $< $(LDFLAGS) -o $@ + +main.o: circle.cpp + + $(CXX) $(CXXFLAGS) $< -o $@ + +clean: + + rm *.o && rm $(EXE) diff --git a/C++/Bresenham/README.md b/C++/Bresenham/README.md new file mode 100644 index 0000000..8f7a9f3 --- /dev/null +++ b/C++/Bresenham/README.md @@ -0,0 +1,10 @@ +# Bresenham's Algorithm to draw a circle + +## Requirements +This implementation uses SDL 2 to draw the circle on a separate window. So, make sure your system has SDL 2 installed before you run. + +## How to run +This project has an associated makefile. Just run make to build the project and run ./circle executable. + +## About the algorithm +This implementation is based on this [wikipedia page](https://en.wikipedia.org/wiki/Midpoint_circle_algorithm). diff --git a/C++/Bresenham/circle b/C++/Bresenham/circle new file mode 100755 index 0000000..77f50df Binary files /dev/null and b/C++/Bresenham/circle differ diff --git a/C++/Bresenham/circle.cc b/C++/Bresenham/circle.cc new file mode 100644 index 0000000..0deb24e --- /dev/null +++ b/C++/Bresenham/circle.cc @@ -0,0 +1,87 @@ +/** + * Bresenham's Algorithm to draw a circle. + * + * @author rhythm1705 + * @version 12 April 2020 + */ + +#include +#include + +#define WINDOW_SIZE 640 // the lenght and width of the window +#define RADIUS 150 // the radius of the circle + +// Draw point in all octants +void drawPoints(SDL_Renderer *ren, int xc, int yc, int x, int y) { + SDL_RenderDrawPoint(ren, xc + x, yc + y); + SDL_RenderDrawPoint(ren, xc - x, yc + y); + SDL_RenderDrawPoint(ren, xc + x, yc - y); + SDL_RenderDrawPoint(ren, xc - x, yc - y); + SDL_RenderDrawPoint(ren, xc + y, yc + x); + SDL_RenderDrawPoint(ren, xc - y, yc + x); + SDL_RenderDrawPoint(ren, xc + y, yc - x); + SDL_RenderDrawPoint(ren, xc - y, yc - x); + SDL_RenderPresent(ren); + // delay to show animation + SDL_Delay(20); +} + +// Calculate radius error +int radiusError(int x, int y, int r) { + return (x * x) + (y * y) - (r * r); +} + +// Bresenham's Algorithm to draw a circle +void circleBres(SDL_Renderer *ren, int xc, int yc, int r) { + int x = r; + int y = 0; + while (x >= y) { + drawPoints(ren, xc, yc, x, y); + int decision = 2 * (radiusError(x, y, r) + (2 * y) + 1) + (1 - (2 * x)); + y++; + if (decision > 0) { + x--; + } + } +} + +int main() { + SDL_Event event; + // initialize SDL video subsystem + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + std::cout << "SDL_INIT Error: " << SDL_GetError() << std::endl; + return 1; + } + // open a window to draw the circle on + SDL_Window *win = SDL_CreateWindow("Circle using Bresenham's Algorithm", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_SIZE, WINDOW_SIZE, SDL_WINDOW_SHOWN); + if (win == nullptr){ + std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; + SDL_Quit(); + return 1; + } + // create a renderer to render the circle + SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (ren == nullptr){ + SDL_DestroyWindow(win); + std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; + SDL_Quit(); + return 1; + } + // set center and radius of circle + int xc = WINDOW_SIZE / 2; + int yc = WINDOW_SIZE / 2; + int r = RADIUS; + // set color of circle + SDL_SetRenderDrawColor(ren, 255, 0, 0, 255); + // draw circle + circleBres(ren, xc, yc, r); + // quit window on close + while (1) { + if (SDL_PollEvent(&event) && event.type == SDL_QUIT) + break; + } + SDL_DestroyRenderer(ren); + SDL_DestroyWindow(win); + SDL_Quit(); + return 0; +} diff --git a/C++/Bresenham/circle.o b/C++/Bresenham/circle.o new file mode 100644 index 0000000..65ad9fa Binary files /dev/null and b/C++/Bresenham/circle.o differ