Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<GL/glut.h>
- #include<cstdio>
- #include<cmath>
- #include<map>
- #include<vector>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- typedef struct
- {
- int h,k;
- int selected;
- } data;
- typedef struct
- {
- int downX,downY,upX,upY;
- int buttontype;
- } SQBUTTONS;
- #define MARGIN 140
- #define VERTEX_MODE_INSIDE_AREA atX>MARGIN && vertex_mode
- #define EDGE_MODE_INSIDE_AREA atX>MARGIN && edge_mode
- #define BUTTON_AREA atX<MARGIN
- #define VERTEX_BUTTON 1
- #define EDGE_BUTTON 2
- #define ALGOWIN_BUTTON 3
- #define PANEL_MATRIX_BUTTON 1
- #define COLOR_RED glColor3f(255.0f, 0.0f, 0.0f);
- #define COLOR_BLACK glColor3f(0.0f, 0.0f, 0.0f);
- #define COLOR_WHITE glColor3f(1.0f, 1.0f, 1.0f);
- #define COLOR_YELLOW glColor3f(255.0f, 110.0f, 0.0f);
- vector<data>centers;
- vector<int>edges[100];
- vector<SQBUTTONS>v_buttons;
- vector<SQBUTTONS>v_panelbuttons;
- map<int,pair<int,int> >co_ordinate_map;
- int adj_mat[100][100];
- int WIDTH = 1000;
- int HEIGHT = 800;
- int width = WIDTH;
- int height = HEIGHT;
- //////
- int nodes=0;
- int vertex_mode=1;
- int edge_mode=0;
- int algo_panelmode=0;
- int rad=6;
- int connection_V1;
- int connection_V2;
- int atX,atY;
- int selected_node;
- //////
- /////////
- bool incircle(int x,int y);
- void draw_circle(int H,int K,int r);
- int inbutton(int x,int y,int where_i_clicked);
- void draw_button(void);
- void DATAENTRY();
- void draw_text(int tx,int ty,string s,void *font);
- void drawpanel();
- /////////
- void display ()
- {
- // printf("calling display(%d)\n",++noOfDisplayCalls);
- // Function that clears the window. If you don't want to clear the view window, don't call glClear function. make it as a comment statement.
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // --------------
- // YOUR CODE ...
- // --------------
- COLOR_RED
- glLineWidth(5);
- glBegin(GL_LINE_STRIP);
- glVertex2f(120,0);
- glVertex2f(120,HEIGHT);
- glEnd();
- ////button
- draw_button();
- ////////
- glLineWidth(1);
- vector<data>::iterator it;
- vector<int>::iterator Lit;
- int nodenumbers=1;
- for(int i=1;i<=nodes;i++)
- {
- double x=co_ordinate_map[i].first;
- double y=co_ordinate_map[i].second;
- for(Lit=edges[i].begin();Lit<edges[i].end();Lit++)
- {
- double x1=co_ordinate_map[*Lit].first;
- double y1=co_ordinate_map[*Lit].second;
- COLOR_WHITE
- glBegin(GL_LINE_STRIP);
- glVertex2f(x,y);
- glVertex2f(x1,y1);
- glEnd();
- }
- }
- for(it=centers.begin();it<centers.end();it++)
- {
- if((*it).selected==0)
- glColor3f(3.5f, 2.5f, 1.5f);
- if((*it).selected==1)
- COLOR_RED
- if((*it).selected==2)
- COLOR_YELLOW
- draw_circle((*it).h,(*it).k,rad);
- char temp[10];
- sprintf(temp,"%d",nodenumbers);
- COLOR_RED
- void * font = GLUT_BITMAP_HELVETICA_10;
- draw_text((*it).h-10,(*it).k-17,temp,font);
- nodenumbers++;
- }
- if(algo_panelmode)
- {
- drawpanel();
- }
- glutSwapBuffers();
- }
- void mouseMovement(int mx, int my)
- {
- atX=mx;
- atY=my;
- atY=height-atY;
- }
- void mouseAction(int button, int state, int x, int z)
- // Record cursor position in response to mouse button action
- {
- switch(button)
- {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN)
- {
- if(VERTEX_MODE_INSIDE_AREA )
- {
- int too_close=0;
- vector<data>::iterator it;
- for(it=centers.begin();it<centers.end();it++)
- {
- int H=(*it).h;
- int K=(*it).k;
- int dist=(atX-H)*(atX-H)+(atY-K)*(atY-K);
- if(dist<2*4*rad*rad){too_close=1; break;}
- }
- if(!too_close)
- {
- data temp;
- temp.h=atX;
- temp.k=atY;
- printf("(%d %d)\n",atX,atY);
- temp.selected=0;
- centers.push_back(temp);
- nodes++;
- co_ordinate_map[nodes].first=atX;
- co_ordinate_map[nodes].second=atY;
- }
- glutPostRedisplay();
- }
- if(EDGE_MODE_INSIDE_AREA )
- {
- int click_on_node=0,a_node_before=0,new_edge_now=0;
- if(selected_node) a_node_before=selected_node;
- if(selected_node)new_edge_now=1;
- click_on_node=incircle(atX,atY);
- if(!new_edge_now) connection_V1=selected_node;
- if(!click_on_node)a_node_before=0;
- if(!click_on_node)puts("RESET");
- else if(new_edge_now)
- {
- connection_V2=selected_node;
- edges[connection_V1].push_back(connection_V2);
- adj_mat[connection_V1][connection_V2]=1;
- printf(">%d - %d now have a connection\n",connection_V1,connection_V2);
- }
- glutPostRedisplay();
- }
- if(algo_panelmode)
- {
- puts("PANEL MODE");
- int onbutton=inbutton(atX,atY,2);
- if(onbutton==PANEL_MATRIX_BUTTON)
- {
- for(int i=1;i<=nodes;i++)
- {
- for(int j=1;j<=nodes;j++)
- printf("%d",adj_mat[i][j]);
- puts("");
- }
- }
- }
- if(BUTTON_AREA)
- {
- int onbutton=inbutton(atX,atY,1);
- if(onbutton==VERTEX_BUTTON)
- {
- vertex_mode=1;
- edge_mode=0;
- algo_panelmode=0;
- glutPostRedisplay();
- }
- else if(onbutton==EDGE_BUTTON)
- {
- vertex_mode=0;
- edge_mode=1;
- algo_panelmode=0;
- glutPostRedisplay();
- }
- else if(onbutton==ALGOWIN_BUTTON)
- {
- vertex_mode=0;
- edge_mode=0;
- algo_panelmode=1;
- glutPostRedisplay();
- }
- }
- }
- break;
- }
- } // end mouse
- // Respond to window resizing, preserving proportions.
- // Parameters give new window size in pixels.
- void reshapeMainWindow (int newWidth, int newHeight)
- {
- // ----------------------------
- // DO NOT ALTER THIS FUNCTION ...
- // ----------------------------
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( 0, newWidth, 0, newHeight, 0, 1 );
- glViewport( 0, 0, newWidth, newHeight );
- HEIGHT = newWidth;
- WIDTH = newHeight;
- glMatrixMode(GL_MODELVIEW);
- glutPostRedisplay();
- }
- // Respond to function keys.
- // Parameters give key code and mouse coordinates.
- int main(int argc, char *argv[])
- {
- DATAENTRY();
- // GLUT initialization.
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(width, height);
- glutCreateWindow("GLUT Skeleton Program");
- // Register call backs.
- glutDisplayFunc(display);
- glutReshapeFunc(reshapeMainWindow);
- glutPassiveMotionFunc(mouseMovement);
- glutMouseFunc(mouseAction);
- // Enter GLUT loop.
- glutMainLoop();
- return 0;
- }
- bool incircle(int x,int y)
- {
- int H,K;
- int dist,flag=0,count=0;
- vector<data>::iterator it;
- for(it=centers.begin();it<centers.end();it++)
- {
- if((*it).selected==1 && !selected_node )
- (*it).selected=0;
- count++;
- H=(*it).h;
- K=(*it).k;
- dist=(H-x)*(H-x)+(K-y)*(K-y);
- if(dist<=rad*rad)
- {
- if(selected_node && !(*it).selected) (*it).selected=2;
- else (*it).selected=1;
- selected_node=count;
- flag=1;
- }
- }
- if(!flag)
- {
- for(it=centers.begin();it<centers.end();it++)
- (*it).selected=0;
- selected_node=0;
- }
- return flag;
- }
- int inbutton(int x,int y,int where_i_clicked)
- {
- if(where_i_clicked==1)
- {
- int H,K,flag=0;
- H=52;
- K=90;
- int dist=(H-x)*(H-x)+(K-y)*(K-y);
- if(dist<=5*5)
- flag=VERTEX_BUTTON;
- vector<SQBUTTONS>::iterator it;
- for(it=v_buttons.begin();it<v_buttons.end();it++)
- {
- if(x<=(*it).upX && x>=(*it).downX && y<=(*it).upY && y>=(*it).downY){flag=(*it).buttontype;break;}
- }
- return flag;
- }
- else
- {
- int flag=0;
- vector<SQBUTTONS>::iterator it;
- for(it=v_panelbuttons.begin();it<v_panelbuttons.end();it++)
- if(x<=(*it).upX && x>=(*it).downX && y<=(*it).upY && y>=(*it).downY){flag=(*it).buttontype;break;}
- return flag;
- }
- }
- void draw_circle(int H,int K,int r)
- {
- glBegin(GL_LINE_LOOP);
- for(int rr=1;rr<=r;rr++)
- for (int angle= 0; angle<=360; angle+=1)
- glVertex2f(H+ sin(angle) * rr, K + cos(angle) * rr);
- glEnd();
- }
- void draw_button(void)
- {
- glLineWidth(2);
- glBegin(GL_LINE_STRIP);
- glVertex2f(95,150);
- glVertex2f(95,110);
- glVertex2f(20,110);
- glVertex2f(20,150);
- glVertex2f(95,150);
- glEnd();
- string s = "Algo panel";
- void * font = GLUT_BITMAP_HELVETICA_12;
- draw_text(25,130,s,font);
- if(edge_mode || algo_panelmode)
- COLOR_WHITE
- else
- COLOR_RED
- draw_circle(52,90,5);
- glLineWidth(1);
- if(edge_mode)
- glColor3f(255.0f, 0.0f, 0.0f);
- else
- glColor3f(1.0f, 1.0f, 1.0f);
- if(algo_panelmode)
- glColor3f(1.0f, 1.0f, 1.0f);
- glBegin(GL_LINES);
- for(int i=65;i<=70;i++)
- {
- glVertex2f(72,i);
- glVertex2f(32,i);
- }
- glEnd();
- }
- void draw_text(int tx,int ty,string s,void *font)
- {
- glRasterPos2i(tx,ty);
- for (string::iterator i = s.begin(); i != s.end(); ++i)
- {
- char c = *i;
- glutBitmapCharacter(font, c);
- }
- }
- void drawpanel()
- {
- COLOR_WHITE
- glBegin(GL_QUADS);
- glVertex2f(MARGIN,HEIGHT);
- glVertex2f(MARGIN,400);
- glVertex2f(MARGIN+850,400);
- glVertex2f(MARGIN+850,HEIGHT);
- glEnd();
- COLOR_RED
- void * font = GLUT_BITMAP_HELVETICA_18;
- draw_text(420,780,"ALGORITHM CONTROL PANEL",font);
- COLOR_BLACK
- font = GLUT_BITMAP_HELVETICA_12;
- draw_text(MARGIN+35,720,"Show Matrix",font);
- glBegin(GL_LINE_STRIP);
- glVertex2f(MARGIN+15,740);
- glVertex2f(MARGIN+15,700);
- glVertex2f(MARGIN+125,700);
- glVertex2f(MARGIN+125,740);
- glVertex2f(MARGIN+15,740);
- glEnd();
- }
- void DATAENTRY(void)
- {
- SQBUTTONS B;
- B.downX=32; B.upX=72; B.downY=65; B.upY=70; B.buttontype=EDGE_BUTTON;
- v_buttons.push_back(B);
- B.downX=20; B.upX=95; B.downY=110; B.upY=150; B.buttontype=ALGOWIN_BUTTON;
- v_buttons.push_back(B);
- B.downX=MARGIN+15; B.upX=MARGIN+125; B.downY=700; B.upY=740; B.buttontype=PANEL_MATRIX_BUTTON;
- v_panelbuttons.push_back(B);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement