• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 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)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift-技巧(十) Protocol 的灵活使用发布时间:2022-07-13
下一篇:
【swift学习笔记】一.页面转跳的条件判断和传值发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap