One 、 explain
stay GIS field , It is a basic method to judge whether the point is within the polygon range , Here we mainly talk about the implementation principle .
The principle is simple , There is one GIS theory , A point sends rays in one direction , If the intersection point of the ray and each edge of the polygon is odd, it means that the point is within the scope of the polygon .
( The picture is quoted from :https://blog.csdn.net/qq_27161673/article/details/52973866)
Two 、 Implementation code
So the implementation code is very clear , Any point creates a ray to the right , Why is the right side , Because the horizontal line can be maintained y unchanged , This situation is the simplest and better understood . The implementation code is as follows :
// Determine whether the point is within the polygon range
function queryPtInPolygon(point, polygon) {
var p1, p2, p3, p4; p1 = point;
p2 = { x: 1000000000000, y: point.y }; var count = 0;
// Compare each edge to the ray
for (var i = 0; i < polygon.length - 1; i++) {
p3 = polygon[i]; p4 = polygon[i + 1];
if (checkCross(p1, p2, p3, p4) == true) {
count++;
}
}
p3 = polygon[polygon.length - 1]; p4 = polygon[0];
if (checkCross(p1, p2, p3, p4) == true) {
count++;
} return (count % 2 == 0) ? false : true; // Judge whether two line segments intersect
function checkCross(p1, p2, p3, p4) {
var v1 = { x: p1.x - p3.x, y: p1.y - p3.y },
v2 = { x: p2.x - p3.x, y: p2.y - p3.y }, v3 = { x: p4.x - p3.x, y: p4.y - p3.y },
v = crossMul(v1, v3) * crossMul(v2, v3); v1 = { x: p3.x - p1.x, y: p3.y - p1.y };
v2 = { x: p4.x - p1.x, y: p4.y - p1.y }; v3 = { x: p2.x - p1.x, y: p2.y - p1.y };
return (v <= 0 && crossMul(v1, v3) * crossMul(v2, v3) <= 0) ? true : false; } // Calculate the vector cross product
function crossMul(v1, v2) {
return v1.x * v2.y - v1.y * v2.x;
}
}
}
Test case download address :https://download.csdn.net/download/jiangfei200809/9213835
JS Realize more related articles about whether the point is within the polygon range
- java/c# Judge whether the point is in the polygon area
java/c# Judge whether the point is in the polygon area year 06 month 29 Japan ⁄ comprehensive ⁄ common 1547 word ⁄ Font size Small in Big ⁄ Comment off Recently, I helped others solve a problem , How to judge a coordinate point , Whether it is within the polygon area ( A two-dimensional ). ...
- PHP Judge whether the point is inside or outside the polygon area
Xiaoxie blog original address https://xgs888.top/post/view?id=79 PHP Judge whether the point is inside or outside the polygon area : According to the ray method of mathematical knowledge , If the number of points where rays intersect geometric polygons is odd, it is in the interior of geometry : accidentally ...
- C# Judge whether the point is in the rectangular box
Welcome to join the group QQ Group 830426796 use System.Drawing.Drawing2D.GraphicsPath and Region Classes unite , And then use Region.IsVisible(poi ...
- PHP Judge whether the point is in the polygon
How to judge whether a point is in a polygon , When will this scenario be used . Let's simulate a real scene . Our company is an express company , There are 6 A point . Every minute has 3-5 Workers are in charge of the nearby express delivery , So according to the service area of each point, we can ...
- Baidu Maps Judge marker Whether it's in a polygon
I drew a circle yesterday , sentence marker Whether there is an inner circle . Let's draw polygons today , Judge marker In a polygon . Need to introduce a js <script type="text/javascript&quo ...
- Use Baidu API(JavaScript edition ) Realize drawing any polygon on the map , And judge whether the given longitude and latitude are within the polygon range . And the ranging function between two points
Statement of rights : This article is an original blog article , No reprint without the permission of the blogger . Use Baidu API(JavaScript edition ) Realize drawing any polygon on the map , And judge whether the given longitude and latitude are within the polygon range . And the ranging function between two points . Draw polygon ( Blue ) ...
- js To determine the specific type of the object
You may know that js You can use typeof To judge . Look at the following <script> alert(typeof 1);//number alert(typeof "2" ...
- JS Code to determine IE6,IE7,IE8,IE9!
JS Code to determine IE6,IE7,IE8,IE9!2011 year 12 month 15 Japan Thursday 14:01 Sometimes we need to make web pages JS testing IE Version of , Here's the test Microsoft Internet Explorer Three generations of versions ...
- JS The problem of judging the mouse button in
JS The problem of judging the mouse button in .IE The left button is window.event.button = 1 Right click is window.event.button = 2 The middle key is window.event.button = 4 There are no buttons ...
- ----------jqery and js How to determine checkbox Is it checked? -------- How to choose one of the two radio buttons , And use jquery Get the selected value
jqery and js How to determine checkbox Is it checked? jquery: <div id="divId" class="divTable"> <div ...
Random recommendation
- AnjularJS series 3 —— Two-way binding of data
Third articles , Two way data binding Data binding is AnguarJS One of the characteristics of , Avoid writing lots of initial code to save development time The data binding Directive provides you with Model Project to view Methods . These projections can be seamless , Apply to without any influence web Should be ...
- 2014 Amazon online written test questions and solutions (MMChess problem )
The whole idea : The key is to know the current Steps The full arrangement in the array is enough , And need not repeat the full arrangement . That is, the key lies in the implementation of non recursive Full Permutation ~ In fact, direct use STL Medium next_permutation Algorithm , Here I realize another ...
- Revit How to customize shortcut keys in
The best way to draw is the left-hand keyboard , Right hand mouse , Using shortcut keys will greatly improve drawing efficiency ,Revit It also provides the function to customize the shortcut keys of drawing tools (Revit2011 And later versions ), There are two ways to call up the custom shortcut window , The first is Revit window ...
- Calling / Running a report in Oracle forms 10g / 11g
Calling / Running a report in Oracle forms 10g / 11g Below is the procedure to call a report in Orac ...
- JS Basic knowledge of ( Scope / Garbage management )
1.js There is no block-level scope if (true) { var color = “blue”; } alert(color); //”blue” for (var i=0; i < 10; i++){ ...
- 18.allegro Regional constraint rule settings
One . Line width and line spacing --- ---------------- And then create an area on the board ----- ---- --- ---- ------------------------------
- Linux command - id: Display the identity of the user
Command format id [OPTION]... [USERNAME] Command parameter -a Ignore , Designed for compatibility with other versions only . -Z, --context Show only the security environment of the current user . -g, --group Show only ...
- gc Study ( turn )
One .GC Features and all kinds of GC The choice of 1. The characteristics of the garbage collector 2. The choice of garbage collector 2.1 continuity VS. parallel 2.2 Concurrent VS. stop-the-world 2.3 Compress VS. Uncompressed VS. Copy ...
- Linux And shell Programming functions use
linux shell You can define functions , And then in shell You can call... In the script . Let's talk about its definition , And what you need to pay attention to . Discuss with the author :http://www.cnblogs.com/int ...
- Past questions The minister's travel Tree form DP
Topic link : The minister's travel Ideas : Brocade bag said to use wide search , Unfortunately, this question does not say the range of data , Worry about the complexity , I'll use the tree directly DP-- Find the farthest path of the tree . Take the city as an example 1 It is the root node of the whole tree ,d(i) Said to i Is the farthest path of the subtree of the root node , One more f ...