point find_source_point(Projector p)
{
    point q1,q2;
    float x3 = (p.l.p1.x + p.l.p2.x) / 2;
    float y3 = (p.l.p1.y + p.l.p2.y) / 2;
    if(p.l.m == 0)
    {
        q1.x = x3;
        q1.y = y3 - p.distance;
        q2.x = x3;
        q2.y = y3 + p.distance;
        if(find_side(p.l.p1,p.l.p2,q1) == 1)
            return q1;
        else
            return q2;
    }
    float m1 = -1/p.l.m;
    float c1 = y3 - m1*x3;
    float d = p.distance;
    float s = sqrt( (d*d) / (m1*m1+1) );
    q1.x = x3 - s;
    q1.y = m1*q1.x+c1;
    q2.x = x3 + s;
    q2.y = m1*q2.x+c1;
    if(find_side(p.l.p1,p.l.p2,q1) == 1)
        return q1;
    else
        return q2;
}
Esempio n. 2
0
/* Choose a move for the computer. */
void computer_move(void)
{
  int square;
  int row, col;

  /* Use first strategy rule that returns valid square */
  square = find_win(computer);
  if (!square)
    square = find_win(user);
  if (!square)
    square = middle_open();
  if (!square)
    square = find_corner();
  if (!square)
    square = find_side();

  printf("\nI am choosing square %d!\n", square);

  row = (square - 1) / 3;
  col = (square - 1) % 3;

  board[row][col] = computer;

  return;
}
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);

}
Esempio n. 4
0
void	raycast(t_env *e, double diff_x, double diff_y)
{
	double	ray_x;
	double	ray_y;

	ray_x = e->pos[0];
	ray_y = e->pos[1];
	while ((e->map[(int)(ray_y + diff_y)][(int)(ray_x + diff_x)]
			&& e->map[(int)ray_y][(int)ray_x] == '0') ||
		(e->map[(int)(ray_y + diff_y)][(int)(ray_x + diff_x)]
			&& e->map[(int)ray_y][(int)ray_x] == '2' && e->xray == 1))
	{
		ray_x += diff_x;
		ray_y += diff_y;
	}
	e->hit_x = (int)ray_x;
	e->hit_y = (int)ray_y;
	e->prev_rayx = ray_x - 2 * diff_x;
	e->prev_rayy = ray_y - 2 * diff_y;
	find_side(e, ray_x, ray_y);
	e->dist_ray = sqrt((e->pos[0] - ray_x) * (e->pos[0] - ray_x) +
			(e->pos[1] - ray_y) * (e->pos[1] - ray_y)) *
		cos(DEG_TO_RAD(e->angle) - DEG_TO_RAD(e->ray_angle));
}
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);
    }
}
void draw_world()
{
    world_z = world.height+25;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -world_z);
    int i,j,k;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    int c = 0;
    draw_box(world.width,world.height);
    glColor3f(color[c].r,color[c].g,color[c].b);
    for(i=0; i<num_projectors; i++)
    {
        if(select_type == PROJECTOR && select_number == i)
            draw_selected_line(projector[i].l);
        else
            draw_line(projector[i].l);
        draw_point(projector[i].d);
        glColor3f(color[c].r,color[c].g,color[c].b);
    }
    c++;
    glColor3f(color[c].r,color[c].g,color[c].b);
    for(i=0; i<num_mirrors; i++)
    {
        if(select_type == MIRROR && select_number == i)
            draw_selected_line(mirror[i].l);
        else
            draw_line(mirror[i].l);
        glColor3f(color[c].r,color[c].g,color[c].b);
    }
    c++;
    glColor3f(color[c].r,color[c].g,color[c].b);
    for(i=0; i<num_blocks; i++)
    {
        if(select_type == BLOCK && select_number == i)
            draw_selected_line(block[i].l);
        else
            draw_line(block[i].l);
        glColor3f(color[c].r,color[c].g,color[c].b);
    }
    /*for(i=0; i<num_projectors; i++)
    {
        for(j=0; j<projector[i].num_pixels; j++)
            draw_line(projector[i].pixels[j]);
        draw_point(projector[i].d);
    }*/
    for(i=0; i<num_projectors; i++)
    {
        for(j=0; j<projector[i].num_pixels; j++)
        {
            glColor3f(color[c].r,color[c].g,color[c++].b);
            point p,fp;
            float dist = INF;
            int type = 1,num;
            for(k=0; k<num_mirrors; k++)
            {
                p = find_intersection(mirror[k].l,projector[i].pixels[j]);
                if( (abs(p.x) <= world.width/2) && (abs(p.y) <= world.height/2) && check_point_on_line_segment(mirror[k].l,p) )
                    if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                    {
                        float d = distance_between_points(p,projector[i].pixels[j].p1);
                        if( d < dist )
                        {
                            fp = p;
                            dist = d;
                            type = 1;
                            num = k;
                            if(find_side(mirror[k].l.p1,mirror[k].l.p2,projector[i].pixels[j].p1) == -1)
                                type = 2;
                        }
                    }
            }
            for(k=0; k<num_blocks; k++)
            {
                p = find_intersection(block[k].l,projector[i].pixels[j]);
                if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(block[k].l,p))
                    if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                    {
                        float d = distance_between_points(p,projector[i].pixels[j].p1);
                        if( d < dist )
                        {
                            fp = p;
                            dist = d;
                            type = 2;
                        }
                    }
            }
            for(k=0; k<num_world; k++)
            {
                p = find_intersection(world.l[k],projector[i].pixels[j]);
                if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(world.l[k],p))
                    if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                    {
                        float d = distance_between_points(p,projector[i].pixels[j].p1);
                        if( d < dist )
                        {
                            fp = p;
                            dist = d;
                            type = 2;
                        }
                    }
            }
            for(k=0; k<num_projectors; k++)
            {
                if(k!=i)
                {
                    p = find_intersection(projector[k].l,projector[i].pixels[j]);
                    if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(projector[k].l,p))
                        if(find_side(projector[i].l.p1,projector[i].l.p2,p) == -1)
                        {
                            float d = distance_between_points(p,projector[i].pixels[j].p1);
                            if( d < dist )
                            {
                                fp = p;
                                dist = d;
                                type = 2;
                            }
                        }
                }
            }
            draw_line2(fp,projector[i].pixels[j].p2);
            if(type==1)
            {
                //float angle = find_angle(projector[i].pixels[j],mirror[num].l);
                //float angle = find_angle2(projector[i].pixels[j].p1,fp,mirror[num].l.p1,mirror[num].l.p2);
                line n;
                n.p1 = fp;
                n.m = tan(PI + 2*atan(mirror[num].l.m) - atan(projector[i].pixels[j].m));
                n.c = n.p1.y - n.p1.x*n.m;
                draw_reflections(n,num,1);
            }
        }
    }
    //glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();
    //glPushMatrix();
    //glTranslatef(0.0f, 0.0f, -5.0f);
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    //glRotatef(rot,1,2,0);
    //glRasterPos2f(-0.5,0);
    //glColor4f(0.0f, 1.0f, 1.0f,1.0f);
    //glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, "Rishi Raj Singh Jhelumi");
    //glBegin(GL_POINTS);
    //glVertex3f(0,0,-world_z);
    //glEnd();
    //glPopMatrix();
    glFlush();
    //glutSwapBuffers();
}
void draw_reflections(line l,int m,int side)
{
    int i,j,k;
    point p,fp;
    float dist = INF;
    int type = 1,num;
    for(k=0; k<num_mirrors; k++)
    {
        if(k!=m)
        {
            p = find_intersection(mirror[k].l,l);
            if( (abs(p.x) <= world.width/2) && (abs(p.y) <= world.height/2) && check_point_on_line_segment(mirror[k].l,p) )
                if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
                {
                    float d = distance_between_points(p,l.p1);
                    if( d < dist )
                    {
                        fp = p;
                        dist = d;
                        type = 1;
                        num = k;
                        if(find_side(mirror[k].l.p1,mirror[k].l.p2,l.p1) == -1)
                            type = 2;
                    }
                }
        }
    }
    for(k=0; k<num_blocks; k++)
    {
        p = find_intersection(block[k].l,l);
        if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(block[k].l,p))
            if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
            {
                float d = distance_between_points(p,l.p1);
                if( d < dist )
                {
                    fp = p;
                    dist = d;
                    type = 2;
                }
            }
    }
    for(k=0; k<num_world; k++)
    {
        p = find_intersection(world.l[k],l);
        if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(world.l[k],p))
            if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
            {
                float d = distance_between_points(p,l.p1);
                if( d < dist )
                {
                    fp = p;
                    dist = d;
                    type = 2;
                }
            }
    }
    for(k=0; k<num_projectors; k++)
    {
        p = find_intersection(projector[k].l,l);
        if(abs(p.x) <= world.width/2 && abs(p.y) <= world.height/2 && check_point_on_line_segment(projector[k].l,p))
            if(find_side(mirror[m].l.p1,mirror[m].l.p2,p) == side)
            {
                float d = distance_between_points(p,l.p1);
                if( d < dist )
                {
                    fp = p;
                    dist = d;
                    type = 2;
                }
            }
    }
    draw_line2(fp,l.p1);
    if(type==1)
    {
        //float angle = find_angle(l,mirror[m].l);
        line n;
        n.p1 = fp;
        n.m = tan(PI + 2*atan(mirror[num].l.m) - atan(l.m));
        n.c = n.p1.y - n.p1.x*n.m;
        draw_reflections(n,num,1);
    }
}
Esempio n. 8
0
/* takes a file handle, file position should be at the start of the Anadisk
   dump data.  reads the info for each sector and builds it into a list
   of sides.  each side holds a list of tracks, and each track a list of sectors */
struct disk_side *parse_image(FILE *fp)
{
	struct sect_info si;
	struct disk_side *sides = NULL;
	struct disk_side *curr_side, *new_side;
	struct disk_track *curr_track, *new_track;
	struct disk_sector *curr_sector, *new_sector;

	
	while (get_sector_info(fp, &si))
	{
		curr_side = find_side(sides, si.phy_side);
		if (curr_side == NULL)
		{
			new_side = make_side(si.phy_side);
			if (sides == NULL)
				sides = new_side;
			else
			{
				for (curr_side = sides; curr_side->next != NULL; curr_side = curr_side->next)
					;
				curr_side->next = new_side;
			}
			max_sides++;
			
			curr_side = new_side;
		}
		
		
		curr_track = find_track(curr_side->tracks, si.phy_cylinder);
		if (curr_track == NULL)
		{
			new_track = make_track(si.phy_cylinder, si.phy_side);
			if (curr_side->tracks == NULL)
				curr_side->tracks = new_track;
			else
			{
				for (curr_track = curr_side->tracks; curr_track->next != NULL; curr_track = curr_track->next)
					;
				curr_track->next = new_track;
			}
			curr_side->track_count++;
			if (curr_side->track_count > max_tracks)
				max_tracks = curr_side->track_count;
			
			curr_track = new_track;
		}
		
		
		new_sector = make_sector(si.log_sector, si.log_cylinder, si.log_side, si.size, ftell(fp));
		if (si.size > sector_size)
			sector_size = si.size;
		if (curr_track->sectors == NULL)
			curr_track->sectors = new_sector;
		else
		{
			for (curr_sector = curr_track->sectors; curr_sector->next != NULL; curr_sector = curr_sector->next)
				;
			curr_sector->next = new_sector;
		}
		curr_track->sector_count++;
		if (curr_track->sector_count > max_sectors)
			max_sectors = curr_track->sector_count;
				
		skip_sector(fp, &si);
	}
	
	return sides;
}