Element intersection detection

Source: Internet
Author: User

Recently done a project which involves a feature of intersect detection and now share it with the encouragement

First of all talk about what conditions to meet, to calculate the two elements are intersecting, the following is a:

Ax1 is the projection of the beginning of element A on the x-axis, Ax2 is the projection of the end point of element A on the x-axis, Ay1 is the projection of the start point of element A on the y-axis, and Ay2 is the projection of the end point of element A on the y-axis.

Bx1 is the projection of the starting point of element B on the x-axis, Bx2 is the projection of the end point of element B on the x-axis, By1 is the projection of the start point of element B on the y-axis, and By2 is the projection of the end of element B on the y-axis.

Element A and Element B intersect when both element A and element B intersect at the same time as the projection of the y-axis and x-axis

The conditions at which element A and element B intersect in the projection of the x-axis are: Ax1 <= Bx1 && Ax2 >= Bx1 | | Ax1 <= Bx1 && Ax2 >= Bx2

The conditions on which element A and element B intersect on the y-axis are: Ay1 <= By1 && Ay2 >= By1 | | Ay1 <= By1 && Bx2 >= By2

The specific code is as follows:

functionCompare (elema,elemb,gap) {varBooleans,elemapos,elembpos, Elemashadow_x,elemashadow_y, elembshadow_x,elembshadow_y, intersect_    x,intersect_y; //the position of the elementElemapos =Elema.offset (); Elembpos=Elemb.offset (); //projection of the Elemaelemashadow_x = [Elemapos.left, Elemapos.left +elema.width ()]; Elemashadow_y= [Elemapos.top, Elemapos.top +elema.height ()]; //projection of the Elembelembshadow_x = [Elembpos.left-gap, Elembpos.left + elemb.width () +Gap]; Elembshadow_y= [Elembpos.top-gap, Elembpos.top + elemb.height () +Gap]; //detect if intersecting on x-axisintersect_x = (Elemashadow_x[0] >= elembshadow_x[0] && elemashadow_x[0] <= elembshadow_x[1])        || (Elemashadow_x[1] >= elembshadow_x[0] && elemashadow_x[1] <= elembshadow_x[1]); //detect if intersecting on y-axisIntersect_y = (Elemashadow_y[0] >= elembshadow_y[0] && elemashadow_y[0] <= elembshadow_y[1])        || (Elemashadow_y[1] >= elembshadow_y[0] && elemashadow_y[1] <= elembshadow_y[1]); Booleans= Intersect_x &&intersect_y; returnBooleans}

Explanation of the parameter gap: Because the function required in this project is to require that the spacing between two elements be 10px, when the spacing between two elements is less than or equal to 10 is considered to intersect.

This says that the function uses the jquery library

Element intersection detection

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.