static void
find_three_point_rectangle (Rectangle *r,
                            Point      points[4],
                            gint       p)
{
  Point a = points[p       % 4];  /* 0 1 2 3 */
  Point b = points[(p + 1) % 4];  /* 1 2 3 0 */
  Point c = points[(p + 2) % 4];  /* 2 3 0 1 */
  Point d = points[(p + 3) % 4];  /* 3 0 1 2 */
  Point i1;                       /* intersection point */
  Point i2;                       /* intersection point */
  Point i3;                       /* intersection point */

  if (intersect_x (b, c, a,  &i1) &&
      intersect_y (c, d, i1, &i2) &&
      intersect_x (d, a, i2, &i3))
    add_rectangle (points, r, i3, i3, i1, i1);

  if (intersect_y (b, c, a,  &i1) &&
      intersect_x (c, d, i1, &i2) &&
      intersect_y (d, a, i2, &i3))
    add_rectangle (points, r, i3, i3, i1, i1);

  if (intersect_x (d, c, a,  &i1) &&
      intersect_y (c, b, i1, &i2) &&
      intersect_x (b, a, i2, &i3))
    add_rectangle (points, r, i3, i3, i1, i1);

  if (intersect_y (d, c, a,  &i1) &&
      intersect_x (c, b, i1, &i2) &&
      intersect_y (b, a, i2, &i3))
    add_rectangle (points, r, i3, i3, i1, i1);
}
Exemple #2
0
int ray_trace(int h,int w,double x0, double y0, double k0, int p, double D)
{
	int px,py,pp=(p*k0>0?1:-1);
	double nx0,ny0,nx1,ny1,nx,ny,d0,d1;
	printf("%d %d %.4lf %.4lf %.6lf %d %.4lf\n", h,w,x0,y0,k0,p,D);
	if(D<=0) return 0;
	if(hmap[h][w]=='X' && x0!=mx && y0!=my && equal(my,k0*(mx-x0)+y0)) {
		d0=distance(x0,y0,mx,my);
	printf("@@@%.4lf %.4lf\n",D,d0);
		if(d0<D || equal(d0,D)) return 1;
		else return 0;
	}
//	printf(">%c %.4lf s:%.7lf\n",hmap[h][w],d0,fabs(my-k0*(mx-x0)-y0));

	if(p>0) nx0=floor(x0+1);
	else nx0=ceil(x0-1);
	if(pp>0) ny1=floor(y0+1);
	else ny1=ceil(y0-1);
	ny0=intersect_x(x0,y0,k0,nx0); nx1=intersect_y(x0,y0,k0,ny1);
	d0=distance(x0,y0,nx0,ny0); d1=distance(x0,y0,nx1,ny1);
//	printf("n0: %.4lf %.4lf %.4lf ", nx0, ny0, d0);
//	printf("n1: %.4lf %.4lf %.4lf\n", nx1, ny1,d1);
	if(equal(d0,d1)) {
		if(hmap[h+p][w+pp]=='#') {
			if(hmap[h+p][w]=='#' && hmap[h][w+pp]=='#')
				return ray_trace(h,w,nx0,ny0,k0,-1*p,D-d0);
			else if(hmap[h+p][w]=='#') 
				return ray_trace(h+p,w,nx0,ny0,ref_x(k0),p,D-d0);
			else if(hmap[h][w+pp]=='#')
				return ray_trace(h,w+pp,nx0,ny0,ref_y(k0),-1*p,D-d0);
			else return 0;
		} else return ray_trace(h+p,w+pp,nx0,ny0,k0,p,D-d0);
	}	
	if(d0<d1) {
		if(hmap[h+p][w]=='#') 
			return ray_trace(h,w,nx0,ny0,ref_x(k0),p,D-d0);
		else return ray_trace(h+p,w,nx0,ny0,k0,p,D-d0);
	} else {
		if(hmap[h][w+pp]=='#') 
			return ray_trace(h,w,nx1,ny1,ref_y(k0),-1*p,D-d1);
		else return ray_trace(h,w+pp,nx1,ny1,k0,p,D-d1);
	}
}
static void
find_maximum_aspect_rectangle (Rectangle *r,
                               Point      points[4],
                               gint       p)
{
  Point a = points[ p      % 4];  /* 0 1 2 3 */
  Point b = points[(p + 1) % 4];  /* 1 2 3 0 */
  Point c = points[(p + 2) % 4];  /* 2 3 0 1 */
  Point d = points[(p + 3) % 4];  /* 3 0 1 2 */
  Point i1;                       /* intersection point */
  Point i2;                       /* intersection point */
  Point i3;                       /* intersection point */

  if (intersect_x (b, c, a, &i1))
    {
      i2.x = i1.x + 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (c, d, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      i2.x = i1.x - 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (c, d, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);
    }

  if (intersect_y (b, c, a, &i1))
    {
      i2.x = i1.x + 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (c, d, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      i2.x = i1.x - 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (c, d, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);
    }

  if (intersect_x (c, d, a,  &i1))
    {
      i2.x = i1.x + 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (b, c, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      i2.x = i1.x - 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (b, c, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);
    }

  if (intersect_y (c, d, a,  &i1))
    {
      i2.x = i1.x + 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (b, c, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      i2.x = i1.x - 1.0 * r->aspect;
      i2.y = i1.y + 1.0;

      if (intersect (d, a, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (a, b, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);

      if (intersect (b, c, i1, i2, &i3))
        add_rectangle (points, r, i1, i3, i1, i3);
    }
}