-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
182 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// BezierGenerator.swift | ||
// MaLiang | ||
// | ||
// Created by Harley.xk on 2017/11/10. | ||
// | ||
|
||
import UIKit | ||
|
||
class BezierGenerator { | ||
|
||
enum Style { | ||
case linear | ||
case quadratic // this is the only style currently supported | ||
case cubic | ||
} | ||
|
||
// var segements = 20 | ||
|
||
init() { | ||
} | ||
|
||
init(beginPoint: CGPoint) { | ||
begin(with: beginPoint) | ||
} | ||
|
||
func begin(with point: CGPoint) { | ||
points.append(point) | ||
} | ||
|
||
func pushPoint(_ point: CGPoint) -> [CGPoint] { | ||
points.append(point) | ||
if points.count < style.pointCount { | ||
return [] | ||
} | ||
step += 1 | ||
let result = genericPathPoints() | ||
return result | ||
} | ||
|
||
func finish() { | ||
step = 0 | ||
points.removeAll() | ||
} | ||
|
||
private var points: [CGPoint] = [] | ||
private var style: Style = .quadratic | ||
|
||
private var step = 0 | ||
private func genericPathPoints() -> [CGPoint] { | ||
|
||
var begin: CGPoint | ||
var control: CGPoint | ||
let end = CGPoint.middle(p1: points[step], p2: points[step + 1]) | ||
|
||
var vertices: [CGPoint] = [] | ||
if step == 1 { | ||
begin = points[0] | ||
let middle1 = CGPoint.middle(p1: points[0], p2: points[1]) | ||
control = CGPoint.middle(p1: middle1, p2: points[1]) | ||
} else { | ||
begin = CGPoint.middle(p1: points[step - 1], p2: points[step]) | ||
control = points[step] | ||
} | ||
|
||
/// segements are based on distance about start and end point | ||
let dis = begin.distance(to: end) | ||
let segements = max(Int(dis / 5), 2) | ||
|
||
for i in 0 ..< segements { | ||
let t = CGFloat(i) / CGFloat(segements) | ||
let x = pow(1 - t, 2) * begin.x + 2.0 * (1 - t) * t * control.x + t * t * end.x | ||
let y = pow(1 - t, 2) * begin.y + 2.0 * (1 - t) * t * control.y + t * t * end.y | ||
vertices.append(CGPoint(x: x, y: y)) | ||
} | ||
vertices.append(end) | ||
return vertices | ||
} | ||
} | ||
|
||
extension BezierGenerator.Style { | ||
var pointCount: Int { | ||
switch self { | ||
case .quadratic: return 3 | ||
default: return Int.max | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+378 Bytes
(110%)
Example/MaLiang/Images.xcassets/brush.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-13.2 KB
(43%)
Example/MaLiang/Images.xcassets/pen.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+7.14 KB
(150%)
Example/MaLiang/Images.xcassets/pencil.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters