国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

百度地圖判斷多邊形區(qū)域是否重復(fù)

Charles / 3249人閱讀

摘要:原文來自前端時間遇到個問題,就是判斷百度地圖里的多個任意多邊形區(qū)域是否重復(fù),在網(wǎng)上看了很多的文章都沒有找到解決方案,功夫不負(fù)有心人,在網(wǎng)上找到個可以判斷是否重復(fù)的,但是在包含的情況下就不能判斷,后來自己加入根據(jù)點(diǎn)判斷點(diǎn)是否在多邊形內(nèi)來判斷重

原文來自 taoeer.top

前端時間遇到個問題,就是判斷百度地圖里的多個任意多邊形區(qū)域是否重復(fù),在網(wǎng)上看了很多的文章都沒有找到解決方案,功夫不負(fù)有心人,在網(wǎng)上找到個可以判斷是否重復(fù)的,但是在包含的情況下就不能判斷,后來自己加入根據(jù)點(diǎn)判斷點(diǎn)是否在多邊形內(nèi)來判斷重復(fù),問題已解決,在此把代碼貼出來,供大家參考

//#region 驗證兩個面是否相交的算法 (此函數(shù)摘抄自網(wǎng)絡(luò))
function intersectsPolygonAndPolygon (polygon1LinearRings, polygon2LinearRings) {

// polygon1LinearRings : array[LinearRing,...]
function intersectsByPolygon (polygon1LinearRings, polygon2LinearRings) {
var intersect = false;

intersect = intersectsByLinearRings(polygon1LinearRings, polygon2LinearRings);

if(!intersect) {
    // check if this poly contains points of the ring/linestring
    for(i=0, len=polygon2LinearRings.length; i 0) {
    contained = containsPointByLinearRing(point, LinearRings[0]);
    if( numRings > 1) {
        // check interior rings
        var hole;
        for(var i=1; i 0) {
        fig = parseFloat(num.toPrecision(sig));
    }
    return fig;
}

var digs = 14;
var px = approx(point.x, digs);
var py = approx(point.y, digs);
function getX(y, x1, y1, x2, y2) {
    return (y - y2) * ((x2 - x1) / (y2 - y1)) + x2;
}

var numSeg = LinearRing.length - 1;
var start, end, x1, y1, x2, y2, cx, cy;
var crosses = 0;

for(var i=0; i= x1 && px <= x2) || // right or vert
                x1 >= x2 && (px <= x1 && px >= x2)) { // left or vert
                // point on edge
                crosses = -1;
                break;
            }
        }
        // ignore other horizontal edges
        continue;
    }
    cx = approx(getX(py, x1, y1, x2, y2), digs);
    if(cx == px) {
        // point on line
        if(y1 < y2 && (py >= y1 && py <= y2) || // upward
            y1 > y2 && (py <= y1 && py >= y2)) { // downward
            // point on edge
            crosses = -1;
            break;
        }
    }
    if(cx <= px) {
        // no crossing to the right
        continue;
    }
    if(x1 != x2 && (cx < Math.min(x1, x2) || cx > Math.max(x1, x2))) {
        // no crossing
        continue;
    }
    if(y1 < y2 && (py >= y1 && py < y2) || // upward
        y1 > y2 && (py < y1 && py >= y2)) { // downward
        ++crosses;
    }
}

var contained = (crosses == -1) ?
    // on edge
    1 :
    // even (out) or odd (in)
    !!(crosses & 1);

return contained;
}


function intersectsByLinearRings (LinearRing1, LinearRings2) {
var intersect = false;
var segs1 = getSortedSegments(LinearRing1);
var segs2 = getSortedSegments(LinearRings2);

var seg1, seg1x1, seg1x2, seg1y1, seg1y2,
    seg2, seg2y1, seg2y2;
// sweep right
outer: for(var i=0, len=segs1.length; i seg1x2) {
            // seg1 still left of seg2
            break;
        }
        if(seg2.x2 < seg1x1) {
            // seg2 still left of seg1
            continue;
        }
        seg2y1 = seg2.y1;
        seg2y2 = seg2.y2;
        if(Math.min(seg2y1, seg2y2) > Math.max(seg1y1, seg1y2)) {
            // seg2 above seg1
            continue;
        }
        if(Math.max(seg2y1, seg2y2) < Math.min(seg1y1, seg1y2)) {
            // seg2 below seg1
            continue;
        }
        if(segmentsIntersect(seg1, seg2)) {
            intersect = true;
            break outer;
        }
    }
}
return intersect;
}

function getSortedSegments(points) {
var numSeg = points.length - 1;
var segments = new Array(numSeg), point1, point2;
for(var i=0; i= 0 && along1 <= 1 && along2 >=0 && along2 <= 1) {
        // intersect
        if(!point) {
            intersection = true;
        } else {
            // calculate the intersection point
            var x = seg1.x1 + (along1 * x12_11);
            var y = seg1.y1 + (along1 * y12_11);
            intersection = { "x":x, "y":y };
        }
    }
}
if(tolerance) {
    var dist;
    if(intersection) {
        if(point) {
            var segs = [seg1, seg2];
            var seg, x, y;
            // check segment endpoints for proximity to intersection
            // set intersection to first endpoint within the tolerance
            outer: for(var i=0; i<2; ++i) {
                seg = segs[i];
                for(var j=1; j<3; ++j) {
                    x = seg["x" + j];
                    y = seg["y" + j];
                    dist = Math.sqrt(
                        Math.pow(x - intersection.x, 2) +
                        Math.pow(y - intersection.y, 2)
                    );
                    if(dist < tolerance) {
                        intersection.x = x;
                        intersection.y = y;
                        break outer;
                    }
                }
            }

        }
    } else {
        // no calculated intersection, but segments could be within
        // the tolerance of one another
        var segs = [seg1, seg2];
        var source, target, x, y, p, result;
        // check segment endpoints for proximity to intersection
        // set intersection to first endpoint within the tolerance
        outer: for(var i=0; i<2; ++i) {
            source = segs[i];
            target = segs[(i+1)%2];
            for(var j=1; j<3; ++j) {
                p = {x: source["x"+j], y: source["y"+j]};
                result = distanceToSegment(p, target);
                if(result.distance < tolerance) {
                    if(point) {
                        intersection = { "x":p.x, "y":p.y };
                    } else {
                        intersection = true;
                    }
                    break outer;
                }
            }
        }
    }
}
return intersection;
};

function distanceToSegment(point, segment) {
var result = distanceSquaredToSegment(point, segment);
result.distance = Math.sqrt(result.distance);
return result;
};

function distanceSquaredToSegment(point, segment) {
var x0 = point.x;
var y0 = point.y;
var x1 = segment.x1;
var y1 = segment.y1;
var x2 = segment.x2;
var y2 = segment.y2;
var dx = x2 - x1;
var dy = y2 - y1;
var along = ((dx * (x0 - x1)) + (dy * (y0 - y1))) /
    (Math.pow(dx, 2) + Math.pow(dy, 2));
var x, y;
if(along <= 0.0) {
    x = x1;
    y = y1;
} else if(along >= 1.0) {
    x = x2;
    y = y2;
} else {
    x = x1 + along * dx;
    y = y1 + along * dy;
}
return {
    distance: Math.pow(x - x0, 2) + Math.pow(y - y0, 2),
    x: x, y: y,
    along: along
};
}

return intersectsByPolygon(polygon1LinearRings, polygon2LinearRings);
}
//#endregion

function railsIsOverlap (rails) {
var i, j, k, v, l, n;
if (rails.length < 2) {
return false;
}
for (i = 0, j = rails.length - 1; i < j; i++) {
var rail = rails[i];
var railPath = rail.getPath();
for (k = i + 1, v = rails.length; k < v; k++) {
    var railed = rails[k];
    var railedPath = railed.getPath();
    for (l = 0 , n = railPath.length; l < n; l ++) {
        if (BMapLib.GeoUtils.isPointInPolygon(new BMap.Point(railPath[l].lng, railPath[l].lat), railed)) {
            layer.alert("片區(qū)不能重復(fù)");
            return true;
        }
    }
    for (l = 0, n =  railedPath.length; l < n; l ++) {
        if (BMapLib.GeoUtils.isPointInPolygon(new BMap.Point(railedPath[l].lng, railedPath[l].lat), rail)) {
            // console.log(53)
            layer.alert("片區(qū)不能重復(fù)");
            return true;
        }
    }
}
}

var lines = [];
for ( i = 0 ; i < rails.length; i ++) {
var line = rails[i].getPath();
lines.push([]);
for (j = 0 ;j < line.length ; j ++) {
    var p = {
        x: line[j].lng,
        y: line[j].lat
    };
    lines[i].push(p);
}

lines[i].push(lines[i][0])
}
for (i = 0; i < lines.length - 1; i ++) {
var p1 = lines[i];
for (j = i + 1; j < lines.length; j ++) {
    var p2 = lines[j];
    if (intersectsPolygonAndPolygon(p1,p2)) {
        layer.alert("片區(qū)不能重復(fù)!");
        return true;
    }
}
}
return false;
}

使用時直接調(diào)用railsIsOverlap函數(shù)就行,參數(shù)是多邊形數(shù)組

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/91305.html

相關(guān)文章

  • 百度地圖判斷邊形區(qū)域是否重復(fù)

    摘要:原文來自前端時間遇到個問題,就是判斷百度地圖里的多個任意多邊形區(qū)域是否重復(fù),在網(wǎng)上看了很多的文章都沒有找到解決方案,功夫不負(fù)有心人,在網(wǎng)上找到個可以判斷是否重復(fù)的,但是在包含的情況下就不能判斷,后來自己加入根據(jù)點(diǎn)判斷點(diǎn)是否在多邊形內(nèi)來判斷重 原文來自 taoeer.top 前端時間遇到個問題,就是判斷百度地圖里的多個任意多邊形區(qū)域是否重復(fù),在網(wǎng)上看了很多的文章都沒有找到解決方案,功夫不...

    gself 評論0 收藏0
  • 基于百度地圖區(qū)域重疊判斷

    摘要:原文鏈接前些日的一個小需求用戶在后臺劃不規(guī)則區(qū)域,區(qū)域之間不能重疊,如圖判斷分兩步判斷多變形是否有相交線段,無則進(jìn)行第二步判斷公式判斷多變形之間是否存在頂點(diǎn)與多邊形的包含關(guān)系代碼如下點(diǎn)線面線段是否相交判斷兩多 原文鏈接: Fyerls Blog 前些日的一個小需求:用戶在后臺劃不規(guī)則區(qū)域,區(qū)域之間不能重疊,如圖showImg(https://segmentfault.com/img/b...

    OBKoro1 評論0 收藏0
  • 仿鏈家地圖找房的簡單實(shí)現(xiàn)

    摘要:本篇目錄使用入門簡單使用流程鏈家地圖找房效果區(qū)域點(diǎn)位氣泡數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)方法區(qū)域邊界獲取區(qū)域點(diǎn)位經(jīng)緯度獲取區(qū)域邊界小結(jié)最近由于項目需要,開始調(diào)研如何使用百度地圖實(shí)現(xiàn)類似于鏈家的地圖找房的功能,從而開始學(xué)習(xí)百度地圖相關(guān)內(nèi)容。 本篇目錄: 使用入門 簡單使用流程 鏈家地圖找房效果 區(qū)域點(diǎn)位氣泡 數(shù)據(jù)結(jié)構(gòu) 實(shí)現(xiàn) addOverlay方法 區(qū)域邊界 獲取區(qū)域點(diǎn)位經(jīng)緯度 獲取區(qū)域邊...

    RancherLabs 評論0 收藏0
  • vue 地圖可視化(1)

    摘要:百度地圖百度地圖作為項目中地圖可視化最重要的一部分,不止其為國人自己的地圖,還因為其完善的技術(shù)文檔案例和多樣化的開源插件等上有的組件可以直接使用,有興趣的同學(xué)可以直接上手這里不采用已經(jīng)封裝好的地圖組件,而是從零開始,采用原生的百度地圖一 百度地圖 百度地圖作為項目中地圖可視化最重要的一部分,不止其為國人自己的地圖,還因為其完善的技術(shù)文檔案例和多樣化的開源插件(echarts、Mapv等...

    1treeS 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<