bool Segment::intersects(Segment const& other) const { Vec2D selfDelta = delta(); Vec2D otherDelta = other.delta(); if(selfDelta.cross(otherDelta) != 0) { float t = other.a.subtract(a).cross(otherDelta) / selfDelta.cross(otherDelta); float u = other.a.subtract(a).cross(selfDelta) / selfDelta.cross(otherDelta); if(t >= 0 && t <= 1 && u >= 0 && u <= 1) { return true; } } return false; }
Vec2D Segment::intersectionPoint(Segment const& other) const { Vec2D selfDelta = delta(); Vec2D otherDelta = other.delta(); if(selfDelta.cross(otherDelta) != 0) { float t = other.a.subtract(a).cross(otherDelta) / selfDelta.cross(otherDelta); float u = other.a.subtract(a).cross(selfDelta) / selfDelta.cross(otherDelta); if(t >= 0 && t <= 1 && u >= 0 && u <= 1) { return a.add(selfDelta.scale(t)); } } return Vec2D(0, 0); }
bool circleLineIntersect(Vec2D const& p, float const r, Vec2D const& l1, Vec2D const& l2, float const lr) { Vec2D ld = l2 - l1; Vec2D ln = ld.normal(); Vec2D l1p = p - l1; Vec2D l2p = p - l2; if(ln.cross(l1p) * ln.cross(l2p) < 0) { return l1p.projectioni(ln).lengthSquared() <= (r + lr) * (r + lr); } else if(l1p.lengthSquared() < l2p.lengthSquared()) { return l1p.lengthSquared() <= (r + lr) * (r + lr); } else { return l2p.lengthSquared() <= (r + lr) * (r + lr); } }