Results 1 to 12 of 12

Thread: [RESOLVED] Graph a Circle with Delay Function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Resolved [RESOLVED] Graph a Circle with Delay Function

    I'm trying to graph a circle with a delay function with it. I know that using a timer is necessary. I created a code in timer1_click

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Me.startDelay += 1
    
            If Me.startDelay = 360 Then
                Me.Timer1.Stop()
            End If
    
            Me.Refresh()
        End Sub
    Code of the graphing of circle:
    Code:
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
    If DrawIt Then
                x = CType(TextBox1.Text, Integer)
                y = CType(TextBox2.Text, Integer)
                r = CType(TextBox3.Text, Integer)
                FillCircle(g, x, y, 3, Brushes.Black)
                DrawCircle(g, x, y, r, Pens.Red)
    End If
    End Sub
    Private Sub DrawCircle(ByVal g As Graphics, ByVal x As Single, ByVal y As Single, ByVal r As Single, ByVal p As Pen)
            g.DrawEllipse(p, x - r, y - r, r * 2, r * 2)
    End Sub
    
    Private Sub FillCircle(ByVal g As Graphics, ByVal x As Single, ByVal y As Single, ByVal r As Single, ByVal b As Brush)
            g.FillEllipse(b, x - r, y - r, r * 2, r * 2)
    End Sub
    Now how can I implement the timer delay to the graphing of the circle?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Graph a Circle with Delay Function

    Your Tick event makes no sense. Why have a Timer Tick 360 times when you can simply multiply the Interval by 360 and have it Tick once?

    All you need is a Boolean flag that indicates whether to draw the circle or not. When the Timer Ticks, toggle the flag and then Refresh. In the Paint event handler, test that flag and draw the circle if and only if it has the appropriate value.
    Code:
    Public Class Form1
    
        Private drawCircle As Boolean = False
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Me.Timer1.Stop()
            Me.drawCircle = True
            Me.Refresh()
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            If Me.drawCircle Then
                'Draw circle here.
            End If
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: Graph a Circle with Delay Function

    I tried to do your suggestion but it doesn't delay the drawing of circle. Here's what I did:

    Code:
    Private DrawCircles As Boolean = False
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Me.Timer1.Start()
            g = Me.CreateGraphics()
        End Sub
    
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint  ' //the drawing of Cartesian Plane
            Dim g As Graphics = e.Graphics
            g.TranslateTransform(500, 310)   'Put 0,0 of the plot at pixel 500,310
            g.ScaleTransform(1, -1)          'Reverse the Y axis so +Y is above 0,0
    
            g.FillRectangle(Brushes.White, New Rectangle(-250, -250, 500, 500))  'a 500x500 area centered on the 0,0 coordinate
            g.DrawLine(Pens.Black, 0, -250, 0, 250)
            g.DrawLine(Pens.Black, -250, 0, 250, 0)
    
            If Me.DrawCircles Then
                If DrawIt Then
                    x = CType(TextBox1.Text, Integer)
                    y = CType(TextBox2.Text, Integer)
                    r = CType(TextBox3.Text, Integer)
                    FillCircle(g, x, y, 3, Brushes.Black)
                    DrawCircle(g, x, y, r, Pens.Red)
    
                End If
    End Sub
    
    Private Sub DrawCircle(ByVal g As Graphics, ByVal x As Single, ByVal y As Single, ByVal r As Single, ByVal p As Pen)
            g.DrawEllipse(p, x - r, y - r, r * 2, r * 2)
        End Sub
    
        Private Sub FillCircle(ByVal g As Graphics, ByVal x As Single, ByVal y As Single, ByVal r As Single, ByVal b As Brush)
            g.FillEllipse(b, x - r, y - r, r * 2, r * 2)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If TextBox1.TextLength <> 0 And TextBox2.TextLength <> 0 And TextBox3.TextLength <> 0 Then
                Label6.Text = "Center of Circle: (" + (CType(TextBox1.Text, Integer)).ToString + ", " + CType(TextBox2.Text, Integer).ToString + ")"
                DrawIt = True
                Invalidate()
            Else
                MessageBox.Show("Please fill-up the fields first before plotting the circle", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Me.Timer1.Stop()
            Me.DrawCircles = True
            Me.Refresh()
        End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Graph a Circle with Delay Function

    What is the Interval of your Timer set to? Did you multiply it by 360 from what it was before, like I said to?

    By the way, get rid of this line from the Load event handler:
    Code:
    g = Me.CreateGraphics()
    and move your declaration of `g` to inside the Paint event handler. You don;t need to create a Graphics object because you're using the one that the Paint event provides.

    Also, why do you have both `DrawCircles` and `DrawIt` fields?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Graph a Circle with Delay Function

    Note that the Interval of a Timer is measured in milliseconds, so 1000 is 1 second.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: Graph a Circle with Delay Function

    I want something like this solution of yours from other post. How can I implement something like this on my code? I need time delay of the drawing of the circle like this

    Code:
        Private sweepAngle As Integer = 1
         
        Private Sub Timer1_Tick(ByVal sender As Object, _
                                ByVal e As EventArgs) Handles Timer1.Tick
            Me.sweepAngle += 1
         
            If Me.sweepAngle = 360 Then
                Me.Timer1.Stop()
            End If
         
            Me.Refresh()
        End Sub
         
        Private Sub Form1_Paint(ByVal sender As Object, _
                                ByVal e As PaintEventArgs) Handles Me.Paint
            e.Graphics.DrawArc(Pens.Black, 10, 10, 48, 48, 0, Me.sweepAngle)
        End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Graph a Circle with Delay Function

    So when you say that you want a time delay, do you actually mean that you want it to be drawn gradually? I took your meaning to be that you want it to be drawn all in one go but at some specified time after the form is first displayed. That code of mine will draw the circle gradually, in 1 degree increments. If that's what you want then you already have the code, so what's the issue? I think that maybe you need to provide a clear, more detailed explanation of what you're trying to achieve and how the code you already have doesn't achieve it.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: Graph a Circle with Delay Function

    I want to draw gradually each of the circles. For example, I input the center of the circle: (60,30) and its radius: 60. Then I'll click on Draw Circle button. This should draw the circle gradually on the coordinate plane. And if I'll click on the octant 2 button, this should also draw the circle gradually on the second octant. I have eight octant buttons all in all.
    Last edited by angel06; Oct 20th, 2015 at 12:18 PM.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Graph a Circle with Delay Function

    You need to reset your sweepAngle to 1, and start your timer each time you press one of the buttons.
    If you don't want redraw all the circles in slow action, you will need a way of maintaining which circle you want to use sweepAngle on (the one that should be drawn slowly), and using either 360 for the others with drawArc, or using a circle command, so have a conditional test in your drawing to choose drawArc.

    It would probably be better to restructure the drawing, so that you can draw the circles independently without having to invalidate the whole form, but I don't have time to give an example of that.

    For the first quick step, just add code like this in each button press to see how it acts.
    Code:
    '
          sweepAngle = 1
          Timer1.Start()
    Code:
    'for example in octantTwo_Click...
      Private Sub octantTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles octantTwo.Click
        If TextBox1.TextLength <> 0 And TextBox2.TextLength <> 0 And TextBox3.TextLength <> 0 Then
          sweepAngle = 1
          Timer1.Start()
          Label6.Text = "Center: (" + Math.Abs(CType(TextBox2.Text, Integer)).ToString + ", " + CType(TextBox1.Text, Integer).ToString + ")"
          Label9.Text = "Octant 2"
          DrawIt2 = True
          Invalidate()
        Else
          MessageBox.Show("Please fill-up the fields first before plotting the circle", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
      End Sub

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Graph a Circle with Delay Function

    Assuming you might only want to draw the last octant selected slow, then one way that could be done is use an index to indicate which circle should be draw slow, and check for that in your draw circle sub.
    vb.net Code:
    1. '.....
    2.   Private DrawIt7 As Boolean
    3.   Private DrawIt8 As Boolean
    4.   Private DrawSlow, ImDrawing As Integer 'Add two values that can be compared to see which circle should be drawn slow
    5.  
    6. 'Modify each button press to set which circle should be drawn slow
    7. 'for example, set DrawSlow to 3 in the octantThree_Click event (note sweep angle reset, and timer started as well)
    8.   Private Sub octantThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles octantThree.Click
    9.     If TextBox1.TextLength <> 0 And TextBox2.TextLength <> 0 And TextBox3.TextLength <> 0 Then
    10.       Label6.Text = "Center: (" + "-" + Math.Abs(CType(TextBox2.Text, Integer)).ToString + ", " + CType(TextBox1.Text, Integer).ToString + ")"
    11.       Label9.Text = "Octant 3"
    12.       sweepAngle = 1
    13.       Timer1.Start()
    14.       DrawSlow = 3
    15.       DrawIt3 = True
    16.       Invalidate()
    17.     Else
    18.       MessageBox.Show("Please fill-up the fields first before plotting the circle", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    19.     End If
    20.   End Sub
    21.  
    22. 'Note which circle you are drawing in the paint event by setting the ImDrawing variable, for example DrawIt3 and DrawIt4 cases.
    23.     If DrawIt3 Then
    24.       ImDrawing = 3
    25.       x = CType(TextBox1.Text, Integer)
    26.       y = CType(TextBox2.Text, Integer)
    27.       r = CType(TextBox3.Text, Integer)
    28.       FillCircle(g, -y, x, 3, Brushes.Black)
    29.       DrawCircle(g, -y, x, r, Pens.Cyan)
    30.  
    31.     End If
    32.  
    33.     If DrawIt4 Then
    34.       ImDrawing = 4
    35.       x = CType(TextBox1.Text, Integer)
    36.       y = CType(TextBox2.Text, Integer)
    37.       r = CType(TextBox3.Text, Integer)
    38.       FillCircle(g, -x, y, 3, Brushes.Black)
    39.       DrawCircle(g, -x, y, r, Pens.Gold)
    40.  
    41.     End If
    42.  
    43. 'And in your DrawCircle Sub, choose to drawArc or DrawCircle based on whether ImDrawing matches DrawSlow.
    44.   Private Sub DrawCircle(ByVal g As Graphics, ByVal x As Single, ByVal y As Single, ByVal r As Single, ByVal p As Pen)
    45.     If ImDrawing = DrawSlow Then
    46.       g.DrawArc(p, x - r, y - r, r * 2, r * 2, 0, Me.sweepAngle)
    47.     Else
    48.       g.DrawEllipse(p, x - r, y - r, r * 2, r * 2)
    49.     End If
    50.  
    51.   End Sub

    p.s. As already mentioned, you don't need that Private g As Graphics declaration at the top, or setting it in the Form_Load event.
    If you haven't set it in the IDE properties, I would add
    DoubleBuffered = True
    in the Form_Load event, to minimize flashing while you're drawing
    Code:
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        DoubleBuffered = True
      End Sub
    p.s. I think I would change your timer code to make it draw the circle somewhat faster.
    Code:
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.sweepAngle += 3
        If Me.sweepAngle >= 360 Then
          Me.Timer1.Stop()
        End If
        Me.Refresh()
      End Sub
    Maybe allow the user to pick a "speed", by changing that 3 to another value (between 1 and 180).

    p.p.s. Given the colors you're using, drawing on a black background (swap your black and white pens and brushes), helps the circles stand out (other than the blue). If you want to stay with a white background, then probably choose a number of "dark" colors, not yellow and cyan.Name:  CircleGraph.jpg
Views: 561
Size:  27.6 KB
    The blue is the upper right, which is extremely hard to see on the image in the forum, since they've shrunk the image. The shrunk image makes it hard to see a few of the other circles, but in normal size on your screen, it is just the blue that doesn't stand out.
    Last edited by passel; Oct 19th, 2015 at 12:21 PM.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2012
    Posts
    30

    Re: Graph a Circle with Delay Function

    Quote Originally Posted by passel View Post
    Assuming you might only want to draw the last octant selected slow, then one way that could be done is use an index to indicate which circle should be draw slow, and check for that in your draw circle sub.
    vb.net Code:
    1. '.....
    2.   Private DrawIt7 As Boolean
    3.   Private DrawIt8 As Boolean
    4.   Private DrawSlow, ImDrawing As Integer 'Add two values that can be compared to see which circle should be drawn slow
    5.  
    6. 'Modify each button press to set which circle should be drawn slow
    7. 'for example, set DrawSlow to 3 in the octantThree_Click event (note sweep angle reset, and timer started as well)
    8.   Private Sub octantThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles octantThree.Click
    9.     If TextBox1.TextLength <> 0 And TextBox2.TextLength <> 0 And TextBox3.TextLength <> 0 Then
    10.       Label6.Text = "Center: (" + "-" + Math.Abs(CType(TextBox2.Text, Integer)).ToString + ", " + CType(TextBox1.Text, Integer).ToString + ")"
    11.       Label9.Text = "Octant 3"
    12.       sweepAngle = 1
    13.       Timer1.Start()
    14.       DrawSlow = 3
    15.       DrawIt3 = True
    16.       Invalidate()
    17.     Else
    18.       MessageBox.Show("Please fill-up the fields first before plotting the circle", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    19.     End If
    20.   End Sub
    21.  
    22. 'Note which circle you are drawing in the paint event by setting the ImDrawing variable, for example DrawIt3 and DrawIt4 cases.
    23.     If DrawIt3 Then
    24.       ImDrawing = 3
    25.       x = CType(TextBox1.Text, Integer)
    26.       y = CType(TextBox2.Text, Integer)
    27.       r = CType(TextBox3.Text, Integer)
    28.       FillCircle(g, -y, x, 3, Brushes.Black)
    29.       DrawCircle(g, -y, x, r, Pens.Cyan)
    30.  
    31.     End If
    32.  
    33.     If DrawIt4 Then
    34.       ImDrawing = 4
    35.       x = CType(TextBox1.Text, Integer)
    36.       y = CType(TextBox2.Text, Integer)
    37.       r = CType(TextBox3.Text, Integer)
    38.       FillCircle(g, -x, y, 3, Brushes.Black)
    39.       DrawCircle(g, -x, y, r, Pens.Gold)
    40.  
    41.     End If
    42.  
    43. 'And in your DrawCircle Sub, choose to drawArc or DrawCircle based on whether ImDrawing matches DrawSlow.
    44.   Private Sub DrawCircle(ByVal g As Graphics, ByVal x As Single, ByVal y As Single, ByVal r As Single, ByVal p As Pen)
    45.     If ImDrawing = DrawSlow Then
    46.       g.DrawArc(p, x - r, y - r, r * 2, r * 2, 0, Me.sweepAngle)
    47.     Else
    48.       g.DrawEllipse(p, x - r, y - r, r * 2, r * 2)
    49.     End If
    50.  
    51.   End Sub

    p.s. As already mentioned, you don't need that Private g As Graphics declaration at the top, or setting it in the Form_Load event.
    If you haven't set it in the IDE properties, I would add
    DoubleBuffered = True
    in the Form_Load event, to minimize flashing while you're drawing
    Code:
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        DoubleBuffered = True
      End Sub
    p.s. I think I would change your timer code to make it draw the circle somewhat faster.
    Code:
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.sweepAngle += 3
        If Me.sweepAngle >= 360 Then
          Me.Timer1.Stop()
        End If
        Me.Refresh()
      End Sub
    Maybe allow the user to pick a "speed", by changing that 3 to another value (between 1 and 180).

    p.p.s. Given the colors you're using, drawing on a black background (swap your black and white pens and brushes), helps the circles stand out (other than the blue). If you want to stay with a white background, then probably choose a number of "dark" colors, not yellow and cyan.Name:  CircleGraph.jpg
Views: 561
Size:  27.6 KB
    The blue is the upper right, which is extremely hard to see on the image in the forum, since they've shrunk the image. The shrunk image makes it hard to see a few of the other circles, but in normal size on your screen, it is just the blue that doesn't stand out.
    Thanks big time Sir Passel for the help and for reminding me again about that graphics declaration on the Form_Load event I'll keep in mind all of your suggestions. You really helped me a lot!

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Graph a Circle with Delay Function

    Quote Originally Posted by angel06 View Post
    I want to draw gradually each of the circles. For example, I input the center of the circle: (60,30) and its radius: 60. Then I'll click on Draw Circle button. This should draw the circle gradually on the coordinate plane. And if I'll click on the octant 2 button, this should also draw the circle gradually on the second octant. I have eight octant buttons all in all.
    I'd just like to point out that it wasn't until post #8 that you even mentioned that there were multiple circles involved. In future, please provide a FULL and CLEAR explanation of the issue up front. Otherwise, you're expecting us to know things that we can't possibly know.

Tags for this Thread

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