Get each and every cgpoint(x,y) coordinates using UIBezierPath like all pixel(x,y)
Asked Answered
H

1

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

Habituate answered 22/11, 2019 at 7:7 Comment(0)
E
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.

Edea answered 22/11, 2019 at 8:20 Comment(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 UIBezierPathHabituate
Can you please let me know about that path coordinates come under UIBezierPath with curve line, just need to curve line x,y coordinatesHabituate
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...Habituate

© 2022 - 2024 — McMap. All rights reserved.