提取元组中的元组Haskell
我必须做一个程序,它决定两个圆在haskell中是否重叠。 我定义了以下内容:提取元组中的元组Haskell,haskell,tuples,geometry,overlapping,Haskell,Tuples,Geometry,Overlapping,我必须做一个程序,它决定两个圆在haskell中是否重叠。 我定义了以下内容: -- | A 2D Point. type Point = (Float,Float) -- | A Circle is a pair of its center point and its radius. type Circle = (Point,Float) 我需要做一个距离函数,计算两点之间的距离(因此是两个圆的中心),然后做一个函数,通过检查两个中心之间的距离是否小于半径(或半径)之和来确定它们是否重叠
-- | A 2D Point.
type Point = (Float,Float)
-- | A Circle is a pair of its center point and its radius.
type Circle = (Point,Float)
我需要做一个距离函数,计算两点之间的距离(因此是两个圆的中心),然后做一个函数,通过检查两个中心之间的距离是否小于半径(或半径)之和来确定它们是否重叠
问题是中心是一个多边形,半径是一个元素
以下是我为距离制作的函数:
-- | Distance between two points.
distance :: Point -> Point -> Float
distance p1 p2 = ((snd p1 - fst p1)^2 + (snd p2 - snd p1)^2)^(1/2)
现在我需要做距离<2*半径,但我不能把它们结合起来,因为距离应该在一个元素上,半径应该在一个元素上
以下是我尝试过的:
-- | 'True' if the given circles overlap, else 'False'.
overlap :: Circle -> Circle -> Bool
overlap c1 c2 = [distance x,y | x<-(x,y):c1, y<-(x1,y1):c2] < [sum z,z1 | z<-(z):c1, z1<-(z1):c2]
你实际上已经解决了自己的问题,只是你还不知道而已 通过检查两个中心之间的距离是否小于半径(或半径)之和来确定它们是否重叠的函数 我将把这句话直接翻译成Haskell:
a function which decides if they are overlapping
| by checking that the distance
| | between the two centres
| | | | is smaller than
| | | | | the sum of
| | | | | |
| | | | | the radiuses
| | | | | | | |
v v v v v v v v
overlap c1 c2 = distance (centre c1) (centre c2) < radius c1 + radius c2
就这么简单 非常感谢您的帮助,这里有一个
sqrt
函数,它可能比^(1/2)
更具可读性。
a function which decides if they are overlapping
| by checking that the distance
| | between the two centres
| | | | is smaller than
| | | | | the sum of
| | | | | |
| | | | | the radiuses
| | | | | | | |
v v v v v v v v
overlap c1 c2 = distance (centre c1) (centre c2) < radius c1 + radius c2
centre c = fst c
radius c = snd c