Changeset 1691


Ignore:
Timestamp:
10/27/06 13:31:10 (14 years ago)
Author:
sorber
Message:

loci.visbio.util.MathUtil

I split getDistance(double[] a, double[] b, double[] v, boolean segment) into three methods

1) getDistance(same signature)
2) double[] getProjection(same signature)
3) getDistance(double[] p, double[] v)

so the projection of a point to a line segment can be returned separately. I wanted to get the coordinates of a projection as well as the distance for FreeformTool.java. One can still call getDistance() as before, so no code outside of this class needs/ed to be rewritten.
Plus, computing the distance between two points (3) is a handy method on its own.

-ACS

File:
1 edited

Legend:

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

    r1390 r1691  
    3636 
    3737  /** 
    38    * Gets the distance between the endpoints p and q, using 
     38   * Gets the distance between the points p and q, using 
    3939   * the given conversion values between pixels and microns. 
    4040   * 
     
    5959   * @param b Coordinates of the line's second endpoint 
    6060   * @param v Coordinates of the standalone endpoint 
    61    * @param segment Whether distance computation should be 
    62    *                constrained to the given line segment 
     61   * @param segment Whether distance computation should be constrained to the given line segment 
     62   * 
    6363   */ 
    6464  public static double getDistance(double[] a, double[] b, double[] v, 
    6565    boolean segment) 
    6666  { 
     67    double[] p = getProjection(a, b, v, segment); 
     68    return getDistance(p, v); 
     69  } 
     70   
     71  /** 
     72   * Computes the minimum distance between the point p and the point v. 
     73   * 
     74   * @param p Coordinates of the first point 
     75   * @param v Coordinates of the second point 
     76   * 
     77   */ 
     78  public static double getDistance (double[] p, double[] v) { 
     79    int len = p.length;  // redundant with getProjection 
     80    double sum = 0; 
     81    for (int i=0; i<len; i++) { 
     82      double dist = p[i] - v[i];  
     83      sum += dist * dist; 
     84    } 
     85    return Math.sqrt(sum); 
     86  } 
     87 
     88  /** 
     89   * Computes the projection of the point v onto the line segment a-b. 
     90   * 
     91   * @param a Coordinates of the segment's first endpoint 
     92   * @param b Coordinates of the segment's second endpoint 
     93   * @param v Coordinates of the point to be projected  
     94   * @param segment Whether the projection should be constrained to the given line segment  
     95   * 
     96   */ 
     97 
     98  public static double[] getProjection (double[] a, double[] b, double[] v, boolean segment)  
     99  { 
    67100    int len = a.length; 
    68  
    69101    // vectors 
    70102    double[] ab = new double[len]; 
     
    85117    double[] p = new double[len]; 
    86118    for (int i=0; i<len; i++) p[i] = c * ab[i] + a[i]; 
    87  
    88     // determine which point (a, b or p) to use in distance computation 
     119     
    89120    int flag = 0; 
    90121    if (segment) { 
     
    97128    } 
    98129 
    99     double sum = 0; 
    100     for (int i=0; i<len; i++) { 
    101       double q; 
    102       if (flag == 0) q = p[i] - v[i]; // use p 
    103       else if (flag == 1) q = a[i] - v[i]; // use a 
    104       else q = b[i] - v[i]; // flag == 2, use b 
    105       sum += q * q; 
    106     } 
    107  
    108     return Math.sqrt(sum); 
    109   } 
    110  
     130    if (flag == 0) return p; 
     131    else if (flag == 1) return a; 
     132    else return b; 
     133  } 
     134   
    111135  /** Rounds the value to nearest value along the given progression. */ 
    112136  public static int getNearest(double val, int min, int max, int step) { 
Note: See TracChangeset for help on using the changeset viewer.