locked
VB for interactive graphic plotting RRS feed

  • Question

  • I am totally new to VB (but have some experience with the old style programming) and want to decide if I should learn VB so that I can make a desktop program for the following function.

    In an X-Y coordinate system (set up by VB), I want to plot a curve based on an equation (you can assume the curve is simply a straight line, if you want a more specific description) that connects two points in the coordinate system. I need to be able to move the points by mouse, and  need the program to show the coordinates of the points and the slope of the line connecting them.

    If this is doable with VB, please tell me which parts of VB I should study. I do not intend to be proficient in the entire VB language.

    Thank you very much.

    Thursday, July 23, 2020 10:42 PM

All replies

  • Hi

    Yes, that is very much within the scope of VB.NET capabilities.

    Presumably, you have the maths understanding,

    The movement of the points is straightforward drag/drop type of thing. The connection between points could be straight line or perhaps Spline Curves - again, reasonably straightforward.

    The coordinates are readily available, and the slope readily calculated from the coordinates.

    Here is an example from a dynamic scenario where there are incoming data points ploted with spline curve connections. (non-draggable points in this example)


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 23, 2020 11:18 PM
    • Proposed as answer by KHURRAM RAHIM Sunday, November 22, 2020 3:56 PM
    Thursday, July 23, 2020 11:17 PM
  • Thank you for your confirmation that this is something doable with VB. I will study it a little more.
    Friday, July 24, 2020 12:13 AM
  • Hi

    Here is the code to produce the output in the image I posted. If you look through it, the 'work' is done in the Sub Pan_Paint, and there is very little needed. Most of the rest of the code is for simulating incoming data and to scale the Image so can be ignored.

    This is a stand alone example and just needs a blank Form1 and copy/replace default code.

    ' blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim WithEvents pan As New Panel
      Dim WithEvents HS As New HScrollBar
      Dim WithEvents TBx As New TrackBar
      Dim WithEvents TBy As New TrackBar
      Dim Lab, lab2 As New Label
      Dim pts As New List(Of PointF)
      Dim rects As New List(Of Rectangle)
      Dim rect As New Rectangle(0, 0, 4, 4)
      Dim myPen As New Pen(Color.Blue, 4)
      Dim xOffset As Single = 0D
      Dim scx As Single = 1D
      Dim scy As Single = 0.5D
    
      Dim r As New Random
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = New Size(700, 500)
        DoubleBuffered = True
        ' set up various controls
        With pan
          .Width = ClientSize.Width - 20
          .Height = ClientSize.Height - 100
          .Location = New Point(10, 10)
          .BorderStyle = BorderStyle.FixedSingle
          .BackColor = Color.FromKnownColor(KnownColor.PaleGoldenrod)
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
        End With
        With HS
          .Width = pan.Width
          .Height = 20
          .BackColor = Color.FromKnownColor(KnownColor.Pink)
          .Location = New Point(0, pan.Bottom - 28)
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
          .Maximum = 0
        End With
        With TBx
          .LargeChange = 1
          .SmallChange = 1
          .Maximum = 20
          .Minimum = 0
          .Location = New Point(pan.Left + 20, pan.Bottom + 8)
          .Width = pan.Width \ 2
          .Value = 10
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
        End With
        With TBy
          .LargeChange = 1
          .SmallChange = 1
          .Minimum = 2
          .Maximum = 20
          .Location = New Point(TBx.Left, TBx.Bottom + 8)
          .Width = pan.Width \ 2
          .Value = 11
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
        End With
        With Lab
          .AutoSize = True
          .Location = New Point(TBy.Right + 20, TBy.Top - 10)
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
          .BorderStyle = BorderStyle.FixedSingle
          .BackColor = Color.FromKnownColor(KnownColor.PaleGoldenrod)
          .Font = New Font("Arial", 14)
          .Text = "Data Points"
        End With
        With lab2
          .AutoSize = True
          .Location = New Point(TBx.Right + 20, TBx.Top)
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
          .Font = New Font("Arial", 14)
          .Text = "Click Panel to Pause/Resume"
        End With
    
        pan.Controls.Add(HS)
        Controls.AddRange({pan, TBx, TBy, Lab, lab2})
        ' -----------------------------
    
        scx = CSng(TBx.Value / 10)
        scy = CSng(TBy.Value / 5)
    
        Timer1.Interval = 120
        Timer1.Enabled = True
    
      End Sub
      Private Sub Pan_Paint(sender As Object, e As PaintEventArgs) Handles pan.Paint
        If pts.Count < 2 Then Exit Sub
        e.Graphics.TranslateTransform(xOffset, pan.Height \ 5)
        e.Graphics.ScaleTransform(scx, scy)
        e.Graphics.DrawCurve(myPen, pts.ToArray)
        For Each re As Rectangle In rects
          e.Graphics.FillRectangle(New SolidBrush(Color.Red), re)
        Next
      End Sub
      Private Sub pan_Resize(sender As Object, e As EventArgs) Handles pan.Resize
        HS.LargeChange = pan.Width - 40
        HS.SmallChange = pan.Width \ 6
      End Sub
      Private Sub HS_Scroll(sender As Object, e As ScrollEventArgs) Handles HS.Scroll
        xOffset = -HS.Value
        pan.Invalidate()
      End Sub
      Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Static c As Integer = 0
        ' Simulate incoming data points
        Static x As Integer = 0
        x += Timer1.Interval \ 100
        Dim rand As Integer = r.Next(-20, 100)
        pts.Add(New Point(x * 5, rand))
        rects.Add(New Rectangle(x * 5 - 4, rand - 2, 8, 4))
        HS.Maximum = CInt(pts.Last.X * scx)
        Lab.Text = "Pts: " & pts.Count.ToString
        If c Mod 4 = 0 Then pan.Invalidate()
        ' set slight time diff for arrival
        Timer1.Interval = r.Next(150, 300)
        c += 1
      End Sub
      Private Sub Tbx_Scroll(sender As Object, e As EventArgs) Handles TBx.Scroll
        scx = CSng(TBx.Value / 10)
        If scx < 0.01 Then scx = 0.01
      End Sub
      Private Sub Tby_Scroll(sender As Object, e As EventArgs) Handles TBy.Scroll
        scy = CSng(TBy.Value / 5)
        If scy < 0.01 Then scy = 0.01
      End Sub
      Private Sub pan_MouseClick(sender As Object, e As MouseEventArgs) Handles pan.MouseClick
        Timer1.Enabled = Not Timer1.Enabled
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Friday, July 24, 2020 12:18 AM
  • This is really a great help. Thanks again.
    Friday, July 24, 2020 4:02 AM
  • Hi MDSmithC,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 30, 2020 2:34 AM