Painted painted levels: fostered fostered fostered

Tags: “iOS” “Swift” “Sell curve” author: Taisei Xiaojian Review: QiShare team


1. Introduction of Bezier curve

Take the third order: let P0, P02, and P2 be three points in different order on a parabola. The two tangents at P0 and P2 intersect at P1, and the tangents at P02 intersect P0P1 and P2P1 at P01 and P11, then the following proportion holds:

This is called the tri-tangent theorem of a parabola.

When P0 and P2 are fixed, the parameter t is introduced to make the above ratio t:(1-t), namely:

When P0 and P2 are fixed, the parameter t is introduced to make the above ratio t:(1-t), namely:

When t changes from 0 to 1, formula 1 and Formula 2 represent the first and second edges of the control biedage. they are two primary Bezier curves. Substitute formula 1 and 2 into Formula 3 to obtain:

When t changes from 0 to 1, it represents a quadratic Bezier curve defined by three vertices, P0, P1, and P2.

And show that:

This quadratic Bezier curve P02 can be defined as a linear combination of a Bezier curve determined by the first two vertices (P0,P1) and the last two vertices (P1,P2) respectively.

And so on,

The cubic Bezier curve P03 defined by four control points can be defined as a linear combination of two quadratic Bezier curves determined by (P0,P1,P2) and (P1,P2,P3) respectively, by (n+1) control points Pi(I =0,1… ,n) defined n Bezier curves P0n can be defined by the front and rear n control points defined by the two (n-1) times Bezier curves P0N-1 and P1n-1 linear combination:

The recursive calculation formula of Bezier curve is obtained

Bezier curves are mathematical curves used in two-dimensional graphics applications. Curve definition: starting point, ending point (also known as anchor point), control point. By adjusting the control points, the shape of the Bezier curve changes. In 1962, French mathematician Pierre Bezier was the first to study this vector curve drawing method, and gave a detailed calculation formula, so the curve drawn according to such a formula is named after his family name, called Bezier curve.

2. Swift is used to draw Bessel curve

2.1 Ordinary line drawing

Drawing code of ordinary lines with Swift is as follows:

import UIKit

class BezierPathView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")}override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        self.drawLine()
        self.drawCommonCurve()
        self.drawSmoothPath()
    }
    
    func drawLine(a) {
        
        let offset:CGFloat = 50.0
        
        // Draw a rectangle
        //let rectpath = UIBezierPath(rect: CGRect.init(x: 15, y: offset, width: 300, height: 60))
        let rectpath = UIBezierPath(roundedRect: CGRect.init(x: 15, y: offset, width: 300, height: 60), cornerRadius: 5.0)

        rectpath.lineWidth = 5.0
        UIColor.green.setStroke()
        rectpath.stroke()
        UIColor.red.setFill()
        rectpath.fill()
        
        // Draw a line
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 25.0, y: offset + 130.0))
        path.addLine(to: CGPoint(x: 300.0, y: offset + 130.0))
        path.lineWidth = 5.0
        UIColor.cyan.setStroke()
        path.stroke()
    }
    
    func drawCommonCurve(a) {
        
        let offset:CGFloat = 260.0
        
        let curvePath = UIBezierPath()
        curvePath.move(to: CGPoint(x: 30.0, y: offset))
        curvePath.addQuadCurve(to: CGPoint(x: 350.0, y: offset), controlPoint: CGPoint(x: 350.0, y: offset + 100))
        UIColor.blue.setStroke()
        curvePath.stroke()
    }
    
    func drawSmoothPath(a) {
        
        let offset:CGFloat = 430
        
        let pointCount:Int = 4
        let pointArr:NSMutableArray = NSMutableArray.init(a)for i in 0.pointCount {
            let px: CGFloat = 15 + CGFloat(i) * CGFloat(80)
            let py: CGFloat = i % 2 = = 0 ? offset - 60 : offset + 60
            let point: CGPoint = CGPoint.init(x: px, y: py)
            pointArr.add(point)
        }
        
        let bezierPath = UIBezierPath()
        bezierPath.lineWidth = 2.0
        var prevPoint: CGPoint!
        
        for i in 0 ..< pointArr.count {
            let currPoint:CGPoint = pointArr.object(at: i) as! CGPoint
            
            // Draw a green circle
            let arcPath = UIBezierPath()
            arcPath.addArc(withCenter: currPoint, radius: 3, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
            UIColor.green.setStroke()
            arcPath.stroke()
            
            // Draw a smooth curve
            if i= =0 {
                bezierPath.move(to: currPoint)
            }
            else {
                let conPoint1: CGPoint = CGPoint.init(x: CGFloat(prevPoint.x + currPoint.x) / 2.0, y: prevPoint.y)
                let conPoint2: CGPoint = CGPoint.init(x: CGFloat(prevPoint.x + currPoint.x) / 2.0, y: currPoint.y)
                bezierPath.addCurve(to: currPoint, controlPoint1: conPoint1, controlPoint2: conPoint2)
            }
            prevPoint = currPoint
        }
        UIColor.red.setStroke()
        bezierPath.stroke()
    }
}
Copy the code

Running results:

Complex graphics can also be drawn.

Source Github address


Recommended articles:

Swift 5.1 (10) – Properties iOS App Background keep-active Use CGAffineTransform iN Swift iOS performance monitoring (1) — CPU power monitoring iOS performance monitoring (2) — Main thread lag monitoring iOS performance monitoring (3) — Add animation to the view with SwiftUI write a simple page with SwiftUI iOS App startup Optimization (3) — make your own tool to monitor the startup Time of the App — use “Time” “Profiler” tool monitoring App startup time iOS App startup optimization (a) – understand the App startup process qidance Weekly