圆 - 轴细长矩形交点

时间:2015-03-11 12:20:32

标签: c++ geometry

我想创建一个带矩形的函数,圆圈返回一个布尔值,看它们是否相交。这样做最有效,最简单的方法是什么?该函数看起来像这样:

bool intersect(rectX, rectY, rectWidth, rectHeight, circleX, circleY, radius) 
{
    bool intersect;
    //code I need
    return intersect;
}

Please help me find the code I need. Thanks!

1 个答案:

答案 0 :(得分:0)

你的答案终于,但我也想通知你/未来的观众,以下内容。

当您通过one point AND height-width表示一个矩形而不是轴系统上的特定矩形时,矩形的一般形式可以在任何地方绘制。

所以,在你的情况下:矩形可以在任何方向绘制(使其远离圆形,例如:UP,LEFT,RIGHT,BOTTOM等边到rectX,rectY)。这就是为什么这两种可能性产生的原因,你的可能性取决于你需要的东西:

无论您如何绘制矩形

[A] 100%保证交叉

[b]至少有一种方法可以绘制矩形,使其与圆相交

案例A:

bool assuredIntersect(rectX, rectY, rectWidth, rectHeight, circleX, circleY, radius){
    bool intersect;
    float distance=((rectX-circleX)^2+(rectY-circleY)^2)^0.5;
    intersect=(radious>=distance);
    return intersect;
}

案例B:

bool canIntersect(rectX, rectY, rectWidth, rectHeight, circleX, circleY, radius){
    bool intersect;
    float distance=((rectX-circleX)^2+(rectY-circleY)^2)^0.5;
    float diagonal=((rectX+rectHeight)^2+(rectY-rectWidth)^2)^0.5;
    intersect=((radious+diagonal)<distance);
    return intersect;
}

但是,如果您通过两点一对(rectX1,rectY1 AND rectX2,rectY2 AND height or width)表示矩形。然后你可以专门用来表示一个矩形。

注意:如果矩形方向是固定的(如短边(rectX,rectY)与x轴垂直或成角度)则为矩形变得具体,因为我们可以计算(rectX2,rectY2)。例如:如果angle是90,那么第二个点将是(rectX + rectHeight,rectY + rectWidth)。

如果我们有这样的函数参数:

#include <math.h>
#define PI 3.14159265

bool intersect(rX1, rY1, rX2, rY2, rAngle, cX, cY, cR){
//can be `intersect(rX1,rY1,rH,rW,rAngle, cX,cY, cR)`, and calculate rX2,rY2
//can be `intersect(rX1,rY1,rX2,rY2,rH, cX,cY, cR)`, and calculate rAngle
    bool intersect;
    //assume (rX1,rY1) as origin AND rectangle`s-side attached to this point is on both axis,
    //THEN we need to recalculate coordinates according to this assumption

    rAngle=rAngle*PI/180; //angle in radian
    //NOTE: if in place of rAngle, rHeight or rWidth is given then you can calculate rAngle by trigonometry.

    //moving rX1,xY1 to (0,0)
    cX=cX-rX1; cY=cY-rY1;
    rX2-=rX1; rY2-=rY1; rX1=rY1=0;

    //rotating axis, rectangle, circle...
    float cosA=cos(rAngle), sinA=sin(rAngle);
    float tempX= cosA*rX2 + sinA*rY2;
    float tempY= sinA*rX2 + cosA*rY2;
    rX2=tempX; rY2=tempY;
    tempX=cosA*cX + sinA*cY;
    tempY=sinA*cX + cosA*cY;
    cX=tempX; cY=tempY;

    rX1-=cR;rY1-=cR; //enlarging(creating) virtual rectangle around original; After this...
    rX2+=cR;rY2+=cR; //...if circle centre is inside this rectangle it will intersect with original rectangle
    intersect=(cX<=rX2 && cX>=rX1 && cY<=rY2 && cY>=rY1);
    return intersect;
}

point(x,y) rotation

所以,如果你不知道角度那么你可以考虑前两种情况。


[ANSWER]如果矩形是轴对齐的,那么函数将是:

bool intersect(rectX, rectY, rectWidth, rectHeight, circleX, circleY, radius){
    bool intersect;

    //calculating rX2,xY2
    rX2=rectX + rectWidth; rY2=rectY + rectHeight;

    rectX-=radius;rectY-=radius; //enlarging(creating) virtual rectangle around original; After this...
    rX2+=radius;rY2+=radius; //...if circle centre is inside this rectangle it will intersect with original rectangle ...
    intersect=(circleX<=rX2 && circleX>=rectX && circleY<=rY2 && circleY>=rectY);
    return intersect;
}