Thursday, July 16, 2015

CSC Links

 for online presentation slide : www.prezi.com

CLISP OpenGL Programs : http://www.informatimago.com/develop/lisp/l99/
 and
 https://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html

ADAPTER : https://www.youtube.com/user/sujeshanto

http://videoant.com/tutorial/design-pattern-adapter-pattern-tamil-tutorial/cso_q2T8aeE/7149

 C++ :  https://www.youtube.com/watch?v=BExS00WQZ64&index=3&list=PLTZbNwgO5ebo64D1k0DJQGX30X6iSTmRr

Window-To-Viewport Mapping.cpp

W2V2 


#include <GL/glut.h>

const int screenWidth = 640;
const int screenHeight = 480;       

// defining the Window
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(left, right, bottom, top);
}

// setting the Viewport
void setViewport(int left, int right, int bottom, int top)
{
    glViewport(left, bottom, right - left, top - bottom);
}

void myInit(void)
{
    glClearColor (1.0, 1.0, 1.0, 0.0);   // set black background color
    glColor3f (1.0f, 0.0f, 0.0f);       // set the drawing color
}

void drawShape()
{   
    glBegin(GL_LINE_LOOP);
        glVertex2i(150,100);
        glVertex2i(350, 100);
        glVertex2i(400, 300);
        glVertex2i(500, 300);
        glVertex2i(250,450);
        glVertex2i(00, 300);
        glVertex2i(100, 300);
    glEnd();
    glFlush();
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);        // clear the screen
    setWindow(0.0, 640.0, 0.0, 480.0);  // set the window
   
    int bottomX = 0;
    int bottomY = 0;
    int width = 64;
    int height = 48;
     int i=0; 
      int j=0; 
    //setViewport( left,  right,  bottom,  top)
   
     for(int bottomX=0; bottomX<screenWidth; bottomX+=width) 
      { 
           for(int bottomY=0;  bottomY<screenHeight; bottomY+=height) 
           { 
                setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport 
                if((j%2==0 || i%2==1) && (j%2==1 || i%2==0)){ 
                setWindow(0.0, 640.0, 0.0, 480.0);  
                drawShape(); 
                } 
                else{ 
                     setWindow(0.0,640.0, 480.0, 0.0);  
                     drawShape(); 
                } 
                     j++; 
           } 
           i++; 
      } 
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size
    glutCreateWindow("Window-To-Viewport Mapping");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();

    return 0;
}

Window-To-Viewport Mapping.cpp


W2V1 


#include <GL/glut.h>

const int screenWidth = 640;
const int screenHeight = 480;       

// defining the Window
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(left, right, bottom, top);
}

// setting the Viewport
void setViewport(int left, int right, int bottom, int top)
{
    glViewport(left, bottom, right - left, top - bottom);
}
void myInit(void)
{
    glClearColor (1.0, 1.0, 1.0, 0.0);   // set black background color
    glColor3f (1.0f, 0.0f, 0.0f);       // set the drawing color
}

void drawShape()
{  
    glBegin(GL_LINE_LOOP);
        glVertex2i(150,100);
        glVertex2i(350, 100);
        glVertex2i(400, 300);
        glVertex2i(500, 300);
        glVertex2i(250,450);
        glVertex2i(00, 300);
        glVertex2i(100, 300);
    glEnd();
    glFlush();
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);        // clear the screen
    setWindow(0.0, 640.0, 0.0, 480.0);  // set the window
   
    int bottomX = 0;
    int bottomY = 0;
    int width = 64;
    int height = 48;
    //setViewport( left,  right,  bottom,  top
    while(bottomY<screenHeight)
    {
         bottomX =0 ;
        while(bottomX<screenWidth)
        {
         //setViewport( left,  right,  bottom,  top)
   
        setViewport(bottomX, bottomX + width, bottomY,  bottomY + height);  // set the viewport

        drawShape();
          bottomX =bottomX+width ;
        }
         bottomY =bottomY+height ;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size
    glutCreateWindow("Window-To-Viewport Mapping");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();

    return 0;
}

Thursday, July 9, 2015

Geometric Transformation

2D Shape
#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);
trans();
           }
      }
 }
 
 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 );
 }