修复 Swift simd 库中的错误(以及缺少文档?)
Fix error in Swift simd library (and missing documentation?)
我下载了 Apple 的“DemoBot”项目来尝试了解更多关于 GameplayKit
的信息,但是有一个错误阻止了它的编译。它位于似乎使用 simd
库的扩展中。我对 SIMD 进行了一些研究,作为新手,我可以说这些东西超出了我的理解范围。但我只想让这段代码正常工作,以便我可以演示该项目。
错误是由以下代码中的这一行引起的:
let fractionOfComponent = max(0, min(1, componentInSegment))
错误信息是:
Argument passed to call that takes no arguments
谁能告诉我如何解决这个错误?
此外,我在哪里可以找到 simd
库的文档?我搜索了 Apple Documentation,但 simd
库中似乎没有任何内容。我如何在没有获得计算机科学学位的情况下学习如何使用该图书馆中的 API?
import CoreGraphics
import simd
// Omitting the other extensions here
// Extend `float2` to provide a convenience method for working with pathfinding graphs.
extension float2 {
/// Calculates the nearest point to this point on a line from `pointA` to `pointB`.
func nearestPointOnLineSegment(lineSegment: (startPoint: float2, endPoint: float2)) -> float2 {
// A vector from this point to the line start.
let vectorFromStartToLine = self - lineSegment.startPoint
// The vector that represents the line segment.
let lineSegmentVector = lineSegment.endPoint - lineSegment.startPoint
// The length of the line squared.
let lineLengthSquared = distance_squared(lineSegment.startPoint, lineSegment.endPoint)
// The amount of the vector from this point that lies along the line.
let projectionAlongSegment = dot(vectorFromStartToLine, lineSegmentVector)
// Component of the vector from the point that lies along the line.
let componentInSegment = projectionAlongSegment / lineLengthSquared
// Clamps the component between [0 - 1].
let fractionOfComponent = max(0, min(1, componentInSegment))
return lineSegment.startPoint + lineSegmentVector * fractionOfComponent
}
}
更新:
感谢 giorashc 的回答,我能够通过用以下内容替换有问题的行来修复错误:
let componentMin = Swift.min(1, componentInSegment)
let fractionOfComponent = Swift.max(0, componentMin)
但是,我仍然想为 Swift 的 simd
库(尤其是新手可以理解的)找到一个好的文档资源。如果有人知道这样的资源那就太好了。
float2
已经有一个不带参数的 min
方法。 (在Xcode中选择进入它们的定义时,有对方法的解释)
尝试使用 Swift.min
显式调用 swift 的实现
我下载了 Apple 的“DemoBot”项目来尝试了解更多关于 GameplayKit
的信息,但是有一个错误阻止了它的编译。它位于似乎使用 simd
库的扩展中。我对 SIMD 进行了一些研究,作为新手,我可以说这些东西超出了我的理解范围。但我只想让这段代码正常工作,以便我可以演示该项目。
错误是由以下代码中的这一行引起的:
let fractionOfComponent = max(0, min(1, componentInSegment))
错误信息是:
Argument passed to call that takes no arguments
谁能告诉我如何解决这个错误?
此外,我在哪里可以找到 simd
库的文档?我搜索了 Apple Documentation,但 simd
库中似乎没有任何内容。我如何在没有获得计算机科学学位的情况下学习如何使用该图书馆中的 API?
import CoreGraphics
import simd
// Omitting the other extensions here
// Extend `float2` to provide a convenience method for working with pathfinding graphs.
extension float2 {
/// Calculates the nearest point to this point on a line from `pointA` to `pointB`.
func nearestPointOnLineSegment(lineSegment: (startPoint: float2, endPoint: float2)) -> float2 {
// A vector from this point to the line start.
let vectorFromStartToLine = self - lineSegment.startPoint
// The vector that represents the line segment.
let lineSegmentVector = lineSegment.endPoint - lineSegment.startPoint
// The length of the line squared.
let lineLengthSquared = distance_squared(lineSegment.startPoint, lineSegment.endPoint)
// The amount of the vector from this point that lies along the line.
let projectionAlongSegment = dot(vectorFromStartToLine, lineSegmentVector)
// Component of the vector from the point that lies along the line.
let componentInSegment = projectionAlongSegment / lineLengthSquared
// Clamps the component between [0 - 1].
let fractionOfComponent = max(0, min(1, componentInSegment))
return lineSegment.startPoint + lineSegmentVector * fractionOfComponent
}
}
更新:
感谢 giorashc 的回答,我能够通过用以下内容替换有问题的行来修复错误:
let componentMin = Swift.min(1, componentInSegment)
let fractionOfComponent = Swift.max(0, componentMin)
但是,我仍然想为 Swift 的 simd
库(尤其是新手可以理解的)找到一个好的文档资源。如果有人知道这样的资源那就太好了。
float2
已经有一个不带参数的 min
方法。 (在Xcode中选择进入它们的定义时,有对方法的解释)
尝试使用 Swift.min
显式调用 swift 的实现