[英]Get the max and min values from an Array of CGPoints
We have an array of CGPoints:我们有一个 CGPoints 数组:
let points = [(1234.0, 1053.0), (1241.0, 1111.0), (1152.0, 1043.0)]
What I'm trying to do is get the CGPoint with the highest x value and the one with the highest y value in the array.我想要做的是获得数组中 x 值最高的 CGPoint 和数组中 y 值最高的那个。 I will be using these points to create a CGRect:
我将使用这些点来创建一个 CGRect:
extension CGRect {
init(p1: CGPoint, p2: CGPoint) {
self.init(x: min(p1.x, p2.x),
y: min(p1.y, p2.y),
width: abs(p1.x - p2.x),
height: abs(p1.y - p2.y))
}
}
I know there a way to get max and min values in an array by doing something like this:我知道有一种方法可以通过执行以下操作来获取数组中的最大值和最小值:
points.min()
points.max()
but these don't seem to work since its an array of CGPoints.但这些似乎不起作用,因为它是一组 CGPoints。 Is it possible to get these values from the array?
是否可以从数组中获取这些值?
You can use max(by:)
.您可以使用
max(by:)
。
let minXPoint = points.min(by: {$0.x < $1.x}) //(1152.0, 1043.0)
let maxYPoint = points.max(by: {$0.y < $1.y}) //(1241.0, 1111.0)
You can't do that with just a single call to min()
/ max()
你不能只调用一次
min()
/ max()
off of the highest and lowest of the y and x coordinates
离开 y 和 x 坐标的最高点和最低点
The point with the minimal x value could be different from the point with the minimal x value.具有最小 x 值的点可能与具有最小 x 值的点不同。 So any method that returns only one of the points (as
min()
/ max()
do) isn't sufficient.因此,任何只返回一个点的方法(如
min()
/ max()
所做的)都是不够的。
You either need two separate calls to each, like:您要么需要两个单独的调用,例如:
let minX = points.min(by: { $0.x < $1.x })!.x
let minY = points.min(by: { $0.y < $1.y })!.y
let maxX = points.min(by: { $0.x > $1.x })!.x
let maxY = points.min(by: { $0.y > $1.y })!.y
or you could try doing it all in one swoop with reduce:或者您可以尝试使用 reduce 一口气完成所有操作:
let initialAccumulator = (
minX: CGFloat.max, minY: CGFloat.max, maxX: CGFloat.min, maxY: CGFloat.min)
let (minX, minY, maxX, maxY) = points.reduce(initialAccumulator) { accumulator, point in
return (
minX: min(accumulator.minX, point.x),
minY: min(accumulator.minY, point.y),
maxX: max(accumulator.maxX, point.x),
maxY: max(accumulator.maxY, point.y)
)
}
I would probably do this like so:我可能会这样做:
extension CGRect {
static func minimalRect(containing points: [CGPoint]) -> CGRect? {
if points.isEmpty { return nil }
let minX = points.min(by: { $0.x < $1.x })!.x
let minY = points.min(by: { $0.y < $1.y })!.y
let maxX = points.min(by: { $0.x > $1.x })!.x
let maxY = points.min(by: { $0.y > $1.y })!.y
return CGRect(
x: minX,
y: minY,
width: (minX - maxX).magnitude,
height: (minY - maxY).magnitude
)
}
}
let points = [(1234.0, 1053.0), (1241.0, 1111.0), (1152.0, 1043.0)].map(CGPoint.init)
let r = CGRect.minimalRect(containing: points)
print(r ?? .zero) // => (1152.0, 1043.0, 89.0, 68.0)
You can map values to find the min and max in x and y coordinates like below.您可以通过 map 值找到 x 和 y 坐标中的最小值和最大值,如下所示。 If you're not sure if
points
array contains any data, use guard statement to avoid force unwrapping:如果您不确定
points
数组是否包含任何数据,请使用 guard 语句来避免强制展开:
let xArray = points.map(\.x)
let yArray = points.map(\.y)
guard let minX = xArray.min(),
let maxX = xArray.max(),
let minY = yArray.min(),
let maxY = yArray.max() else { return }
And from there:从那里:
let minPoint = CGPoint(x: minX, y: minY)
let maxPoint = CGPoint(x: minY, y: minY)
then you can modify your extension function because you already know which values are min and max:那么您可以修改您的扩展 function 因为您已经知道哪些值是最小值和最大值:
extension CGRect {
init(minPoint: CGPoint, maxPoint: CGPoint) {
self.init(x: minPoint.x,
y: minPoint.y,
width: maxPoint.x - minPoint.x,
height: maxPoint.y - minPoint.y)
}
}
As Leo Dabus suggested in the comment below, you can do it all in one go inside failable initializer extension:正如 Leo Dabus 在下面的评论中所建议的那样,您可以在一个 go 内的可失败初始化程序扩展中完成所有操作:
extension CGRect {
init?(points: [CGPoint]) {
let xArray = points.map(\.x)
let yArray = points.map(\.y)
if let minX = xArray.min(),
let maxX = xArray.max(),
let minY = yArray.min(),
let maxY = yArray.max() {
self.init(x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY)
} else {
return nil
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.