Results 1 to 9 of 9

Thread: [RESOLVED] (basically) Plotting a circle in VB5

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    Resolved [RESOLVED] (basically) Plotting a circle in VB5

    Hello,
    I am trying to create a "simple" wind direction indicator (in VB5) in which I type the wind direction (in degrees) into a textbox and a small diagram is created. At first I thought this would be simple! However, since I have literally no understanding of the maths involved, and am relatively new to programming, I am just totally confused (to say the least).

    My thought was to create a multi-dimensional array called DEGREES with 360 items, each item holding the (extremely mysteriously calculated) X, Y value of a "degree" of a circle. Then using PSET I would plot the 360° circle around a given CENTER point with a specified radius of about 1 inch (all drawn inside a frame or picturebox so I can set it's visible property depending on the circumstances).

    It seems it would then be easy to draw an arrow from DEGREES(X: ie 225) to CENTER , thus showing the wind direction (actually, it shows where the wind is coming from - a direction of 360 is "out of" the north so the arrow head would be in the center and the end of the line would be at 360°).

    First, does this seem like a reasonable approach or are there easier solutions?

    Secondly, I am just totally clueless regarding the math, the more I try to research the more confused I get. I find plenty of formulas, however they appear to be in a foreign language to me - plus I have no idea how to actually translate them into a program!

    So... I'm wondering if anyone would be willing to help me create such a routine. Or possibly just create one and document it really well so I can learn AND understand what's going on for future reference.

    This is just a personal project. It will not be used in any commercial undertaking whatsoever. I'm just trying to expand my programming skills with a project that is usefull to me.

    I greatly appreciate any and all help!

    Thank you,
    David

    p.s. I now realize why I should have been paying attention in my math classes (that was forty years ago and I was going to be a "rock" star anyway...) I fully intend to find a good online math course now!

  2. #2
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: (basically) Plotting a circle in VB5

    as i understand it you want a circle with an arrow pointing and an angle. Well do draw the circle you can use the forms circle method. VB5 I would imagine uses radians. to convert from degrees to radians by radians = (degrees/180)* pi.
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  3. #3
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: (basically) Plotting a circle in VB5

    heres some VB code i whiped up, hopefully it will work in vb5

    Code:
    Option Explicit
    Private Const Pi As Double = 3.141592654
    Private Const circleDiameter As Integer = 100
    Dim cy As Integer
    Dim cx As Integer
    Dim oldY As Single
    Dim oldx As Single
    
    Private Sub Command1_Click()
    Dim angle As Double
    angle = (txtDirection / 180) * Pi
    
    Me.Line (cx, cy)-(oldx, oldY), Me.BackColor
    
    oldY = cy + Sin(angle - (0.5 * Pi)) * circleDiameter
    oldx = cx + Cos(angle - (0.5 * Pi)) * circleDiameter
    
    Me.Line (cx, cy)-(oldx, oldY)
    
    End Sub
    
    Private Sub Form_Load()
        Me.ScaleMode = vbPixels
        Me.AutoRedraw = True
        cx = Me.ScaleWidth / 2
        cy = Me.ScaleHeight / 2
        Me.Circle (cx, cy), circleDiameter, vbBlack
    End Sub
    You will need a command button and a textbox named txtDirection
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    Red face Re: (basically) Plotting a circle in VB5

    Hi Rich,

    The code runs fine and indeed does exactly what I was trying to accomplish!

    However, would it be possible for you to explain the concept taking place in the commandclick() routine? I just do not seem to be able to grasp the basic principles underlying the code.

    Thank you very much for your help.

    David

  5. #5
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: (basically) Plotting a circle in VB5

    glad i understood the question which is always a good start right the code.

    Code:
    Private Sub Command1_Click()
    Dim angle As Double
    angle = (txtDirection / 180) * Pi
    
    Me.Line (cx, cy)-(oldx, oldY), Me.BackColor
    
    oldY = cy + Sin(angle - (0.5 * Pi)) * circleDiameter
    oldx = cx + Cos(angle - (0.5 * Pi)) * circleDiameter
    
    Me.Line (cx, cy)-(oldx, oldY)
    
    End Sub
    Line by line sorry if i cover stuff you already know.

    Declare the angle as a double since radians are horibble numbers like 1.4543534, and 3.34234234 well they are based on PI

    Convert the angle (degs) to radians, radians are in terms of PI. Pi is equivalent to 180 degs so we divide by 180 and times by Pi.

    The first draw line draws over any old line to remove it from the screen.

    To calculate the new coordinates we use the sine of the angle and the cos of the angle added to the center points. Cos and sine are trigonometric ratios, they are opposite so you can fine the difference inX and the difference in Y from a diagonal line and its length. Take 45 degrees, the DX is the same as the DY, therefore cos(45) = sine(45).
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    Re: (basically) Plotting a circle in VB5

    Hi Rich,
    I should probably change the subject line to something like "Thick as a brick..."

    I understand the conversion to degrees... and that's about it!

    I don't quite understand the sine/cos and their relationship to the solution.

    If you have some time, would you mind explaining each of the variables in each of the equations? Things like, I understand that oldx and oldy are neeed to overdraw the previous line. But I don't understand why (or how) they come in to play when drawing the new line.

    Also, try as I might, I can't seem to boil the starting and ending points of the line down to fit with-in a PSET statement (the line gets jagged looking so I thought I'd rewrite it using PSET).

    I really GREATLY appreiciate the time and efforts you have spent on trying to educate me and do not want to take too much advantage of you!

    Cheers!

    David

  7. #7
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: (basically) Plotting a circle in VB5

    Thats correct the oldY and oldX are used to redraw over the old line. But when the new line is draw it becomes the old line. So oldY and oldX are updated with the new lines coordinates and it is drawn using them. Then the oldy and oldx are ready to use when the line we have just drawn needs to be erased.

    Well with sine and cosine the best thing to do is do some examples.

    1) if i have a line of length 10 at an angle of 45 degree, what is the distance in the x direction and y direction traveled.

    Well we know from logic that a line at 45 degrees goes the same distance across as it does up. So in theory sin 45 and cos 45 are the same. Try it on your calculator. thats and easy example but:

    If we had a line of 10 at an angle of 30 thats not so easy. Just use sin 30 to get the distance traveled along the y axis and use cos 30 to get the distance along the x axis.

    Draw youself some triangles with a length for the hypotenus and try working out the other sides .

    Pset wont make a difference to the lines jagedness, just extra trouble, fire up MS paint and draw a line, still jagged. If you want your line to apear smooth you will have to use anti aliasing but thats another story.
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    Re: (basically) Plotting a circle in VB5

    Hi Rich,

    Thank you very much. I think I'm getting it! I'm going to spend some time with some triangles and do more study of basic geometry and trig.

    You've been a great help. I really appreciate it.

    Sincerely,

    David

  9. #9
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: (basically) Plotting a circle in VB5

    No problem
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

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