【问题标题】:Calculating the distance CGPoint travelled from one place to another计算CGPoint从一个地方到另一个地方的距离
【发布时间】:2016-09-06 15:46:58
【问题描述】:

我必须使用什么方法来计算 CGPoint 从旧位置到新位置的距离?

var point: CGPoint = CGPointMake(x:50.0, y:50.0)

还有一种用LMB拖动点的方法:

func mouseDragged(event: NSEvent) {

    var pointDragged = self.convertPoint(event.locationInWindow, fromView: nil)
}

【问题讨论】:

  • 需要找X偏移吗?

标签: macos swift2 mouseevent cgpoint


【解决方案1】:

使用Pythagorean theoremmouseLocation = event.mouseLocation(),其中event 的类型为NSEvent

let point1: CGPoint = CGPoint(x: 2.0, y: 9.0)
let point2: CGPoint = CGPoint(x: 4.0, y: 13.0)

let xDist = (point1.x - point2.x)
let yDist = (point1.y - point2.y)
let distance = sqrt((xDist * xDist) + (yDist * yDist))

print(distance)

point1 将是您的起始位置,您可以从mouseDragged 函数中的NSEvent 获取用户点击的位置 - point2

mouseLocation = event.mouseLocation()
point2 = CGPointMake(mouseLocation.x, mouseLocation.y)

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 2013-06-19
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 2012-01-14
    相关资源
    最近更新 更多