Javascript 检查鼠标在圆或多边形内单击

标签 javascript

任何人都知道如何检查是否在圆或多边形内单击了鼠标。我的问题是我想检查鼠标是否在圆圈或多边形内被点击。圆或多边形坐标已存储在数组中。非常感谢任何帮助

最佳答案

根据 some other answers 的建议, 我点击了一些链接和 found the c code here . 这是用于查找点是否在多边形中的 JavaScript 翻译

Copyright (c) 1970-2003, Wm. Randolph Franklin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.
  2. Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution.
  3. The name of W. Randolph Franklin may not be used to endorse or promote products derived from this Software without specific prior written permission.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function pnpoly( nvert, vertx, verty, testx, testy ) {
    var i, j, c = false;
    for( i = 0, j = nvert-1; i < nvert; j = i++ ) {
        if( ( ( verty[i] > testy ) != ( verty[j] > testy ) ) &&
            ( testx < ( vertx[j] - vertx[i] ) * ( testy - verty[i] ) / ( verty[j] - verty[i] ) + vertx[i] ) ) {
                c = !c;
        }
    }
    return c;
}

nvert - Number of vertices in the polygon. Whether to repeat the first vertex at the end is discussed below.
vertx, verty - Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy - X- and y-coordinate of the test point.

关于Javascript 检查鼠标在圆或多边形内单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212604/

相关文章:

javascript - 如何在提交前检查 SQL 数据库的输入值?

javascript - 如何在同一网页上使用多个具有不同图像的引导巨型屏幕?

javascript - Backbone Js - 解析数据问题

javascript - 点击图像

javascript - 访问 json 对象中的元素

javascript - 如何在不刷新 map 的情况下更新 gmaps 中的标记?

javascript - CodeMirror 在编辑器文本区域上使用 jQuery .keyup

javascript - 如何将 td 插入数组 es6 Reactjs

javascript - 在 div 中预览多个图像时出错

javascript - 如何从 javascript 字符串数组中解析 csv 数据以在 d3 图中使用