Mindblock here,但我不知道如何使它不那么难看:
def getClosestSphere(ray: Ray, spheres: List[Sphere]): Sphere = {
val map = new HashMap[Double, Sphere]
for (sphere <- spheres) {
val intersectPoint = sphere.intersectRay(ray)
map.put(intersectPoint, sphere)
}
map.minBy(_._1)._2
}
你能看到我在做什么吗?我有一个球体列表,其中每个球体都有一个方法 intersectRay,返回一个双精度值。
我想采用该函数的最小结果的球体。我知道有一个很好的功能结构可以让我在一行中做到这一点,我只是看不到它:(