Exemplo n.º 1
0
/*********************************************
||divide_line() function
||Purpose: A line segment is sent in with the 
||	  number of degrees it is off the 
||	  plane and how many iterations are left
||precondition: A line segment is sent in
||postcondition: The line is split into 4 line 
|| 		 segments and each line is sent
|| 		 recursively if there are iterations
|| 		 left to execute.
*********************************************/
void divide_line(GLfloat *a, GLfloat *b, int degrees, int m)
{
	//Declarations
	double lengthOfSide, //holds the length of line (a,b)
		angle1, // holds the radians of -60
		angle2, // holds the radians of +60
		radians; //holds the radians for the degrees sent in
	GLfloat	r[2], s[2], t[2]; //new points to divide current line into 4


	if(m > 0)
	{
		//Find the length of the line
		lengthOfSide = length(a, b);

		//Convert degrees to radians
		radians = degreesToRadians(degrees); //original angle of line
		angle1 = degreesToRadians(-60+degrees); //turn to the right
		angle2 = degreesToRadians(60+degrees); //turn to the left

		//Math to create new points
		r[0] = a[0] + ((lengthOfSide/3.0) * cos(radians)); //find new x
		r[1] = a[1] + ((lengthOfSide/3.0) * sin(radians)); //find new y
		s[0] = r[0] + ((lengthOfSide/3.0) * cos(angle1)); //find new x
		s[1] = r[1] + ((lengthOfSide/3.0) * sin(angle1)); //find new y	
		t[0] = s[0] + ((lengthOfSide/3.0) * cos(angle2)); //find new x
		t[1] = s[1] + ((lengthOfSide/3.0) * sin(angle2)); //find new y	
	

		//Next iteration
		glColor3f(1.0,0.0,0.0); //red line
		divide_line(a, r, degrees, m-1);
		glColor3f(0.0,1.0,0.0); //green line
		divide_line(r, s, degrees - 60, m-1);
		glColor3f(0.0,0.0,1.0); //blue line
		divide_line(s, t, degrees + 60, m-1);
		glColor3f(0.0,0.5,0.5); //aqua line
		divide_line(t, b, degrees, m-1);
	}
	else
		drawLine(a,b);	//Draw line when m == 0
}
Exemplo n.º 2
0
/*********************************************
||display() function
||Purpose: Draw 
**********************************************/
void display()
{
    int timer, start, end;

    start = glutGet(GLUT_ELAPSED_TIME); //start timer

    glBegin(GL_LINES);

   //First snowflake
    divide_line(v[0], v[1], 0, n); 
    divide_line(v[1], v[2], 120, n); 
    divide_line(v[2], v[0], -120, n); 

  //Second snowflake
   if(drawSecondSnowflake)
    {
    	divide_line(w[0], w[1], 0, n); 
    	divide_line(w[1], w[2], 120, n); 
    	divide_line(w[2], w[0], -120, n); 
    }

    glEnd();
    glFlush();

    end = glutGet(GLUT_ELAPSED_TIME); //end timer
    timer = end-start; //find elapsed time
    drawMouseHertz(timer); //send it in to be stored

}
void scanner()
{
    int i;
    scanf("%f%f",&world.width,&world.height);
    float w = world.width/2;
    float h = world.height/2;
    world.p[0].x = -w;
    world.p[0].y = -h;
    world.p[1].x = w;
    world.p[1].y = -h;
    world.p[2].x = w;
    world.p[2].y = h;
    world.p[3].x = -w;
    world.p[3].y = h;
    for(i=0; i<num_world; i++)
    {
        point p=world.p[i],q=world.p[(i+1)%4];
        world.l[i].p1 = p;
        world.l[i].p2 = q;
        world.l[i] = find_slope_intercept(world.l[i]);
    }

    scanf("%d",&num_projectors);
    for(i=0; i<num_projectors; i++)
    {
        scanf("%f%f%f%f",&projector[i].l.p1.x,&projector[i].l.p1.y,&projector[i].l.p2.x,&projector[i].l.p2.y);
        scanf("%f",&projector[i].distance);
        scanf("%d",&projector[i].num_pixels);
        projector[i].l = find_slope_intercept(projector[i].l);
        projector[i].d = find_source_point(projector[i]);
        divide_line(projector[i].l.p1,projector[i].l.p2,projector[i].num_pixels,i);
    }

    scanf("%d",&num_blocks);
    for(i=0; i<num_blocks; i++)
    {
        scanf("%f%f%f%f",&block[i].l.p1.x,&block[i].l.p1.y,&block[i].l.p2.x,&block[i].l.p2.y);
        block[i].l = find_slope_intercept(block[i].l);
    }

    scanf("%d",&num_mirrors);
    for(i=0; i<num_mirrors; i++)
    {
        scanf("%f%f%f%f",&mirror[i].l.p1.x,&mirror[i].l.p1.y,&mirror[i].l.p2.x,&mirror[i].l.p2.y);
        mirror[i].l = find_slope_intercept(mirror[i].l);
    }
    mouse.p.x = mouse.p.y = 0;
}
void gaze_cursor(int value)
{
    if(!gaze_cursor_mode)
    {
        glutTimerFunc(10,gaze_cursor,0);
        return;
    }
    int i = 0;
    for(i=0; i<num_projectors; i++)
    {
        point center;
        center.x = (projector[i].l.p1.x + projector[i].l.p2.x)/2;
        center.y = (projector[i].l.p1.y + projector[i].l.p2.y)/2;
        float m = tan(atan2(center.y - mouse.p.y , center.x - mouse.p.x));
        float m1 = -1.0/m;
        float x3 = center.x;
        float y3 = center.y;
        float l = distance_between_points(projector[i].l.p1,projector[i].l.p2);
        float val = sqrt((l*l)/(4+4*m1*m1));
        float c1 = y3 - m1*x3;
        float x2 = x3 + val;
        float x1 = 2*x3 - x2;
        float y1 = x1*m1 + c1;
        float y2 = x2*m1 + c1;
        projector[i].l.p1.x = x1;
        projector[i].l.p1.y = y1;
        projector[i].l.p2.x = x2;
        projector[i].l.p2.y = y2;
        if(find_side(projector[i].l.p1,projector[i].l.p2,projector[i].d) == -1)
        {
            float x2 = x3 - val;
            float x1 = 2*x3 - x2;
            float y1 = x1*m1 + c1;
            float y2 = x2*m1 + c1;
            projector[i].l.p1.x = x1;
            projector[i].l.p1.y = y1;
            projector[i].l.p2.x = x2;
            projector[i].l.p2.y = y2;
        }
        projector[i].l.m = m1;
        projector[i].l.c = c1;
        //projector[i].l = find_slope_intercept(projector[i].l);
        projector[i].d = find_source_point(projector[i]);
        divide_line(projector[i].l.p1,projector[i].l.p2,projector[i].num_pixels,i);
    }
    glutTimerFunc(10,gaze_cursor,0);

}
void update_values(float x1,float y1,float x2,float y2,int selected,int i)
{
    if(selected == PROJECTOR)
    {
        if(abs(projector[i].l.p1.x+x1) >= world.width/2 || abs(projector[i].l.p2.x+x2) >= world.width/2
                || abs(projector[i].l.p1.y+y1) >= world.height/2 || abs(projector[i].l.p2.y+y2) >= world.height/2)
            return;
        projector[i].l.p1.x += x1;
        projector[i].l.p1.y += y1;
        projector[i].l.p2.x += x2;
        projector[i].l.p2.y += y2;
        projector[i].l = find_slope_intercept(projector[i].l);
        projector[i].d = find_source_point(projector[i]);
        divide_line(projector[i].l.p1,projector[i].l.p2,projector[i].num_pixels,i);
    }
    else if(selected == BLOCK)
    {
        if(abs(block[i].l.p1.x+x1) >= world.width/2 || abs(block[i].l.p2.x+x2) >= world.width/2
                || abs(block[i].l.p1.y+y1) >= world.height/2 || abs(block[i].l.p2.y+y2) >= world.height/2)
            return;
        block[i].l.p1.x += x1;
        block[i].l.p1.y += y1;
        block[i].l.p2.x += x2;
        block[i].l.p2.y += y2;
        block[i].l = find_slope_intercept(block[i].l);
    }
    else
    {
        if(abs(mirror[i].l.p1.x+x1) >= world.width/2 || abs(mirror[i].l.p2.x+x2) >= world.width/2
                || abs(mirror[i].l.p1.y+y1) >= world.height/2 || abs(mirror[i].l.p2.y+y2) >= world.height/2)
            return;
        mirror[i].l.p1.x += x1;
        mirror[i].l.p1.y += y1;
        mirror[i].l.p2.x += x2;
        mirror[i].l.p2.y += y2;
        mirror[i].l = find_slope_intercept(mirror[i].l);
    }
}
Exemplo n.º 6
0
/*********************************************
||display() function
||Purpose: Draw 2 triangles and if need be divide it N times
**********************************************/
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRotatef(spin, 1.0, 1.0, 0.0); //Rotate coordinates on x=y axis
    glBegin(GL_LINES);

    //First snowflake
    divide_line(v[0], v[1], 0, n);
    divide_line(v[1], v[2], 120, n);
    divide_line(v[2], v[0], -120, n);

    //Second snowflake
    divide_line(w[0], w[1], 0, n);
    divide_line(w[1], w[2], 120, n);
    divide_line(w[2], w[0], -120, n);

    glEnd();
    glFlush();
}
void rotate_object(float rot_val,int side,int selected,int i)
{
    if(selected == PROJECTOR)
    {
        float x2,y2,c,m,d;
        m = tan(atan(projector[i].l.m) + rot_val);
        c = projector[i].l.p1.y - m*projector[i].l.p1.x;
        d = distance_between_points(projector[i].l.p1,projector[i].l.p2);
        x2 = projector[i].l.p1.x + sqrt( (d*d) / (1 + m*m));
        y2 = m*x2 + c;
        point p;
        p.x=x2,p.y=y2;
        if(find_side(projector[i].l.p1,projector[i].l.p2,p) == side)
        {
            x2 = projector[i].l.p1.x - sqrt( (d*d) / (1 + m*m));
            y2 = m*x2 + c;
        }
        if( abs(x2) >= world.width/2 || abs(y2) >= world.height/2 )
            return;
        projector[i].l.m = m;
        projector[i].l.c = c;
        projector[i].l.p2.x = x2;
        projector[i].l.p2.y = y2;
        projector[i].l = find_slope_intercept(projector[i].l);
        projector[i].d = find_source_point(projector[i]);
        divide_line(projector[i].l.p1,projector[i].l.p2,projector[i].num_pixels,i);
    }
    else if(selected == BLOCK)
    {
        float x2,y2,c,m,d;
        m = tan(atan(block[i].l.m) + rot_val);
        c = block[i].l.p1.y - m*block[i].l.p1.x;
        d = distance_between_points(block[i].l.p1,block[i].l.p2);
        x2 = block[i].l.p1.x + sqrt( (d*d) / (1 + m*m));
        y2 = m*x2 + c;
        point p;
        p.x=x2,p.y=y2;
        if(find_side(block[i].l.p1,block[i].l.p2,p) == side)
        {
            x2 = block[i].l.p1.x - sqrt( (d*d) / (1 + m*m));
            y2 = m*x2 + c;
        }
        if( abs(x2) >= world.width/2 || abs(y2) >= world.height/2 )
            return;
        block[i].l.m = m;
        block[i].l.c = c;
        block[i].l.p2.x = x2;
        block[i].l.p2.y = y2;
        block[i].l = find_slope_intercept(block[i].l);
    }
    else
    {
        float x2,y2,c,m,d;
        m = tan(atan(mirror[i].l.m) + rot_val);
        c = mirror[i].l.p1.y - m*mirror[i].l.p1.x;
        d = distance_between_points(mirror[i].l.p1,mirror[i].l.p2);
        x2 = mirror[i].l.p1.x + sqrt( (d*d) / (1 + m*m));
        y2 = m*x2 + c;
        point p;
        p.x=x2,p.y=y2;
        if(find_side(mirror[i].l.p1,mirror[i].l.p2,p) == side)
        {
            x2 = mirror[i].l.p1.x - sqrt( (d*d) / (1 + m*m));
            y2 = m*x2 + c;
        }
        if( abs(x2) >= world.width/2 || abs(y2) >= world.height/2 )
            return;
        mirror[i].l.m = m;
        mirror[i].l.c = c;
        mirror[i].l.p2.x = x2;
        mirror[i].l.p2.y = y2;
        mirror[i].l = find_slope_intercept(mirror[i].l);
    }
}