-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new mask types: hear, ring, gear and superEllipse #518
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ extension UIBezierPath { | |
|
||
/** | ||
Create a Bezier path for a circle shape. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
*/ | ||
convenience init(circleIn bounds: CGRect) { | ||
|
@@ -38,7 +38,7 @@ extension UIBezierPath { | |
|
||
/** | ||
Create a Bezier path for a polygon shape with provided sides. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
- Parameter sides: The number of the polygon sides. | ||
*/ | ||
|
@@ -62,7 +62,7 @@ extension UIBezierPath { | |
|
||
/** | ||
Create a Bezier path for a parallelogram shape with provided top-left angle. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
- Parameter topLeftAngle: The top-left angle of the parallelogram shape. | ||
*/ | ||
|
@@ -129,7 +129,7 @@ extension UIBezierPath { | |
|
||
/** | ||
Create a Bezier path for a star shape with provided points. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
- Parameter sides: The number of the star points. | ||
*/ | ||
|
@@ -162,10 +162,148 @@ extension UIBezierPath { | |
|
||
close() | ||
} | ||
|
||
/** | ||
Create a Bezier path for a heart shape. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
*/ | ||
convenience init(heartIn bounds: CGRect) { | ||
self.init() | ||
var x: CGFloat = bounds.origin.x | ||
var y: CGFloat = bounds.origin.y | ||
|
||
// square bounds | ||
let width = ceil(min(bounds.size.width, bounds.size.height)) | ||
let height = width | ||
x = x + (bounds.size.width - width)/2 | ||
y = y + (bounds.size.height - height)/2 | ||
|
||
let lowerPoint = CGPoint(x: x + width / 2, y: (y + height )) | ||
move(to: lowerPoint) | ||
|
||
addCurve(to: CGPoint(x: x, y: (y + (height/4))), | ||
controlPoint1:CGPoint(x: (x + (width/2)), y: (y + (height*3/4))), | ||
controlPoint2: CGPoint(x: x, y: (y + (height/2)))) | ||
|
||
addArc(withCenter: CGPoint(x: (x + (width/4)), y: (y + (height/4))), | ||
radius: (width/4), | ||
startAngle: .pi, | ||
endAngle: 0, | ||
clockwise: true) | ||
|
||
addArc(withCenter: CGPoint(x: (x + (width * 3/4)), y: (y + (height/4))), | ||
radius: (width/4), | ||
startAngle: .pi, | ||
endAngle: 0, | ||
clockwise: true) | ||
|
||
addCurve(to: lowerPoint, | ||
controlPoint1: CGPoint(x: (x + width), y: (y + (height/2))), | ||
controlPoint2: CGPoint(x: (x + (width/2)), y: (y + (height*3/4))) ) | ||
} | ||
|
||
/** | ||
Create a Bezier path for a ring shape. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
- Parameter radius: The radius of the shape. | ||
*/ | ||
convenience init(ringIn bounds: CGRect, radius: CGFloat) { | ||
let center = CGPoint(x: bounds.midX, y: bounds.midY) | ||
let diameter = ceil(min(bounds.width, bounds.height)) | ||
let innerRadius = max(1, diameter / 2 - radius) | ||
let outerRadius = diameter / 2 | ||
|
||
self.init() | ||
addArc(withCenter: .zero, radius: innerRadius, startAngle: 0, endAngle: .pi * 2, clockwise: true) | ||
|
||
move(to: CGPoint(x: outerRadius, y: 0)) | ||
|
||
addArc(withCenter: .zero, radius: outerRadius, startAngle: 0, endAngle: .pi * 2, clockwise: true) | ||
|
||
apply(CGAffineTransform(translationX: center.x, y: center.y)) | ||
usesEvenOddFillRule = true | ||
} | ||
|
||
/** | ||
Create a Bezier path for a gear shape. | ||
|
||
- Parameter bounds: The bounds of shape. | ||
- Parameter radius: The radius of the shape. | ||
- Parameter cogs: The number of cogs (min: 2) | ||
*/ | ||
convenience init(gearIn bounds: CGRect, radius: CGFloat, cogs: Int) { | ||
let center = CGPoint(x: bounds.midX, y: bounds.midY) | ||
let diameter = ceil(min(bounds.width, bounds.height)) | ||
let innerRadius = max(1, diameter / 2 - radius) | ||
let outerRadius = diameter / 2 | ||
|
||
self.init() | ||
|
||
guard cogs > 2 else { return } | ||
|
||
let angle: CGFloat = .pi / CGFloat(cogs) | ||
var radius = (outerRadius, innerRadius) | ||
|
||
addArc(withCenter: .zero, radius: innerRadius/2, startAngle: 0, endAngle: .pi * 2, clockwise: true) | ||
move(to: CGPoint(x: radius.0, y: 0)) | ||
|
||
for _ in 0..<cogs*2 { | ||
addArc(withCenter: .zero, radius: radius.0, startAngle: 0, endAngle: -angle, clockwise: false) | ||
apply(CGAffineTransform(rotationAngle: angle)) | ||
swap(&radius.0, &radius.1) | ||
} | ||
|
||
apply(CGAffineTransform(translationX: center.x, y: center.y)) | ||
} | ||
|
||
/** | ||
Create a Bezier path for a super ellipse shape. | ||
https://en.wikipedia.org/wiki/Superellipse | ||
|
||
- Parameter n: The super ellipse main parameter. | ||
*/ | ||
convenience init(superEllipseInRect bounds: CGRect, n: CGFloat = CGFloat.𝑒){ | ||
let a = bounds.width / 2 | ||
let b = bounds.height / 2 | ||
let n_2 = 2 / n | ||
let center = CGPoint(x: bounds.midX, y: bounds.midY) | ||
let centerLeft = CGPoint(x: bounds.origin.x, y: bounds.midY) | ||
|
||
let x = { (t: CGFloat) -> CGFloat in | ||
let cost = cos(t) | ||
return center.x + cost.sign() * a * pow(abs(cost), n_2) | ||
} | ||
|
||
let y = { (t: CGFloat) -> CGFloat in | ||
let sint = sin(t) | ||
return center.y + sint.sign() * b * pow(abs(sint), n_2) | ||
} | ||
|
||
self.init() | ||
move(to:centerLeft) | ||
|
||
let factor = max((a+b)/10, 32) | ||
for t in stride(from: (-CGFloat.pi), to: CGFloat.pi, by: CGFloat.pi/factor) { | ||
addLine(to: CGPoint(x: x(t), y: y(t))) | ||
} | ||
close() | ||
} | ||
|
||
} | ||
|
||
private extension UIBezierPath { | ||
func point(from angle: CGFloat, radius: CGFloat, offset: CGPoint) -> CGPoint { | ||
return CGPoint(x: radius * cos(angle) + offset.x, y: radius * sin(angle) + offset.y) | ||
} | ||
} | ||
|
||
extension CGFloat { | ||
static let 𝑒 = CGFloat(M_E) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool |
||
func sign() -> CGFloat { | ||
if self<0 { return -1 } | ||
else if self>0 { return 1 } | ||
else { return 0 } | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may be able to encapsulate these four lines into a method because they are same as line 231.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want something like this ?
I can make CGRect extension
like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to have extensions. They look better to me.