Javascript检查鼠标在圆形或多边形内单击 [英] Javascript check Mouse clicked inside the Circle or Polygon

查看:100
本文介绍了Javascript检查鼠标在圆形或多边形内单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何检查在圆圈或多边形内单击鼠标的方法。我的问题是我想检查一下,如果鼠标已经在圆圈或多边形内部被克隆了。圆或多边形坐标已存储在数组中。非常感谢任何帮助

Any one know how to check that wheter a mouse is clicked inside the circle or polygon. My problem is I want to check that if mouse has been clciked inside the circle or polygon. circle or polygon coordinates has been stored inside an array. Any help is really appreciated

推荐答案

正如其他一些答案,我按照一些链接和在这里找到了c代码
这是用于查找点是否在多边形中的JavaScript转换

As suggested by some other answers, I followed some links and found the c code here. Here is the JavaScript translation for finding whether a point is in a polygon


版权所有(c)1970-2003,Wm。 Randolph Franklin

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. 源代码的重新分发必须保留上述版权声明,此条件列表和以下免责声明。

  2. 二进制形式的再分发必须在随分发提供的文档和/或其他材料中复制上述版权声明。

  3. W. Randolph Franklin的名称可能不是用于在未经事先书面许可的情况下认可或推广源自本软件的产品。

本软件按原样提供,不提供任何形式的明示或暗示保证,包括但不限于以下保证:适销性,特定用途的适用性和不侵权。在任何情况下,作者或版权所有者均不对任何索赔,损害或其他责任承担任何责任,无论是在合同,侵权行为还是其他方面,由本软件引起或与之相关,或者与本软件的使用或其他交易有关。软件。

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 - 多边形中的顶点数。是否在末尾重复第一个顶点将在下面讨论。

vertx,verty - 包含多边形顶点的x坐标和y坐标的数组。

testx,testy - 测试点的X和y坐标。

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检查鼠标在圆形或多边形内单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆