View Problem

Find the distance between two points

DiskEdit
ruby
distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(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);
DiskEdit
perl
use Math::Complex;
$a = Math::Complex->make(0, 3);
$b = Math::Complex->make(4, 0);
$distance = abs($a - $b);
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
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)))
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) );

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