我有一个x,y坐标元组的列表。我应该使用什么算法重新排列这些点,以便我可以通过点绘制一条线(跟踪)。 样本列表看起来像 points = [(170,156),(154,154),(162,148),(178,164),(135,166),(131,166),(150,158),(158,150) ,(166,152),(139,166),(182,168),(144,163),(174,160)] 我已经尝试根据点之间的距离重新排列点。但我觉得它没有效率,因为我的数据集包含大约400个坐标。
matplotlib中的绘图函数应该能够跟踪点。这就是我的目标
答案 0 :(得分:0)
extension UIImage {
func to8BitLayer(color: UIColor = .black) -> CALayer? {
guard let cgImage = self.cgImage else { return nil }
let height = Int(self.size.height * scale)
let width = Int(self.size.width * scale)
let colorSpace = CGColorSpaceCreateDeviceGray()
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width, space: colorSpace, bitmapInfo: CGImageAlphaInfo.alphaOnly.rawValue) else {
print("Couldn't create CGContext")
return nil
}
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
guard let image = context.makeImage() else {
print("Couldn't create image from context")
return nil
}
// Note that self.size corresponds to the non-scaled (retina) dimensions, so is not the same size as the context
let frame = CGRect(origin: .zero, size: self.size)
let mask = CALayer()
mask.contents = image
mask.contentsScale = scale
mask.frame = frame
let layer = CALayer()
layer.backgroundColor = color.cgColor
layer.mask = mask
layer.contentsScale = scale
layer.frame = frame
return layer
}
}
这将按第一个元素的值,然后由第二个元素对点进行排序。所以你的名单会增加