0

I just draw path using UIBezierPath and i would like to get each and every CGPoint that come under UIBezierPath. I try with following code

func forEach( body: [![enter image description here][1]][1]@escaping @convention(block) (CGPathElement) -> Void) {
    typealias Body = @convention(block) (CGPathElement) -> Void
    let callback: @convention(c) (UnsafeMutableRawPointer, UnsafePointer<CGPathElement>) -> Void = { (info, element) in
        let body = unsafeBitCast(info, to: Body.self)
        body(element.pointee)
    }
    //print(MemoryLayout.size(ofValue: body))
    let unsafeBody = unsafeBitCast(body, to: UnsafeMutableRawPointer.self)
    self.apply(info: unsafeBody, function: unsafeBitCast(callback, to: CGPathApplierFunction.self))
}

func getPathElementsPoints() -> [CGPoint] {
    var arrayPoints : [CGPoint]! = [CGPoint]()
    self.forEach { element in
        switch (element.type) {
        case CGPathElementType.moveToPoint:
            arrayPoints.append(element.points[0])
        case .addLineToPoint:
            arrayPoints.append(element.points[0])
        case .addQuadCurveToPoint:
            arrayPoints.append(element.points[0])
            arrayPoints.append(element.points[1])
        case .addCurveToPoint:
            arrayPoints.append(element.points[0])
            arrayPoints.append(element.points[1])
            arrayPoints.append(element.points[2])
        default: break
        }
    }
    return arrayPoints
}
func getPathElementsPointsAndTypes() -> ([CGPoint],[CGPathElementType]) {
    var arrayPoints : [CGPoint]! = [CGPoint]()
    var arrayTypes : [CGPathElementType]! = [CGPathElementType]()
    self.forEach { element in
        switch (element.type) {

        case CGPathElementType.moveToPoint:
            arrayPoints.append(element.points[0])
            arrayTypes.append(element.type)
        case .addLineToPoint:
            arrayPoints.append(element.points[0])
            arrayTypes.append(element.type)
        case .addQuadCurveToPoint:
            arrayPoints.append(element.points[0])
            arrayPoints.append(element.points[1])
            arrayTypes.append(element.type)
            arrayTypes.append(element.type)
        case .addCurveToPoint:
            arrayPoints.append(element.points[0])
            arrayPoints.append(element.points[1])
            arrayPoints.append(element.points[2])
            arrayTypes.append(element.type)
            arrayTypes.append(element.type)
            arrayTypes.append(element.type)
        default: break
        }
    }
    return (arrayPoints,arrayTypes)
}

I am able to get points using this above code with CGPoint, but it's give only that control point.

Any one suggest for me how can i get all points

Thanksenter image description here

1 Answer 1

1

A simple (maybe inaccurate depending on the use case) way to do this would be:

    let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
    let bezierPath = UIBezierPath(ovalIn: rect.insetBy(dx: 10, dy: 10))

    let pathBounds = bezierPath.bounds
    let minX = Int(pathBounds.minX)
    let maxX = Int(pathBounds.maxX)
    let minY = Int(pathBounds.minY)
    let maxY = Int(pathBounds.maxY)

    var allPoints: [CGPoint] = []
    var pathPoints: [CGPoint] = []

    for x in minX...maxX {
        for y in minY...maxY {

            let point = CGPoint(x: CGFloat(x), y: CGFloat(y))
            allPoints.append(point)

            if bezierPath.contains(point) {
                pathPoints.append(point)
            }
        }
    }

    print("All Points: \(allPoints.count)")
    print("Path Points: \(pathPoints.count)")

For me this prints

All Points: 6561 
Path Points: 5025

Hope this is helpful.

3
  • Thanks to identify the (x, y) coordinates, but here I am getting only some points Like I have one speed curve chart that speed curve chart have one original path curve so that path curve I want to (x, y)coordinates that comes under UIBezierPath
    – Nik Jack
    Nov 25, 2019 at 5:37
  • Can you please let me know about that path coordinates come under UIBezierPath with curve line, just need to curve line x,y coordinates
    – Nik Jack
    Nov 26, 2019 at 7:10
  • for this example, I am getting value like x: 0.0, y:183.0 again x:1.0, y: 182.0 like this but I want with floating point also like 24.75, 56.25 etc...
    – Nik Jack
    Dec 16, 2019 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.