Wednesday, June 11, 2014

Opengl,C++ : Draw pattern with Geometric transformation

First specify the starting points using left mouse click.Then press "k" to draw a given pattern.

 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
    
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 double angle = 90;   
 double sangle = 45; //The angle for the rotation (in degrees)  
   
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
   
 Point2D p1,p2,p3,p4,sp1,sp2,sp3;  
 void DrawpolygonSegment(Point2D pt1, Point2D pt2,Point2D pt3,Point2D pt4)  
 {  
        glPointSize(1.0);  
     glBegin(GL_LINES);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
             glVertex2i(pt4.x, pt4.y);  
             glVertex2i(pt1.x, pt1.y);  
     glEnd();  
     glFlush();  
   
 }  
 void DrawLineSegment(Point2D pt1, Point2D pt2,Point2D pt3)  
 {  
      glLineWidth(1.0);  
      glBegin(GL_LINES);  
           glVertex2i(pt1.x, pt1.y);  
           glVertex2i(pt2.x, pt2.y);  
      glBegin(GL_LINES);  
           glVertex2i(pt2.x, pt2.y);  
           glVertex2i(pt3.x, pt3.y);  
      glEnd();  
      glFlush();  
   
 }  
   
   
 Point2D translate(Point2D p, float tx, float ty){  
     p.x =p.x+tx; //.....wite the equations for translation   
     p.y = p.y+ty; //.....wite the equations for translation  
     return p;  
 }  
   
 Point2D rotate(Point2D p,Point2D c, float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   
      ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);  
   
      return ptemp;  
 }  
   
 Point2D reflect_4(Point2D p){  
      Point2D ptemp;  
      ptemp.x=p.x;  
      ptemp.y=-p.y;  
      return ptemp;  
 }  
   
 Point2D Scale(Point2D p,double sx,double sy)  
 {  
      Point2D sp;  
      sp.x=sx*(p.x+90);  
      sp.y=sy*(p.y);  
      return sp;  
 }  
   
 void trans()  
 {  
      Point2D Trans_p1 = translate(p1,300,300);  
      Point2D Trans_p2 = translate(p2,300,300);  
   Point2D Trans_p3 = translate(p3,300,300);  
      DrawLineSegment(Trans_p1,Trans_p2,Trans_p3);  
        
      Point2D Scale_p1 = translate(Scale(p1,0.5,0.5),300,300);  
      Point2D Scale_p2 = translate(Scale(p2,0.5,0.5),300,300);  
   Point2D Scale_p3 = translate(Scale(p3,0.5,0.5),300,300);  
      DrawLineSegment(Scale_p1,Scale_p2,Scale_p3);  
   
      Point2D ref4_p1 =translate(reflect_4(p1),300,300);  
      Point2D ref4_p2 = translate(reflect_4(p2),300,300);  
   Point2D ref4_p3 = translate(reflect_4(p3),300,300);  
      DrawLineSegment(ref4_p1,ref4_p2,ref4_p3);  
   
      Point2D refScale4_p1 =translate(reflect_4(Scale(p1,0.5,0.5)),300,300);  
      Point2D refScale4_p2 = translate(reflect_4(Scale(p2,0.5,0.5)),300,300);  
   Point2D refScale4_p3 = translate(reflect_4(Scale(p3,0.5,0.5)),300,300);  
      DrawLineSegment(refScale4_p1,refScale4_p2,refScale4_p3);  
   
      //Point2D rotate_p1 =rotate(Trans_p1,angle);  
      int i=1;  
      while(i<=3){  
           Point2D rotate_p2 =rotate(Trans_p2,Trans_p1,angle*i);  
           Point2D rotate_p3 =rotate(Trans_p3,Trans_p1,angle*i);  
           Point2D rotate_p4 =rotate(ref4_p2,Trans_p1,angle*i);                 
           DrawpolygonSegment(Trans_p1,rotate_p2,rotate_p3,rotate_p4);  
   
           Point2D rotate_Scalep1 =rotate(Scale_p1,Trans_p1,angle*i);  
           Point2D rotate_Scalep2 =rotate(Scale_p2,Trans_p1,angle*i);  
           Point2D rotate_Scalep3 =rotate(Scale_p3,Trans_p1,angle*i);  
           Point2D rotate_Scalep4 =rotate(refScale4_p2,Trans_p1,angle*i);            
           DrawpolygonSegment(rotate_Scalep1,rotate_Scalep2,rotate_Scalep3,rotate_Scalep4);  
   
           i++;  
      }  
   
           sp1.x=0;  
           sp1.y=0;  
           sp2.x=100;  
           sp2.y=16;  
           sp3.x=175;  
           sp3.y=0;  
      i=1;  
   
      while(i<=4){  
                if(i==1)sangle=45;  
                if(i==2)sangle=135;  
                if(i==3)sangle=225;  
                if(i==4)sangle=315;  
                Point2D Trans_sp1 = translate(sp1,300,300);  
                Point2D Trans_sp2 = translate(sp2,300,300);  
                Point2D Trans_sp3 = translate(sp3,300,300);  
                //DrawLineSegment(Trans_sp1,Trans_sp2,Trans_sp3);  
   
                Point2D rotate_sp2 =rotate(Trans_sp2,Trans_sp1,sangle);  
                Point2D rotate_sp3 =rotate(Trans_sp3,Trans_sp1,sangle);  
   
                DrawLineSegment(Trans_p1,rotate_sp2,rotate_sp3);  
   
                Point2D ref4_sp1 =translate(reflect_4(sp1),300,300);  
                Point2D ref4_sp2 =translate( reflect_4(sp2),300,300);  
                Point2D ref4_sp3 =translate( reflect_4(sp3),300,300);  
   
                Point2D rotateref_sp2 =rotate(ref4_sp2,Trans_sp1,sangle);  
                Point2D rotateref_sp3 =rotate(ref4_sp3,Trans_sp1,sangle);  
   
                DrawLineSegment(Trans_p1,rotateref_sp2,rotateref_sp3);  
                     i++;  
      }  
 }  
   
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='k') glutIdleFunc(trans);  
 }  
   
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
                         
                                     p1.x=0;  
                                     p1.y=0;  
                                     p2.x=70;  
                                     p2.y=40;  
                                     p3.x=300;  
                                     p3.y=0;  
                                     //DrawLineSegment(p1,p2,p3);  
           }  
      }  
 }  
   
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
    
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
     glutDisplayFunc( myDisplay );  
        glutMouseFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

No comments:

Post a Comment