-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
31 lines (25 loc) · 1.53 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//: [< Previous](@previous) [Home](Introduction) [Next >](@next)
//: # Spin class
//: Your designer is very pleased this morning, because they've designed a neat geometric shape for a client logo. It might look complicated, but really it's just eight squares with each one rotated by 45 degrees around its bottom-left corner.
//:
//: Your designer tried writing the Core Graphics code themselves, but things aren't going to plan: they don't have enough boxes, and the ones they *do* have are all being drawn in the top-left corner.
//:
//: Part of the problem is that Core Graphics measures angles in *radians*: if you rotate by `.pi` radians it's the same as rotating 180 degrees, and if you rotate `.pi / 4` radians it's the same as rotating 45 degrees.
//:
//: - Experiment: Can you fix this code so that it draws the designer's sketch as they want? You might want to start by adjusting the `translateBy()` call.
import UIKit
let rect = CGRect(x: 0, y: 0, width: 1000, height: 1000)
let renderer = UIGraphicsImageRenderer(bounds: rect)
let boxRectangle = CGRect(x: 0, y: 0, width: 300, height: 300)
let rendered = renderer.image { ctx in
ctx.cgContext.setLineWidth(8)
ctx.cgContext.translateBy(x: 500, y: 500)
for _ in 1...8 {
ctx.cgContext.addRect(boxRectangle)
ctx.cgContext.rotate(by: .pi / 4)
}
UIColor.red.setStroke()
ctx.cgContext.strokePath()
}
showOutput(rendered)
//: [< Previous](@previous) [Home](Introduction) [Next >](@next)