Home > Software engineering >  Create 5 Point Star with e.graphics but set the x and y start position to 110,110 in VB.NET
Create 5 Point Star with e.graphics but set the x and y start position to 110,110 in VB.NET

Time:11-12

I need to create a small 5 point star that is 15 points wide by 15 points high, where the x starting position is 110 and the y starting position is 110 on my print document.

the code I found online so far creates a star, but in the top left corner at 0,0 position.

         ' Make the points for a star.
                Dim pts(4) As Point
                Dim cx As Integer = 15 \ 2
                Dim cy As Integer = 15 \ 2
                Dim theta As Double = -Math.PI / 2
                Dim dtheta As Double = Math.PI * 0.8
                For i As Integer = 0 To 4
                    pts(i).X = CInt(cx   cx * Math.Cos(theta))
                    pts(i).Y = CInt(cy   cy * Math.Sin(theta))
                    theta  = dtheta
                Next i

                e.Graphics.FillPolygon(Brushes.Black, pts)

how do i now move the star graphic to position x.110 y.110?

CodePudding user response:

Try using the sources below.

 Private Sub DrawStar(ByVal prm_StartX As Integer, ByVal prm_StartY As Integer)

      Try

         Dim pts(4) As Point
         Dim cx As Integer = prm_StartX
         Dim cy As Integer = prm_StartY
         Dim theta As Double = -Math.PI / 2
         Dim dtheta As Double = Math.PI * 0.8
         For i As Integer = 0 To 4
            pts(i).X = CInt(cx   7 * Math.Cos(theta))
            pts(i).Y = CInt(cy   7 * Math.Sin(theta))
            theta  = dtheta
         Next i

         Dim Pen As New Pen(Color.Red, 2)

         ' Draw
         Dim formGraphics As System.Drawing.Graphics
         formGraphics = Me.CreateGraphics()
         formGraphics.DrawPolygon(Pen, pts)

         Pen.Dispose()
         formGraphics.Dispose()

      Catch ex As Exception

      End Try

   End Sub

   Private Sub Toy1Form_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick

      DrawStar(e.X, e.Y)

   End Sub
  • Related