前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >判断一个坐标点是否在封闭曲线内的方法(swift)

判断一个坐标点是否在封闭曲线内的方法(swift)

作者头像
woopDast1
发布2020-09-04 10:39:36
9490
发布2020-09-04 10:39:36
举报
文章被收录于专栏:iOSeriOSer
代码语言:javascript
复制
 1 //用playground运行即可
 2 import UIKit
 3 
 4 var str = "Hello, playground"
 5 
 6 let lTestPointsOne : [(Double, Double)]  = [(0,0)]
 7 let lTestPoints : [(Double, Double)]  = [(0,0),(0,1),(1,1)]
 8 let lPoints : [(Double, Double)]  = [(0,0),(0,1),(1,2),(2,1),(3,2),(3,0)]
 9 
10 func judgeIf(point:(Double,Double), isIn pointArr:[(Double, Double)])->Bool{
11     if 0 == pointArr.count {
12         return false
13     }
14     let lPath = UIBezierPath.init()
15     let l1stPoint = pointArr.first
16     lPath.move(to: CGPoint.init(x: l1stPoint!.0, y: l1stPoint!.1))
17     
18     for i in 1..<pointArr.count {
19         let lP = pointArr[i]
20         lPath.addLine(to: CGPoint.init(x: lP.0, y: lP.1))
21     }
22     
23     lPath.close()
24     return lPath.contains(CGPoint.init(x: point.0, y: point.1))
25 }
26 
27 print(lPoints)
28 judgeIf(point: (0,0), isIn: lPoints)
29 judgeIf(point: (1,0), isIn: lPoints)
30 judgeIf(point: (3,-1), isIn: lPoints)
31 judgeIf(point: (0.5,2), isIn: lPoints)
32 judgeIf(point: (1,1), isIn: lPoints)
33 judgeIf(point: (2,1), isIn: lPoints)
34 judgeIf(point: (2,0.5), isIn: lPoints)
35 judgeIf(point: (2,1.5), isIn: lPoints)
36 judgeIf(point: (3,1), isIn: lPoints)
37 judgeIf(point: (3.3,8.1), isIn: lPoints)
38 
39 judgeIf(point: (0,0), isIn: lTestPoints)
40 judgeIf(point: (0,1), isIn: lTestPoints)
41 judgeIf(point: (0.5,0.5), isIn: lTestPoints)
42 judgeIf(point: (0.5,0.7), isIn: lTestPoints)
43 judgeIf(point: (0.5,0.1), isIn: lTestPoints)
44 judgeIf(point: (0,8), isIn: lTestPoints)
45 judgeIf(point: (1,0), isIn: lTestPoints)
46 
47 judgeIf(point: (1,0), isIn: lTestPointsOne)
48 judgeIf(point: (0,0), isIn: lTestPointsOne)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档