-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2: Implement Arc Demo, fixes for path construction
- fixed the bug where appending an arc wouldn't have changed the path's current position - changed the assert condition for path construction (decreased accuracy) - .black, .white and .clear colors are now in .deviceGray color space
- Loading branch information
1 parent
ea8af3f
commit 3d2df3f
Showing
7 changed files
with
136 additions
and
7 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,90 @@ | ||
import SwiftyHaru | ||
|
||
let document = PDFDocument() | ||
let page = document.addPage(width: 200, height: 220) | ||
|
||
// Draw the grid | ||
|
||
let horizontalLabels = stride(from: 50, through: 150, by: 50).map(String.init) | ||
let verticalLabels = stride(from: 10, through: 210, by: 10).map(String.init) | ||
|
||
let labels = Grid.Labels(top: Grid.LabelParameters(sequence: "" + horizontalLabels, | ||
offset: Vector(x: 0, y: -6)), | ||
bottom: Grid.LabelParameters(sequence: "" + horizontalLabels, | ||
offset: Vector(x: 0, y: 6)), | ||
left: Grid.LabelParameters(sequence: "" + verticalLabels, | ||
frequency: 1, | ||
offset: Vector(x: 6, y: 0))) | ||
|
||
let serifs = Grid.Serifs(top: .default, | ||
bottom: .default, | ||
left: Grid.SerifParameters(frequency: 1), | ||
right: nil) | ||
|
||
let grid = Grid(width: page.width, height: page.height, labels: labels, serifs: serifs) | ||
page.draw(object: grid, position: .zero) | ||
|
||
/* Draw pie chart | ||
* | ||
* a: 45% Red | ||
* b: 25% Blue | ||
* c: 15% green | ||
* d: other yellow | ||
*/ | ||
|
||
let center = Point(x: 100, y: 100) | ||
|
||
var temporaryPosition: Point | ||
|
||
var a = Path() | ||
.moving(to: center) | ||
.appendingLine(toX: 100, y: 180) | ||
.appendingArc(center: center, | ||
radius: 80, | ||
beginningAngle: 0, | ||
endAngle: 360 * 0.45) | ||
temporaryPosition = a.currentPosition | ||
a.appendLine(to: center) | ||
|
||
var b = Path() | ||
.moving(to: center) | ||
.appendingLine(to: temporaryPosition) | ||
.appendingArc(center: center, radius: 80, beginningAngle: 360 * 0.45, endAngle: 360 * 0.7) | ||
temporaryPosition = b.currentPosition | ||
b.appendLine(to: center) | ||
|
||
var c = Path() | ||
.moving(to: center) | ||
.appendingLine(to: temporaryPosition) | ||
.appendingArc(center: center, radius: 80, beginningAngle: 360 * 0.7, endAngle: 360 * 0.85) | ||
temporaryPosition = c.currentPosition | ||
c.appendLine(to: center) | ||
|
||
var d = Path() | ||
.moving(to: center) | ||
.appendingLine(to: temporaryPosition) | ||
.appendingArc(center: center, radius: 80, beginningAngle: 360 * 0.85, endAngle: 360) | ||
.appendingLine(to: center) | ||
|
||
// Draw center circle | ||
let centerCircle = Path().appendingCircle(center: center, radius: 30) | ||
|
||
page.draw { context in | ||
|
||
context.fillColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1) | ||
context.fill(a) | ||
|
||
context.fillColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1) | ||
context.fill(b) | ||
|
||
context.fillColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1) | ||
context.fill(c) | ||
|
||
context.fillColor = #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1) | ||
context.fill(d) | ||
|
||
context.fillColor = .white | ||
context.fill(centerCircle) | ||
} | ||
|
||
document.display() |
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,17 @@ | ||
import Quartz | ||
import PlaygroundSupport | ||
import SwiftyHaru | ||
|
||
public extension SwiftyHaru.PDFDocument { | ||
|
||
public func display() { | ||
|
||
let view = PDFView(frame: NSRect(x: 0, y: 0, width: 480, height: 640)) | ||
|
||
view.document = PDFDocument(data: getData()) | ||
|
||
view.scaleFactor = 0.75 | ||
|
||
PlaygroundPage.current.liveView = view | ||
} | ||
} |
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,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
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