由于UIBeizerPath
支持fill()
填充封闭区域。
我的要求是如何从UIBeizerPath()
方法获得与fill()
方法相同的封闭区域。
如果我能帮助找到beizerpath的区域和周边,我将非常感激。
假设我希望在字母A'中获取封闭区域,我可以获取所有beizerPath
CGPoints
但不能将子路径定义为evenOdd规则用于获取所需的字段属性,最后得到区域。我很想知道fill()
如何能够获得封闭区域。
我在solution中获得了beizerPath
字体,并希望从中获取任何字体的区域。
我怎么能做到这一点。
答案 0 :(得分:4)
extension CGPath{
func forEach( body: @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))
}
}
extension UIBezierPath{
var length: CGFloat{
var pathLength:CGFloat = 0.0
var current = CGPoint.zero
var first = CGPoint.zero
self.cgPath.forEach{ element in
pathLength += element.distance(to: current, startPoint: first)
if element.type == .moveToPoint{
first = element.point
}
if element.type != .closeSubpath{
current = element.point
}
}
return pathLength
}
}
extension CGPathElement{
var point: CGPoint{
switch type {
case .moveToPoint, .addLineToPoint:
return self.points[0]
case .addQuadCurveToPoint:
return self.points[1]
case .addCurveToPoint:
return self.points[2]
case .closeSubpath:
return CGRect.null.origin
}
}
func distance(to point: CGPoint, startPoint: CGPoint ) -> CGFloat{
switch type {
case .moveToPoint:
return 0.0
case .closeSubpath:
return point.distance(to:startPoint)
case .addLineToPoint:
return point.distance(to:self.points[0])
case .addCurveToPoint:
return BezierCurveLength(p0: point, c1: self.points[0], c2: self.points[1], p1: self.points[2])
case .addQuadCurveToPoint:
return BezierCurveLength(p0: point, c1: self.points[0], p1: self.points[1])
}
}
}
extension CGPoint{
func distance(to:CGPoint) -> CGFloat{
let dx = pow(to.x - self.x,2)
let dy = pow(to.y - self.y,2)
return sqrt(dx+dy)
}
}
// Helper Functions
func CubicBezierCurveFactors(t:CGFloat) -> (CGFloat,CGFloat,CGFloat,CGFloat){
let t1 = pow(1.0-t, 3.0)
let t2 = 3.0*pow(1.0-t,2.0)*t
let t3 = 3.0*(1.0-t)*pow(t,2.0)
let t4 = pow(t, 3.0)
return (t1,t2,t3,t4)
}
func QuadBezierCurveFactors(t:CGFloat) -> (CGFloat,CGFloat,CGFloat){
let t1 = pow(1.0-t,2.0)
let t2 = 2.0*(1-t)*t
let t3 = pow(t, 2.0)
return (t1,t2,t3)
}
// Quadratic Bezier Curve
func BezierCurve(t:CGFloat,p0:CGFloat,c1:CGFloat,p1:CGFloat) -> CGFloat{
let factors = QuadBezierCurveFactors(t: t)
return (factors.0*p0) + (factors.1*c1) + (factors.2*p1)
}
// Quadratic Bezier Curve
func BezierCurve(t:CGFloat,p0:CGPoint,c1:CGPoint,p1:CGPoint) -> CGPoint{
let x = BezierCurve(t: t, p0: p0.x, c1: c1.x, p1: p1.x)
let y = BezierCurve(t: t, p0: p0.y, c1: c1.y, p1: p1.y)
return CGPoint(x: x, y: y)
}
// Cubic Bezier Curve
func BezierCurve(t:CGFloat,p0:CGFloat, c1:CGFloat, c2:CGFloat, p1:CGFloat) -> CGFloat{
let factors = CubicBezierCurveFactors(t: t)
return (factors.0*p0) + (factors.1*c1) + (factors.2*c2) + (factors.3*p1)
}
// Cubic Bezier Curve
func BezierCurve(t: CGFloat, p0:CGPoint, c1:CGPoint, c2: CGPoint, p1: CGPoint) -> CGPoint{
let x = BezierCurve(t: t, p0: p0.x, c1: c1.x, c2: c2.x, p1: p1.x)
let y = BezierCurve(t: t, p0: p0.y, c1: c1.y, c2: c2.y, p1: p1.y)
return CGPoint(x: x, y: y)
}
// Cubic Bezier Curve Length
func BezierCurveLength(p0:CGPoint,c1:CGPoint, c2:CGPoint, p1:CGPoint) -> CGFloat{
let steps = 12
var current = p0
var previous = p0
var length:CGFloat = 0.0
for i in 1...steps{
let t = CGFloat(i) / CGFloat(steps)
current = BezierCurve(t: t, p0: p0, c1: c1, c2: c2, p1: p1)
length += previous.distance(to: current)
previous = current
}
return length
}
// Quadratic Bezier Curve Length
func BezierCurveLength(p0:CGPoint,c1:CGPoint, p1:CGPoint) -> CGFloat{
let steps = 12
var current = p0
var previous = p0
var length:CGFloat = 0.0
for i in 1...steps{
let t = CGFloat(i) / CGFloat(steps)
current = BezierCurve(t: t, p0: p0, c1: c1, p1: p1)
length += previous.distance(to: current)
previous = current
}
return length
}