Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sunt mentionate fisiere (biblioteci) care urmeaza sa fie incluse
- #include <GL/freeglut.h> // nu trebuie uitat freeglut.h (sau glut.h sau gl.h & glu.h)
- #include <iostream>
- #include <conio.h>
- using namespace std;
- struct vect
- {
- float x, y, z;
- };
- int nr_pct;
- vect coord[100];
- GLint winWidth = 1024, winHeight = 768;
- vect vectpro(vect u, vect, v) {
- vect p;
- p.x = v.x - u.x;
- p.y = v.y - u.y;
- p.z = v.z - u.z;
- return p;
- }
- vect Produs(vect p1, vect p2, vect p3) {
- vect vector12 = vectpro(p1, p2);
- vect vector23 = vectpro(p2, p3);
- vect rezultat_produs;
- rezultat_produs.x = vector12.y * vector23.z - vector12.z * vector23.y;
- rezultat_produs.y = vector12.x * vector23.z - vector12.z * vector23.x;
- rezultat_produs.z = vector12.x * vector23.y - vector12.y * vector23.x;
- return rezultat_produs;
- }
- void afisare(vect vector1) {
- cout << vector1.x << "*i + " << vector1.y << "*j + " << vector1.z << "*k" << endl;
- }
- bool CheckAngle(vect point1, vect point2, vect point3, int i) {
- vect produs_vectori = Produs(point1, point2, point3);
- double f.produs_vectori = produs_vectori.x + produs_vectori.y + produs_vectori.z;
- if (produs_vectori.x < 0 || produs_vectori.y < 0 || produs_vectori.z < 0) {
- cout << "Punct: I = " << coord[i + 1].x << " / J = " << coord[i + 1].y << " / K = " << coord[i + 1].z << endl;
- return true;
- }
- else {
- cout << "Punct: I = " << coord[i + 1].x << " / J = " << coord[i + 1].y << " / K = " << coord[i + 1].z << endl;
- return false;
- }
- }
- void init(void) // initializare fereastra de vizualizare
- {
- glClearColor(1.0, 1.0, 1.0, 1.0); // precizeaza culoarea de fond a ferestrei de vizualizare
- glMatrixMode(GL_PROJECTION); // se precizeaza este vorba de o reprezentare 2D, realizata prin proiectie ortogonala
- gluOrtho2D(0.0, 1024.0, 0.0, 768.0); // sunt indicate coordonatele extreme ale ferestrei de vizualizare
- }
- void mouse2(int button, int state, int x, int y)
- {
- static int i = 0;
- switch (button) {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN) {
- coord[i].x = x;
- coord[i].y = y;
- i++;
- }
- break;
- }
- }
- void reshapeFcn(GLint newWidth, GLint newHeight)
- {
- glViewport(0, 0, newWidth, newHeight);
- winWidth = newWidth;
- winHeight = newHeight;
- }
- //Punct rotund = concav
- //Punct patrat = convex
- void desen(void) // procedura desenare
- {
- system("cls");
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0, 0.0, 0.0);
- glBegin(GL_POLYGON);
- for (int i = 0; i < coordNr; i++) {
- glVertex2i(coord[i].x, coord[i].y);
- }
- glEnd();
- glcoordize(10.0);
- for (int i = 0; i < coordNr; i++) {
- if (i == coordNr - 2) {
- if (CheckAngle(coord[i], coord[i + 1], coord[0], i)) {
- glColor3f(1.0, 0.0, 0.0);
- glEnable(GL_POINT_SMOOTH);
- glBegin(GL_coord);
- glVertex2i(coord[i + 1].x, coord[i + 1].y);
- glEnd();
- glDisable(GL_POINT_SMOOTH);
- }
- else {
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_coord);
- glVertex2i(coord[i + 1].x, coord[i + 1].y);
- glEnd();
- }
- }
- else
- if (i == coordNr - 1) {
- if (CheckAngle(coord[i], coord[0], coord[1], i)) {
- glColor3f(1.0, 0.0, 0.0);
- glEnable(GL_POINT_SMOOTH);
- glBegin(GL_coord);
- glVertex2i(coord[0].x, coord[0].y);
- glEnd();
- glDisable(GL_POINT_SMOOTH);
- }
- else {
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_coord);
- glVertex2i(coord[0].x, coord[0].y);
- glEnd();
- }
- }
- else {
- if (CheckAngle(coord[i], coord[i + 1], coord[i + 2], i)) {
- glColor3f(1.0, 0.0, 0.0);
- glEnable(GL_POINT_SMOOTH);
- glBegin(GL_coord);
- glVertex2i(coord[i + 1].x, coord[i + 1].y);
- glEnd();
- glDisable(GL_POINT_SMOOTH);
- }
- else {
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_coord);
- glVertex2i(coord[i + 1].x, coord[i + 1].y);
- glEnd();
- }
- }
- }
- glFlush(); // proceseaza procedurile OpenGL cat mai rapid
- }
- //100 100
- //700 100
- //350 350
- //700 700
- //100 700
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv); // initializare GLUT
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // se utilizeaza un singur buffer | modul de colorare RedGreenBlue (= default)
- glutInitWindowPosition(500, 500); // pozitia initiala a ferestrei de vizualizare (in coordonate ecran)
- glutInitWindowSize(1024, 768); // dimensiunile ferestrei
- cout << "Numarul de puncte: ";
- cin >> coordNr;
- //for (int i = 0; i < coordNr; i++) {
- // cout << "Punctul: " << i + 1 << " Coordonata I: ";
- // cin >> coord[i].x;
- // cout << "Punctul: " << i + 1 << " Coordonata J: ";
- // cin >> coord[i].y;
- // coord[i].z = 0; //Pentru ca reprezentarea se face in 2D, nu mai are rost sa iau si a treia coordonata.
- //}
- for (int i = 0; i < coordNr; i++) {
- }
- glutCreateWindow("ConvexConcavePolygonEdges "); // creeaza fereastra, indicand numele ferestrei de vizualizare - apare in partea superioara
- init(); // executa procedura de initializare
- glClear(GL_COLOR_BUFFER_BIT); // reprezentare si colorare fereastra de vizualizare
- glutMouseFunc(mouse2);
- glutDisplayFunc(desen); // procedura desen este invocata ori de cate ori este nevoie
- glutMainLoop(); // ultima instructiune a programului, asteapta (eventuale) noi date de intrare
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment