Skip to content

Instantly share code, notes, and snippets.

@mimukit
Created June 13, 2016 06:41
Show Gist options
  • Save mimukit/eac9ab21dd8e58ed3b6037faeba1952b to your computer and use it in GitHub Desktop.
Save mimukit/eac9ab21dd8e58ed3b6037faeba1952b to your computer and use it in GitHub Desktop.
Midpoint Circle Drawing Algorithm Implementation using OpenGL
#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();
}
@tavik000
Copy link

THANK YOU

@nightHawk26
Copy link

THANK YOU so much, really helping for my assignment

@CleverFool77
Copy link

void draw_circle(Point p1,GLint radius)
{
    GLint x = 0;
    GLint y = radius;
    GLint p = 1- radius;

    void callcircle(Point , GLint ,GLint );

    callcircle(p1,x,y);

    while(x < y)
    {
        x++;
         if(p < 0)
        {
            p += 2*x +1;
        }
        else{
            y--;
            p += 2*(x-y)+1;
        }
        callcircle(p1,x,y);
    }
    
}
void callcircle(Point p, GLint x, GLint y)
{
		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.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);
		
}

It's not showing full circle when i used mid-point, Can I know what's the problem ?

@manikya-aravind
Copy link

THANKS FOR HELPING IN MY ASSIGNMENT :)

@skj-skj
Copy link

skj-skj commented Oct 9, 2019

thank you,
just a suggestion in line 83:
glutCreateWindow ("Midpoint Algorithm");

@andraantariksa
Copy link

@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