Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualCornstalks

Posted 22 August 2013 - 04:13 PM

Simplest version:

// Helper function for calculating sqrt of ints
uint32_t sqrti(uint32_t input)
{
    uint32_t op  = input;
    uint32_t res = 0;
    uint32_t one = 1uL << 30;
 
    while (one > op)
    {
        one >>= 2;
    }
 
    while (one != 0)
    {
        if (op >= res + one)
        {
            op = op - (res + one);
            res = res +  2 * one;
        }
        res >>= 1;
        one >>= 2;
    }
    return res;
}
 
struct Vertex2i
{
    int x, y;
};

Vertex2i start = {0, 0}; // Starting point
Vertex2i goal = {100, 100}; // Goal point
 
int distance = 100; // Move distance
 
Vertex2i d = goal - start; // Compute the direction (d is direction)
 
Vertex2i end = (distance * d) / sqrti(d.x * d.x + d.y * d.y);
// end is your end point, moved about 100 units toward goal from start

#1Cornstalks

Posted 22 August 2013 - 04:03 PM

Simplest version:

uint32_t sqrti(uint32_t input)
{
    uint32_t op  = input;
    uint32_t res = 0;
    uint32_t one = 1uL << 30;
 
    while (one > op)
    {
        one >>= 2;
    }
 
    while (one != 0)
    {
        if (op >= res + one)
        {
            op = op - (res + one);
            res = res +  2 * one;
        }
        res >>= 1;
        one >>= 2;
    }
    return res;
}
 
struct Vertex2i
{
    int x, y;
};
 
Vertex2i start = {0, 0};
Vertex2i goal = {100, 100};
 
int distance = 100;
 
Vertex2i d = goal - start; // d is direction
 
Vertex2i end = (distance * d) / sqrti(d.x * d.x + d.y * d.y);
// end is your end point, moved about 100 units toward goal from start

PARTNERS