查找落在填充画布弧内的 x/y 坐标

发布于 2024-12-05 18:12:15 字数 241 浏览 1 评论 0原文

这是 fiddle

我在圆的外侧绘制的弧 - 我想知道如何找到它们覆盖的所有 x/y 坐标,这样我就不必每次使用 isPointInPath() 重新绘制它们来确定鼠标光标是否位于它们上方。我正在考虑将所有 x/y 坐标写入一个数组,我可以检查鼠标位置 x/y 坐标,如果找到匹配项,则更改光标。问题是,我不知道导出所有 x/y 值的代码。

Here is the fiddle

The arcs I've drawn around the outside of the circle - I'd like to know how to find all of the x/y coordinates that they cover so that I don't have to re-draw them every time using isPointInPath() to determine whether the mouse cursor is over them or not. I was thinking about writing all of the x/y coordinates to an array that I could check against the mouse position x/y coordinates and if I find a match then I change the cursor. The problem is, I don't know the code to derive all of the x/y values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮了一弯 2024-12-12 18:12:15

实际上,您不必重新绘制弧线即可使用 .isPointInPath() - 只需省略对 .fill().Stroke()< 的任何调用即可/code> 你将有一个路径,你可以用它来测试它是否包含一个点。

我建议使用一个函数来概述弧路径(.beginPath()、路径命令、.closePath()),然后使用两个函数来调用它——其中一个函数是调用圆弧路径函数,然后设置填充样式并填充路径以绘制它,另一个调用圆弧路径函数,然后仅测试点是否在路径中。

You don't actually have to redraw your arcs to use .isPointInPath()-- just omit any calls to .fill() or .stroke() and you'll have a path which you can use to test whether it contains a point.

I would suggest having one function which outlines the arc path (.beginPath(), path commands, .closePath()) and then two functions which call it-- one which calls the arc path function, then sets the fill style and fills the path to draw it, and another which calls the arc path function and then just tests whether a point is in the path.

落墨 2024-12-12 18:12:15

这是您应该使用的方法:
http://en.wikipedia.org/wiki/Point_in_polygon

在此处输入图像描述

它的工作方式实际上非常简单:如果在任意点结束的光线穿过多边形周界的次数为甚至,相应的点必须位于多边形之外。如果是奇数,则它在多边形内。

这是 Pimvdb 找到的一个函数:

function isPointInPoly(poly, pt){
    for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
        ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
        && (c = !c);
    return c;
}

This is the method you should use:
http://en.wikipedia.org/wiki/Point_in_polygon

enter image description here

The way it works is actually extremely simple: if the amount of times a ray that ends at any point passes through the polygon perimeter is even, the respective point HAS to be outside of the polygon. If it's odd, it's within the polygon.

Here's a function found by Pimvdb:

function isPointInPoly(poly, pt){
    for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
        ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
        && (c = !c);
    return c;
}
木緿 2024-12-12 18:12:15

我不会将你所拥有的称为“弧”(它们更像是弓形矩形),但这里有一个如何编写函数来确定一个点是否在这样的东西内的大致草图:

  1. 计算圆的中心端点和半径。
    1. 如果该点比闭合弧更接近中心(到中心平方的距离大于闭合半径平方),则返回 false。
    2. 如果该点距离中心比远弧更远,则返回 false。
  2. 计算矩形端点相对于圆心的起始角度和终止角度。 (提示:Math.atan2
    1. 计算该点相对于圆心的角度。如果它不在端点的角度之间,则返回 false。
      • 当心跨越 Math.atan2 包装值的端点。
  3. 如果其他测试通过,则返回 true。

您无法计算该区域中的“所有”点,因为它们的数量是无限的。创建图像中所有整数像素坐标的查找表是可能的,但很浪费。

会做的,而不是你所建议的,是使用保留模式图形系统(提示:SVG)并让它使用其远距离跟踪鼠标事件。 -更高效的代码。

I wouldn't call what you have 'arcs' (they're more like bowed rectangles), but here's a broad sketch of how to write a function to determine if a point is within such a thing:

  1. Calculate the center of the circle from the end points and radius.
    1. If the point is closer to the center than the close arc (distance-to-center-squared is greater than close-radius-squared) then return false.
    2. If the point is farther from the center than the far arc then return false.
  2. Calculate the start and end angles for the endpoints of your rectangles with respect to the center of the circle. (Hint: Math.atan2)
    1. Calculate the angle for the point with respect to the center of the circle. If it is not between the angles for the end points, return false.
      • Beware endpoints that cross the wrapping values for Math.atan2.
  3. Return true if other tests passed.

You can't calculate "all" points in this region, as there an an infinite number of them. Creating a lookup table of all integer pixel coordinates in your image is possible, but wasteful.

What I would do, instead of what you are proposing, is use a retained-mode graphics system (hint: SVG) and let it track mouse events for me using its far-more-efficient code.

累赘 2024-12-12 18:12:15

这是一个更简单的方法:

我更改了drawtabs函数来检查鼠标是否在选项卡内:
http://jsfiddle.net/kkDqz/4/

警告

此方法很简单,但需要您在 mousemove 上重绘所有内容

Here's an easier method:

I've altered the drawtabs function to also check if mouse is within a tab:
http://jsfiddle.net/kkDqz/4/

WARNING

This method is easy, but requires you to redraw EVERYTHING on mousemove.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文