bool DoesLineAndBoxCollide( const CVector2< PointType >& p1, const CVector2< PointType >& p2, const CVector2< PointType >& rect_low, const CVector2< PointType >& rect_high ) { if( IsPointInsideRect( p1, rect_low, rect_high ) ) return true; if( IsPointInsideRect( p2, rect_low, rect_high ) ) return true; CVector2< PointType > result; typedef CVector2< PointType > vec2; if( LineIntersection( p1, p2, rect_low, vec2( rect_low.x, rect_high.y ), result ) ) return true; if( LineIntersection( p1, p2, vec2( rect_low.x, rect_high.y ), rect_high, result ) ) return true; if( LineIntersection( p1, p2, rect_high, vec2( rect_high.x, rect_low.x ), result ) ) return true; if( LineIntersection( p1, p2, vec2( rect_high.x, rect_low.x ), rect_low, result ) ) return true; return false; }
bool CIRect::Intersect(const CIPoint& C, const CIPoint& D) // line intersection { if (Sector((int)A.x, (int)A.y)&Sector((int)B.x, (int)B.y)) return false; CXPoint X; if (LineIntersection(C,D, CIPoint(A.x, DBL_MIN), CIPoint(A.x, DBL_MAX), &X)) return true; if (LineIntersection(C,D, CIPoint(B.x, DBL_MIN), CIPoint(B.x, DBL_MAX), &X)) return true; if (LineIntersection(C,D, CIPoint(DBL_MIN, A.y), CIPoint(DBL_MAX, A.y), &X)) return true; if (LineIntersection(C,D, CIPoint(DBL_MIN, B.y), CIPoint(DBL_MAX, B.y), &X)) return true; return false; }
/* Returns true if the given point is in the given domain */ static int PointInDomain(struct Domain *d, int x, int y) { int i, xi; int intersections = 0; /* Point-in-polygon algorithm. We check for intersection with a horizontal line going through (x, y). */ for (i = 0; i < d->num_borders; ++i) { if (LineIntersection(d->borders[i], &xi, y) && xi < x) { ++intersections; } } /* Odd number of intersections to the left of (x, y) implies the point is inside the domain. */ return (intersections % 2) == 1; }
bool IsSegmentIntersect(float Ax, float Ay, float Bx, float By, float Cx, float Cy, float Dx, float Dy, bool TestSegEnd) { if (!IsLineIntersect(Ax, Ay, Bx, By, Cx, Cy, Dx, Dy)) { return false; } Coord pos = LineIntersection(Ax, Ay, Bx, By, Cx, Cy, Dx, Dy); pos = Coord(round(pos.x),round(pos.y)); bool res = false; if (Bx >= Ax) { res= (pos.x <= Bx) && (pos.x >= Ax); } else { res = (pos.x <= Ax) && (pos.x >= Bx); } if (By >= Ay) { res= res && (pos.y <= By) && (pos.y >= Ay); } else { res = res && (pos.y <= Ay) && (pos.y >= By); } if (Dx >= Cx) { res = res && (pos.x <= Dx) && (pos.x >= Cx); } else { res = res && (pos.x <= Cx) && (pos.x >= Dx); } if (Dy >= Cy) { res = res && (pos.y <= Dy) && (pos.y >= Cy); } else { res = res && (pos.y <= Cy) && (pos.y >= Dy); } if (!TestSegEnd) { // Prise de marge pour absorber les arrondis if (Distance(pos, Coord(Ax,Ay)) <= 1 || Distance(pos, Coord(Bx,By)) <= 1 || Distance(pos, Coord(Cx,Cy)) <= 1 || Distance(pos, Coord(Dx,Dy)) <= 1) res = false; } return res; }
Coord LineIntersection(Coord A, Coord B, Coord C, Coord D) { return LineIntersection(A.x, A.y, B.x, B.y, C.x, C.y, D.x, D.y); }