Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <vector>
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <math.h>
- #include <map>
- struct vertex{
- double x, y;
- };
- struct pair{
- struct vertex * arr[2];
- };
- struct line{
- struct pair * point = NULL;
- std::vector<pair*> L;
- } lines;
- static double px, py =0;
- static int a=1080;
- static int b=1080;
- static bool mod= false, make_shape = true;
- static std::vector<vertex*> shape;
- static std::vector<pair*> res;
- void build();
- void game_loop_drow();
- void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
- int main(int argc, const char *argv[]){
- if (!glfwInit()){
- std::cout << "faild\n";
- return 0;
- }
- GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
- if (!window){
- std::cout<< "faild\n";
- return 0;
- }
- glfwMakeContextCurrent(window);
- glClearColor(1, 1, 1, 0.0f);
- glfwSetCursorPosCallback(window, cursor_pos_callback);
- glfwSetKeyCallback(window, key_callback);
- glfwSetMouseButtonCallback(window, mouse_button_callback);
- glEnable(GL_DEPTH_TEST);
- while (!glfwWindowShouldClose(window)){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- game_loop_drow();
- glfwSwapBuffers(window);
- glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
- void game_loop_drow(){
- glLineWidth(2.0);
- glColor3f(0, 1, 0);
- glBegin(GL_LINE_LOOP);
- for (std::vector<vertex*>::iterator point = shape.begin(); point!= shape.end(); ++point)
- glVertex3f((*point)->x, (*point)->y, 0);
- glEnd();
- if (!mod) {
- glColor3f(1, 0, 1);
- for (std::vector<pair*>::iterator point = lines.L.begin(); point!= lines.L.end(); ++point){
- glBegin(GL_LINES);
- for (int i =0; i<2 ;i++)
- glVertex3f((*point)->arr[i]->x, (*point)->arr[i]->y, 0);
- glEnd();
- }
- if (lines.point != NULL){
- glBegin(GL_LINES);
- glVertex3f(lines.point->arr[0]->x, lines.point->arr[0]->y, 0);
- glVertex3f(px, py, 0);
- glEnd();
- }
- return;
- } else {
- glColor3f(0, 0, 0);
- for (std::vector<pair*>::iterator point = res.begin(); point!= res.end(); ++point){
- glBegin(GL_LINES);
- for (int i =0; i<2 ;i++)
- glVertex3f((*point)->arr[i]->x, (*point)->arr[i]->y, 0);
- glEnd();
- }
- }
- }
- void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
- px= xpos/(a/2)-1;
- py= 1-(ypos+50)/(a/2);
- return;
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
- // std::cout << key <<std::endl;
- if( action == 1) return;
- switch (key ){
- case 66: // Build
- mod ^= true;
- if (mod) build();
- break;
- case 78: // Next
- make_shape ^= true;
- break;
- case GLFW_KEY_ESCAPE:
- if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
- glfwSetWindowShouldClose(window, true);
- }
- break;
- default:
- break;
- }
- return;
- }
- double scalar_product(vertex A, vertex B){
- return A.x*B.x + A.y*B.y;
- }
- void build(){
- shape.push_back(shape[0]);
- for (std::vector<pair*>::iterator i = lines.L.begin(); i!= lines.L.end(); ++i){
- bool end = false;
- pair *cur_line = *i;
- double t_start = 0, t_final = 1;
- vertex D {cur_line->arr[1]->x - cur_line->arr[0]->x, cur_line->arr[1]->y - cur_line->arr[0]->y};
- for (std::vector<vertex*>::iterator i= shape.begin(); i!=shape.end()-1; ++i){
- pair cur_edge;
- cur_edge.arr[0] = *i;
- cur_edge.arr[1] = *(i+1);
- vertex edge_D {cur_edge.arr[1]->x - cur_edge.arr[0]->x, cur_edge.arr[1]->y - cur_edge.arr[0]->y};
- vertex W {cur_line->arr[0]->x - cur_edge.arr[0]->x, cur_line->arr[0]->y - cur_edge.arr[0]->y};
- vertex N {edge_D.y, -edge_D.x};
- double Ws = scalar_product(W,N), Ds = scalar_product(D, N);
- std::cout << Ds << " " << Ws << std::endl;
- double t =-Ws/Ds;
- if (Ds == 0){
- if (Ws < 0){
- end = true;
- break;
- } else{
- continue;
- }
- } else if (Ds < 0){
- if (t<=1)
- t_start = std::max(t,t_start);
- else
- end = true;
- } else{
- if (t>=0)
- t_final = std::min(t, t_final);
- else
- end = true;
- }
- }
- if (end || t_start>t_final) {
- continue;
- }
- pair *pair_t =new pair;
- pair_t->arr[0] = new vertex{ cur_line->arr[0]->x + t_start*(cur_line->arr[1]->x - cur_line->arr[0]->x), cur_line->arr[0]->y + t_start*(cur_line->arr[1]->y - cur_line->arr[0]->y) };
- pair_t->arr[1] = new vertex{ cur_line->arr[0]->x + t_final*(cur_line->arr[1]->x - cur_line->arr[0]->x), cur_line->arr[0]->y + t_final*(cur_line->arr[1]->y - cur_line->arr[0]->y) };
- res.push_back(pair_t);
- }
- shape.pop_back();
- return;
- }
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
- if (action==0 && mods==0){
- vertex *V = new vertex{px, py};
- if (make_shape){
- shape.push_back(V);
- } else {
- if (lines.point != NULL){
- lines.point->arr[1] = V;
- lines.L.push_back(lines.point);
- lines.point = NULL;
- } else{
- lines.point = new pair;
- lines.point->arr[0] = V;
- }
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement