Skip to content

Instantly share code, notes, and snippets.

@AjayKrP
Created March 13, 2017 12:16
Show Gist options
  • Save AjayKrP/0fc9371f4616ddd5b0ba65f9c01c3ea3 to your computer and use it in GitHub Desktop.
Save AjayKrP/0fc9371f4616ddd5b0ba65f9c01c3ea3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include<iostream>
#define PI 3.14159
// Center of the cicle = (320, 240)
int xc = 320, yc = 240;
// Plot eight points using circle's symmetrical property
float round_value(float v)
{
return floor(v + 0.5);
}
void LineDDA(double X1, double Y1, double X2, double Y2)
{
double dx=(X2-X1);
double dy=(Y2-Y1);
double steps;
float xInc,yInc,x=X1,y=Y1;
steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
glBegin(GL_POINTS);
glVertex2d(x,y);
int k;
for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
glVertex2d(round_value(x), round_value(y));
}
glEnd();
}
void plot_point(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(xc+x, yc+y);
glVertex2i(xc+x, yc-y);
glVertex2i(xc+y, yc+x);
glVertex2i(xc+y, yc-x);
glVertex2i(xc-x, yc-y);
glVertex2i(xc-y, yc-x);
glVertex2i(xc-x, yc+y);
glVertex2i(xc-y, yc+x);
glEnd();
}
// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
int x=0,y=r;
float pk=(5.0/4.0)-r;
/* Plot the points */
/* Plot the first point */
plot_point(x,y);
/* Find all vertices till x=y */
while(x < y)
{
x = x + 1;
if(pk < 0)
pk = pk + 2*x+1;
else
{
y = y - 1;
pk = pk + 2*(x - y) + 1;
}
plot_point(x,y);
}
glFlush();
}
void draw_triangle(int r){
float length_x = r*cos(PI/6);
float length_y = r*sin(PI/6);
/*glBegin(GL_TRIANGLES);
glVertex2d((xc - length_x),(yc - length_y));
glVertex2d((xc + length_x), (yc - length_y));
glVertex2d(xc, (yc + r));
glEnd();*/
LineDDA((xc - length_x),(yc - length_y),(xc + length_x),(yc - length_y));
LineDDA((xc + length_x),(yc - length_y),(xc),(yc + r));
LineDDA((xc ),(yc + r),(xc - length_x),(yc - length_y));
}
// Function to draw two concentric circles
void concentric_circles(void)
{
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
int radius1 = 100, radius2 = 200;
bresenham_circle(radius1);
draw_triangle(radius1);
draw_triangle(radius2);
bresenham_circle(radius2);
}
void Init()
{
/* Set clear color to white */
glClearColor(1.0,1.0,0.0,0);
/* Set fill color to black */
glColor3f(1.0,0.0,0.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
int main(int argc, char **argv)
{
/* Initialise GLUT library */
glutInit(&argc,argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
/* Create the window with title "DDA_Line" */
glutCreateWindow("bresenham_circle");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
glutDisplayFunc(concentric_circles);
/* Keep displaying untill the program is closed */
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment