Page 1 of 1

ZoneNPoly.getDistanceToZone(int x, int y)

Posted: Tue Jun 16, 2009 9:52 pm
by Pere
Actually this method calculates the distance beetween the point x,y and the npoly's nearest vertex to x,y.
I need a few more accuracy with this method, making it to calculate the nearest distance to the zone, including the segments (in one of my implementations I need precision with the boolean expression [getDistanceToZone(player) < 200]).

The implementation is (maybe improvable):

Code: Select all

public double getDistanceToZone(int x, int y){    double test, shortestDist;    double u = ((x - _x[_x.length - 1]) * (_x[0] - _x[_x.length - 1]) + (y - _y[_y.length - 1]) * (_y[0] - _y[_y.length - 1])) /            (Math.pow(_x[0] - _x[_x.length - 1], 2) + Math.pow(_y[0] - _y[_y.length - 1], 2));    if (u > 0 && u < 1)        shortestDist = Math.pow((_x[0] + u * (_x[_x.length - 1] - _x[0])) - x, 2) +            Math.pow((_y[0] + u * (_y[_y.length - 1] - _y[0])) - y, 2);    else        shortestDist = Math.pow(_x[0] - x, 2) + Math.pow(_y[0] - y, 2);        for (int i = 1; i < _y.length; i++)    {        u = ((x - _x[_x.length - 1]) * (_x[0] - _x[_x.length - 1]) + (y - _y[_y.length - 1]) * (_y[0] - _y[_y.length - 1])) /                (Math.pow(_x[0] - _x[_x.length - 1], 2) + Math.pow(_y[0] - _y[_y.length - 1], 2));        if (u > 0 && u < 1)            test = Math.pow((_x[i] + u * (_x[i - 1] - _x[i])) - x, 2) +                Math.pow((_y[i] + u * (_y[i - 1] - _y[i])) - y, 2);        else            test = Math.pow(_x[i] - x, 2) + Math.pow(_y[i] - y, 2);        if (test < shortestDist)            shortestDist = test;    }        return Math.sqrt(shortestDist);} 
Considerate it a contrib if you see it ok, I've tested it and works ^^

The question is: will this change affect to the server performance? There are a lot more calculations for every coord...

Thank you very much.