Results 1 to 11 of 11

Thread: How to create arc with chords in a picture box

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    8

    Question How to create arc with chords in a picture box

    Name:  curve1.jpg
Views: 870
Size:  7.5 KB
    Name:  curve2.jpg
Views: 775
Size:  8.1 KB
    How to create an arc with chord and also to draw offsets inside the arc, the number of offsets required is inputted from the text box. The image output has to be developed in vb.net windows form application using visual studio
    The above pictures shows the image to get in the picture box (first picture offset 3 and second picture offset 5) the offset value has to be entered in "n" numbers depends on each case

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

    Re: How to create arc with chords in a picture box

    Drawing in Windows Forms is done using GDI+. You would add a PictureBox to your form and handle its Paint event, then do your drawing in the event handler. You can find plenty of information and examples of using GDI+ around this site and the web. If you follow the CodeBank link in my signature below, you'll find three threads containing examples of GDI+ drawing. In your case, you'd call the DrawArc, DrawLine and DrawString methods of the Graphics object provided by the event handler. The maths to calculate what and where to draw will be basically the same as if you were doing this by hand with pen and paper.

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

    Re: How to create arc with chords in a picture box

    I guess a question might be do you need to know the values, i.e. the math behind calculating the intersection point of a line with the arc, or do you just need to draw it.

    If I just needed to draw it, I might use the arc itself as a clipping region, then just drawn n evenly spaced vertical lines and let the lines be visually clipped by the drawing process rather than by me calculating the end points.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    8

    Re: How to create arc with chords in a picture box

    Code:
    Public Class Form1
        Private startAngle As Single = 180.0F
        Private sweepAngle As Single = 180.0F
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            ' Create pen.
            Dim blackPen As New Pen(Color.Black, 3)
            Dim redPen As New Pen(Color.Red, 3)
            Dim rect As New Rectangle(20, 20, 200, 200)
            ' Create points that define line.
            Dim point1 As New Point(20, 120)
            Dim point2 As New Point(220, 120)
            ' Draw arc to screen.
            e.Graphics.DrawArc(redPen, rect, startAngle, sweepAngle)
    
            ' Draw line to screen.
            e.Graphics.DrawLine(blackPen, point1, point2)
            redPen.Dispose()
        End Sub
    End Class
    i am able to form arc with chord but unable to form n number of offsets (which is shown in the above figure)

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

    Re: How to create arc with chords in a picture box

    You need to store the value n in a field and then read it in the Paint event handler. Logically, you would use a For loop from 1 to n to draw the lines. Obviously you'll need some basic arithmetic to calculate the distance between the lines.

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

    Re: How to create arc with chords in a picture box

    Well, I figured a simple approach to the math, i.e. how to draw n lines by dividing the arc into n+1 areas, but I'll leave that to you.

    You didn't answer my question of whether the point of the exercise was to calculate the intersection of the line segment of the chord with the arc, or simply to draw them.

    As I stated, I think just drawing vertical lines the height of your rectangle without actually calculating the end point of where it hits the arc would be easiest, just letting the graphics clip the line based on your setting a clipping region based on your arc.

    I'll show you setting the region in your drawing, and then draw a couple of arbitrary lines so you see them being clipped. You'll need to figure how to space the lines based on n as jmcilhinney said.
    Code:
    Public Class Form1
      Private startAngle As Single = 180.0F
      Private sweepAngle As Single = 180.0F
    
      Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        ' Create pen.
        Dim blackPen As New Pen(Color.Black, 3)
        Dim redPen As New Pen(Color.Red, 3)
        Dim rect As New Rectangle(20, 20, 200, 200)
        ' Create points that define line.
        Dim point1 As New Point(20, 120)
        Dim point2 As New Point(220, 120)
        ' Draw arc to screen.
        e.Graphics.DrawArc(redPen, rect, startAngle, sweepAngle)
    
        ' Draw line to screen.
        e.Graphics.DrawLine(blackPen, point1, point2)
        redPen.Dispose()
    
        'limit further drawing to the inside of the arc
        Dim gp As New Drawing2D.GraphicsPath
        gp.AddArc(rect, startAngle, sweepAngle)
        e.Graphics.SetClip(gp)
    
        'draw two arbitrary vertical lines to see them clipped
        point1.X = 50
        e.Graphics.DrawLine(blackPen, point1, New Point(point1.X, 20))
        point1.X = 100
        e.Graphics.DrawLine(blackPen, point1, New Point(point1.X, 20))
      
        blackPen.Dispose()
      End Sub
     
    End Class
    Last edited by passel; Mar 1st, 2018 at 04:28 AM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    8

    Re: How to create arc with chords in a picture box

    Thank you
    is there any possibility to show a text like s1,s2,s3 etc... sn. for each vertical lines

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

    Re: How to create arc with chords in a picture box

    How is text usually drawn using GDI+? It would be exactly the same here.

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

    Re: How to create arc with chords in a picture box

    As jmcihinney is alluding, in case you don't know, there is a type that is used to hold a series of characters. The type is called a String.
    When you type in e.Graphics.Draw in the IDE you should be shown a list of different methods that begin with the word "Draw", such as DrawArc and DrawLine which you've already used above. Just look down the list and see if anything looks like it may be used to draw a string.

    Of course, that may not have been your question, as you weren't specific.
    You may have been asking how to create the strings, "s1", "s2", etc. based on your loop index.
    For that there is a number of ways you could create the string, such as String.Format or String.Append with the .ToString method used to convert your index number to a string.

    Let us know if you have a specific problem. This site is dedicated to helping you with your code, but if you haven't attempted to write some code, you don't have any code for us to help you with.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    8

    Re: How to create arc with chords in a picture box

    Code:
            e.Graphics.DrawString("O = " & Label1.Text, New Font("Verdana", 8), New SolidBrush(Color.DarkBlue), New Point(112, 120), New StringFormat(StringFormatFlags.DirectionVertical))
    The following code helps in getting the output
    thank you

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

    Re: How to create arc with chords in a picture box

    If that helps, then so be it.
    Things to be aware of.

    A number of classes, particularly when dealing with graphics, require creating objects or resources that need to be cleaned up when the object goes out of scope.
    A Dispose method is provided so that you can have the object release the created objects or resources as soon as possible. You should call the Dispose method for any objects you create that provide the Dispose method.

    I would stay away from creating any objects as parameters to a method since you won't have the opportunity to call the Dispose method of those objects since you won't have a reference to them.

    Some things, such as a Point type, are not a class but are a structure, and a structure is what is known as a Value type. The variable, or argument in this case being passed for the parameter of the method, is not a reference to the object, but is the object itself. In that case there is no dispose method associated with it and no clean up necessary when the value goes out of scope. The memory allocated for the structure will be reclaimed immediately when the method returns (it is allocated temporarily in a memory area know as the stack which increases as needed with each call and shrinks back to its previous allocation with each return. (Technically the stack isn't changing size, but a "stackpointer" that indicates how much of the stack is is used, i.e. where the current top of the stack is, is decremented or incremented to allocate or deallocate stack memory for the current method being called or returned from).

    So, using New to create a Point has no significant overhead and is ok to use as an argument to a method, although if the method provides the option of using X,Y in place of a point, and I don't already have an existing point to pass, I would normally just pass the X and Y values rather than create a point. But even that may be a bit of a trade off since now you're passing two parameters to the method, instead of one so it may be argued that neither way makes a significant difference in that case.

    But, back to the classes that provide a Dispose method. In those cases, you should instantiate an object of the class before calling the method, and pass that instantiated object to the method. That way you can call the Dispose method on those objects when you no longer need them, i.e. after the method returns, or later if you need to use the object for more than one method call, especially if you're using the object within a loop.

    There are two ways to "call" the Dispose method. Either explicitly calling it, or by using a Using ... End Using block.
    An example of the first way would be:
    Code:
    Dim fnt As New Font("Veranda", 8)
    Dim fmt as New StringFormat(StringFormatFlags.DirectionVertical)
    
    e.Graphics.DrawString("O = " & Label1.Text, fnt, Brushes.DarkBlue, 112, 120, fmt)
    
    fmt.Dispose()
    fnt.Dispose()
    An example of the second way:
    Code:
        Using fnt As New Font("Veranda", 8)
          Using fmt As New StringFormat(StringFormatFlags.DirectionVertical)
            e.Graphics.DrawString("O = " & "1", fnt, Brushes.DarkBlue, 112, 120, fmt)
          End Using
        End Using
    In the examples, you can see I passed X,Y values rather than use New Point(112, 120). There may be no significant reason to do that. I just normally avoid using New when I can as I associate using New to create things additional overhead that is not necessary when you can reuse an existing instance of an object within the scope of the code.

    The other thing to note in the example is I didn't use New SolidBrush to create a brush.
    There is two reason for that. As I said there is overhead in creating objects, i.e. they take processing time and memory to create them, and in the case of a SolidBrush object, it has a Dispose method that should be called when you're done with the brush, so that is additional processing needed to clean up.

    The fact that it takes time to create a brush and clean up a brush, but a brush is a necessary thing to use with many graphical drawing methods (as are Pens) that a full set (i.e. one for each color defined in the known color enumeration) of brushes and pens are already created for you to use as needed so you don't have to create and dispose your own versions if all you need is a standard solid brush, or a standard Pen (i.e. with a width of 1).
    So, I don't create a SolidBrush of color DarkBlue and have to dispose of it later, I just use the DarkBlue brush that already exists in the Brushes class.

    Bottom line, if I'm going to use an object multiple times in my paint method, I'll generally create it at the start of the paint event handler and dispose of it at the end of the paint handler, or in the case of a font that I may want to use in more that one control's paint event, I would probably create it once at the form class scope, and just use it everywhere needed and only dispose of it when the form is closing (FormClosing event handler).
    Last edited by passel; Mar 7th, 2018 at 03:19 AM.

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