Geometry 圆-矩形碰撞检测完成示例

Geometry 圆-矩形碰撞检测完成示例,geometry,collision-detection,Geometry,Collision Detection,我需要一个算法来检测一个圆是否碰到了一个正方形,我看到了这篇文章: 看起来我应该选择ShreevatsaR的答案,但我是一个数学傻瓜,我不知道如何完成这个算法。有谁能抽出时间给我举一个完整的例子吗?我已经在网上搜索过了,但还没有找到一个有效的例子 非常感谢 索伦 编辑: 好的,这是我的尝试。它不工作,它从未检测到任何碰撞 typedef struct { double x; double y; } point; typedef struct { point one;

我需要一个算法来检测一个圆是否碰到了一个正方形,我看到了这篇文章:

看起来我应该选择ShreevatsaR的答案,但我是一个数学傻瓜,我不知道如何完成这个算法。有谁能抽出时间给我举一个完整的例子吗?我已经在网上搜索过了,但还没有找到一个有效的例子

非常感谢
索伦

编辑:

好的,这是我的尝试。它不工作,它从未检测到任何碰撞

typedef struct {
    double x;
    double y;
} point;

typedef struct {
    point one;
    point two;
} segment;

typedef struct { 
    point center;
    double radius;
} circle;

typedef struct {
    point p;
    int width;
    int height;
    point a;
    point b;
    point c;
    point d;
} rectangle;

double slope(point one, point two) {
    return (double)(one.y-two.y)/(one.x-two.x);
}

double distance(point p, segment s) {
    // Line one is the original line that was specified, and line two is 
    // the line we're constructing that runs through the specified point, 
    // at a right angle to line one.
    //

    // if it's a vertical line return the horizontal distance
    if ( s.one.x == s.two.x)    
        return fabs(s.one.x - p.x);

    // if it's a horizontal line return the vertical distance
    if ( s.one.y == s.two.y )  
        return fabs(s.one.y - p.y); 

    // otherwise, find the slope of the line
    double m_one = slope(s.one, s.two); 

    // the other slope is at a right angle.
    double m_two = -1.0 / m_one;  

    // find the y-intercepts.
    double b_one = s.one.y - s.one.x * m_one; 
    double b_two = p.y - p.x * m_two;

    // find the point of intersection
    double x = (b_two - b_one) / (m_one - m_two); 
    double y = m_one * x + b_one;

    // find the x and y distances
    double x_dist = x - p.x;  
    double y_dist = y - p.y;

    // and return the total distance.
    return sqrt(x_dist * x_dist + y_dist * y_dist); 
}

bool intersectsCircle(segment s, circle c) { 
    return distance(c.center, s) <= c.radius;
}

bool pointInRectangle(point p, rectangle r)
{
    float right = r.p.x + r.width;
    float left = r.p.x - r.width;
    float top = r.p.y + r.height;
    float bottom = r.p.y - r.height;
    return ((left <= p.x && p.x <= right) && (top <= p.y && p.y <= bottom));
}

bool intersect(circle c, rectangle r) {
    segment ab;
    ab.one = r.a;
    ab.two = r.b;
    segment bc;
    ab.one = r.b;
    ab.two = r.c;
    segment cd;
    ab.one = r.c;
    ab.two = r.d;
    segment da;
    ab.one = r.d;
    ab.two = r.a;
    return pointInRectangle(c.center, r) ||
                            intersectsCircle(ab, c) ||
                            intersectsCircle(bc, c) ||
                            intersectsCircle(cd, c) ||
                            intersectsCircle(da, c);
}
typedef结构{
双x;
双y;
}点;
类型定义结构{
第一点;
第二点;
}段;
类型定义结构{
点中心;
双半径;
}圆圈;
类型定义结构{
p点;
整数宽度;
内部高度;
a点;
b点;
c点;
d点;
}矩形;
双坡度(点一、点二){
返回(双)(1.y-2.y)/(1.x-2.x);
}
双距离(点p,段s){
//第一行是指定的原始行,第二行是
//我们正在构造的穿过指定点的线,
//与第一行成直角。
//
//如果是垂直线,返回水平距离
如果(s.one.x==s.two.x)
返回晶圆厂(s.one.x-p.x);
//如果是水平线,返回垂直距离
如果(s.1.y==s.2.y)
返回晶圆厂(s.one.y-p.y);
//否则,请查找直线的坡度
双m_1=斜率(s.1,s.2);
//另一个坡度为直角。
双m_2=-1.0/m_1;
//找到y形截距。
双倍b_-one=s.one.y-s.one.x*m_-one;
双b_-two=p.y-p.x*m_-two;
//找到交点
双x=(b_2-b_1)/(m_1-m_2);
双y=m_1*x+b_1;
//求x和y距离
双x_dist=x-p.x;
双y_dist=y-p.y;
//并返回总距离。
返回sqrt(x_dist*x_dist+y_dist*y_dist);
}
布尔交叉圆(s段,c圆){

返回距离(c.center,s)他似乎留下的主要部分是兴趣圆(line,circle)

#包括
类型定义结构{
双x;
双y;
}点;
类型定义结构{
第一点;
第二点;
}段;
类型定义结构{
点中心;
双半径;
}圆圈;
双坡度(点和一、点和二){
返回(双)(1.y-2.y)/(1.x-2.x);
}
双距离(点和点、段和s){
//第一行是指定的原始行,第二行是
//我们正在构造的穿过指定点的线,
//与第一行成直角。
//
//如果是垂直线,返回水平距离
如果(s.one.x==s.two.x)
返回晶圆厂(s.one.x-p.x);
//如果是水平线,返回垂直距离
如果(s.1.y==s.2.y)
返回晶圆厂(s.one.y-p.y);
//否则,请查找直线的坡度
双m_1=斜率(s.1,s.2);
//另一个坡度为直角。
双m_2=-1.0/m_1;
//找到y形截距。
双倍b_-one=s.one.y-s.one.x*m_-one;
双b_-two=p.y-p.x*m_-two;
//找到交点
双x=(b_2-b_1)/(m_1-m_2);
双y=m_1*x+b_1;
//求x和y距离
双x_dist=x-p.x;
双y_dist=y-p.y;
//并返回总距离。
返回sqrt(x_dist*x_dist+y_dist*y_dist);
}
布尔交叉圆(s段,c圆){

返回距离(圆心,s)我有(轻模板)这应该做这些相交测试,但我还没有时间来测试它们。特别是,我有线段圆相交测试和平行四边形圆相交测试,它应该计算相交面积和相交点。同样,在撰写此评论时,这是完全未经测试的,所以您将需要d测试/调整它们以满足您的需要。

在您链接的问题中,有一个完整的圆/矩形碰撞检测实现。除此之外,我怀疑任何人都愿意提供帮助,除非您认真努力并展示您的尝试。非常感谢:)我正试图将此纳入我的代码中,并实现缺失的部分“pointInRectangle”函数,但我不理解“pointInRectangle”的描述:0≤ AP·AB≤ AB·AB与0≤ AP·AD≤ AD·AD我猜“AP”是“矩形a到p”的直线,对吗?但是我怎么能把一个由两个变量组成的点乘以任何东西呢?谢谢你嗨,杰瑞,你有时间看看我的尝试并帮我吗?
#include <math.h>

typedef struct {
    double x;
    double y;
} point;

typedef struct {
    point one;
    point two;
} segment;

typedef struct { 
    point center;
    double radius;
} circle;

double slope(point &one, point &two) {
    return (double)(one.y-two.y)/(one.x-two.x);
}

double distance(point &p, segment &s) {
// Line one is the original line that was specified, and line two is 
// the line we're constructing that runs through the specified point, 
// at a right angle to line one.
//

    // if it's a vertical line return the horizontal distance
    if ( s.one.x == s.two.x)    
      return fabs(s.one.x - p.x);

    // if it's a horizontal line return the vertical distance
    if ( s.one.y == s.two.y )  
      return fabs(s.one.y - p.y); 

    // otherwise, find the slope of the line
    double m_one = slope(s.one, s.two); 

    // the other slope is at a right angle.
    double m_two = -1.0 / m_one;  

    // find the y-intercepts.
    double b_one = s.one.y - s.one.x * m_one; 
    double b_two = p.y - p.x * m_two;

    // find the point of intersection
    double x = (b_two - b_one) / (m_one - m_two); 
    double y = m_one * x + b_one;

    // find the x and y distances
    double x_dist = x - p.x;  
    double y_dist = y - p.y;

    // and return the total distance.
    return sqrt(x_dist * x_dist + y_dist * y_dist); 
}

bool IntersectsCircle(segment s, circle c) { 
    return distance(circle.center, s) <= circle.radius;
}