Ord’s Blog
RSS icon Home icon
  • Distance betwen points in Java

    Posted on February 19th, 2009 Ord 1 comment

    I needed to calculate the distance between two postals codes, in the case where I have latitude and longitude for each one.  It took some searching around the net to find the right formula, although along the way I found dozens of pages that would calculate the distances for me.

    The method milesBetween in my ZipCode class reports the number of miles between this Zip Code and the supplied argument.

    double milesBetween(ZipCode zip){
      double distance;
      distance = ( 3958 * Math.PI * Math.sqrt(
        (this.latitude - zip.latitude) * (this.longitude - zip.longitude) +
        Math.cos( Math.toRadians(this.latitude)) * Math.cos( Math.toRadians(zip.latitude)) *
        ( (this.longitude - zip.longitude) * (this.longitude - zip.longitude) )
        )/180);
     
      return distance;
    }

    The constant 3958 is the (average) radius of the earth in miles.  Chaging to 6369 would give us the distances in Kilometers instead.

  • Blogging I go

    Posted on February 15th, 2009 Ord 3 comments

    Ok, after several years of saying ” I gotta get that blog thing going soon” here I am, blogging your interwebs.

    Anyway, I will be doing my best to keep this updated with current projects and so on.