RosettaCodeData/Task/Closest-pair-problem/Icon/closest-pair-problem.icon

26 lines
784 B
Plaintext

record point(x,y)
procedure main()
minDist := 0
minPair := &null
every (points := [],p1 := readPoint()) do {
if *points == 1 then minDist := dSquared(p1,points[1])
every minDist >=:= dSquared(p1,p2 := !points) do minPair := [p1,p2]
push(points, p1)
}
if \minPair then {
write("(",minPair[1].x,",",minPair[1].y,") -> ",
"(",minPair[2].x,",",minPair[2].y,")")
}
else write("One or fewer points!")
end
procedure readPoint() # Skips lines that don't have two numbers on them
suspend !&input ? point(numeric(tab(upto(', '))), numeric((move(1),tab(0))))
end
procedure dSquared(p1,p2) # Compute the square of the distance
return (p2.x-p1.x)^2 + (p2.y-p1.y)^2 # (sufficient for closeness)
end