Browse Source

Trigo: Add a GetRotated function to get a rotated VECTOR2I

This makes it easier to use value semantics and consts when
constructing geometry.
jobs
John Beard 1 year ago
parent
commit
72e48aed52
  1. 30
      libs/kimath/include/trigo.h
  2. 17
      pcbnew/tools/pcb_point_editor.cpp

30
libs/kimath/include/trigo.h

@ -63,20 +63,46 @@ bool SegmentIntersectsSegment( const VECTOR2I& a_p1_l1, const VECTOR2I& a_p2_l1,
*/
void RotatePoint( int *pX, int *pY, const EDA_ANGLE& aAngle );
/**
* Rotate a VECTOR2I in place by aAngle.
*/
inline void RotatePoint( VECTOR2I& point, const EDA_ANGLE& aAngle )
{
RotatePoint( &point.x, &point.y, aAngle );
}
/**
* Return a new VECTOR2I that is the result of rotating aVector by aAngle.
*/
inline VECTOR2I GetRotated( const VECTOR2I& aVector, const EDA_ANGLE& aAngle )
{
VECTOR2I result = aVector;
RotatePoint( &result.x, &result.y, aAngle );
return result;
}
/**
* Calculate the new point of coord coord pX, pY, for a rotation center cx, cy.
*/
void RotatePoint( int *pX, int *pY, int cx, int cy, const EDA_ANGLE& aAngle );
inline void RotatePoint( VECTOR2I& point, const VECTOR2I& centre, const EDA_ANGLE& aAngle )
/**
* Rotate a VECTOR2I in place by aAngle about aCentre
*/
inline void RotatePoint( VECTOR2I& aPoint, const VECTOR2I& aCentre, const EDA_ANGLE& aAngle )
{
RotatePoint( &aPoint.x, &aPoint.y, aCentre.x, aCentre.y, aAngle );
}
/**
* Return a new VECTOR2I that is the result of rotating aVector by aAngle.
*/
inline VECTOR2I GetRotated( const VECTOR2I& aVector, const VECTOR2I& aCentre,
const EDA_ANGLE& aAngle )
{
RotatePoint( &point.x, &point.y, centre.x, centre.y, aAngle );
VECTOR2I result = aVector;
RotatePoint( &result.x, &result.y, aCentre.x, aCentre.y, aAngle );
return result;
}

17
pcbnew/tools/pcb_point_editor.cpp

@ -1280,13 +1280,12 @@ private:
// There are two modes - when the text is between the crossbar points, and when it's not.
if( !KIGEOM::PointProjectsOntoSegment( m_originalTextPos, m_oldCrossBar ) )
{
VECTOR2I rotTextOffsetFromCbCenter = m_originalTextPos - m_oldCrossBar.Center();
RotatePoint( rotTextOffsetFromCbCenter, rotation );
VECTOR2I rotTextOffsetFromCbEnd =
m_originalTextPos
- KIGEOM::GetNearestEndpoint( m_oldCrossBar, m_originalTextPos );
RotatePoint( rotTextOffsetFromCbEnd, rotation );
const VECTOR2I cbNearestEndToText =
KIGEOM::GetNearestEndpoint( m_oldCrossBar, m_originalTextPos );
const VECTOR2I rotTextOffsetFromCbCenter =
GetRotated( m_originalTextPos - m_oldCrossBar.Center(), rotation );
const VECTOR2I rotTextOffsetFromCbEnd =
GetRotated( m_originalTextPos - cbNearestEndToText, rotation );
// Which of the two crossbar points is now in the right direction? They could be swapped over now.
// If zero-length, doesn't matter, they're the same thing
@ -1310,8 +1309,8 @@ private:
// Perpendicular from the crossbar line to the text position
// We need to keep this length constant
VECTOR2I rotCbNormalToText = m_originalTextPos - origTextPointProjected;
RotatePoint( rotCbNormalToText, rotation );
const VECTOR2I rotCbNormalToText =
GetRotated( m_originalTextPos - origTextPointProjected, rotation );
const VECTOR2I newProjected = newCrossBar.A + ( newCrossBar.B - newCrossBar.A ) * oldRatio;
return newProjected + rotCbNormalToText;

Loading…
Cancel
Save