25#ifndef __SHAPE_SEGMENT_H
26#define __SHAPE_SEGMENT_H
28#include <geometry/seg.h>
29#include <geometry/shape.h>
31#include <math/vector2d.h>
35class SHAPE_SEGMENT :
public SHAPE
43 SHAPE_SEGMENT(
const VECTOR2I& aA,
const VECTOR2I& aB,
int aWidth = 0 ) :
49 SHAPE_SEGMENT(
const SEG& aSeg,
int aWidth = 0 ) :
59 return new SHAPE_SEGMENT( m_seg, m_width );
62 const BOX2I
BBox(
int aClearance = 0 )
const override
64 return BOX2I( m_seg.A, m_seg.B - m_seg.A ).
Inflate( aClearance + ( m_width + 1 ) / 2 );
67 bool Collide(
const SHAPE* aShape,
int aClearance, VECTOR2I* aMTV )
const override
72 bool Collide(
const SHAPE* aShape,
int aClearance = 0,
int* aActual =
nullptr,
73 VECTOR2I* aLocation =
nullptr )
const override
78 bool Collide(
const SEG& aSeg,
int aClearance = 0,
int* aActual =
nullptr,
79 VECTOR2I* aLocation =
nullptr )
const override
81 if( aSeg.A == aSeg.B )
82 return Collide( aSeg.A, aClearance, aActual, aLocation );
84 int min_dist = ( m_width + 1 ) / 2 + aClearance;
85 ecoord dist_sq = m_seg.SquaredDistance( aSeg );
87 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
90 *aLocation = m_seg.NearestPoint( aSeg );
93 *aActual = std::max( 0, (
int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
101 bool Collide(
const VECTOR2I& aP,
int aClearance = 0,
int* aActual =
nullptr,
102 VECTOR2I* aLocation =
nullptr )
const override
104 int min_dist = ( m_width + 1 ) / 2 + aClearance;
105 ecoord dist_sq = m_seg.SquaredDistance( aP );
107 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
110 *aLocation = m_seg.NearestPoint( aP );
113 *aActual = std::max( 0, (
int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
121 void SetSeg(
const SEG& aSeg )
126 const SEG& GetSeg()
const
131 void SetWidth(
int aWidth )
141 bool IsSolid()
const override
146 void Rotate(
double aAngle,
const VECTOR2I& aCenter = { 0, 0 } )
override
151 m_seg.A = m_seg.A.
Rotate( aAngle );
152 m_seg.B = m_seg.B.
Rotate( aAngle );
158 void Move(
const VECTOR2I& aVector )
override
164 virtual const std::string Format( )
const override;
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:281
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
Definition shape_segment.h:57
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
Definition shape_segment.h:78
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Definition shape_segment.h:146
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition shape_segment.h:62
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
Definition shape_segment.h:67
bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape_segment.h:101
An abstract shape on 2D plane.
Definition shape.h:122
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:170
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:132
VECTOR2< T > Rotate(double aAngle) const
Rotate the vector by a given angle.
Definition vector2d.h:365