我有一个定义了一些笛卡尔坐标的文件(points.txt):
A 1.0 2.2
B 2.1 3.0
C 3.5 4.0
D 4.0 5.0
我有第二个文件 (routes.txt),其中包含基于 points.txt 中的点定义的路线。
route1 ACDB
route2 ABC
我需要找到每条路线的长度。到目前为止,我计算了两点之间的距离,如下所示:
type Point = (String, Float, Float)
distance_points :: IO ()
distance_points = do s <- readFile "pontos.txt"
putStr "Fom: "
p1 <- getLine
putStr "To: "
p2 <- getLine
print ( distance (search_point p1 (map words (lines s))) (search_point p2 (map words (lines s))))
search_point :: String -> [[String]] -> Point
search_point pt ([p,c1,c2]:xs) = if pt == p then (p, read(c1)::Float, read(c2)::Float)
else search_point pt xs
distance :: Point -> Point -> Float
distance (s1,x1,y1) (s2,x2,y2) = sqrt ((x1-x2)^2 + (y1-y2)^2)
如何计算路线的完整距离?
另外,如果我有几条路线,我怎样才能找到最长的一条?
提前致谢。