Original address: http://www.html-js.com/article/1538 (front end stew)
Ray method is a very simple and intuitive way to judge whether a plane interior point is inside a polygon. In addition to the Ray method there are many other methods, today we will introduce a way to judge by the number of rotation.
A closed curve in a plane is the number of rotation (also called a winding number) of a point, representing the total number of times the curve bypasses that point. The following diagram shows a dynamic concept of the number of rotation: the number of rotation of the red curve in the figure (the position of the person) is 2.
The number of rotation is a basic concept in topology, which has very important properties and uses. This article is not intended to be discussed at this point, which requires considerable mathematical knowledge, otherwise it will be very boring and difficult to understand. For the time being, we just need to remember one characteristic of the rotation number: When the number of turns is 0 o'clock, the point is outside the closed curve.
For a given point and polygon, how to calculate the number of rotation.
Connect points and all vertices of polygons, respectively, with segments.
Calculates the angle at which the connection to the adjacent vertex is somewhat.
Calculates all angles and. Note that each angle has a direction, so it is possible to have a negative value.
Finally, the rotation number is calculated according to the angle accumulation value. Having read the introduction at the beginning of this article, it is easy to understand that 360° (2π) is equivalent to a rotation.
After the introduction of the idea, the following two points is to pay attention to the implementation of the problem.
The number of JavaScript is only 64-bit double-precision floating-point. For the unreasonable number of trigonometric functions, floating point calculation will inevitably cause some errors, so in the final calculation of the number of rotation need to do a rounding operation.
Typically, the range of values for a corner within a planar rectangular coordinate system is-π to π, which is the range of JavaScript trigonometric math.atan2 () return values. But JavaScript can not directly calculate the angle of any two lines, we can only calculate two lines and X positive axis angle, and then take the difference between the two. The result of this difference is likely to go beyond the-π to π, so we also need to deal with the difference beyond the range of values. code implementation:
/** * @description Rotation number method to determine whether the point is within the polygon * @param {Object} p to be judged by the point, format: {x:x coordinates, Y:Y coordinates} * @param {Array} poly polygon Vertices, The format of the array member is the same as the geometric relationship of P * @return {String} point P and Polygon poly/function Windingnumber (p, poly) {var px = p.x, p
y = p.y, sum = 0 for (var i = 0, L = poly.length, j = l-1; I < l; j = i, i++) {var sx = poly[i].x, SY = poly[i].y, tx = poly[j].x, Ty = poly[j].y//points coincide with polygon vertices or on edges of polygons if ((sx-p X) * (PX-TX) >= 0 && (sy-py) * (py-ty) >= 0 && (PX-SX) * (ty-sy) = = (py-sy) * (TX-SX) {return ' on '}///dot angle to adjacent vertex line var angle = math.atan2 (sy-py, sx-px)-Math.atan2 (Ty-py, TX-PX)//ensures that the angle does not exceed the range of values (-π to π) if (angle >= math.pi) {angle = Angle-math.pi * 2} else I
F (Angle <=-math.pi) {angle = angle + Math.PI * 2} sum + = angle}//Calculate the number of rotation and judge the geometric relationship between the points and polygons Return Math.Round (SUM/MATH.PI) = = 0?
' Out ': ' In '}
First: Ray theory
Second: X-ray method implementation
Third: Revolution number method Implementation