iOS Dev-determine if a point is in an area

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.