0% found this document useful (0 votes)
24 views3 pages

OpenGL Circle and Grid Drawing Code

Uploaded by

mhmdaldhshwry280
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

OpenGL Circle and Grid Drawing Code

Uploaded by

mhmdaldhshwry280
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <cstdlib>

#include <gl/glut.h>
#include <cmath>

void drawCircle(float cx, float cy, float r, int segments = 30) {


glBegin(GL_TRIANGLE_FAN);
glVertex2f(cx, cy); // ‫مركز الدائرة‬
for (int i = 0; i <= segments; ++i) {
float theta = 2.0f * 3.1415926f * float(i) / float(segments);
float x = r * cos(theta);
float y = r * sin(theta);
glVertex2f(cx + x, cy + y);
}
glEnd();
}

void drawText(float x, float y, const char* text) {


glRasterPos2f(x, y);
for (int i = 0; text[i] != '\0'; i++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text[i]);
}
}

void drawAxesWithTicksAndGrid() {
// ‫المحاور الرئيسية‬
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(-10.0, 0.0);
glVertex2f(10.0, 0.0);
glVertex2f(0.0, -10.0);
glVertex2f(0.0, 10.0);
glEnd();

// ‫الشبكة الخفيفة‬
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_LINES);
for (float i = -10.0f; i <= 10.0f; i += 0.2f) {
if (fabs(i) < 0.001f) continue;
glVertex2f(i, -10.0f);
glVertex2f(i, 10.0f);
glVertex2f(-10.0f, i);
glVertex2f(10.0f, i);
}
glEnd();

// ‫الشرطات الكبيرة‬
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
for (float i = -10.0; i <= 10.0; i += 2.0f) {
if (fabs(i) == 10.0f) {
// 10± ‫شرطـة واحدة عند‬
glVertex2f(i, 0.0f);
glVertex2f(i, 0.1f); // X ‫للأعلى فقط‬

glVertex2f(0.0f, i);
glVertex2f(0.1f, i); // Y ‫لليمين فقط‬
}
}
glEnd();
// ‫الشرطات الصغيرة‬
glColor3f(0.6f, 0.6f, 0.6f);
glBegin(GL_LINES);
for (float i = -10.0; i <= 10.0; i += 0.2f) {
if (fmod(i, 2.0f) == 0.0f) continue;
glVertex2f(i, -0.03f);
glVertex2f(i, 0.03f);
glVertex2f(-0.03f, i);
glVertex2f(0.03f, i);
}
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawAxesWithTicksAndGrid();

glColor3f(0, 0, 0);
float r = 0.026f;

drawCircle(-.6, .2, r);


drawCircle(.4, 1.2, r);
drawCircle(-.8, 1.2, r);
drawCircle(-.2, 1.4, r);
drawCircle(-.2, 1, r);
drawCircle(.6, 1.6, r);
drawCircle(-.4, 2, r);
drawCircle(.2, .4, r);

drawText(-.69, .17, "L");


drawText(.31, 1.17, "R");
drawText(-.89, 1.17, "A");
drawText(-.29, 1.37, "B");
drawText(-.29, .97, "C");
drawText(.51, 1.57, "D");
drawText(-.49, 1.97, "G");
drawText(.13, .37, "F");
drawText(-.09, 2.3, "Y");
drawText(1.87, .09, "X");

drawText(-.09, .2, "1");


drawText(-.09, .4, "2");
drawText(-.09, .6, "3");
drawText(-.09, .8, "4");
drawText(-.09, 1, "5");
drawText(-.09, 1.2, "6");
drawText(-.09, 1.4, "7");
drawText(-.09, 1.6, "8");
drawText(-.09, 1.8, "9");
drawText(-.15, 2, "10");

drawText(-.09, -.2, "1");


drawText(-.09, -.4, "2");
drawText(-.09, -.6, "3");
drawText(-.09, -.8, "4");
drawText(-.09, -1, "5");
drawText(-.09, -1.2, "6");
drawText(-.09, -1.4, "7");
drawText(-.09, -1.6, "8");
drawText(-.09, -1.8, "9");
drawText(-.15, -2, "10");

drawText(.17, -.12, "1");


drawText(.37, -.12, "2");
drawText(.57, -.12, "3");
drawText(.77, -.12, "4");
drawText(.97, -.12, "5");
drawText(1.17, -.12, "6");
drawText(1.37, -.12, "7");
drawText(1.57, -.12, "8");

glBegin(GL_LINES);
glVertex2f(-.8,1.2);
glVertex2f(-.2,1.4);
glVertex2f(-.4,2);
glVertex2f(.2,.4);
glEnd();

glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex2f(.6, 1.6);
glVertex2f(-.2, 1);
glEnd();

glFlush();
}

void init() {
glClearColor(1, 1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2, 2, -2.5, 2.5);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitWindowPosition(380, 84);
glutInitWindowSize(600, 600);
glutCreateWindow("Graph Axes with Fine Grid");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

You might also like