Results 1 to 8 of 8

Thread: [2008] Resolved - Drawing spirals using the Graphics object ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    [2008] Resolved - Drawing spirals using the Graphics object ?

    I have been trying now for 2 nights to draw spirals in vb.net and its starting to get to me now :0(

    Here's a link to the formula ...

    http://www.mathematische-basteleien.de/spiral.htm

    here's what i was trying todo ...

    the code below is just drawing points which i was later going to edit to connect from point to point as soon as i got the spiral function working.

    Code:
        Function Draw()
            Dim iImageCenterPointH As Integer = pbOut.Height / 2
            Dim iImageCenterPointW As Integer = pbOut.Width / 2
            Dim iCircleSpace As Integer = 10
            '# x²+y² = R² or [y = sqr(R²-x²) und y = -sqr(R²-x²)]
    
            Dim oGrapics As System.Drawing.Graphics
            Dim myImage As New Bitmap(pbOut.Width, pbOut.Height)
            pbOut.Image = myImage
    
            oGrapics = pbOut.CreateGraphics
    
    
            For i As Integer = 0 To 100 Step 5
                Dim x, y, r As Integer
    
                '# step 1 >>
                x = i
                y = 0
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW + r, iImageCenterPointH, Color.Black)
    
                '# step 2 >^
                x = i
                y = i
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW + (r / 2), iImageCenterPointH + (r / 2), Color.Black)
    
                '# step 3 ^^
                x = 0
                y = i
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW, iImageCenterPointH + r, Color.Black)
    
                '# step 4 ^^
                x = i
                y = -i
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW - (r / 2), iImageCenterPointH + (r / 2), Color.Black)
    
                x = -i
                y = i
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW + (r / 2), iImageCenterPointH - (r / 2), Color.Black)
    
                x = -i
                y = 0
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW - r, iImageCenterPointH, Color.Black)
    
                x = -i
                y = -i
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW - (r / 2), iImageCenterPointH - (r / 2), Color.Black)
    
                x = 0
                y = -i
                r = (y * y) + (x * x)
                r = Math.Sqrt(r)
                myImage.SetPixel(iImageCenterPointW, iImageCenterPointH - r, Color.Black)
            Next
            pbOut.Image = myImage
            Return ""
        End Function
    im not 100% the output is matching the formula here's what im getting atm ...
    Attached Images Attached Images  
    Last edited by illskills; Feb 20th, 2009 at 03:41 PM. Reason: Resolved

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: [2008] Drawing spirals using the Graphics object ?

    would it make more sense going from the Rsqrd value rather than the x y cords?

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Drawing spirals using the Graphics object ?

    Here is the code I use to draw a spiral on the form. You can specify the starting and ending angle. When drawing a spiral, think trigonometry. Imagine a pen moving along a ruler at a steady pace. The result would be a straight line, of course. But now spin the ruler from its end on the surface of the table, also at a steady rate. The result is a spiral. The depth between layers depends on the ratio between the straight line motion of the pen along the ruler, and the circular motion of the ruler itself:

    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        Private Points As List(Of PointF)
        Private PenWidth As Int16
    
        Private Enum Direction
            Clockwise = -1
            AntiClockwise = 1
        End Enum
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Points = New List(Of PointF)
            PenWidth = 3
    
            Dim Center As PointF = New PointF(CSng(Me.Width / 2), CSng(Me.Height / 2))
            Dim Separation As Single = 10 + PenWidth
            Dim Turns As Integer = 8
            Dim Rotation As Direction = Direction.Clockwise
            Dim Displacement As Single = 20
            Dim StartAngle As Double = Math.PI / 4
            Dim EndAngle As Double
    
            StartAngle = Math.PI - StartAngle     'Orientate the angle relative to the 12'oclock position
            EndAngle = 2 * Turns * Math.PI + StartAngle
    
            If Rotation = Direction.Clockwise Then
                EndAngle = StartAngle
                StartAngle = 2 * Turns * Math.PI + EndAngle
            End If
    
            For Angle = StartAngle To EndAngle Step CInt(Rotation) * (Math.PI / 180)
                Points.Add(New PointF(CSng(Center.X + Displacement * Math.Sin(Angle)), CSng(Center.Y + Displacement * Math.Cos(Angle))))
                Displacement += Separation / 360
            Next
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawCurve(New Pen(Color.Black, PenWidth), Points.ToArray())
        End Sub
    End Class
    Last edited by MaximilianMayrhofer; Feb 20th, 2009 at 12:45 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: [2008] Drawing spirals using the Graphics object ?

    thanks bud was starting to get annoying writing my own function ;0)

    ill check the code now n let you know how it goes ;0)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: [2008] Drawing spirals using the Graphics object ?

    superb worked a treat ;0)
    many thanks

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: [2008] Resolved - Drawing spirals using the Graphics object ?

    thought you might want to check my modifications .... i whacked it into a function ....

    if you want the code ill pm it to you ;0)
    Attached Images Attached Images  

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Resolved - Drawing spirals using the Graphics object ?

    Yep I recognise the shape of my spirals It's alright, I put my code in a function a long time ago so that I could play around with spinning spirals.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: [2008] Resolved - Drawing spirals using the Graphics object ?

    im using it to design pieces of glass for my uni project i have waves / circles and now spirals working im using it to emulate me pouring pva glue onto glass for sand blasting later ... my little app can churn out more drawings in an hour than i could do in a year lol ;0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width