Rectf get_next_free_rect(TileMap *tilemap, const Rectf &r)
{
  int rx = c_round(r.left / static_cast<float>(TILE_SIZE));
  int ry = c_round(std::min (r.top, r.bottom)  / static_cast<float>(TILE_SIZE));
  
  float fw = r.right - r.left;
  float fh = fabsf(r.bottom   - r.top);
  
  int rw = c_roundup (fw / static_cast<float>(TILE_SIZE));
  int rh = c_roundup (fh / static_cast<float>(TILE_SIZE));

  std::vector<Rectf> rects;

  // find first set of free rectangle
  // simply iterate the rectangles around current position
  for(int d=1; d<20; d++) // not more than 20 steps
  {
    for(int i=-d; i<=d; i++)
    {
      if (is_rect_free(tilemap, i + rx, -d + ry, rw, rh))
        rects.push_back( Rect(i + rx, -d + ry, rw, rh));
      if (is_rect_free(tilemap, i + rx, d + ry, rw, rh))
        rects.push_back( Rect(i + rx, d + ry, rw, rh));

      if (is_rect_free(tilemap, -d + rx, i + ry, rw, rh))
        rects.push_back( Rect(-d + rx, i + ry, rw, rh));
      if (is_rect_free(tilemap,  d + rx, i + ry, rw, rh))
        rects.push_back( Rect(d  + rx, i + ry, rw, rh));
    }
    if (rects.size())
      break;
  }
  assert(rects.size());

  // find nearest rectangle in this set
  float distance=10000.0f;
  float dx,dy,d;
  Rectf nr;
  for (std::vector<Rectf>::iterator i = rects.begin(); i != rects.end(); ++i)
  {
    dx = i->left - r.left / static_cast<float>(TILE_SIZE);
    dy = i->top  - r.top  / static_cast<float>(TILE_SIZE);
    d = sqrtf( dx * dx + dy * dy );
    if (d < distance)
    {
      distance=d;
      nr=*i;
    }
  }

  nr.right += nr.left; 
  nr.bottom += nr.top; 

  return nr;
}
Пример #2
0
txInteger fx_Date_parse_fraction(txByte* theCharacter, txString* theString)
{
	txByte c = *theCharacter;
	txString p = *theString;
	txNumber fraction = 100;
	txNumber aResult = ((c - '0') * fraction);
	c = *p++;
	while (('0' <= c) && (c <= '9')) {
		fraction /= 10;
		aResult = aResult + ((c - '0') * fraction);
		c = *p++;
	}
	*theCharacter = c;
	*theString = p;
	return (txInteger)c_round(aResult);
}