码迷,mamicode.com
首页 > Web开发 > 详细

js 判断一个点是否在一个多边形之内

时间:2016-11-08 11:36:32      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:length   style   cti   point   htm   bst   git   code   www   

出处: https://github.com/substack/point-in-polygon/blob/master/index.js

github: https://github.com/substack/point-in-polygon

module.exports = function (point, vs) {
    // ray-casting algorithm based on
    // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
    
    var x = point[0], y = point[1];
    
    var inside = false;
    for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
        var xi = vs[i][0], yi = vs[i][1];
        var xj = vs[j][0], yj = vs[j][1];
        
        var intersect = ((yi > y) != (yj > y))
            && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
        if (intersect) inside = !inside;
    }
    
    return inside;
};
//example:
var
inside = require(‘point-in-polygon‘); var polygon = [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ] ]; //左上, 右上, 右下, 左下 console.dir([ inside([ 1.5, 1.5 ], polygon), inside([ 4.9, 1.2 ], polygon), inside([ 1.8, 1.1 ], polygon) ]);

判断一个点是否在一个圆这内:

function pointInsideCircle(point, circle, r) {
    if (r===0) return false
    var dx = circle[0] - point[0]
    var dy = circle[1] - point[1]
    return dx * dx + dy * dy <= r * r
}

 

js 判断一个点是否在一个多边形之内

标签:length   style   cti   point   htm   bst   git   code   www   

原文地址:http://www.cnblogs.com/zlog/p/6042270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!