From 7277ae93faae51fcf1371c551a913d36d25bcf8a Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Thu, 16 Nov 2023 14:06:31 +0000
Subject: [PATCH] Close a polygon that is marked as filled before hit-testing.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16096
---
 common/eda_shape.cpp | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/common/eda_shape.cpp b/common/eda_shape.cpp
index 2c94ee011e..f4e2bc33c4 100644
--- a/common/eda_shape.cpp
+++ b/common/eda_shape.cpp
@@ -922,9 +922,22 @@ bool EDA_SHAPE::hitTest( const VECTOR2I& aPosition, int aAccuracy ) const
 
     case SHAPE_T::POLY:
         if( IsFilled() )
-            return m_poly.Collide( aPosition, maxdist );
+        {
+            if( !m_poly.COutline( 0 ).IsClosed() )
+            {
+                SHAPE_POLY_SET copy( m_poly );
+                copy.Outline( 0 ).Append( copy.Outline( 0 ).CPoint( 0 ) );
+                return copy.Collide( aPosition, maxdist );
+            }
+            else
+            {
+                return m_poly.Collide( aPosition, maxdist );
+            }
+        }
         else
+        {
             return m_poly.CollideEdge( aPosition, nullptr, maxdist );
+        }
 
     default:
         UNIMPLEMENTED_FOR( SHAPE_T_asString() );