Created
June 13, 2016 06:41
-
-
Save mimukit/eac9ab21dd8e58ed3b6037faeba1952b to your computer and use it in GitHub Desktop.
Midpoint Circle Drawing Algorithm Implementation using OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <GL/glut.h> | |
using namespace std; | |
int pntX1, pntY1, r; | |
void plot(int x, int y) | |
{ | |
glBegin(GL_POINTS); | |
glVertex2i(x+pntX1, y+pntY1); | |
glEnd(); | |
} | |
void myInit (void) | |
{ | |
glClearColor(1.0, 1.0, 1.0, 0.0); | |
glColor3f(0.0f, 0.0f, 0.0f); | |
glPointSize(4.0); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluOrtho2D(0.0, 640.0, 0.0, 480.0); | |
} | |
void midPointCircleAlgo() | |
{ | |
int x = 0; | |
int y = r; | |
float decision = 5/4 - r; | |
plot(x, y); | |
while (y > x) | |
{ | |
if (decision < 0) | |
{ | |
x++; | |
decision += 2*x+1; | |
} | |
else | |
{ | |
y--; | |
x++; | |
decision += 2*(x-y)+1; | |
} | |
plot(x, y); | |
plot(x, -y); | |
plot(-x, y); | |
plot(-x, -y); | |
plot(y, x); | |
plot(-y, x); | |
plot(y, -x); | |
plot(-y, -x); | |
} | |
} | |
void myDisplay(void) | |
{ | |
glClear (GL_COLOR_BUFFER_BIT); | |
glColor3f (0.0, 0.0, 0.0); | |
glPointSize(1.0); | |
midPointCircleAlgo(); | |
glFlush (); | |
} | |
void main(int argc, char** argv) | |
{ | |
cout << "Enter the coordinates of the center:\n\n" << endl; | |
cout << "X-coordinate : "; cin >> pntX1; | |
cout << "\nY-coordinate : "; cin >> pntY1; | |
cout << "\nEnter radius : "; cin >> r; | |
glutInit(&argc, argv); | |
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize (640, 480); | |
glutInitWindowPosition (100, 150); | |
glutCreateWindow ("Line Drawing Alogrithms"); | |
glutDisplayFunc(myDisplay); | |
myInit (); | |
glutMainLoop(); | |
} | |
@CleverFool77 you're overwriting the same pixel. Try this
glVertex2f(p.x + x, p.y + y);
glVertex2f(p.x - x, p.y + y);
glVertex2f(p.x + x, p.y - y);
glVertex2f(p.x - x, p.y - y);
glVertex2f(p.y + x, p.x + y);
glVertex2f(p.y - x, p.x + y);
glVertex2f(p.y + x, p.x - y);
glVertex2f(p.y - x, p.x - y);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you,
just a suggestion in line 83:
glutCreateWindow ("Midpoint Algorithm");