Skip to content

Instantly share code, notes, and snippets.

@abrarShariar
Created July 9, 2018 04:18
Show Gist options
  • Save abrarShariar/8c9cc73cdd2af620cc7366170526309f to your computer and use it in GitHub Desktop.
Save abrarShariar/8c9cc73cdd2af620cc7366170526309f to your computer and use it in GitHub Desktop.
Circle (8 quad) - Computer Graphics
#include<iostream>
#include <GL/gl.h>
#include <GL/glut.h>
using namespace std;
int R;
int h, k;
int startX, startY;
void drawAxes(){
glBegin(GL_LINES);
glVertex2i(0, 300);
glVertex2i(0, -300);
glEnd();
glBegin(GL_LINES);
glVertex2i(-300, 0);
glVertex2i(300, 0);
glEnd();
}
//8th quad
void draw8thQuad(){
int x1 = R + k;
int y1 = 0 + h;
int h = 1 - R;
while(x1 > y1){
//print points
glBegin(GL_POINTS);
glVertex2i(x1, -y1);
glEnd();
if(h <= 0) {
//go e
y1 = y1 + 1;
//update h
h = h + (2* y1) + 3;
} else {
//go se
y1 = y1 + 1;
x1 = x1 - 1;
//update h
h = h + 2 * (y1 - x1) + 5;
}
}
}
//6th quad
void draw6thQuad(){
int x1 = 0 + h;
int y1 = R + k;
int h = 1 - R;
//startX = x1;
//startY = y1;
while(y1 > x1){
//print points
glBegin(GL_POINTS);
glVertex2i(-x1, -y1);
glEnd();
if(h <= 0) {
//go e
x1 = x1 + 1;
//update h
h = h + (2* x1) + 3;
} else {
//go se
x1 = x1 + 1;
y1 = y1 - 1;
//update h
h = h + 2 * (x1 - y1) + 5;
}
}
}
//5tjh quad
void draw5thQuad(){
int x1 = R + k;
int y1 = 0 + h;
int h = 1 - R;
while(x1 > y1){
//print points
glBegin(GL_POINTS);
glVertex2i(-x1, -y1);
glEnd();
if(h <= 0) {
//go e
y1 = y1 + 1;
//update h
h = h + (2* y1) + 3;
} else {
//go se
y1 = y1 + 1;
x1 = x1 - 1;
//update h
h = h + 2 * (y1 - x1) + 5;
}
}
}
//4th quad
void draw4thQuad(){
int x1 = R + k;
int y1 = 0 + h;
int h = 1 - R;
while(x1 > y1){
//print points
glBegin(GL_POINTS);
glVertex2i(-x1, y1);
glEnd();
if(h <= 0) {
//go e
y1 = y1 + 1;
//update h
h = h + (2* y1) + 3;
} else {
//go se
y1 = y1 + 1;
x1 = x1 - 1;
//update h
h = h + 2 * (y1 - x1) + 5;
}
}
}
// 3rd quad
void draw3rdQuad(){
int x1 = 0 + h;
int y1 = R + k;
int h = 1 - R;
//startX = x1;
//startY = y1;
while(y1 > x1){
//print points
glBegin(GL_POINTS);
glVertex2i(-x1,y1);
glEnd();
if(h <= 0) {
//go e
x1 = x1 + 1;
//update h
h = h + (2* x1) + 3;
} else {
//go se
x1 = x1 + 1;
y1 = y1 - 1;
//update h
h = h + 2 * (x1 - y1) + 5;
}
}
}
//7th quad
void draw7thQuad(){
int x1 = 0 + h;
int y1 = R + k;
int h = 1 - R;
//startX = x1;
//startY = y1;
while(y1 > x1){
//print points
glBegin(GL_POINTS);
glVertex2i(x1,-y1);
glEnd();
if(h <= 0) {
//go e
x1 = x1 + 1;
//update h
h = h + (2* x1) + 3;
} else {
//go se
x1 = x1 + 1;
y1 = y1 - 1;
//update h
h = h + 2 * (x1 - y1) + 5;
}
}
}
//1st quadrant
void draw1stQuad(){
int x1 = R + k;
int y1 = 0 + h;
int h = 1 - R;
while(x1 > y1){
//print points
glBegin(GL_POINTS);
glVertex2i(x1, y1);
glEnd();
if(h <= 0) {
//go e
y1 = y1 + 1;
//update h
h = h + (2* y1) + 3;
} else {
//go se
y1 = y1 + 1;
x1 = x1 - 1;
//update h
h = h + 2 * (y1 - x1) + 5;
}
}
}
// 2nd quadrant
void draw2ndQuad(){
int x1 = 0 + h;
int y1 = R + k;
int h = 1 - R;
//startX = x1;
//startY = y1;
while(y1 > x1){
//print points
glBegin(GL_POINTS);
cout<<"x: "<<x1<<" y: "<<y1<<endl;
glVertex2i(x1, y1);
glEnd();
if(h <= 0) {
//go e
x1 = x1 + 1;
//update h
h = h + (2* x1) + 3;
} else {
//go se
x1 = x1 + 1;
y1 = y1 - 1;
//update h
h = h + 2 * (x1 - y1) + 5;
}
}
}
void display(void)
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);//(R,G,B)
drawAxes();
draw2ndQuad();
draw1stQuad();
draw7thQuad();
draw3rdQuad();
draw4thQuad();
draw5thQuad();
draw6thQuad();
draw8thQuad();
glFlush();
}
void init(void)
{
/* select clearing (background) color */
glClearColor(0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-300, 300, -300, 300);
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with “hello”
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
/*
R = 150;
h = 5;
k = 10;
*/
cout<<"R: ";
cin>>R;
cout<<endl;
cout<<"h: ";
cin>>h;
cout<<endl;
cout<<"k: ";
cin>>h;
cout<<endl;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(300, 100);
glutCreateWindow("Circle");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment