iOS sometimes needs to decide whether to touch the area of a graph. That is, whether the touch point is within the area of a graph.
There are many ways to solve the problem, here is a brief introduction.
We can create an area through Cgpath, where the area is made up of two-point segments and closed by the path, and then you can use the Cgpath correlation function Cgpathcontainspoint to determine if the point is in the area.
For example, a simple rectangle is created here. Its frame is (4, 4, 10, 10). The coordinates of the four vertices are (4, 4), (4, 14), (14, 14), (14, 4), respectively
Obviously, (1, 1) is not within the region (5, 5) in this region.
We can verify that the code is as follows:
-(void) viewdidload { [super viewdidload]; Cgmutablepathref pathref=cgpathcreatemutable (); Cgpathmovetopoint (Pathref, NULL, 4, 4); Cgpathaddlinetopoint (Pathref, NULL, 4, +); Cgpathaddlinetopoint (Pathref, NULL, N, +); Cgpathaddlinetopoint (Pathref, NULL, 4); Cgpathaddlinetopoint (Pathref, NULL, 4, 4); Cgpathclosesubpath (pathref); Cgpoint Point=cgpointmake (5, 5); Cgpoint Outpoint=cgpointmake (1, 1); if (Cgpathcontainspoint (Pathref, NULL, point, NO)) { NSLog (@ "point in path!"); } if (! Cgpathcontainspoint (Pathref, NULL, Outpoint, NO)) { NSLog (@ "outpoint out path!");} }
iOS Dev-determine if a point is in an area