0

My first post here and somewhat new to VB.NET, so I'm aware that I may breach some protocols. So please be patient.

A bit of background on what I'm trying to do. I've created a Windows form with a picturebox that the user can draw objects on (representing walls, doors, etc). The coordinates for the objects are saved in a SQL backend (think of it as vector drawing in a Paint like interface). The next step is to move a light source (like a person) walking through the object. I've created a light polygon in SQL representing a crude 2D raytrace and created a difference of this from the mat's polygon. Essentially this difference represents the light paths where objects can be seen.

The last step, and the one I'm stuck on, is how to display the final geometry I create (a complex curvepolygon) on the picturebox which will essentially cover up the objects that are not lit by the light source yet. At it's most basic, how do you import a geometry from SQL and then display it in a picturebox in a windows form/picturebox.

Thanks and appreciate any help. Bear in mind that I'm somewhat new to .NET, and am using this project as a way to teach myself VB.NET and SQL.

ACA
  • 1
  • 2

1 Answers1

0

A little disappointed that there is nobody that can answer this question, but it happens. So I came up with a workaround that while not ideal, will hopefully help someone else stuck in the same situation. Maybe someone will see this and come up with a more elegant solution.

So once I have my final "shadow" polygon, I can't find a VB.NET solution to draw it as is, but that doesn't mean I can't convert it to a format that is possible to draw in a picturebox. Here's the code I came up with:

    Private Sub createShadows()
    Dim myPen As Pen
    Dim myBrush As Brush
    Dim myPoints As Point()
    Dim listPoints As New List(Of Point)
    Dim x As Double
    Dim y As Double

    ' Convert the complex shape into a polygon shape in SQL and bring all the vertex
    ' points into VB and put into a string variable (curBlack)
    mConn.Open()
    Dim cmd = New SqlCommand("Select CurrentBlack.STCurveToLine().STAsText() From tbl_Map_Master Where MapID = " &
                             cmbDN.SelectedItem(0), mConn)
    Dim curBlack As String = cmd.ExecuteScalar
    mConn.Close()

    ' Now parse the vertex points from SQL format into a set of VB points 
    curBlack = curBlack.Replace("POLYGON ((", "").Replace(")", "") & ",,"
    While curBlack.Length > 1
        x = CInt(Strings.Left(curBlack, InStr(curBlack, " "))) * iZoom + centerX
        curBlack = Strings.Right(curBlack, Len(curBlack) - InStr(curBlack, " "))
        y = CInt(Strings.Left(curBlack, InStr(curBlack, ",") - 1)) * iZoom + centerY
        curBlack = Strings.Right(curBlack, Len(curBlack) - InStr(curBlack, ",") - 1)
        listPoints.Add(New Point(x, y))
    End While
    myPoints = listPoints.ToArray

    ' Now use the points array to draw a filled polygon
    myPen = New Pen(Drawing.Color.White, 0)
    myBrush = New SolidBrush(Color.FromArgb(170, 0, 0, 0))
    Dim myGraphics As Graphics = pbDN.CreateGraphics
    myGraphics.DrawPolygon(myPen, myPoints)
    myGraphics.FillPolygon(myBrush, myPoints)
End Sub

As I said, this is not the ideal solution, so it slows things down a bit performance wise, but it works. I'm sure being a VB.NET amateur, heck I can't even get the code sample to look right in here, even the current code is not optimal, but it gets the job done. Hope someone finds this helpful.

ACA
  • 1
  • 2