38 const double dr =
pitch.x;
39 const double dPhi =
pitch.y;
40 const double rMax =
extent.x;
41 const double phiMax =
extent.y;
43 if( dr <= 0.0 || dPhi <= 0.0 )
46 const double r = std::hypot( local.
x, local.
y );
47 const int maxRIdx =
static_cast<int>( rMax / dr );
48 const int rIdx = std::clamp(
static_cast<int>( std::round( r / dr ) ), 0, maxRIdx );
52 double phi = std::atan2( local.
y, local.
x );
57 const int maxPIdx =
static_cast<int>( phiMax / dPhi );
58 const int pIdx = std::clamp(
static_cast<int>( std::round( phi / dPhi ) ), 0, maxPIdx );
60 const double phiQ = pIdx * dPhi;
61 const double rSnapped = rIdx * dr;
63 candidate =
VECTOR2D( rSnapped * std::cos( phiQ ), rSnapped * std::sin( phiQ ) );
69 const double dx =
pitch.x;
70 const double dy =
pitch.y;
72 if( dx <= 0.0 || dy <= 0.0 )
76 const int nX =
static_cast<int>(
extent.x / dx );
77 const int nY =
static_cast<int>(
extent.y / dy );
79 const int xIdx = std::clamp(
static_cast<int>( std::round( local.
x / dx ) ), -nX, nX );
80 const int yIdx = std::clamp(
static_cast<int>( std::round( local.
y / dy ) ), -nY, nY );
82 candidate =
VECTOR2D( xIdx * dx, yIdx * dy );
86 default: wxFAIL_MSG( wxT(
"GRID_GEOMETRY::Snap: unhandled KIND" ) );
return aPoint;
101 const double r = std::hypot( local.
x, local.
y );
103 if( r >
extent.x + aTolerance )
106 const double phiMax =
extent.y;
107 double phi = std::atan2( local.
y, local.
x );
116 return local.
x >= -
extent.x - aTolerance && local.
x <=
extent.x + aTolerance
117 && local.
y >= -
extent.y - aTolerance && local.
y <=
extent.y + aTolerance;
119 default: wxFAIL_MSG( wxT(
"GRID_GEOMETRY::Contains: unhandled KIND" ) );
return false;
136 default: wxFAIL_MSG( wxT(
"GRID_GEOMETRY::Area: unhandled KIND" ) );
return 0.0;
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
double orientation
Rotation about origin, radians CCW.
VECTOR2D extent
Cartesian: (dx, dy) = size/2 from origin.
VECTOR2D pitch
Cartesian: (dx, dy); polar: (dr, dPhi rad).
bool Contains(const VECTOR2D &aPoint, double aTolerance=0.0) const
VECTOR2D Snap(const VECTOR2D &aPoint) const
Snap a point to the nearest on-grid position.
VECTOR2I GetRotated(const VECTOR2I &aVector, const EDA_ANGLE &aAngle)
Return a new VECTOR2I that is the result of rotating aVector by aAngle.
VECTOR2< double > VECTOR2D