从圆画圆和线段
Draw circle and segments from circle
我想画一个圆,然后从这个圆上画出线段。抱歉,因为我在 Whosebug 上找到了很多主题,但仍然没有人能帮助我完全理解。你能给我一个示例代码吗?我想要如下图所示的结果。非常感谢
打开一个新的 Swift 3 游乐场并将以下代码粘贴到其中。您应该会在右侧看到一个 "w 512 h 512" 注释,点击它会出现一只眼睛。那时你应该看到一些图纸。
//: Playground - noun: a place where people can play
import UIKit
import CoreGraphics
public func makePieChart()-> UIImage?
{
let size = CGSize(width: 512, height:512)
let centerX = size.width/2.0
let centerY = size.height/2.0
let center = CGPoint(x: centerX, y: centerY)
let chartRadius = size.width/2.0
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
defer
{
UIGraphicsEndImageContext()
}
guard let quartz = UIGraphicsGetCurrentContext() else
{
return nil
}
let π = CGFloat.pi
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: 0.0, endAngle: π/3.0, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.red.cgColor)
quartz.fillPath()
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: π/3.0, endAngle: π, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.yellow.cgColor)
quartz.fillPath()
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: π, endAngle: 0.0, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.blue.cgColor)
quartz.fillPath()
return UIGraphicsGetImageFromCurrentImageContext()
}
let image = makePieChart()
我想画一个圆,然后从这个圆上画出线段。抱歉,因为我在 Whosebug 上找到了很多主题,但仍然没有人能帮助我完全理解。你能给我一个示例代码吗?我想要如下图所示的结果。非常感谢
打开一个新的 Swift 3 游乐场并将以下代码粘贴到其中。您应该会在右侧看到一个 "w 512 h 512" 注释,点击它会出现一只眼睛。那时你应该看到一些图纸。
//: Playground - noun: a place where people can play
import UIKit
import CoreGraphics
public func makePieChart()-> UIImage?
{
let size = CGSize(width: 512, height:512)
let centerX = size.width/2.0
let centerY = size.height/2.0
let center = CGPoint(x: centerX, y: centerY)
let chartRadius = size.width/2.0
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
defer
{
UIGraphicsEndImageContext()
}
guard let quartz = UIGraphicsGetCurrentContext() else
{
return nil
}
let π = CGFloat.pi
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: 0.0, endAngle: π/3.0, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.red.cgColor)
quartz.fillPath()
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: π/3.0, endAngle: π, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.yellow.cgColor)
quartz.fillPath()
quartz.move(to: center)
quartz.addArc(center: center, radius: chartRadius, startAngle: π, endAngle: 0.0, clockwise: false)
quartz.closePath() // path will complete back at the last move to (center of the circle)
quartz.setFillColor(UIColor.blue.cgColor)
quartz.fillPath()
return UIGraphicsGetImageFromCurrentImageContext()
}
let image = makePieChart()