Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

How to compare two Shapes in SwiftUI?

I'm trying to implement following function: One Shape (displayed as a stroke) is displayed and the user is supposed to draw this Shape again with his fingers. The current approach is:

var body: some View {
    ZStack{

        // DRAWING FIELD
        Rectangle()
            .foregroundColor(Color.gray)
            .edgesIgnoringSafeArea(.all)
            .gesture(
                DragGesture()
                    .onChanged{ value in
                        self.viewModel.addNewPoint(value)
                    }
                    .onEnded{_ in
                        self.viewModel.compareShapes()
                        self.viewModel.removeAllPoints()
                    }
            )
        
        //DISPLAYING SHAPE FOR USER
        MyShape()
            .stroke(lineWidth: 5)

       //DRAWING LINE FROM USER
       DrawnLine(points: viewModel.drawnLine)
            .stroke(lineWidth: 5)
            .foregroundColor(.blue)
    }
}

struct MyShape: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            path.move(to: CGPoint(x: 100, y: 100))
            path.addLine(to: CGPoint(x: 200, y: 300))
            path.addLine(to: CGPoint(x: 300, y: 300))
        }
    }
}

struct DrawnLine: Shape {
    var points: [CGPoint]

    func path(in rect: CGRect) -> Path {
        var path = Path()
        guard let firstPoint = points.first else { return path }

        path.move(to: firstPoint)
        for pointIndex in 1..<points.count {
            path.addLine(to: points[pointIndex])
        }
        return path
    }
}

ViewModel:

class HanziWriterViewModel: ObservableObject {

    @Published var drawnLine: [CGPoint] = []

    public func addNewPoint(_ value: DragGesture.Value){
        self.drawnLine.append(value.location)
    }

    public func removeAllPoints(){
        self.drawnLine.removeAll()
    }

    public func compareShapes(){
        // here I have to compare both shapes
    }
}

I'm not sure what is the best way to compare two Shapes with each other. It would be helpful to know how many percent of the given line are covered by the finger-drawn line. Since I already store all CGPoints of my finger-drawn line, I need to compare these points with the given Shape. Is there any way to convert the Shape to an array of CGPoints? Or any other recommendations how to solve this problem?

enter image description here

The drawn line is only one single line.

EDIT: Just to clarify my question and the purpose: I need to compare the black line with the blue line, so it's not enough to check whether the CGPoints of the blue line are in the Shape with shape.contains(point)

question from:https://stackoverflow.com/questions/66050757/how-to-compare-two-shapes-in-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...