Android determines whether a vertex is in a closed path

Source: Internet
Author: User

To determine whether a vertex is in a closed path, you can regard the path as a set of points, that is, the path can be considered as a polygon, the problem is to judge whether a point is in a polygon.

Http://en.wikipedia.org/wiki/point_in_polygonis used to determine whether a vertex is in a polygon.

The followingCodeDescribes a set of cable categories, which can determine whether a point is in a set of cable areas drawn by the user's finger:

/*** A polygon represents a lasso. ** @ author snow **/public class lasso {// polygon coordinatesprivate float [] mpolyx, mpolyy; // Number of size in polygonprivate int mpolysize; /*** default constructor ** @ Param PX * polygon coordinates x * @ Param py * polygon coordinates y * @ Param PS * polygon sides count */Public Lasso (float [] PX, float [] py, int PS) {This. mpolyx = px; this. mpolyy = py; this. Mpolysize = Ps;}/*** constructor ** @ Param pointfs * points list of the lasso */Public Lasso (list <pointf> pointfs) {This. mpolysize = pointfs. size (); this. mpolyx = new float [this. mpolysize]; this. mpolyy = new float [this. mpolysize]; for (INT I = 0; I <this. mpolysize; I ++) {This. mpolyx [I] = pointfs. get (I ). x; this. mpolyy [I] = pointfs. get (I ). y;} log. D ("lasso", "lasso size:" + mpolysize);}/*** check if T His polygon contains the point. ** @ Param x * point coordinate X * @ Param y * point coordinate y * @ return point is in polygon flag */Public Boolean contains (float X, float y) {boolean result = false; For (INT I = 0, j = mpolysize-1; I <mpolysize; j = I ++) {If (mpolyy [I] <Y & mpolyy [J]> = y) | (mpolyy [J] <Y & mpolyy [I]> = y )) {If (mpolyx [I] + (Y-mpolyy [I])/(mpolyy [J]-mpolyy [I]) * (MPO Lyx [J]-mpolyx [I]) <X) {result =! Result ;}} return result ;}}

When a user's finger is scratched on the screen, the point that the finger is crossed can be saved to instantiate the lasso class, or the point of the closed path object can be retrieved through the pathmeasure class after the user's finger is lifted, then instantiate the lasso class.

In the lasso class, the contains method is used to determine whether the vertex is within the polygon,Source codeSee http://stackoverflow.com/a/2922778/1969158.

Related Article

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.