Results 1 to 5 of 5

Thread: Drawing a rotating needle line

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Location
    Telford, UK
    Posts
    10

    Drawing a rotating needle line

    Hiya people

    Wondered if anyone could help with drawing a line on a gauge I am doing for a flight sim please?

    I have got the following code for drawing a rotating needle on screen:

    Code:
    linA.X1 = Cos((intDataValue * PI) / 125) * 83 + linA.X2
    linA.Y1 = Sin((intDataValue * PI) / 125) * 83 + linA.Y2
    But it is not drawing in the right place!

    The gauge goes from 0 to 250. PI is set as 3.14159. And intDataValue is the speed the sim is reporting from 0 to 250...

    The gauge starts with the needle vertical at the following coordinates:

    x1, y1 - 88, 5
    x2, y2 - 88, 88

    And the gauge is shown below...


    Many thanks in advance

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Drawing a rotating needle line

    Ok, some clarifications and then explanations.

    1. The guage is actual size?
    2. If so, 83 is the length of the needle from guage edge to gauge edge or close to that?
    3. I assume the object holding the gauge image has scalemode = vbPixels?

    If above is correct, here is your calculation and you may want to hard code the length/2 value.

    Code:
    Dim PI_Radians As Double
    
    Private Sub Form_Load()
        PI_Radians = 1 / ((Atn(1) * 4) / 180)
    End Sub
    
    ' here's your new calc
    LinA.X1 = Cos(((intDataValue * 1.44) - 90) / PI_Radians) * (83 / 2) + LinA.X2
    LinA.Y1 = Sin(((intDataValue * 1.44) - 90) / PI_Radians) * (83 / 2) + LinA.Y2
    What is 1.44? A full circle is 360degrees. Well your guage is 250. So we need to offset the speed value by the ratio: 360/250 = 1.44

    Why use -90? At 0 degrees we have a flat horizontal line, but you want 0 degrees to be at 12:00 high, so we subtract 90 degrees

    What is that PI_Radians stuff? In VB we use Radians not degrees. There is a simple ratio to convert degrees to radians:
    :: DegreesToRadians = degrees / 57.29578 ... or
    :: DegreesToRadians = (degrees*PI)/180, but more calcs so slower
    So how does 57.29578 come about,
    a. PI can be calculated as Atn(1)*4
    b. Then 1/(PI/180)) will give us 57.29578-ish

    Why divide your length in half? Because we want the end point rotated at distance gauge length/2, not the full length (see assumption 2 above)

    Edited: Final note. Place your line's X2,Y2 at the center of the gauge, then extend X1,Y1 out to the edge of the gauge's circular boder, vertically. Subtract Y1 from Y2 and that should be the value you use for 83/2 above. Ugh, I hate trig
    Last edited by LaVolpe; Jan 25th, 2010 at 11:53 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Drawing a rotating needle line

    Here's my effort, I fall into the 'likes trig' camp
    Code:
    Option Explicit
    Private Const PI As Double = "3.141592653589793" '<- coerce from string to max double
    
    Private Sub Form_Load()
        linA.X2 = picSpeedo.ScaleWidth / 2!
        linA.Y2 = picSpeedo.ScaleHeight / 2!
        
        Speedo 180 '<- just to demo Speedo sub
    End Sub
    
    Private Sub Speedo(ByVal lngDataValue As Long)
    Const MAX As Long = 250 'total units in full circle
    Const SZ As Double = 40 'length of needle in scaleunits
    Dim rad As Double
    
        If lngDataValue < 0 Or lngDataValue > MAX Then lngDataValue = 0
        
        rad = PI * 2# * (lngDataValue / MAX)
        'to make 0 vertical and +ve clockwise cos -> sin and sin -> -cos
        linA.X1 = linA.X2 + (Sin(rad) * SZ)
        linA.Y1 = linA.Y2 - (Cos(rad) * SZ)
    End Sub
    I tried it with the pic you showed so I adjusted the needle length to fit pixels.
    W o t . S i g

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Location
    Telford, UK
    Posts
    10

    Re: Drawing a rotating needle line [Solved]

    Thank you very much LaVolpe

    The only bit of the code I had to change was the length of the line as I need it to stay at 83. So I just removed the division by 2 of the needle length from the code (as the graphic shown is just a thumbnail of the actual forms background image - it is actually 179 x 179 pixels)

    Cheers
    Glyn
    Last edited by glynd02; Jan 26th, 2010 at 06:00 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Location
    Telford, UK
    Posts
    10

    Re: Drawing a rotating needle line

    Thanks Milk

    I'm in the "what the heck is trig" and "please nooooo trig - ahh my head is hurting" camps

    Cheers

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