From 3f73d8c2b50cdbdf5f0c18070022ff36cecf0366 Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Sun, 9 May 2021 20:01:47 +0100 Subject: [PATCH] SHAPE_LINE_CHAIN::Area() should always be positive The algorithm relied on the assumption that the points were ordered anti-clockwise. When ordered in a clockwise fashion, the result was correct but negative. Refer to https://www.mathopenref.com/coordpolygonarea2.html for more information. --- libs/kimath/src/geometry/shape_line_chain.cpp | 2 +- pcbnew/dialogs/dialog_board_statistics.cpp | 4 ++-- pcbnew/router/pns_mouse_trail_tracer.cpp | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libs/kimath/src/geometry/shape_line_chain.cpp b/libs/kimath/src/geometry/shape_line_chain.cpp index ef45b04f37..ae5e1a0861 100644 --- a/libs/kimath/src/geometry/shape_line_chain.cpp +++ b/libs/kimath/src/geometry/shape_line_chain.cpp @@ -1714,7 +1714,7 @@ double SHAPE_LINE_CHAIN::Area() const j = i; } - return -area * 0.5; + return std::fabs( area * 0.5 ); // The result would be negative if points are anti-clockwise } diff --git a/pcbnew/dialogs/dialog_board_statistics.cpp b/pcbnew/dialogs/dialog_board_statistics.cpp index 0b7e840616..6bd52f57be 100644 --- a/pcbnew/dialogs/dialog_board_statistics.cpp +++ b/pcbnew/dialogs/dialog_board_statistics.cpp @@ -323,13 +323,13 @@ void DIALOG_BOARD_STATISTICS::getDataFromPCB() for( int i = 0; i < polySet.OutlineCount(); i++ ) { SHAPE_LINE_CHAIN& outline = polySet.Outline( i ); - m_boardArea += std::fabs( outline.Area() ); + m_boardArea += outline.Area(); // If checkbox "subtract holes" is checked if( m_checkBoxSubtractHoles->GetValue() ) { for( int j = 0; j < polySet.HoleCount( i ); j++ ) - m_boardArea -= std::fabs( polySet.Hole( i, j ).Area() ); + m_boardArea -= polySet.Hole( i, j ).Area(); } if( boundingBoxCreated ) diff --git a/pcbnew/router/pns_mouse_trail_tracer.cpp b/pcbnew/router/pns_mouse_trail_tracer.cpp index d5aa80bba8..58ec68e2aa 100644 --- a/pcbnew/router/pns_mouse_trail_tracer.cpp +++ b/pcbnew/router/pns_mouse_trail_tracer.cpp @@ -119,7 +119,7 @@ DIRECTION_45 MOUSE_TRAIL_TRACER::GetPosture( const VECTOR2I& aP ) PNS_DBG( dbg, AddLine, straight, m_forced ? BLUE : GREEN, 100000, "mt-straight" ); - double areaS = std::abs( straight.Area() ); + double areaS = straight.Area(); SHAPE_LINE_CHAIN diag( DIRECTION_45().BuildInitialTrace( p0, aP, true, false ) ); diag.Append( m_trail.Reverse() ); @@ -128,7 +128,7 @@ DIRECTION_45 MOUSE_TRAIL_TRACER::GetPosture( const VECTOR2I& aP ) PNS_DBG( dbg, AddLine, diag, YELLOW, 100000, "mt-diag" ); - double areaDiag = std::abs( diag.Area() ); + double areaDiag = diag.Area(); double ratio = areaS / ( areaDiag + 1.0 ); // heuristic to detect that the user dragged back the cursor to the beginning of the trace @@ -152,7 +152,7 @@ DIRECTION_45 MOUSE_TRAIL_TRACER::GetPosture( const VECTOR2I& aP ) SHAPE_LINE_CHAIN trail( m_trail ); trail.SetClosed( true ); - if( std::abs( trail.Area() ) > areaCutoff ) + if( trail.Area() > areaCutoff ) areaOk = true; }