Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- GLfloat scrW = 1280, scrH = 960;
- struct vertex {
- GLfloat x, y;
- };
- struct vertexFlag {
- int number;
- bool flag;
- };
- struct vertexInfo {
- int nubmerMain;
- vertexFlag prev, next;
- GLfloat dprev, dnext;
- };
- vector<vertex> mas;
- int flag = 0;
- GLfloat centerScreenW, centerScreenH;
- //вспомогательная функция сортировки по X
- bool compareByX(const GLfloat &a, const GLfloat &b) {
- return a < b;
- }
- void drawhui(){
- vector<vertexInfo> arrayCurrentVertex;
- for (GLfloat currentY = 0; currentY < scrH; currentY++) {
- for (int i = 0; i < mas.size(); i++) {
- if (mas[i].y == currentY) {
- vertexInfo main;
- main.nubmerMain = i;
- vertexFlag prev, next;
- if (i != 0) {
- prev.number = i - 1;
- } else {
- prev.number = mas.size() - 1;
- }
- if (i != mas.size() - 1) {
- next.number = i + 1;
- } else {
- next.number = 0;
- }
- if (currentY >= max(mas[i].y, mas[prev.number].y)) {
- prev.flag = true;
- for (int q = 0; q < arrayCurrentVertex.size(); q++) {
- if (arrayCurrentVertex[q].nubmerMain == prev.number) {
- arrayCurrentVertex[q].next.flag = true;
- }
- }
- } else {
- prev.flag = false;
- }
- if (currentY >= max(mas[i].y, mas[next.number].y)) {
- next.flag = true;
- for (int q = 0; q < arrayCurrentVertex.size(); q++) {
- if (arrayCurrentVertex[q].nubmerMain == next.number) {
- arrayCurrentVertex[q].prev.flag = true;
- }
- }
- } else {
- next.flag = false;
- }
- main.prev = prev;
- main.next = next;
- main.dprev = (mas[prev.number].x - mas[i].x)/abs(mas[prev.number].y - mas[i].y);
- main.dnext = (mas[next.number].x - mas[i].x)/abs(mas[next.number].y - mas[i].y);
- arrayCurrentVertex.push_back(main);
- }
- }
- cout << "-------------------------XYI---------------------------" << endl;
- cout << endl;
- cout << "currentY: " << currentY << endl;
- // for (int i = 0; i < arrayCurrentVertex.size(); i++) {
- // cout << "Number: " << arrayCurrentVertex[i].nubmerMain << endl;
- // cout << "Prev: " << arrayCurrentVertex[i].prev.number << " flag: " << arrayCurrentVertex[i].prev.flag << endl;
- // cout << "Next: " << arrayCurrentVertex[i].next.number << " flag: " << arrayCurrentVertex[i].next.flag << endl;
- // cout << "dprev: " << arrayCurrentVertex[i].dprev << " dnext: " << arrayCurrentVertex[i].dnext << endl;
- // cout << endl;
- //
- // }
- if (!arrayCurrentVertex.empty()) {
- bool flagExit = true;
- for (int i = 0; i < arrayCurrentVertex.size(); i++) {
- if (arrayCurrentVertex[i].prev.flag == false) {
- flagExit = false;
- }
- if (arrayCurrentVertex[i].next.flag == false) {
- flagExit = false;
- }
- }
- if (flagExit) {
- return;
- }
- }
- if (!arrayCurrentVertex.empty()) {
- vector<GLfloat> currentVertexOnX;
- for (int i = 0; i < arrayCurrentVertex.size(); i++) {
- for (int g = 0; g < arrayCurrentVertex.size(); g++) {
- if (arrayCurrentVertex[g].prev.flag == false) {
- GLfloat addx = mas[arrayCurrentVertex[g].nubmerMain].x +
- arrayCurrentVertex[g].dprev *
- abs(currentY - mas[arrayCurrentVertex[g].nubmerMain].y);
- currentVertexOnX.push_back(addx);
- }
- if (arrayCurrentVertex[g].next.flag == false) {
- GLfloat addx = mas[arrayCurrentVertex[g].nubmerMain].x +
- arrayCurrentVertex[g].dnext *
- abs(currentY - mas[arrayCurrentVertex[g].nubmerMain].y);
- currentVertexOnX.push_back(addx);
- }
- }
- }
- sort(currentVertexOnX.begin(), currentVertexOnX.end(), compareByX);
- vector<GLfloat> currentVertexOnXHelp;
- for (int i = 0; i < currentVertexOnX.size() - 1; i++) {
- if (currentVertexOnX[i] != currentVertexOnX[i + 1]) {
- currentVertexOnXHelp.push_back(currentVertexOnX[i]);
- } else {
- i++;
- }
- }
- if (currentVertexOnX[currentVertexOnX.size()-1] != currentVertexOnX[currentVertexOnX.size()-2]) {
- currentVertexOnXHelp.push_back(currentVertexOnX[currentVertexOnX.size()-1]);
- }
- currentVertexOnX.clear();
- currentVertexOnX = currentVertexOnXHelp;
- currentVertexOnXHelp.clear();
- cout << "currentVertexOnX: " << endl;
- for (int p = 0; p < currentVertexOnX.size(); p++) {
- cout << currentVertexOnX[p];
- cout << " ";
- }
- cout << endl;
- if (!currentVertexOnX.empty()) {
- bool flagDraw = false;
- int counter = 0;
- for (GLfloat currentX = 0; currentX < scrW; currentX++) {
- if (currentX >= currentVertexOnX[counter]) {
- flagDraw = !flagDraw;
- counter++;
- }
- if (flagDraw) {
- cout << "x: " << currentX << " ";
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_POINTS);
- glVertex2f(currentX, currentY);
- glEnd();
- }
- // if (!flagDraw) {
- // cout << "NET x: " << currentX << "NET y: " << currentY << " ";
- // }
- }
- }
- currentVertexOnX.clear();
- }
- }
- }
- void drawEdges() {
- if (flag) {
- glBegin(GL_LINE_LOOP);
- } else {
- glBegin(GL_LINE_STRIP);
- }
- glColor3f(1, 1, 1);
- for (int i = 0; i < mas.size(); i++) {
- glVertex2f(mas[i].x, mas[i].y);
- }
- if (flag){
- drawhui();
- }
- }
- void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
- if (action == GLFW_PRESS) {
- switch (key) {
- case GLFW_KEY_1:
- flag = 1;
- break;
- case GLFW_KEY_C:
- mas.clear();
- flag = 0;
- break;
- }
- }
- }
- void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
- double x, y;
- glfwGetCursorPos(window, &x, &y);
- vertex vertexLeftBut;
- vertexLeftBut.x = x;
- vertexLeftBut.y = y;
- mas.push_back(vertexLeftBut);
- }
- }
- int main() {
- GLFWwindow *window;
- if (!glfwInit()) {
- return -1;
- }
- window = glfwCreateWindow(scrW, scrH, "Lab4", NULL, NULL);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetMouseButtonCallback(window, mouseCallback);
- glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
- int screenW, screenH;
- glfwGetFramebufferSize(window, &screenW, &screenH);
- if (!window) {
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- centerScreenW = scrW / 2;
- centerScreenH = scrH / 2;
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glOrtho(0.0, scrW, scrH, 0.0, -1.0, 1.0);
- glViewport(0, 0, screenW, screenH);
- drawEdges();
- glEnd();
- glPopMatrix();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement