检查点是否在矩形的给定距离内?

发布于 2024-10-28 08:17:19 字数 61 浏览 1 评论 0原文

我写了一个布尔值来检查一个点是否在填充矩形的给定距离内,

该矩形由其左下点及其宽度和高度定义

i write a boolean that checks to see if a point is within a given distance of a filled rectangle

the rectangle is defined by its bottom left point and its width and height

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

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

发布评论

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

评论(3

〆一缕阳光ご 2024-11-04 08:17:19

这是作业吗?反正。
假设您的意思是适当的距离,如“距离矩形最近的点之间的距离”:

int IsWithinDistance(int pointX, int pointY, int rectX, int rectY, int rectWidth, int rectHeight, int distanceThreshold)
{
    int x2 = rectX + rectWidth;
    int y2 = rectY + rectHeight;
    int xDiff = (pointX < rectX) ? rectX - pointX : pointX - x2;
    int yDiff = (pointY < rectY) ? rectY - pointY : pointY - y2;
    int distance2;

    xDiff = (xDiff < 0) ? 0 : xDiff;
    yDiff = (yDiff < 0) ? 0 : yDiff;

    distance2 = xDiff * xDiff + yDiff * yDiff;

    return distance2 < (distanceThreshold * distanceThreshold);
}

Is this homework? Anyway.
Assuming you mean proper distance, as in "distance between the closest point to the rectangle":

int IsWithinDistance(int pointX, int pointY, int rectX, int rectY, int rectWidth, int rectHeight, int distanceThreshold)
{
    int x2 = rectX + rectWidth;
    int y2 = rectY + rectHeight;
    int xDiff = (pointX < rectX) ? rectX - pointX : pointX - x2;
    int yDiff = (pointY < rectY) ? rectY - pointY : pointY - y2;
    int distance2;

    xDiff = (xDiff < 0) ? 0 : xDiff;
    yDiff = (yDiff < 0) ? 0 : yDiff;

    distance2 = xDiff * xDiff + yDiff * yDiff;

    return distance2 < (distanceThreshold * distanceThreshold);
}
孤檠 2024-11-04 08:17:19

要查找任意两点之间的距离,您可以使用它:

CGFloat distanceBetweenPoints(CGPoint pt1, CGPoint pt2)
{    
    CGFloat dx = pt2.x - pt1.x;    
    CGFloat dy = pt2.y - pt1.y;    
    return sqrt(dx*dx + dy*dy);
}

您可以使用它来查找到矩形中心或到另一个点(如果您愿意)的距离。

CGFloat distanceToRect = distanceBetweenPoints( aPoint, aRect.center );

To find the distance between any two points you can use this:

CGFloat distanceBetweenPoints(CGPoint pt1, CGPoint pt2)
{    
    CGFloat dx = pt2.x - pt1.x;    
    CGFloat dy = pt2.y - pt1.y;    
    return sqrt(dx*dx + dy*dy);
}

You could use this to find the distance to the center of the rectangle or to another point if you prefer.

CGFloat distanceToRect = distanceBetweenPoints( aPoint, aRect.center );
空城旧梦 2024-11-04 08:17:19

我的方法是这样的。 (这假设 y 随着你的上升而增加。)

BOOL IsWithinDistance(POINT pt, RECT rc, int distance)
{
    return (pt.x > (rc.left - distance) &&
        pt.x < (rc.right + rc.width + distance) &&
        pt.y > (rc.bottom - distance) &&
        pt.y < (rc.bottom + rc.height + distance));
}

My approach would be something like this. (This assumes y increases as you go up.)

BOOL IsWithinDistance(POINT pt, RECT rc, int distance)
{
    return (pt.x > (rc.left - distance) &&
        pt.x < (rc.right + rc.width + distance) &&
        pt.y > (rc.bottom - distance) &&
        pt.y < (rc.bottom + rc.height + distance));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文