Changeset 1795


Ignore:
Timestamp:
11/15/06 14:22:07 (13 years ago)
Author:
sorber
Message:

Added method getDistSegWt to MathUtil.java

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/util/MathUtil.java

    r1691 r1795  
    133133  } 
    134134   
     135   /**  Gets distance to curve: finds out if the nearest point is a node or a segment;  
     136   *  
     137   *  @param x x coordinate of point in question 
     138   *  @param y y coordinate of point in question 
     139   *  @return an array double[3], with element 0 being node index i of one end of  
     140   *  closest line segment (the other end being i+1), and the third being the weight  
     141   *  between zero and one for interpolation along the segment (i, i+1) 
     142   */  
     143  public static double[] getDistSegWt(float[][] nodes, float x, float y) { 
     144    // assumes a non-ragged array of float[2][numNodes] 
     145    double minDist = Double.MAX_VALUE; 
     146    int seg = 0; 
     147    double weight = 0;    
     148 
     149    int numNodes = nodes[0].length; 
     150 
     151    // toss out the trivial case 
     152    if (numNodes == 1) { 
     153      double xdist = x - nodes[0][0]; 
     154      double ydist = y - nodes[1][0]; 
     155      minDist = Math.sqrt(xdist * xdist + ydist * ydist); 
     156    } else { 
     157 
     158      for (int i=0; i<numNodes-1; i++) { 
     159         double[] a = {(double) nodes[0][i], (double) nodes[1][i]}; 
     160         double[] b = {(double) nodes[0][i+1], (double) nodes[1][i+1]}; 
     161         double[] p = {(double) x, (double) y}; 
     162 
     163         double[] proj = getProjection(a, b, p, true); 
     164         double dist = getDistance(p, proj); 
     165 
     166         if (dist < minDist) { 
     167           minDist = dist; 
     168           seg = i; 
     169           double segDist = getDistance (a, b); 
     170           double fracDist = getDistance (a, proj); 
     171           weight = fracDist / segDist; 
     172         } 
     173       }  
     174    } 
     175    double[] retvals = {minDist, (double) seg, weight}; 
     176    return retvals; 
     177  } 
     178 
    135179  /** Rounds the value to nearest value along the given progression. */ 
    136180  public static int getNearest(double val, int min, int max, int step) { 
Note: See TracChangeset for help on using the changeset viewer.