Add lhypot to util.c

Issue #98 wontfix
Joris created an issue

I think I have about 6-8 modules that use this function. It would be very useful to add it to util.c

{{{ #!C static long lhypot(register long dx, register long dy) { register unsigned long r, dd;

    dd = dx*dx+dy*dy;

    if (dx < 0) dx = -dx;
    if (dy < 0) dy = -dy;

    /* initial hypotenuse guess (from Gems) */
    r = (dx > dy) ? (dx+(dy>>1)) : (dy+(dx>>1));

    if (r == 0) return (long)r;

    /* converge 3 times */
    r = (dd/r+r)>>1;
    r = (dd/r+r)>>1;
    r = (dd/r+r)>>1;

    return (long)r;

} }}}

Comments (5)

  1. Former user Account Deleted

    Most distances are only used for comparison, so storing the distance squared and comparing to that is better. game.c does this in IsAntiwarped(). game.c uses lhypot for a silly reason and it should be changed not to. funky/autoturrent uses it because it does rudimentary geometry/physics. Do you have uses of lhypot that don't fit those 2 cases?

  2. Joris reporter

    I have not done stuff with ASSS for a while, but a quick grep tells me I used it to:

    • Determine where to shoot in a destroyable staticturret module (like autoturret)
    • Determine how much damage to deal to a fake player when it is hit by splash damage
    int dist = lhypot(x - bx, y - by);
    damage = dist * - (i32)maxdamage / radius + maxdamage;
    
    • Determine when to trigger prox bombs (could do without hypot)
    • Determine what speed a player if flying at so that I can figure out where to place a repel to give him a speed boost in the proper direction:
    speed = LHypot(p->position.xspeed,  p->position.yspeed);
    wpn.x = p->position.x - (p->position.xspeed / speed) * REPEL_DIST;
    wpn.y = p->position.y - (p->position.yspeed / speed) * REPEL_DIST;
    wpn.xspeed = -p->position.xspeed;
    wpn.yspeed = -p->position.yspeed;
    
    • Deal damage from a nuke to buildings
  3. Former user Account Deleted

    I would have to say then that lhypot doesn't belong in util.c. It's not a general programming utility. I am going to set this bug to wontfix. lhypot would be part of a hypothetical geometry or physics module, which you can file a bug for if you want.

  4. Log in to comment