Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 25, 2013 9:18 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Feb 21, 2010 7:43 pm 
Offline
Regular
User avatar

Joined: Wed Apr 15, 2009 12:51 pm
Posts: 104
It would be nice to get the distance to another point squared, to avoid using sqrt().

You could divide this method ...
Code:
/**
* Get the distance from this point to another
*
* @param other The other point we're measuring to
* @return The distance to the other point
*/
public float distance(Vector2f other) {
    float dx = other.getX() - getX();
    float dy = other.getY() - getY();
   
    return (float) Math.sqrt((dx*dx)+(dy*dy));
}


.. into these:
Code:
/**
* Get the distance squared from this point to another
*
* @param other The other point we're measuring to
* @return The distance squared to the other point
*/
public float distanceSquared(Vector2f other) {
    float dx = other.getX() - getX();
    float dy = other.getY() - getY();
   
    return (float) (dx*dx)+(dy*dy);
}

/**
* Get the distance from this point to another
*
* @param other The other point we're measuring to
* @return The distance to the other point
*/
public float distance(Vector2f other) {
    return (float) Math.sqrt(distanceSquared(other));
}


Thanks a lot!
Greets Lufti


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 04, 2010 6:26 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
In SVN.

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group