none
e.graphics.DrawSpiral Okay folks here is a code update from code I have posted yesterday using an Extension Method. Skip to the 2nd post for Vb.Net versions 2002, 2003 and 2005

    一般討論

  • Hi ALL,

    This code is for 2008, 2010 and later versions of VB.Net as it uses an extension method.

    Please note: If you are using an earlier version ( or you do not like extension methods ) please see the next post.

    Here is the code I posted on Monday April 30th, 2012.>>

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/0802909c-0a9f-4e51-955e-7671306c9a67

    Now you can specify the startRadius to be larger or smaller than the endRadius.  :-)

    The Spiral will always be drawn starting from the startRadius.

    Please read past the 1st code snippet to see how to make this effective.

    Now you can do something as simple as.>>

    e.Graphics.DrawSpiral(aPen, mySpiralCenter, 250, 25, Direction.Clockwise, 25, 45)

    '-------------------------------as shown below------------------------------------------------------------

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            e.Graphics.Clear(Color.Black)
            Dim aPen As New Pen(Color.White, 1)
            Dim mySpiralCenter As New Point(Me.Width \ 4, Me.Height \ 2)
            e.Graphics.DrawSpiral(aPen, mySpiralCenter, 250, 25, Direction.Clockwise, 25, 45)
    
            aPen.Color = Color.Yellow
            mySpiralCenter = New Point(Me.Width * 3 \ 4, Me.Height \ 2)
            'Similar spiral but the startRadius and endRadius are reversed.>>
            e.Graphics.DrawSpiral(aPen, mySpiralCenter, 25, 250, Direction.Clockwise, 25, 45)
    
        End Sub

    '________________________________

    '________________________________

    '________________________________

    • Go to the PROJECT menu and select ADD MODULE...
    • Click on ADD then
    • PASTE the following code in and then the code at the end of this post will work.  ;-)

    '________________________________

    '________________________________

    '________________________________

    Public Module MyExtensions
    
        Public Const PiTimesTwo As Double = Math.PI * 2
        Public Const DegreesInOneRadian As Double = 180 / Math.PI
    
        Public Enum Direction
            AntiClockwise = -1
            Clockwise = 1
        End Enum
    
        ''' <summary>
        ''' Draws a spiral from the startRadius to the endRadius
        ''' at the point specified by 'center' in the direction
        ''' specified by the 'dir' variable as Direction using the pen specified by 'myPen'.
        ''' The startAngle in degrees is measured from the right or the EAST side of a 360 degree compass.
        ''' E.G: If direction is Clockwise and startAngle = 45 then the start will be 45 degrees below EAST
        ''' otherwise if direction is AntiClockwise it will be 45 degrees above EAST relative to the center of the spiral.
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="myPen"></param>
        ''' <param name="center"></param>
        ''' <param name="startRadius"></param>
        ''' <param name="endRadius"></param>
        ''' <param name="dir"></param>
        ''' <param name="distanceBetweenTurnsInPixels"></param>
        ''' <param name="startAngleInDegrees"></param>
        ''' <remarks></remarks>
        <System.Runtime.CompilerServices.Extension()> _
        Public Sub DrawSpiral(ByVal g As System.Drawing.Graphics, ByVal myPen As Pen, ByVal center As Point, ByVal startRadius As Integer, _
                              ByVal endRadius As Integer, ByVal dir As Direction, ByVal distanceBetweenTurnsInPixels As Integer, _
                              ByVal startAngleInDegrees As Double)
    
            Dim angle1 As Double = startAngleInDegrees / DegreesInOneRadian
            Dim angle2 As Double = (startAngleInDegrees + 360) / DegreesInOneRadian
            Dim radius As Double = startRadius
            Dim x1, y1 As Double
    
            Dim mypoints As New List(Of Point)
            If startRadius > endRadius Then
                Do
                    For index As Double = angle1 To angle2 Step 0.05
                        radius = radius - (distanceBetweenTurnsInPixels / (PiTimesTwo / 0.05))
                        x1 = radius * Math.Cos(index) + center.X
                        y1 = dir * radius * Math.Sin(index) + center.Y
                        mypoints.Add(New Point(CInt(Int(x1)), CInt(Int(y1))))
                        If radius <= endRadius Then Exit Do
                    Next
                Loop
                g.DrawCurve(myPen, mypoints.ToArray)
            End If
            If startRadius < endRadius Then
                Do
                    For index As Double = angle1 To angle2 Step 0.05
                        radius = radius + (distanceBetweenTurnsInPixels / (PiTimesTwo / 0.05))
                        x1 = radius * Math.Cos(index) + center.X
                        y1 = dir * radius * Math.Sin(index) + center.Y
                        mypoints.Add(New Point(CInt(Int(x1)), CInt(Int(y1))))
                        If radius >= endRadius Then Exit Do
                    Next
                Loop
                g.DrawCurve(myPen, mypoints.ToArray)
            End If
    
        End Sub
    
    End Module

    '_______________________

    '_______________________

    '_______________________

    PASTE this code to Form1 to try the above extension.>>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.WindowState = FormWindowState.Maximized
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            e.Graphics.Clear(Color.Black)
            Dim aPen As New Pen(Color.White, 1)
            Dim mySpiralCenter As New Point(Me.Width \ 4, Me.Height \ 2)
            e.Graphics.DrawSpiral(aPen, mySpiralCenter, 250, 25, Direction.Clockwise, 25, 45)
    
            aPen.Color = Color.Yellow
            mySpiralCenter = New Point(Me.Width * 3 \ 4, Me.Height \ 2)
            'Similar spiral but the startRadius and endRadius are reversed.>>
            e.Graphics.DrawSpiral(aPen, mySpiralCenter, 25, 250, Direction.Clockwise, 25, 45)
    
        End Sub




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    2012年5月1日 下午 11:53

所有回覆

  • Hi ALL,

    Like this it should work with most if not ALL versions of VB.Net.  :)

    • Go to the PROJECT menu, select ADD MODULE...
    • Click on ADD
    • PASTE this code in.>>

    '--------------------------------------------------------------------

    Public Module MyExtensions
    
        Public Const PiTimesTwo As Double = Math.PI * 2
        Public Const DegreesInOneRadian As Double = 180 / Math.PI
    
        Public Enum Direction
            AntiClockwise = -1
            Clockwise = 1
        End Enum
    
        ''' <summary>
        ''' Draws a spiral from the startRadius to the endRadius
        ''' at the point specified by 'center' in the direction
        ''' specified by the 'dir' variable as Direction using the pen specified by 'myPen'.
        ''' The startAngle in degrees is measured from the right or the EAST side of a 360 degree compass.
        ''' E.G: If direction is Clockwise and startAngle = 45 then the start will be 45 degrees below EAST
        ''' otherwise if direction is AntiClockwise it will be 45 degrees above EAST relative to the center of the spiral.
        ''' </summary>
        ''' <param name="g"></param>
        ''' <param name="myPen"></param>
        ''' <param name="center"></param>
        ''' <param name="startRadius"></param>
        ''' <param name="endRadius"></param>
        ''' <param name="dir"></param>
        ''' <param name="distanceBetweenTurnsInPixels"></param>
        ''' <param name="startAngleInDegrees"></param>
        ''' <remarks></remarks>
        Public Sub DrawSpiral(ByVal g As System.Drawing.Graphics, ByVal myPen As Pen, ByVal center As Point, ByVal startRadius As Integer, _
                              ByVal endRadius As Integer, ByVal dir As Direction, ByVal distanceBetweenTurnsInPixels As Integer, _
                              ByVal startAngleInDegrees As Double)
    
            Dim angle1 As Double = startAngleInDegrees / DegreesInOneRadian
            Dim angle2 As Double = (startAngleInDegrees + 360) / DegreesInOneRadian
            Dim radius As Double = startRadius
            Dim x1, y1 As Double
    
            Dim mypoints As New List(Of Point)
            If startRadius > endRadius Then
                Do
                    For index As Double = angle1 To angle2 Step 0.05
                        radius = radius - (distanceBetweenTurnsInPixels / (PiTimesTwo / 0.05))
                        x1 = radius * Math.Cos(index) + center.X
                        y1 = dir * radius * Math.Sin(index) + center.Y
                        mypoints.Add(New Point(CInt(Int(x1)), CInt(Int(y1))))
                        If radius <= endRadius Then Exit Do
                    Next
                Loop
                g.DrawCurve(myPen, mypoints.ToArray)
            End If
            If startRadius < endRadius Then
                Do
                    For index As Double = angle1 To angle2 Step 0.05
                        radius = radius + (distanceBetweenTurnsInPixels / (PiTimesTwo / 0.05))
                        x1 = radius * Math.Cos(index) + center.X
                        y1 = dir * radius * Math.Sin(index) + center.Y
                        mypoints.Add(New Point(CInt(Int(x1)), CInt(Int(y1))))
                        If radius >= endRadius Then Exit Do
                    Next
                Loop
                g.DrawCurve(myPen, mypoints.ToArray)
            End If
    
        End Sub
    
    End Module

    '------------------------------

    '------------------------------

    To try this code try this for your Form1 code please.

    '------------------------------

    '------------------------------

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.WindowState = FormWindowState.Maximized
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            e.Graphics.Clear(Color.Black)
            Dim aPen As New Pen(Color.White, 1)
            Dim mySpiralCenter As New Point(Me.Width \ 4, Me.Height \ 2)
            DrawSpiral(e.Graphics, aPen, mySpiralCenter, 250, 25, Direction.Clockwise, 25, 45)
    
            aPen.Color = Color.Yellow
            mySpiralCenter = New Point(Me.Width * 3 \ 4, Me.Height \ 2)
            'Similar spiral but here the startRadius and endRadius values have been swapped.>>
            DrawSpiral(e.Graphics, aPen, mySpiralCenter, 25, 250, Direction.Clockwise, 25, 45)
    
        End Sub
    
    End Class




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    2012年5月2日 上午 12:04
  • An update one? You keep your words. Thank you.

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    2012年5月2日 上午 10:52
  • An update one? You keep your words. Thank you.

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Hi CrazyGhost_Von,

    Yes it is one update

    but written for different versions of VB.Net.  ;-)

    Users of VB.Net from 2008 onwards can use either.

    Yes I try to keep to my words - promises.

    By the way, your welcome.  :)




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    2012年5月2日 上午 11:11
  • In your paint method, be sure to dispose of your Pen that you created, otherwise you are leaking a resource.  The easiest way is to use the Using construct:

    Using myPen As New Pen(Color.White, 1)
        'use pen here
    End Using

    This way, the pen is automatically disposed.

    Or, instead of creating your own pen, when using just simple colors, you can use one of the predefined pens.  In this case:  Pens.White.  NOTE:  If you use a predefined pen, don't dispose it!


    2012年5月2日 下午 10:00
  • Hi Chris,

     What exactly do you mean by "leaking a resource" please?

    Regarding the rest of your post, I know what you mean. :)




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    2012年5月2日 下午 11:26
  • "otherwise you are leaking a resource."

    It's mainly a C++ term unless someone has declared a resouce and forgottem to deallocate it. Therefore the resource "is lost" because it is not used but had not been deallocated.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me


    2012年5月3日 上午 01:32