6.5 C
New York
Friday, November 29, 2024

DragGesture.onChange: for loop overloads cpu and freeze app


I’ve an app in which you’ll draw utilizing factors, the code introduced beneath is required in order that when one line (already drawn) intersects with present, the present line is deleted.

After I attempt to do that in DragGesture().onChanged, with every motion the code begins to be executed once more, which causes a CPU overload and the traces are drawn with a delay.

repair that?

Picture("instance")
    .gesture(
        DragGesture()
            .onChanged { value2 in
                let currentPoint2 = value2.location
                self.currentDrawing2.points2.append(currentPoint2)
                if drawings1.rely > 0
                {
                    let points2 = currentDrawing2.points2
                    let points1 = drawings1[0].points1
                    if points2.rely > 0
                    {
                        
                        for i in 0..<points2.count-1 {
                            let current2 = points2[i]
                            let next2 = points2[i+1]
                            for i in 0..<points1.count-1 {
                                let present = points1[i]
                                let subsequent = points1[i+1]
                                
                                
                                let delta1x = subsequent.x - present.x
                                let delta1y = subsequent.y - present.y
                                let delta2x = next2.x - current2.x
                                let delta2y = next2.y - current2.y
                                
                                // create a 2D matrix from our vectors and calculate the determinant
                                let determinant = delta1x * delta2y - delta2x * delta1y
                                
                                if abs(determinant) < 0.0001 {
                                    // if the determinant is successfully zero then the traces are parallel/colinear
                                    //return nil
                                }
                                
                                // if the coefficients each lie between 0 and 1 then we've got an intersection
                                let ab = ((present.y - current2.y) * delta2x - (present.x - current2.x) * delta2y) / determinant
                                
                                if ab > 0 && ab < 1 {
                                    let cd = ((present.y - current2.y) * delta1x - (present.x - current2.x) * delta1y) / determinant
                                    
                                    if cd > 0 && cd < 1 {
                                        // traces cross
                                        
                                        self.currentDrawing2.points2.removeAll()
                                    }
                                }
                            }
                        }
                    }
                }
            }
    )

I attempted to implement this code utilizing a perform, however the perform must be referred to as inside onChanged so it did not work.
I attempted to implement this in .onEnded. All the pieces works effective right here, however I would like it to work in actual time.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles