In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use js to judge whether it is legal to form a single polygon according to coordinates. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Preface
Demand: judge whether the fence drawn by the user is legal in Amap.
Core solution: if the two adjacent segments are judged in reverse order, whether there is an intersection or not, the non-adjacent segments do not intersect.
Install npm install @ turf/helpers @ turf/line-intersect Code / * geometric Geometry tools Library * @ author maybe * @ license https://gitee.com/null_639_5368 * / import * as turf from "@ turf/helpers" import lineIntersect from "@ turf/line-intersect" / * coordinate transition segment * @ param {*} path * @ returns {arr} * / export function pathToLines (path) {const lines = [] Path.forEach ((p, pi) = > {let line; if (pi = = path.length-1) {line = turf.lineString ([path [pi], path [0]]); lines.push (line) return;} line = turf.lineString ([path [pi], path [pi + 1]]) Lines.push (line)}) / / console.log (JSON.stringify (lines)) return lines } / * determine whether a single polygon composed of coordinates is legal * @ param {*} path * @ description Please pass a similar two-dimensional array * @ returns {boolean} * / export function isTruePolygon (path) {/ / judgment array and the length of the array is less than 3 does not constitute a necessary condition to satisfy a facet termination if (! Array.isArray (path) | | path.length
< 3) return false; // 具体坐标也需是一个一维数组,并且数组的长度等于2 if (!path.every(item =>Array.isArray (item) & & item.length = = 2) return false; / / convert coordinates to line segment const lines = pathToLines (path); / / whether it is legal to mark let isTrue = true; / / verify function function check () {/ / reverse loop for (let I = lines.length-1; I > = 0 Const lineNextIndex -) {/ / baseline segment const line = lines [I]; const lineNextIndex = I = 0? Lines.length-1: I-1; const lineLastIndex = I = = lines.length-1? 0: I + 1; const lineNext = lines [lineNextIndex]; const lineLast = lines [lineLastIndex] / / two adjacent segments must have intersections if (! isIntersect (line, lineNext) | |! isIntersect (line, lineLast)) {console.log ('adjacent segments must have intersections', line, lineNext, lineLast, isIntersect (line, lineNext), isIntersect (line, lineLast) isTrue = false Return;} / / non-adjacent segments must have no intersection const noNearLines = lines.filter ((item, I) = > I! = = lineNextIndex & & I! = = lineLastIndex) NoNearLines.forEach (le = > {if (isIntersect (line, le)) {console.log ('non-adjacent segments must have no intersection') isTrue = false; return;}})} check (); isTrue? Console.info ('legal polygons'): console.log (illegal polygons) return isTrue;} function isIntersect (line1, line2) {return lineIntersect (line1, line2) .features.length > 0 } export default {pathToLines, isTruePolygon,} Test import {isTruePolygon} from'. / geometric'const path_false = [[116.403322, 39.920255], [116.385726, 39.909893], [116.410703, 39.897555], [116.402292, 39.892353], [116.389846, 39.891365]] const path_true = [[116.403322, 39.920255], [116.410703, 39.897555], [116.402292] 39.892353], [116.389846, 39.891365]] console.log (isTruePolygon (path_true)) / / trueconsole.log (isTruePolygon (path_false)); / / false is here about how to use js to determine whether it is legal to form a single polygon according to coordinates. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.