This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (99 loc) · 3.29 KB
/
main.cpp
File metadata and controls
120 lines (99 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <cmath>
using namespace std;
int radius; // Store the radius for the circle
void drawGrid() {
glColor3f(0.0, 0.0, 0.0); // Set grid color to black
glBegin(GL_LINES);
// Draw vertical grid lines
for (int i = -radius - 10; i <= radius + 10; i++) {
glVertex2f(i, -radius - 10); // Vertical line
glVertex2f(i, radius + 10);
}
// Draw horizontal grid lines
for (int i = -radius - 10; i <= radius + 10; i++) {
glVertex2f(-radius - 10, i); // Horizontal line
glVertex2f(radius + 10, i);
}
glEnd();
}
void plotCirclePoints(int x, int y) {
glBegin(GL_POINTS);
// Plot the eight symmetric points
glVertex2i(x, y);
glVertex2i(-x, y);
glVertex2i(x, -y);
glVertex2i(-x, -y);
glVertex2i(y, x);
glVertex2i(-y, x);
glVertex2i(y, -x);
glVertex2i(-y, -x);
glEnd();
}
void MidpointCircle(int radius) {
int x = 0;
int y = radius;
int d = 1 - radius;
int deltaE = 3;
int deltaSE = -2 * radius + 5;
cout << "Midpoint Circle Drawing Algorithm:" << endl;
cout << "Radius: " << radius << endl;
cout << "Points on the circle:" << endl;
// Set point color to red
glColor3f(1.0, 0.0, 0.0); // Red color for circle points
// Draw and print the points
while (x <= y) {
// Plot the points on the OpenGL window
plotCirclePoints(x, y);
// Print the points to the console
cout << "(" << x << ", " << y << ")" << endl;
cout << "(" << -x << ", " << y << ")" << endl;
cout << "(" << x << ", " << -y << ")" << endl;
cout << "(" << -x << ", " << -y << ")" << endl;
cout << "(" << y << ", " << x << ")" << endl;
cout << "(" << -y << ", " << x << ")" << endl;
cout << "(" << y << ", " << -x << ")" << endl;
cout << "(" << -y << ", " << -x << ")" << endl;
if (d < 0) {
d += deltaE;
deltaE += 2;
deltaSE += 2;
} else {
d += deltaSE;
deltaE += 2;
deltaSE += 4;
y--;
}
x++;
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear display window
drawGrid(); // Draw the grid
MidpointCircle(radius); // Call the Midpoint Circle Algorithm to draw the circle
glFlush(); // Flush the drawing to the window
}
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0); // White background
glPointSize(5.0); // Larger size of points to make them more visible
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-radius - 10, radius + 10, -radius - 10, radius + 10); // Set window coordinates based on the radius
}
int main(int argc, char** argv) {
// Input radius from the user
cout << "Enter radius of circle: ";
cin >> radius;
// Initialize GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Window size
glutInitWindowPosition(100, 100); // Window position
glutCreateWindow("Midpoint Circle Drawing with Grid"); // Window title
init(); // Initialize OpenGL state
glutDisplayFunc(display); // Set the display callback function
glutMainLoop(); // Enter the event-processing loop
return 0;
}