#include <GL\glut.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <math.h>

GLsizei winWidth=1500, winHeight=705;
GLint ptCtr=0;
int choise = 10;
int s,c,PP , p11,p12;
boolean d = false;
double x,y,z ;
int color , co ;
class scrPt{
public: GLint x,y;
};
static scrPt endPt1,endPt2;
scrPt Pt1,Pt2,Pt3;
void init(void)
{
     glClearColor(1.0, 1.0, 1.0,1.0);
     glMatrixMode(GL_PROJECTION);
     gluOrtho2D(0.0, 200.0, 0.0, 150.0);
 }


  void dis(void)
  {
   // glClear(GL_COLOR_BUFFER_BIT);
    //Quads
      glColor3f(1.0, 1.0, 1.0);
   glBegin(GL_QUADS);
   glVertex2f(0,0);
   glVertex2f(200,0);
   glVertex2f(200,705);
   glVertex2f(0,705);
   glEnd();
   // line1
    glColor3f(0.5, 0.5, 0.5);
    glBegin (GL_LINES);
    glVertex2f (0, 50);
    glVertex2f (200, 50);
    glEnd();
  // line1

    glBegin (GL_LINES);
    glVertex2f (0, 150);
    glVertex2f (200, 150);
    glEnd();

     // line2
    glBegin (GL_LINES);
    glVertex2f (0, 300);
    glVertex2f (200, 300);
    glEnd();

     // line3
    glBegin (GL_LINES);
    glVertex2f (0, 450);
    glVertex2f (200, 450);
    glEnd();

     // line4
    glBegin (GL_LINES);
    glVertex2f (0, 600);
    glVertex2f (200, 600);
    glEnd();


      glColor3f(0.5, 0.3, 0.5);
     glBegin (GL_LINES);
     glVertex2f (50, 80);
     glVertex2f (150, 100);
     glEnd();
    //****
      glColor3f(0.5, 0.3, 0.6);
     glBegin (GL_LINE_LOOP);
     glVertex2f (50, 170);
     glVertex2f (50, 250);
     glVertex2f (150, 250);
     glVertex2f (150, 170);
     glEnd();
    //--------
      glColor3f(0.6, 0.3, 0.6);
     glBegin (GL_LINE_LOOP);
     glVertex2f (50, 350);
     glVertex2f (150, 350);
     glVertex2f (100, 400);
     glEnd();
      //*-*-*-*-*-*

       int x = 0,radius=50,xCenter=100, yCenter=520;
	int y = radius;
	int f = 1-radius;


		while (x < y) {
			x++;
			if (f < 0)
				f += 2*x+1;
			else {
				y--;
				f += 2*(x-y)+1;
    	  }

    	  glBegin(GL_POINTS);
				glVertex2i (xCenter+x, yCenter+y);
				glVertex2i (xCenter+y, yCenter+x);

            	glVertex2i (xCenter-x, yCenter+y);
				glVertex2i (xCenter+y, yCenter-x);

            	glVertex2i (xCenter+x, yCenter-y);
				glVertex2i (xCenter-y, yCenter+x);

            	glVertex2i (xCenter-x, yCenter-y);
				glVertex2i (xCenter-y, yCenter-x);


   	 glEnd();
		}
      //-*-+-+-+-+-+-+*/*/
      glColor3f(0.7, 0.8, 0.9);
      glBegin (GL_POLYGON);
      glVertex2f (50,610);
      glVertex2f (50,650);
      glVertex2f (150,650);
      glVertex2f (150, 610);
      glEnd();




     //*******color****
      //1
        glColor3f(1.0, 0.0, 0.0);
      glBegin (GL_POLYGON);
      glVertex2f (0,0);
      glVertex2f (0,25);
      glVertex2f (50,25);
      glVertex2f (50, 0);
      glEnd();
     //2
        glColor3f(0.0, 1.0, 0.0);
      glBegin (GL_POLYGON);
      glVertex2f (0,25);
      glVertex2f (0,50);
      glVertex2f (50,50);
      glVertex2f (50, 25);
      glEnd();
    //3
       glColor3f(0.0, 0.0, 1.0);
      glBegin (GL_POLYGON);
      glVertex2f (50,0);
      glVertex2f (50,25);
      glVertex2f (100,25);
      glVertex2f (100, 0);
      glEnd();
    //4
     glColor3f(0.8, 0.1, 0.4);
      glBegin (GL_POLYGON);
      glVertex2f (50,25);
      glVertex2f (50,50);
      glVertex2f (100,50);
      glVertex2f (100, 25);
      glEnd();
    //5
     glColor3f(1.0, 0.6, 0.1);
      glBegin (GL_POLYGON);
      glVertex2f (100,0);
      glVertex2f (100,25);
      glVertex2f (150,25);
      glVertex2f (150, 0);
      glEnd();
    //6
     glColor3f(0.6, 0.5, 1.0);
      glBegin (GL_POLYGON);
      glVertex2f (100,25);
      glVertex2f (100,50);
      glVertex2f (150,50);
      glVertex2f (150, 25);
      glEnd();
    //7
     glColor3f(1.0, 0.9, 0.0);
      glBegin (GL_POLYGON);
      glVertex2f (150,0);
      glVertex2f (150,25);
      glVertex2f (200,25);
      glVertex2f (200, 0);
      glEnd();
    //8
     glColor3f(0.2, 0.8, 0.4);
      glBegin (GL_POLYGON);
      glVertex2f (150,25);
      glVertex2f (150,50);
      glVertex2f (200,50);
      glVertex2f (200,25);
      glEnd();
    //end color***********



   glFlush();
  }
void displayFcn(void)
  {
    glClear(GL_COLOR_BUFFER_BIT);
    //Quads
      glColor3f(1.0, 1.0, 1.0);
      dis();
   glFlush();
  }


   void selectcolor( scrPt endPt1 )
{
   if(endPt1.x >0 && endPt1.x <50 && endPt1.y > 0 && endPt1.y < 25 )
        { co = 1 ;x=1.0; y=0.0; z=0.0; glColor3f(x, y, z); }
     else if(endPt1.x >0 && endPt1.x <50 && endPt1.y > 25 && endPt1.y <50 )
         { co = 1 ;x=0.0; y=1.0; z=0.0; glColor3f(x, y, z); }
     else if(endPt1.x >50 && endPt1.x <100 && endPt1.y > 0 && endPt1.y < 25 )
          {co = 1 ; x=0.0; y=0.0; z=1.0; glColor3f(x, y, z); }
     else if(endPt1.x >50 && endPt1.x <100 && endPt1.y > 25 && endPt1.y < 50 )
         { co = 1 ;x=0.8; y=0.1; z=0.4;  glColor3f(x, y, z);}
     else if(endPt1.x >0 && endPt1.x <100 && endPt1.y > 0 && endPt1.y < 25 )
         { co = 1 ;x=1.0; y=0.6; z=0.1;  glColor3f(x, y, z);}
     else if(endPt1.x >0 && endPt1.x <150 && endPt1.y > 25 && endPt1.y <50 )
         { co = 1 ;x=0.6; y=0.5; z=1.0;  glColor3f(x, y, z);}
     else if(endPt1.x >150 && endPt1.x <200 && endPt1.y > 0 && endPt1.y < 25 )
          { co = 1 ;x=1.0; y=0.9; z=0.0; glColor3f(x, y, z); }
     else if(endPt1.x >150 && endPt1.x <200 && endPt1.y > 25 && endPt1.y < 50 )
         { co = 1 ;x=0.2; y=0.8; z=0.4;  glColor3f(x, y, z);}
         else   co = 0 ;


}
void drowCircle(scrPt endPt1,scrPt endPt2)
 {
  int dx , dy ;
  dx = endPt2.x - endPt1.x ;
  dy = endPt2.y - endPt1.y ;
  double r = pow ( ( pow(dx,2) + pow(dy,2)) , 0.5 ) ;
     int x = 0,radius= r , xCenter=endPt1.x , yCenter=endPt1.y;
	int y = radius;
	int f = 1-radius;


		while (x < y) {
			x++;
			if (f < 0)
				f += 2*x+1;
			else {
				y--;
				f += 2*(x-y)+1;
    	  }

    	  glBegin(GL_POINTS);
				glVertex2i (xCenter+x, yCenter+y);
				glVertex2i (xCenter+y, yCenter+x);

            	glVertex2i (xCenter-x, yCenter+y);
				glVertex2i (xCenter+y, yCenter-x);

            	glVertex2i (xCenter+x, yCenter-y);
				glVertex2i (xCenter-y, yCenter+x);

            	glVertex2i (xCenter-x, yCenter-y);
				glVertex2i (xCenter-y, yCenter-x);


   	 glEnd();
		}
   }

 void drowQuads(scrPt endPt1,scrPt endPt2){
 glBegin (GL_LINE_LOOP);
      glVertex2f (endPt1.x,endPt1.y);
      glVertex2f (endPt1.x,endPt2.y);
      glVertex2f (endPt2.x,endPt2.y);
      glVertex2f (endPt2.x,endPt1.y);
      glEnd();
}
 void drowTt(scrPt endPt1,scrPt endPt2){
 glBegin (GL_TRIANGLES);
      glVertex2f (endPt2.x,endPt2.y);
      glVertex2f (endPt1.x-50,endPt1.y);
      glVertex2f (endPt1.x+50,endPt1.y);
      glEnd();
}

void drowRect(scrPt endPt1,scrPt endPt2){

glRectf(endPt1.x-5 , endPt1.y+5 , endPt2.x+5 , endPt2.y-5 );

}


void drowLine(scrPt endPt1,scrPt endPt2){
     glBegin (GL_LINES);
     glVertex2f (endPt1.x, endPt1.y);
     glVertex2f (endPt2.x, endPt2.y);
     glEnd();
}
 void check (scrPt p)
  {
     if( (p.x >0 && p.x <200) && (p.y > 600 && p.y < 705 ))
         choise = 1 ;
     else if(p.x >0 && p.x <200 && p.y > 450 && p.y < 600 )
       {  choise = 2 ; c = 0 ; }
     else if(p.x >0 && p.x <200 && p.y > 300 && p.y < 450 )
       {  choise = 3 ;  c = 0 ; }
     else if(p.x >0 && p.x <200 && p.y > 150 && p.y < 300 )
       {  choise = 4 ;   c= 0 ; }
     else if(p.x >0 && p.x <200 && p.y > 50 && p.y < 150 )
       {  choise = 5 ;   c = 0 ; }
     else if(p.x >0 && p.x <50 && p.y > 0 && p.y < 25 )
         {color = 6 ;   c = 0 ; }
     else if(p.x >0 && p.x <50 && p.y > 25 && p.y <50 )
         {color = 7 ;   c = 0 ; }
     else if(p.x >50 && p.x <100 && p.y > 0 && p.y < 25 )
          {color = 8 ;   c = 0 ; }
     else if(p.x >50 && p.x <100 && p.y > 25 && p.y < 50 )
         { color = 9 ;   c = 0 ;}
     else if(p.x >0 && p.x <100 && p.y > 0 && p.y < 25 )
         {color = 10 ;   c = 0 ; }
     else if(p.x >0 && p.x <150 && p.y > 25 && p.y <50 )
         {color = 11 ;   c = 0 ; }
     else if(p.x >150 && p.x <200 && p.y > 0 && p.y < 25 )
          {color = 12 ;   c = 0 ;  }
     else if(p.x >150 && p.x <200 && p.y > 25 && p.y < 50 )
         {color = 13 ;   c = 0 ; }

     else choise = 0 ;


  }


 void drow(int shape , scrPt Pt1 , scrPt Pt2)
   {
      switch (shape)
         {
            case 1 : drowRect ( Pt1,Pt2) ;
            break ;
            case 2 : drowCircle ( Pt1,Pt2) ;
            break ;
            case 3 : drowTt (Pt1,Pt2) ;
            break ;
            case 4 : drowQuads (Pt1 , Pt2 ) ;
            break ;
            case 5 : drowLine ( Pt1,Pt2) ;
            break ;
         }

   }


void polyline(GLint button,GLint action,GLint xMouse,GLint yMouse)
{
 static scrPt endPt1,endPt2;
    if(ptCtr==0)
        {
            if (button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN)
                {
                  endPt1.x=xMouse;
                  endPt1.y=winHeight-yMouse;
                  ptCtr=1;
                  check(endPt1) ;
                  selectcolor(endPt1);
                  if (co==1) {
                    switch(color)
                           {
                             case 6 : selectcolor(endPt1);
                             break ;
                             case 7 : selectcolor(endPt1);
                             break ;
                             case 8 : selectcolor(endPt1);
                             break ;
                             case 9 : selectcolor(endPt1);
                             break ;
                             case 10 : selectcolor(endPt1);
                             break ;
                             case 11 : selectcolor(endPt1);
                             break ;
                             case 12 : selectcolor(endPt1);
                             break ;
                             case 13 : selectcolor(endPt1);
                             break ;
                           }
                    }

                   if (choise == 1)
                     {
                           if (d==false )
                            {
                              Pt1.x = endPt1.x ;
                              Pt1.y = endPt1.y ;
                              d = true ;
                              c = 1 ;
                              d = false ;
                            }
                     }

                  if (choise == 0) {
                           if (d==false )
                            {
                              Pt1.x = endPt1.x ;
                              Pt1.y = endPt1.y ;
                              d = true ;
                              if (c==1) { drow(1,endPt1,endPt1) ; d = false ; dis();}
                            }
                           else
                            {
                              Pt2.x = endPt1.x ;
                              Pt2.y = endPt1.y ;
                              d = false ;
                              switch(s)
                           {
                             case 2 : drow(2,Pt1,Pt2);dis();
                             break ;
                             case 3 : drow(3,Pt1,Pt2);dis();
                             break ;
                             case 4 : drow(4,Pt1,Pt2);dis();
                             break ;
                             case 5 : drow(5,Pt1,Pt2); dis();
                             break ;
                           }
                            }
                           }
                  else
                     {
                         s = choise ;

                     }




                }
            else if(button==GLUT_RIGHT_BUTTON)exit(0);
       }
    else if(button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN)
           {
              endPt2.x=xMouse;
              endPt2.y=winHeight-yMouse;
              ptCtr=0;
              check(endPt2) ;
              cout<<choise;
              check(endPt2) ;
              selectcolor(endPt2);
               if (co==1) {
                    switch(color)
                           {
                             case 6 : selectcolor(endPt2);
                             break ;
                             case 7 : selectcolor(endPt2);
                             break ;
                             case 8 : selectcolor(endPt2);
                             break ;
                             case 9 : selectcolor(endPt2);
                             break ;
                             case 10 : selectcolor(endPt2);
                             break ;
                             case 11 : selectcolor(endPt2);
                             break ;
                             case 12 : selectcolor(endPt2);
                             break ;
                             case 13 : selectcolor(endPt2);
                             break ;
                           }
                    }
                        if (choise == 1)
                     {
                           if (d==false )
                            {
                              Pt1.x = endPt2.x ;
                              Pt1.y = endPt2.y ;
                              d = false ;
                              c=1 ;
                            }
                     }

                  if (choise == 0) {
                           if (d==false )
                            {
                              Pt1.x = endPt2.x ;
                              Pt1.y = endPt2.y ;
                              d = true ;
                              if (c==1) { drow(1,endPt2,endPt2); d = false ; dis(); }
                            }
                           else
                            {
                              Pt2.x = endPt2.x ;
                              Pt2.y = endPt2.y ;
                              d = false ;
                            switch(s)
                           {
                             case 2 : drow(2,Pt1,Pt2); dis();
                             break ;
                             case 3 : drow(3,Pt1,Pt2);dis();
                             break ;
                             case 4 : drow(4,Pt1,Pt2); dis();
                             break ;
                             case 5 : drow(5,Pt1,Pt2); dis();
                             break ;
                           }

                            }
                           }
                  else
                     {
                         s = choise ;
                     }


                }



   else if(button==GLUT_RIGHT_BUTTON)exit(0);
   glFlush();
    }




void winReshapeFcn(GLint newWidth,GLint newHeight){

glViewport(0,0,newWidth,newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble (newWidth), 0.0, GLdouble (newHeight));
winWidth=newWidth;
winHeight=newHeight;

}



void main(int argc,char** argv)
{	   glutInit(&argc, argv);
  		glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
  		glutInitWindowPosition(-900,-600);
      glutInitWindowSize(winWidth,winHeight);
  		glutCreateWindow("OPEN GL BROJECT");
       init();
  	  	glutDisplayFunc(displayFcn);
      glutReshapeFunc(winReshapeFcn);
      glutMouseFunc(polyline);
     	glutMainLoop();
      }
