Dave's Brain

Browse - Programming Tips - How can I find the distance between two points in Perl?

Date: 2012mar25
Language: Perl

Q.  How can I find the distance between two points in Perl?

A.  Here's a function for that:

sub distance($$$$) { my($x1, $y1, $x2, $y2) = @_; return sqrt(($x2 - $x1) ** 2 + ($y2 - $y1) ** 2); }
This is for 2D only. Notice that ** is the exponent operator.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.