Duane

Duane

  • NA
  • 77
  • 0

Polygon fill not working properly

Aug 23 2012 11:01 AM
I am having a problem filling a polygon. When I use FillPolygon I get a fill that often overlaps the boundary polygon. How can I force the fill to stay within the boundary. I have some code to show what I've attempted. I hope this isn't the only way to do it!

Private Sub pbox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbox2.Click
        Dim myGraphics As Graphics
        Dim myPoints() As Point
        Dim myFillPoints() As Point
        Form1_MouseClick(sender, e)
        Dim PtFill1 As Point = New Point(insPoint.X + 1, insPoint.Y + 1)
        Dim PtFill2 As Point = New Point(insPoint.X + 20, insPoint.Y + 1)
        Dim PtFill3 As Point = New Point(insPoint.X + 20, insPoint.Y + 21)
        Dim PtFill4 As Point = New Point(insPoint.X + 60, insPoint.Y + 21)
        Dim PtFill5 As Point = New Point(insPoint.X + 60, insPoint.Y + 40)
        Dim PtFill6 As Point = New Point(insPoint.X + 40, insPoint.Y + 40)
        Dim PtFill7 As Point = New Point(insPoint.X + 40, insPoint.Y + 60)
        Dim PtFill8 As Point = New Point(insPoint.X + 21, insPoint.Y + 60)
        Dim PtFill9 As Point = New Point(insPoint.X + 21, insPoint.Y + 40)
        Dim PtFill10 As Point = New Point(insPoint.X + 1, insPoint.Y + 40)
        Dim pt1 As Point = insPoint
        Dim pt2 As Point = New Point(insPoint.X + 20, insPoint.Y)
        Dim pt3 As Point = New Point(insPoint.X + 20, insPoint.Y + 20)
        Dim pt4 As Point = New Point(insPoint.X + 60, insPoint.Y + 20)
        Dim pt5 As Point = New Point(insPoint.X + 60, insPoint.Y + 40)
        Dim pt6 As Point = New Point(insPoint.X + 40, insPoint.Y + 40)
        Dim pt7 As Point = New Point(insPoint.X + 40, insPoint.Y + 60)
        Dim pt8 As Point = New Point(insPoint.X + 20, insPoint.Y + 60)
        Dim pt9 As Point = New Point(insPoint.X + 20, insPoint.Y + 40)
        Dim pt10 As Point = New Point(insPoint.X, insPoint.Y + 40)
        myGraphics = Graphics.FromHwnd(hwnd:=ActiveForm().Handle)
        ReDim myPoints(9)
        ReDim myFillPoints(9)
        myPoints(0) = pt1
        myPoints(1) = pt2
        myPoints(2) = pt3
        myPoints(3) = pt4
        myPoints(4) = pt5
        myPoints(5) = pt6
        myPoints(6) = pt7
        myPoints(7) = pt8
        myPoints(8) = pt9
        myPoints(9) = pt10
        myFillPoints(0) = PtFill1
        myFillPoints(1) = PtFill2
        myFillPoints(2) = PtFill3
        myFillPoints(3) = PtFill4
        myFillPoints(4) = PtFill5
        myFillPoints(5) = PtFill6
        myFillPoints(6) = PtFill7
        myFillPoints(7) = PtFill8
        myFillPoints(8) = PtFill9
        myFillPoints(9) = PtFill10
        For Each pts As Point In myPoints
            If pts.X < 50 Or pts.X > 170 Or pts.Y < 140 Or pts.Y > 340 Then
                MsgBox("Shape falls outside of range." & vbCrLf & "Select a different insertion point.", MsgBoxStyle.OkOnly, "WARNING")
                Exit Sub
            Else
                Continue For
            End If
        Next
        myGraphics.DrawPolygon(pen:=Pens.Black, points:=myPoints)
        myGraphics.FillPolygon(Brushes.Indigo, myFillPoints)
    End Sub