bool Rectangle::Intersects(const Circle& circle) const { double radius = circle.GetRadius(); auto cp = circle.GetPosition(); auto t = GetTop(); double distTop = Math::GetDistance(cp, t.GetPointOne(), t.GetPointTwo()); auto l = GetLeft(); double distLeft = Math::GetDistance(cp, l.GetPointOne(), l.GetPointTwo()); auto r = GetRight(); double distRight = Math::GetDistance(cp, r.GetPointOne(), r.GetPointTwo()); auto b = GetBottom(); double distBottom = Math::GetDistance(cp, b.GetPointOne(), b.GetPointTwo()); bool resultTop = distTop <= radius; bool resultLeft = distLeft <= radius; bool resultRight = distRight <= radius; bool resultBottom = distBottom <= radius; bool isInside = Intersects(Point(circle.GetPosition())); return (isInside || resultTop || resultLeft || resultRight || resultBottom); }
bool Line::Intersects(const Circle& circle) const { double r2 = circle.GetRadius(); r2 *= r2; bool eOne_result = ((_extent_one - circle.GetPosition()).GetLengthSquared() < r2); bool eTwo_result = ((_extent_two - circle.GetPosition()).GetLengthSquared() < r2); if(eOne_result) return true; if(eTwo_result) return true; return this->GetDistance(Point(circle.GetPosition())) <= circle.GetRadius(); }
bool Sector::Intersects(const Circle& circle) const { //Intersections performed fast to slow. //FAST: //If bounding circle and argument circle do not intersect: no. //If initial point intersects circle: yes. //If terminal point intersects circle: yes. //If central point intersects circle: yes. //SLOW: //If the angle between the circle and the sector is in-between the start and end angles (mapped to [0, 2pi]): yes. //If the circle intersects the initial line: yes. //If the circle intersects the terminal line: yes. //Otherwise: no. double my_x = this->GetX(); double my_y = this->GetY(); double your_x = circle.GetX(); double your_y = circle.GetY(); double radius_sum = this->GetRadius() + circle.GetRadius(); if(a2de::Point::GetDistance(my_x, my_y, your_x, your_y) > radius_sum) return false; if(this->GetStartPoint().Intersects(circle)) return true; if(this->GetEndPoint().Intersects(circle)) return true; if(Point(this->GetPosition()).Intersects(circle)) return true; double angle = Vector2D::GetFacingAngle(circle.GetPosition(), this->GetPosition()); //Map return values to [0, 360] double sA = this->GetStartAngle(); if(sA > Math::A2DE_PI) { sA -= Math::A2DE_2PI; } double eA = this->GetEndAngle(); if(eA > Math::A2DE_PI) { eA -= Math::A2DE_2PI; } bool within_sector = (sA <= angle && angle <= eA); if(within_sector) return true; Line sl(this->GetPosition(), this->GetStartPoint().GetPosition()); Line tl(this->GetPosition(), this->GetEndPoint().GetPosition()); if(sl.Intersects(circle)) return true; if(tl.Intersects(circle)) return true; return false; }