d3.polygonContains 总是 returns 错误
d3.polygonContains always returns false
在我的 react application
中,我使用一些点绘制了一个 polygon
,然后我试图找出鼠标当前位置是否在多边形内。我正在使用 d3.polygonContains 并向其传递包含当前位置点的点数组,但 它总是 returns false,尽管点在多边形内。
这是一个例子;
let points = [
[ 42.34624, -71.06024 ],
[ 42.33558, -71.06616 ],
[ 42.32632, -71.05835 ],
[ 42.32987, -71.05428 ],
[ 42.34732, -71.05432 ],
[ 42.34618, -71.05973 ],
[ 42.34624, -71.06024 ]
];
let testPoint = [
[42.33288, -71.05835]
];
alert(d3.polygonContains(points, testPoint));
alert(d3.polygonContains(points, (42.33288, -71.05835)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
谁能告诉我我做错了什么??
testPoints
本身必须是一个包含 2 个元素的简单数组,按顺序排列 x
和 y
位置,而不是具有内部数组的数组:
let testPoint = [42.33288, -71.05835];
不幸的是 docs 没有明确说明这一点,它只是说:
d3.polygonContains(polygon, point)
Returns true if and only if the specified point is inside the specified polygon.
但总能检查 source code:
export default function(polygon, point) {
var n = polygon.length,
p = polygon[n - 1],
x = point[0], y = point[1],
//^-- here we see that 'point' is an array with 2 elements
这是进行了更改的代码:
let points = [
[42.34624, -71.06024],
[42.33558, -71.06616],
[42.32632, -71.05835],
[42.32987, -71.05428],
[42.34732, -71.05432],
[42.34618, -71.05973],
[42.34624, -71.06024]
];
let testPoint = [42.33288, -71.05835];
console.log(d3.polygonContains(points, testPoint));
console.log(d3.polygonContains(points, [42.33288, -71.05835]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
在我的 react application
中,我使用一些点绘制了一个 polygon
,然后我试图找出鼠标当前位置是否在多边形内。我正在使用 d3.polygonContains 并向其传递包含当前位置点的点数组,但 它总是 returns false,尽管点在多边形内。
这是一个例子;
let points = [
[ 42.34624, -71.06024 ],
[ 42.33558, -71.06616 ],
[ 42.32632, -71.05835 ],
[ 42.32987, -71.05428 ],
[ 42.34732, -71.05432 ],
[ 42.34618, -71.05973 ],
[ 42.34624, -71.06024 ]
];
let testPoint = [
[42.33288, -71.05835]
];
alert(d3.polygonContains(points, testPoint));
alert(d3.polygonContains(points, (42.33288, -71.05835)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
谁能告诉我我做错了什么??
testPoints
本身必须是一个包含 2 个元素的简单数组,按顺序排列 x
和 y
位置,而不是具有内部数组的数组:
let testPoint = [42.33288, -71.05835];
不幸的是 docs 没有明确说明这一点,它只是说:
d3.polygonContains(polygon, point)
Returns true if and only if the specified point is inside the specified polygon.
但总能检查 source code:
export default function(polygon, point) {
var n = polygon.length,
p = polygon[n - 1],
x = point[0], y = point[1],
//^-- here we see that 'point' is an array with 2 elements
这是进行了更改的代码:
let points = [
[42.34624, -71.06024],
[42.33558, -71.06616],
[42.32632, -71.05835],
[42.32987, -71.05428],
[42.34732, -71.05432],
[42.34618, -71.05973],
[42.34624, -71.06024]
];
let testPoint = [42.33288, -71.05835];
console.log(d3.polygonContains(points, testPoint));
console.log(d3.polygonContains(points, [42.33288, -71.05835]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>