View Problem

Find the distance between two points

DiskEdit
ruby
# the hypotenuse sqrt(x**2+y**2)
distance = Math.hypot(x2-x1,y2-y1)
ExpandDiskEdit
java
double distance = Point2D.distance(x1, y1, x2, y2);
ExpandDiskEdit
java
Point2D point1 = new Point2D.Double(x1, y1);
Point2D point2 = new Point2D.Double(x2, y2);
double distance = point1.distance(point2);
ExpandDiskEdit
java 1.5+
double distance = Math.hypot(x2-x1, y2-y1);
DiskEdit
perl
use Math::Complex;
$a = Math::Complex->make(0, 3);
$b = Math::Complex->make(4, 0);
$distance = abs($a - $b);
DiskEdit
perl
my @a=(0, 3);
my @b=(4, 0);
my $distance = sqrt( ($a[0]-$b[0])**2 + ($a[1]-$b[1])**2 );
ExpandDiskEdit
groovy
distance = distance(x1, y1, x2, y2)
ExpandDiskEdit
groovy
distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
ExpandDiskEdit
scala
val distance$ = distance((34, 78), (67, -45))
println(distance$)
ExpandDiskEdit
scala
val distance$ = distance(new Point(34, 78), new Point(67, -45))
println(distance$)
ExpandDiskEdit
scala
def distance (p1: (Int, Int), p2: (Int, Int)) = {
val (p1x, p1y) = p1
val (p2x, p2y) = p2
val dx = p1x - p2x
val dy = p1y - p2y
Math.sqrt(dx*dx + dy*dy)
}
println(distance((34, 78), (67, -45)))

DiskEdit
scala
def euclideanDistance(point1: List[Double], point2: List[Double]): Double = {
sqrt(point1.zip(point2).foldLeft(0.0){case(sum,(v1,v2)) => sum + pow(v1-v2, 2)})
}
DiskEdit
python
# problem description doesn't say 2D points ;)
from math import sqrt
print sqrt(sum((x-y)**2 for x,y in zip(a, b)))
DiskEdit
python
from math import hypot
print hypot(x2-x1, y2-y1)
ExpandDiskEdit
cpp C++/CLI .NET 2.0
Point p1 = {34, 78}, p2 = {67, -45};
double distance = ::distance(p1, p2);
Console::WriteLine("{0,3:F2}", distance);
ExpandDiskEdit
fsharp
let distance' = distance (34, 78) (67, -45)
printfn "%3.2f" distance'
ExpandDiskEdit
erlang
Distance = distance({point, 34, 78}, {point, 67, -45}),
io:format("~.2f~n", [Distance]).
ExpandDiskEdit
erlang
Distance = distance(point:new(34, 78), point:new(67, -45)),
io:format("~.2f~n", [Distance]).
DiskEdit
ocaml
type point = { x:float; y:float };;
let distance a b = sqrt((a.x -. b.x)**2. +. (a.y -. b.y)**2.);;
DiskEdit
csharp
System.Drawing.Point p = new System.Drawing.Point(13, 14),
p1 = new System.Drawing.Point(10, 10);
double distance = Math.Sqrt(Math.Pow(p1.X - p.X, 2) + Math.Pow(p1.Y - p.Y, 2)));
ExpandDiskEdit
php
$distance = sqrt( pow(($x2 - $x1), 2) + pow(($y2 - $y1),2) );
ExpandDiskEdit
php
class Point2D {
var $x;
var $y;
function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
}
$a = new Point2D($x1,$y1);
$b = new Point2D($x2,$y2);
$distance = sqrt( pow(($b->x - $a->x), 2) + pow(($b->y - $a->y),2) );
DiskEdit
haskell
data Floating n => Point2 n = Point2 n n

distance :: Floating n => Point2 n -> Point2 n -> n
distance (Point2 x1 y1) (Point2 x2 y2) = sqrt (x'*x' + y'*y')
where
x' = x1 - x2
y' = y1 - y2


-- > distance (Point2 5 10) (Point2 3 5)
-- 5.385...
-- > distance (Point2 1 1) (Point2 2 2)
-- 1.414...
DiskEdit
clojure
(defstruct point :x :y)

(defn distance
"Euclidean distance between 2 points"
[p1 p2]
(Math/pow (+ (Math/pow (- (:x p1) (:x p2)) 2)
(Math/pow (- (:y p1) (:y p2)) 2))
0.5))

(distance (struct point 0 0) (struct point 1 1)) ; => 1.4142135623730951
DiskEdit
clojure
(defn distance
"Euclidean distance between 2 points"
[[x1 y1] [x2 y2]]
(Math/sqrt
(+ (Math/pow (- x1 x2) 2)
(Math/pow (- y1 y2) 2))))

(distance [2 2] [3 3])
ExpandDiskEdit
fantom
px1 := 34.0f; py1 := 78.0f; px2 := 67.0f; py2 := -45.0f
distance := |Float x1, Float y1, Float x2, Float y2 -> Float|
{ ((x2-x1).pow(2.0f) + (y2-y1).pow(2.0f)).sqrt }

distance(px1, py1, px2, py2)
ExpandDiskEdit
go
import "fmt"
import "math"

type Point struct {
x, y float64
}

func (p Point) distance(other Point) (float64) {
dx := p.x - other.x
dy := p.y - other.y
return math.Sqrt(dx * dx + dy * dy)
}

func main() {
origin := Point{ 0, 0 }
point := Point{ 1, 1 }

fmt.Println(point.distance(origin))
}

Submit a new solution for ruby, java, perl, groovy ...