Search code examples
iosswiftxcodecursormouse

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


i have a brush with a line width of 16 and when i move my finger on screen i want to detect the color of the pixel that is in front of my brush. I can detect the pixel color on screen that is not an issue. The issue is that it detects the center of my brush but i actually need the pixel in front of my brush. For that i need to know in which angle the user is drawing so that i can calculate the x and y offset from center of my brush to detect on that spot the pixel color.

anyone who have an idea and can point me in the right direction ? I tried with gesture but it is only for swipe and limited to left / right / down and up and when drawing the gesture is not recognized. 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
       }
   }

code already implemented solution from Claude (thx for that)


Solution

  • How many pixels ahead do you want ? Here I take 5. Take care: looking at a single pixel may not give a very robust color setting however.

    Keep the previous position of the finger (previousPos). Then, with the actual position (actualPos), you get the direction with:

    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)