1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

多角形を描く関数を作ってみた

Last updated at Posted at 2017-01-02

Simulator Screen Shot 2017.01.01 22.38.22.png
Simulator Screen Shot 2017.01.01 22.39.32.png
Simulator Screen Shot 2017.01.01 22.39.41.png
Simulator Screen Shot 2017.01.01 22.39.52.png

3角形や4角形は用意されてるけど、5角形やそれ以上はどう描けばいいんだ?って思ったので調べてたらいつの間にか17角形まで調べてしまっていたので関数で使えるようにしたものを公開。

PolygonSample

矩型範囲と描きたい多角形を引数で指定すればそのpathを返します。

enum Polygon : Int {
    case pentagon = 5   // 5角形
    case hexagon = 6    // 6角形
    case octagonal = 8  // 8角形
    case decagon = 10   // 10角形
    case dodecagon = 12 // 12角形
    case hexadecagon = 16 // 16角形
    case heptadecagon = 17 // 17角形
    case star = 0
}

func polygonPath(rect : CGRect, polygon : Polygon) -> UIBezierPath {

	// 省略 
	
	return UIBezierPath
}


CALayer上で使う場合はこんな感じで使えます。

    override func draw(in ctx: CGContext) {
        
        ctx.setStrokeColor(UIColor.red.cgColor)
        ctx.addPath(polygonPath(rect: self.bounds, polygon: polygon).cgPath) // <- 呼び出し
        ctx.drawPath(using: .stroke)
        
    }

参考

8角形以上の多角形の書き方ってネットでも資料が少なくて読みづらいのが多かったので、わざわざ本を買ってしまった・・・。

コンパスと定規の数学: 手で考える幾何学の世界 (アルケミスト双書)

1
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?