Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/freeglut.h>
- #include <iostream>
- #include <windows.h>
- #include <stdio.h>
- #include <math.h>
- #include <mmsystem.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #define DIM 5 //massimo numero di linee rappresentabili, cambiare questo numero per più linee
- #define FIN 500
- #pragma comment(lib, "Winmm.lib")
- float x_1[DIM], y_1[DIM], x_2[DIM], y_2[DIM], v_y, m, q;
- float centro_x[DIM], centro_y[DIM], raggio[DIM], v_a, v_b, v_c;
- int pos = 0, win_x, win_y, pos_x, pos_y, pos_x_1, pos_y_1, pause, cerchio = 0;
- char fine = 'z', f;
- int color, g_x = (FIN / 2), g_y = (FIN / 2), zoom = 0, sound;
- float scale = 1.0f;
- int temp = 0, c, sorg = 0;
- void myInit() {
- glClearColor(color, color, color, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- gluOrtho2D(0, FIN, 0, FIN);
- }
- void draw_line(float x_1, float x_2, float y_1, float y_2) {
- glVertex2f(x_1, y_1);
- glVertex2f(x_2, y_2);
- }
- void DrawCircle(float cx, float cy, float r, int num_segments) {
- glBegin(GL_LINE_LOOP);
- for (int ii = 0; ii < num_segments; ii++) {
- float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle
- float x = r * cosf(theta);//calculate the x component
- float y = r * sinf(theta);//calculate the y component
- glVertex2f(x + cx, y + cy);//output vertex
- }
- glEnd();
- }
- void myDisplay() {
- glClear(GL_COLOR_BUFFER_BIT);
- glClearColor(color, color, color, 1.0);
- /* source and dest are arbitrary rectangles.
- float scaleX = dest.width / source.width;
- float scaleY = dest.height / source.height;
- Point sourceCenter = centerPointOfRect(source);
- Point destCenter = centerPointOfRect(dest);
- glTranslatef(destCenter.x, destCenter.y, 0.0);
- glScalef(scaleX, scaleY, 0.0);
- glTranslatef(sourceCenter.x * -1.0, sourceCenter.y * -1.0, 0.0);
- Draw geometry in question with its normal verts*/
- win_x = glutGet(GLUT_WINDOW_WIDTH);
- win_y = glutGet(GLUT_WINDOW_HEIGHT);
- //printf("\n%d --- %d\n", win_x, win_y);
- g_y = win_y - g_y;
- pos_x = g_x * FIN / win_x;
- pos_y = g_y * FIN / win_y;
- //printf("\n%d --- %d\n", pos_x, pos_y);
- glTranslatef(pos_x, pos_y, 0.0);
- glScalef(scale, scale, 0.0);
- glTranslatef((pos_x * -1.0), (pos_y * -1.0), 0.0);
- printf("\n-----------------------------------------------------\n");
- if (sorg==1)
- {
- sorg = 0;
- scale = 1.0;
- }
- if (color == 0)
- {
- glColor3f(1, 1, 1);
- }
- else {
- glColor3f(0, 0, 0);
- }
- glBegin(GL_LINES);
- draw_line(-100000, FIN*100, (FIN / 2), (FIN / 2));
- draw_line((FIN / 2), (FIN / 2), -100000, FIN*100);
- for (int i = -100000; i < FIN*35; i += 10)
- {
- draw_line(i, i, 255, 245);
- draw_line(255, 245, i, i);
- }
- /*draw_line((100 / FIN * 48), (FIN / 2), (98%FIN), FIN);
- draw_line((FIN / 2), 520, FIN, 980);
- draw_line(980, FIN, 480, (FIN / 2));
- draw_line(FIN, 980, (FIN / 2), 520);*/
- for (int r = 0; r < pos; r++)
- {
- switch (r)
- {
- case 0:
- glColor3f(1, 0, 0);
- break;
- case 1:
- glColor3f(0, 1, 0);
- break;
- case 2:
- glColor3f(0, 0, 1);
- break;
- case 3:
- glColor3f(0, 1, 1);
- break;
- case 4:
- glColor3f(1, 1, 0);
- break;
- default:
- if (color == 0)
- {
- glColor3f(1, 1, 1);
- }
- else {
- glColor3f(0, 0, 0);
- }
- break;
- }
- draw_line(x_1[r], x_2[r], y_1[r], y_2[r]);
- }
- glEnd();
- srand(time(NULL));
- for (int r = 0; r < cerchio; r++)
- {
- switch (r)
- {
- case 0:
- glColor3f(1.0, 0.8, 0.6);
- break;
- case 1:
- glColor3f(0.5, 1, 1);
- break;
- case 2:
- glColor3f(1, 1, 0.6);
- break;
- case 3:
- glColor3f(0.2, 1, 0.1);
- break;
- case 4:
- glColor3f(0.5, 0.8, 1);
- break;
- default:
- if (color == 0)
- {
- glColor3f(1, 1, 1);
- }
- else {
- glColor3f(0, 0, 0);
- }
- break;
- }
- DrawCircle(centro_x[r] + (FIN / 2), centro_y[r] + (FIN / 2), raggio[r], 500);
- }
- glFlush();
- }
- void music() {
- bool played = PlaySound(TEXT("unity.wav"), NULL, SND_FILENAME | SND_ASYNC);
- }
- char isNumber(char* text)
- {
- int j;
- j = strlen(text);
- while (j--)
- {
- if (text[j] >= '-9' || text[j] <= '9')
- continue;
- return 0;
- }
- return 1;
- }
- float readParameter(const char* input) {
- char value[20];
- do
- {
- printf("\t\t%s: ", input);
- scanf("%s", value);
- } while (!isNumber(value));
- return atof(value);
- }
- float readParameter_figura(const char* input) {
- char value[20];
- do
- {
- printf("\t%s ", input);
- scanf("%s", value);
- } while (!isNumber(value));
- return atof(value);
- }
- void ask() {
- printf("\033[1;31m"); //bold red
- printf("<SETTINGS>\n\n");
- printf("\033[1;33m"); //bold yellow
- printf("#Choose a theme:\n\n");
- printf("\033[0m"); //reset
- printf("\t0 >>> Dark mode\n");
- printf("\t1 >>> Light mode\n\n");
- do
- {
- printf("\tEnter mode --> ");
- color = getchar();
- if (color == 2)
- continue;
- // clean buffer
- while ((c = getchar()) != '\n' && c != EOF)
- {
- }
- } while (!isdigit(color) || (color != '1' && color != '0'));
- if (color == 48) {
- color = 0;
- }
- else if (color == 49) {
- color = 1;
- }
- printf("\033[1;36m"); //bold cyan
- if (color == 0)
- {
- printf("\n\tDark mode set!\n\n");
- }
- else if (color == 1) {
- printf("\n\tLight mode set!\n\n");
- }
- printf("\033[1;33m"); //bold yellow
- printf("#Sound Music:\n\n");
- printf("\033[0m"); //reset
- printf("\t0 >>> OFF\n");
- printf("\t1 >>> On\n\n");
- do
- {
- printf("\tEnter value --> ");
- sound = getchar();
- if (sound == 2)
- continue;
- // clean buffer
- while ((c = getchar()) != '\n' && c != EOF)
- {
- }
- } while (!isdigit(sound) || (sound != '1' && sound != '0'));
- if (sound == 48) {
- sound = 0;
- }
- else if (sound == 49) {
- sound = 1;
- }
- printf("\033[1;36m"); //bold cyan
- if (sound == 0)
- {
- printf("\n\tSound Music OFF!\n\n");
- }
- else if (sound == 1) {
- printf("\n\tSound Music ON!\n\n");
- music();
- printf("\tMusic runnin: Unity - TheFatRat\n");
- }
- printf("\033[1;31m"); //bold red
- printf("\n</SETTINGS>\n\n");
- printf("\033[0m"); //reset
- printf("--------------------------------------------------\n\n");
- int figura;
- do
- {
- printf("\033[1;33m"); //bold yellow
- printf("\n#Che figura vuoi inserire?\n\n");
- printf("\033[0m"); //reset
- printf("\t1 >>> Retta\n");
- printf("\t2 >>> circonferenza\n");
- printf("\t3 >>> Summary\n");
- printf("\t4 >>> Stop\n\n");
- do
- {
- //printf("\tEnter value --> ");
- figura = readParameter_figura("Enter value -->");
- } while (figura != 1 && figura != 2 && figura != 3 && figura !=4);
- if (figura == 1)
- {
- if (pos == DIM)
- {
- char decision = 'k';
- printf("\033[1;31m"); //bold red
- printf("\n\t#ATTENZIONE!\n\t\033[0;31mLE RETTE DISPONIBILI SONO FINITE, VUOI INSERIRE UNA RETTA COMUNQUE?\n\t\033[0m(Scegliendo si tutte le rette inserite precedentemente verranno cancellate)\n\t[s/n]\n");
- do
- {
- printf("\n\t-->");
- scanf_s(" %c", &decision);
- } while (decision != 's' && decision != 'n' && decision != 'S' && decision != 'N');
- if (decision == 's' || decision == 'S')
- {
- pos = 0;
- }
- }
- fine = 'p';
- while (fine != 'n' && fine != 'N' && pos != DIM)
- {
- printf("\033[1;32m"); //bold green
- printf("\n\t[RETTA]");
- printf("\033[0m"); //reset
- printf("\n\t-Enter a number(ny = mx+q)\n\n");
- v_y = readParameter("n");
- m = readParameter("m");
- q = readParameter("q");
- if (v_y < 0)
- {
- v_y = -v_y;
- m = -m;
- q = -q;
- }
- x_1[pos] = (FIN / 2);
- y_1[pos] = (FIN / 2);
- if (v_y > 0)
- {
- y_1[pos] += ((float)q / (float)v_y * 10);
- }
- x_2[pos] = x_1[pos];
- y_2[pos] = y_1[pos];
- for (int t = 0; t < 1000; t++)
- {
- x_2[pos] += (v_y * 10);
- y_2[pos] += (m * 10);
- x_1[pos] -= (v_y * 10);
- y_1[pos] -= (m * 10);
- }
- if (v_y == 0 && m != 0)
- {
- x_1[pos] -= ((float)q / (float)m * 10);
- x_2[pos] -= ((float)q / (float)m * 10);
- }
- pos++;
- if (pos != DIM)
- {
- printf("\033[1;32m"); //bold green
- printf("\n\t-Vuoi inserire un'altra retta? [s/n]\n\n");
- printf("\033[0m"); //reset
- do
- {
- printf("\t\t--> ");
- scanf_s(" %c", &fine);
- } while (fine != 's' && fine != 'n' && fine != 'S' && fine != 'N');
- }
- }
- }
- else if (figura == 2)
- {
- fine = 'p';
- while (fine != 'n' && fine != 'N' && cerchio != DIM)
- {
- printf("\033[1;32m"); //bold green
- printf("\n\t[CIRCONFERENZA]");
- printf("\033[0m"); //reset
- printf("\n\t-Enter a number(x^2+y^2+ax+by+c=0)\n\n");
- v_a = readParameter("a");
- v_b = readParameter("b");
- v_c = readParameter("c");
- centro_x[cerchio] = ((-v_a) / 2) * 10;
- centro_y[cerchio] = ((-v_b) / 2) * 10;
- if (pow(centro_x[cerchio], 2) + pow(centro_y[cerchio], 2) - v_c < 0)
- {
- printf("\033[1;31m"); //bold red
- printf("\n\t\t-IL LUOGO GEOMETRICO INSERITO NON E UNA CIRCONFERENZA\n");
- printf("\033[0m"); //reset
- cerchio--;
- }
- else
- {
- raggio[cerchio] = sqrtf(pow(centro_x[cerchio], 2) + pow(centro_y[cerchio], 2) - v_c);
- }
- printf("\nCIRCO: %f, %f , %f , %f", centro_x[cerchio], centro_y[cerchio], v_c, raggio[cerchio]);
- cerchio++;
- if (cerchio != DIM)
- {
- printf("\033[1;32m"); //bold green
- printf("\n\t-Vuoi inserire un'altra circonferenza? [s/n]\n\n");
- printf("\033[0m"); //reset
- do
- {
- printf("\t\t--> ");
- scanf_s(" %c", &fine);
- } while (fine != 's' && fine != 'n' && fine != 'S' && fine != 'N');
- }
- }
- }
- else if(figura == 3)
- {
- printf("\033[1;32m"); //bold green
- printf("\n\t-[SUMMARY]:\n\n");
- printf("\033[0m"); //reset
- if (pos == 0 && cerchio == 0)
- {
- printf("\tNothing has been entered!\n\n\n");
- }
- else
- {
- printf("\t%d lines has been entered\n", pos);
- printf("\t%d circumferences has been entered\n\n\n", cerchio);
- }
- }
- else
- {
- continue;
- }
- } while (figura != 4);
- }
- /*
- void MyKeyboardFunc(unsigned char Key, int x, int y)
- {
- switch (Key)
- {
- case '+':
- scale += 0.01;
- //printf("+\n");
- break;
- case '-':
- scale -= 0.01;
- //printf("-\n");
- break;
- }
- }
- */
- void MouseHandler(int button, int state, int x, int y) {
- if (button == GLUT_MIDDLE_BUTTON
- && state == GLUT_DOWN) {
- g_x = x; g_y = y;
- double b = pow(1.1, zoom);
- scale = (1 / b);
- zoom = 0;
- //printf("%d,%d \n", x, y);
- //printf("\n\n%d\n\n", zoom);
- //printf("%d,%d \n", button, state);
- glutPostRedisplay();
- }
- }
- void look(int lx, int ly) {
- glutPostRedisplay();
- win_x = glutGet(GLUT_WINDOW_WIDTH);
- win_y = glutGet(GLUT_WINDOW_HEIGHT);
- //printf("\n%d --- %d\n", win_x, win_y);
- ly = win_y - ly;
- pos_x = lx * FIN / win_x;
- pos_y = ly * FIN / win_y;
- //printf("\n%d --- %d\n", pos_x, pos_y);
- glBegin(GL_LINES);
- if (color == 0)
- {
- glColor3f(1, 1, 1);
- }
- else {
- glColor3f(0, 0, 0);
- }
- draw_line(pos_x, pos_x, FIN, 0);
- draw_line(FIN, 0, pos_y, pos_y);
- glEnd();
- glFlush();
- }
- void animate() {
- }
- void mouseWheel(int button, int dir, int x, int y)
- {
- if (button == 0 && dir == 1)
- {
- g_x = x; g_y = y;
- if (zoom > 35)
- {
- scale = 1.02;
- }
- else if (zoom > 28)
- {
- scale = 1.05;
- }
- else
- {
- scale = 1.1;
- }
- zoom++;
- printf("\nMousewheel X Y: %d,%d \n", x, y);
- }
- else if (button == 0 && dir == -1)
- {
- g_x = x; g_y = y;
- if (zoom > -4)
- {
- if (zoom > 35)
- {
- scale = (1 / 1.02);
- zoom--;
- }
- else if (zoom > 28)
- {
- scale = (1 / 1.05);
- zoom--;
- }
- else
- {
- scale = (1 / 1.1);
- zoom--;
- }
- }
- else if (zoom <= 0)
- {
- scale = 1.0;
- }
- printf("\nMousewheel X Y: %d,%d \n", x, y);
- }
- else
- {
- scale = 1.0;
- }
- printf("\n\nMousewheel Zoom: %d\n", zoom);
- printf("\nMousewheel Scale: %d\n", scale);
- glutPostRedisplay();
- sorg = 1;
- }
- void move(int bx, int by) {
- if (temp == 0)
- {
- pos_x_1 = bx;
- pos_y_1 = by;
- temp++;
- }
- printf_s("\nMove X Y: %d ____ %d\n",bx,by);
- printf_s("\nSpostamento effettivo: %d ____ %d\n", (bx - pos_x_1 /* (zoom + 1)*/), (pos_y_1 - by /* (zoom + 1)*/));
- if ((bx - pos_x_1) > 20 || (pos_y_1 - by) > 20 || (bx - pos_x_1) < -20 || (pos_y_1 - by) < -20)
- {
- pos_x_1 = bx;
- pos_y_1 = by;
- }
- if (zoom > 20)
- {
- glTranslatef((bx - pos_x_1) / 3, (pos_y_1 - by) / 3, 0.0);
- }
- else if (zoom > 10)
- {
- glTranslatef((bx - pos_x_1) / 2, (pos_y_1 - by) / 2, 0.0);
- }
- else
- {
- glTranslatef((bx - pos_x_1), (pos_y_1 - by), 0.0);
- }
- scale = 1.0;
- glutPostRedisplay();
- pos_x_1 = bx;
- pos_y_1 = by;
- }
- int main(int argc, char** argv) {
- for (int j = 0; j < DIM; j++)
- {
- x_1[j] = 0;
- x_2[j] = 0;
- y_1[j] = 0;
- y_2[j] = 0;
- }
- ask();
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(50, 50);
- glutCreateWindow("Graphic Project");
- myInit();
- glutDisplayFunc(myDisplay);
- //glutKeyboardFunc(MyKeyboardFunc);
- //glutMouseFunc(MouseHandler);
- glutMouseWheelFunc(mouseWheel);
- /*glutMotionFunc(motion_func);
- glutReshapeFunc(change_viewport); // when window is resized
- glutDisplayFunc(render); // when window needs to be drawn
- glutIdleFunc(animate); // when there is nothing else to do
- glutMouseFunc(callback_mouse_button); // when mouse is clicked
- glutKeyboardFunc(keyboard_down); // when a key is down
- glutKeyboardUpFunc(keyboard_up); // when the key goes up
- glutPassiveMotionFunc(look); // when mouse moves*/
- glutMotionFunc(move); // when mouse drags around
- //glutPassiveMotionFunc(look);
- glutIdleFunc(animate);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement