#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#define xpix 500
#include <cstring>
using
namespace
std;
float
r, g, b, x, y;
bool
flag =
true
;
int
counter = 0;
void
plot(
int
x1,
int
y1)
{
glBegin(GL_POINTS);
glVertex2i(x1 + x, y1 + y);
glEnd();
}
void
mouse(
int
button,
int
state,
int
mousex,
int
mousey)
{
if
(button == GLUT_LEFT_BUTTON
&& state == GLUT_DOWN) {
flag =
true
;
x = mousex;
y = 600 - mousey;
}
else
if
(button == GLUT_RIGHT_BUTTON
&& state == GLUT_DOWN) {
if
(counter > 4) {
counter = 0;
}
counter++;
glutPostRedisplay();
}
}
void
keyboard(unsigned
char
key,
int
x,
int
y)
{
switch
(key) {
case
27:
glutHideWindow();
}
}
void
midPointCircleDraw(
void
)
{
if
(counter == 1) {
glColor3f(1, 0, 0);
}
else
if
(counter == 2) {
glColor3f(0, 1, 0);
}
else
if
(counter == 3) {
glColor3f(0, 1, 1);
}
else
if
(counter == 4) {
glColor3f(0.5, 0, 1);
}
else
if
(counter == 5) {
glColor3f(0, 0.5, 1);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 400.0, 0.0, 600.0);
int
r = 50;
if
(flag) {
glBegin(GL_POLYGON);
int
x1 = 0;
int
y1 = r;
float
decision = 5 / 4 - r;
plot(x1, y1);
while
(y1 > x1) {
if
(decision < 0) {
x1++;
decision += 2 * x1 + 1;
}
else
{
y1--;
x1++;
decision += 2 * (x1 - y1) + 1;
}
plot(x1, y1);
plot(x1, -y1);
plot(-x1, y1);
plot(-x1, -y1);
plot(y1, x1);
plot(-y1, x1);
plot(y1, -x1);
plot(-y1, -x1);
}
}
glFlush();
}
void
polarCoordinateCircleDraw(
void
)
{
float
angle_theta;
if
(counter == 1) {
glColor3f(1, 0, 0);
}
else
if
(counter == 2) {
glColor3f(0, 1, 0);
}
else
if
(counter == 3) {
glColor3f(0, 1, 1);
}
else
if
(counter == 4) {
glColor3f(0.5, 0, 1);
}
else
if
(counter == 5) {
glColor3f(0, 0.5, 1);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 800.0, 0.0, 600.0);
if
(flag) {
glBegin(GL_POLYGON);
for
(
int
i = 0; i < 360; i++) {
angle_theta = i * 3.142 / 180;
glVertex2f(x + 50 *
cos
(angle_theta),
y + 50 *
sin
(angle_theta));
}
glEnd();
}
glFlush();
}
int
main(
int
argc,
char
** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(
GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
int
mainWindow = glutCreateWindow(
"Circle Generation using "
"Midpoint Algorithm on "
"left and using Polar "
"Coordinates on right"
);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(
polarCoordinateCircleDraw);
glutMouseFunc(mouse);
int
subWindow1
= glutCreateSubWindow(
mainWindow, 0, 0, 400, 600);
glutInitWindowPosition(100, 100);
glClearColor(1, 0.5, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(midPointCircleDraw);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}