none
Hi ALL, here is a method to produce a SPIRAL. You can specify startRadius, endRadius, direction and distanceBetweenTurnsInPixels as well as the startAngle. Enjoy!!

    Debate general

  • Hi ALL,

    Here is some code you can play around with.  :)

     I thought it was better that you can specify a value for the endRadius so you can limit the spiral.

    Please note: For this to work startRadius must be greater than the value for endRadius as the

    mathematics involved gradually decreases the radius.I have not added any error checking for this.

    Please also note the distanceBetweenTurnsInPixels , so for one full turn make the difference in the startRadius and endRadius

    equal to this distance.

    E.G: startRadius = 120 , endRadius = 50 , distanceBetweenTurnsInPixels =70

    Try different values for these variables.>>

            Dim startRadius As Double = (Me.Height / 2) - 50
            Dim endRadius As Integer = 50
            Dim dir As Direction = Direction.Clockwise
            Dim distanceBetweenTurnsInPixels As Integer = 50
            'Please note the startAngle is from the EAST direction
            ' in the direction specified by the variable 'dir' as Direction.>>
            Dim startAngleInDegrees As Double = 45

    _________________________

    Here is the code in full. Enjoy!!

    _________________________

    >>

    _________________________

    Public Class Form1
    
        Const PiTimesTwo As Double = Math.PI * 2
        Const DegreesInOneRadian As Double = 180 / Math.PI
    
        Public Enum Direction
            AntiClockwise = -1
            Clockwise = 1
        End Enum
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Me.BackColor = Color.Black
            Me.WindowState = FormWindowState.Maximized
    
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            Dim startRadius As Double = (Me.Height / 2) - 50
            Dim endRadius As Integer = 50
            Dim dir As Direction = Direction.Clockwise
            Dim distanceBetweenTurnsInPixels As Integer = 50
            'Please note the startAngle is from the EAST direction
            ' in the direction specified by the variable 'dir' as Direction.>>
            Dim startAngleInDegrees As Double = 45
    
            Dim angle1 As Double = startAngleInDegrees / DegreesInOneRadian
            Dim angle2 As Double = (startAngleInDegrees + 360) / DegreesInOneRadian
            Dim radius As Double = startRadius
            Dim x1, y1 As Double
            Dim myPen As New Pen(Color.White, 1)
            Dim mypoints As New List(Of Point)
    
            Do
                For index As Double = angle1 To angle2 Step 0.05
                    radius = radius - (distanceBetweenTurnsInPixels / (PiTimesTwo / 0.05))
                    x1 = radius * Math.Cos(index) + (Me.Width \ 2)
                    y1 = dir * radius * Math.Sin(index) + (Me.Height \ 2)
                    mypoints.Add(New Point(CInt(Int(x1)), CInt(Int(y1))))
                    If radius <= endRadius Then Exit Do
                Next
            Loop
    
            Me.Text = "Final radius = " & radius.ToString
            e.Graphics.DrawCurve(myPen, mypoints.ToArray)
    
        End Sub
    
    End Class

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

    If anyone wants a version to start at a specific angle and spiral outwards

    then I can post an altered version of the above code, just say yes please.




    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.

    lunes, 30 de abril de 2012 22:56

Todas las respuestas