Visual Studio Graphic Draw Line Pickture Box

  1. #1

    civiltem is offline

    Thread Starter

    New Fellow member


    Question How to create arc with chords in a pic box


  2. #ii

    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 class and handle its Paint consequence, and so do your drawing in the result handler. Y'all can find plenty of data and examples of using GDI+ around this site and the web. If you follow the CodeBank link in my signature below, you lot'll find iii threads containing examples of GDI+ cartoon. In your instance, yous'd call the DrawArc, DrawLine and DrawString methods of the Graphics object provided by the result handler. The maths to summate what and where to depict will be basically the same as if yous were doing this by mitt with pen and paper.

  3. #3

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

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

    If I simply needed to draw it, I might use the arc itself as a clipping region, then but drawn north evenly spaced vertical lines and let the lines be visually clipped by the drawing process rather than past me computing the finish points.


  4. #4

    civiltem is offline

    Thread Starter

    New Member


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

    Code:

    Public Form Form1     Individual startAngle As Single = 180.0F     Individual sweepAngle Every bit Single = 180.0F     Private Sub Form1_Load(sender As Object, eastward Every bit EventArgs) Handles MyBase.Load      Finish Sub     Individual Sub Form1_Paint(ByVal sender As System.Object, ByVal due east Equally Arrangement.Windows.Forms.PaintEventArgs) Handles MyBase.Paint         ' Create pen.         Dim blackPen As New Pen(Color.Black, 3)         Dim redPen Every bit New Pen(Color.Cerise, iii)         Dim rect As New Rectangle(20, 20, 200, 200)         ' Create points that define line.         Dim point1 As New Point(twenty, 120)         Dim point2 As New Point(220, 120)         ' Draw arc to screen.         eastward.Graphics.DrawArc(redPen, rect, startAngle, sweepAngle)          ' Draw line to screen.         east.Graphics.DrawLine(blackPen, point1, point2)         redPen.Dispose()     Stop Sub Stop Class
    i am able to form arc with chord but unable to course north number of offsets (which is shown in the above figure)

  5. #5

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

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

  6. #6

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

    Well, I figured a unproblematic approach to the math, i.due east. how to draw n lines by dividing the arc into n+1 areas, merely I'll leave that to you lot.

    Y'all didn't answer my question of whether the point of the exercise was to summate the intersection of the line segment of the chord with the arc, or only to describe them.

    Equally I stated, I call up only drawing vertical lines the height of your rectangle without actually computing the end point of where it hits the arc would exist easiest, just letting the graphics clip the line based on your setting a clipping region based on your arc.

    I'll testify you setting the region in your cartoon, then draw a couple of arbitrary lines so you see them being clipped. Y'all'll need to figure how to infinite the lines based on due north as jmcilhinney said.

    Code:

    Public Class Form1   Private startAngle As Single = 180.0F   Private sweepAngle Every bit Single = 180.0F    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As Arrangement.Windows.Forms.PaintEventArgs) Handles MyBase.Paint     ' Create pen.     Dim blackPen As New Pen(Color.Black, three)     Dim redPen As New Pen(Color.Red, 3)     Dim rect As New Rectangle(20, twenty, 200, 200)     ' Create points that define line.     Dim point1 As New Point(20, 120)     Dim point2 As New Point(220, 120)     ' Describe arc to screen.     e.Graphics.DrawArc(redPen, rect, startAngle, sweepAngle)      ' Depict line to screen.     e.Graphics.DrawLine(blackPen, point1, point2)     redPen.Dispose()      'limit farther 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 run into them clipped     point1.X = 50     e.Graphics.DrawLine(blackPen, point1, New Point(point1.X, twenty))     point1.X = 100     e.Graphics.DrawLine(blackPen, point1, New Bespeak(point1.X, 20))        blackPen.Dispose()   End Sub   Terminate Class
    Concluding edited past passel; Mar 1st, 2018 at 04:28 AM.

  7. #7

    civiltem is offline

    Thread Starter

    New Member


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

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

  8. #8

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

    How is text usually drawn using GDI+? It would be exactly the aforementioned hither.

  9. #ix

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

    Every bit jmcihinney is alluding, in case y'all don't know, in that location is a type that is used to concord a series of characters. The blazon is called a String.
    When you lot type in e.Graphics.Draw in the IDE you should be shown a list of different methods that begin with the word "Depict", such as DrawArc and DrawLine which you've already used above. Just wait downwardly the list and encounter if anything looks like it may exist used to draw a string.

    Of class, that may non accept been your question, as you weren't specific.
    Y'all may have been asking how to create the strings, "s1", "s2", etc. based on your loop index.
    For that there is a number of means you lot could create the string, such as String.Format or Cord.Append with the .ToString method used to catechumen your index number to a cord.

    Let the states know if y'all take a specific problem. This site is dedicated to helping you with your code, only if yous haven't attempted to write some lawmaking, yous don't have any code for united states of america to help y'all with.


  10. #ten

    civiltem is offline

    Thread Starter

    New Member


    Re: How to create arc with chords in a movie 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 lawmaking helps in getting the output
    thank you

  11. #11

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

    If that helps, so so exist information technology.
    Things to be aware of.

    A number of classes, particularly when dealing with graphics, require creating objects or resources that need to exist cleaned up when the object goes out of scope.
    A Dispose method is provided then that you tin accept the object release the created objects or resources as shortly equally possible. You should phone call the Dispose method for whatsoever objects yous create that provide the Dispose method.

    I would stay abroad from creating any objects as parameters to a method since yous won't have the opportunity to call the Dispose method of those objects since y'all won't accept a reference to them.

    Some things, such as a Point blazon, are non a grade but are a construction, and a structure is what is known every bit a Value type. The variable, or argument in this example 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 upward necessary when the value goes out of scope. The memory allocated for the construction will exist reclaimed immediately when the method returns (information technology is allocated temporarily in a memory surface area know as the stack which increases equally needed with each telephone call and shrinks dorsum to its previous allocation with each return. (Technically the stack isn't changing size, simply a "stackpointer" that indicates how much of the stack is is used, i.e. where the current meridian of the stack is, is decremented or incremented to classify or deallocate stack memory for the current method being called or returned from).

    So, using New to create a Point has no pregnant overhead and is ok to employ as an statement to a method, although if the method provides the option of using X,Y in place of a point, and I don't already take an existing signal to pass, I would commonly but pass the X and Y values rather than create a point. Simply even that may be a bit of a trade off since now y'all're passing ii parameters to the method, instead of one so it may be argued that neither mode makes a significant difference in that case.

    But, dorsum to the classes that provide a Dispose method. In those cases, you should instantiate an object of the course before calling the method, and laissez passer that instantiated object to the method. That manner you tin call the Dispose method on those objects when you no longer demand them, i.e. later the method returns, or later on if y'all demand to use the object for more than one method call, specially if you lot'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 style would be:

    Code:

    Dim fnt As New Font("Veranda", 8) Dim fmt as New StringFormat(StringFormatFlags.DirectionVertical)  eastward.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 Every bit New StringFormat(StringFormatFlags.DirectionVertical)         due east.Graphics.DrawString("O = " & "i", fnt, Brushes.DarkBlue, 112, 120, fmt)       Cease Using     End Using
    In the examples, you tin can see I passed X,Y values rather than use New Point(112, 120). There may exist no pregnant reason to do that. I just ordinarily avert using New when I tin can as I associate using New to create things additional overhead that is not necessary when you tin 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 ii 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 instance of a SolidBrush object, information technology has a Dispose method that should be chosen when you're done with the brush, so that is additional processing needed to clean upwards.

    The fact that it takes time to create a brush and clean up a brush, but a castor is a necessary thing to use with many graphical cartoon methods (as are Pens) that a full set (i.e. one for each color defined in the known colour enumeration) of brushes and pens are already created for you to use every bit needed then you lot don't have to create and dispose your own versions if all you lot need is a standard solid brush, or a standard Pen (i.due east. with a width of i).
    And so, I don't create a SolidBrush of color DarkBlue and have to dispose of information technology subsequently, I merely employ the DarkBlue brush that already exists in the Brushes class.

    Bottom line, if I'thou going to apply an object multiple times in my paint method, I'll mostly create it at the start of the paint upshot handler and dispose of it at the cease of the pigment handler, or in the case of a font that I may want to use in more that ane control's paint outcome, I would probably create it once at the form class scope, and merely use it everywhere needed and just dispose of it when the class is endmost (FormClosing event handler).

    Final edited by passel; Mar seventh, 2018 at 03:xix AM.

Tags for this Thread

Posting Permissions

  • You may not mail service new threads
  • You may not post replies
  • You may non mail service attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML lawmaking is Off

Click Here to Expand Forum to Full Width

jamespeare1953.blogspot.com

Source: https://www.vbforums.com/showthread.php?859443-How-to-create-arc-with-chords-in-a-picture-box

0 Response to "Visual Studio Graphic Draw Line Pickture Box"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel