需要用户在 iphone 屏幕上绘图的角度(方向)

need the angle (direction) of where the user is drawing on iphone screen

我有一个线宽为 16 的画笔,当我在屏幕上移动手指时,我想检测画笔前面像素的颜色。 我可以检测到屏幕上的像素颜色,这不是问题。问题是它检测到我画笔的中心,但我实际上需要画笔前面的像素。 为此,我需要知道用户在哪个角度绘制,以便我可以计算出我画笔中心的 x 和 y 偏移量,以检测该点的像素颜色。

任何有想法并能指出正确方向的人? 我尝试使用手势,但它仅用于滑动并且仅限于向左/向右/向下和向上,并且在绘制手势时无法识别。 screenshot

       override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let width: CGFloat = myDrawarea.frame.size.width
    let height: CGFloat = myDrawarea.frame.size.height
        swiped = true
       if let touch = touches.first {
       var currentPoint = touch.location(in:myDrawarea)
        if currentPoint.x < 0 {
           currentPoint.x = 0
        }
        if currentPoint.x > width {
           currentPoint.x = width
        }
        if currentPoint.y < height/2 - width/2 {
           currentPoint.y = height/2 - width/2
        }
        if currentPoint.y > height/2 + width/2 {
           currentPoint.y = height/2 + width/2
        }

          if abcSelected {

            print ("LP CP ",lastPoint,  currentPoint)
            print("angle", angle(between: lastPoint, ending: currentPoint))

let distanceToTest = 10.0  // Change this
let deltaX = currentPoint.x - lastPoint.x
let deltaY = currentPoint.y - lastPoint.y
let dist = sqrt(deltaX * deltaX + deltaY * deltaY)
if dist == 0 { return } // Have to quit here
let dx = CGFloat(distanceToTest) * deltaX / dist
let dy = CGFloat(distanceToTest) * deltaY / dist
let futurePos = CGPoint(x: currentPoint.x + dx, y: currentPoint.y + dy)
            print ("proposed point", futurePos)
let red2:Float = myDrawarea.colorOfPointRGB(point: futurePos).red1 * 255
let green2:Float = myDrawarea.colorOfPointRGB(point: futurePos).green1 * 255
let blue2:Float = myDrawarea.colorOfPointRGB(point: futurePos).blue1 * 255
let hexValue = String(format:"%02X", Int(red2)) + String(format:"%02X", Int(green2)) + String(format:"%02X", Int(blue2))
      print(hexValue)
 if hexValue != "CCCCFF" {
      print("out the line", hexValue)
                                 //   playSoundEasy(note: "dun_dun_dun")
   }

        }
        drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
        lastPoint = currentPoint
       }
   }

代码已经实现了 Claude 的解决方案(感谢)

你想要提前多少像素?在这里,我取 5。 注意:然而,查看单个像素可能不会给出非常稳健的颜色设置。

保留手指之前的位置(previousPos)。 然后,根据实际位置 (actualPos),您可以得到方向:

let distanceToTest = 5.0  // Change this
let deltaX = actualPos.x - previousPos.x
let deltaY = actualPos.y - previousPos.y
let dist = sqrt(deltaX * deltaX + deltaY * deltaY)
if dist == 0 { return } // Have to quit here
let dx = distanceToTest * deltaX / dist
let dy = distanceToTest * deltaY / dist

let futurePos = CGPoint(x: actualPos.x + dx, y: actualPos.y + dy)