Results 1 to 4 of 4

Thread: Simple trig gone drastically wrong

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160

    Unhappy Simple trig gone drastically wrong

    hi this program should draw a needle like line for a guage

    but i have got the lengths wrong for the needle when it is on different angles, can anyone make sense of where i have gone wrong?

    VB Code:
    1. Private Sub Timer1_Timer()
    2. Dim currenttemp, selectedtemp, a, b, c As Integer
    3. selectedtemp = selectedtemptext.Text
    4. currenttemp = Int(((selectedtemp + 15) - selectedtemp + 1) * Rnd + selectedtemp)
    5. 'read currenttemp
    6. Select Case currenttemp
    7. Case Is = selectedtemp: a = 0
    8. Case Is = (selectedtemp + 1): a = 12
    9. Case Is = (selectedtemp + 2): a = 24
    10. Case Is = (selectedtemp + 3): a = 36
    11. Case Is = (selectedtemp + 4): a = 48
    12. Case Is = (selectedtemp + 5): a = 60
    13. Case Is = (selectedtemp + 6): a = 72
    14. Case Is = (selectedtemp + 7): a = 84
    15. Case Is = (selectedtemp + 8): a = 96
    16. Case Is = (selectedtemp + 9): a = 108
    17. Case Is = (selectedtemp + 10): a = 120
    18. Case Is = (selectedtemp + 11): a = 132
    19. Case Is = (selectedtemp + 12): a = 144
    20. Case Is = (selectedtemp + 13): a = 156
    21. Case Is = (selectedtemp + 14): a = 168
    22. Case Is = (selectedtemp + 15): a = 180
    23. Case Is > (selectedtemp + 15): a = 180
    24. Case Is < selectedtemp: a = 0
    25. Case Else
    26. End Select
    27. '12,24,36,48,60,72,84,96,108,120,132,144,156,168,180
    28. 'a = currenttemp - selectedtemp
    29.  
    30. Cls
    31. If currenttemp < (selectedtemp + 1) Then
    32.     'set g to 0 if current temp is colder then selected temp
    33.     'and draw meter to 0
    34.     g = 0
    35.     Line1.X1 = 500
    36.     Line1.Y1 = 500
    37.     Line1.X2 = 140
    38.     Line1.Y2 = 500
    39.     Else
    40.     If a > 179 Then
    41.         'set g = 1 and draw meter at 180 because temp is too hot
    42.         g = 1
    43.         Line1.X1 = 500
    44.         Line1.Y1 = 500
    45.         Line1.X2 = 860
    46.         Line1.Y2 = 500
    47.         Else
    48.         If a > -1 And a < 91 Then
    49.             'draw meter if temp falls on left hand side
    50.             b = -(360 * (Sin((a) * 0.017453292)))
    51.             c = -(360 * (Cos((a) * 0.017453292)))
    52.             'Line (1000, 1000)-Step(c, b)
    53.             Line1.X1 = 500
    54.             Line1.Y1 = 500
    55.             Line1.X2 = c
    56.             Line1.Y2 = b
    57.             Else
    58.             If a > 90 And a < 181 Then
    59.                 'draw meter if temp falls on right hand side
    60.                 b = (360 * (Sin((a - 90) * 0.017453292)))
    61.                 c = -(360 * (Cos((a - 90) * 0.017453292)))
    62.                 'Line (1000, 1000)-Step(b, c)
    63.                 Line1.X1 = 500
    64.                 Line1.Y1 = 500
    65.                 Line1.X2 = (b + 500)
    66.                 Line1.Y2 = c
    67.             End If
    68.         End If
    69.     End If
    70. End If
    71. End Sub

    thanks for taking the time to even look at this.
    Last edited by Jack Daniels; Aug 1st, 2002 at 04:03 AM.
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  2. #2
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking I'll try :p

    Not *exactly* sure what you're trying to do, but i'll help out anyway.
    My suggestion is mainly to do with shotening your program.

    1) Use loops:
    VB Code:
    1. For loopval = 0 to 15
    2. if currenttemp = selectedtemp + loopval then a = 12*loopval
    3. next loopval
    4.  
    5. If currenttemp> (selectedtemp + 15) then a = 180
    6. If currenttemp< selectedtemp then a = 0

    As for your gauge, what you need to do is something like this:
    VB Code:
    1. Line(fix_point_x, fix_point_y)-(fix_point_x + sin(Degree_Val*180/Pi), fix_point_y+ cos(Degree_Val*180/Pi))

    where:
    Pi = 3.14159
    fix_point_x = the x value of where the meter's needle is fixed.
    fix_point_y = the y value of where the meter's needle is fixed.
    Degree_Val = the angle of the needle. (0 = right, 90 = up, 180 = left, 270 = down)

    N.B. the cos() and sin() might be around the wrong way.
    AND: the 180/Pi *might* be Pi/180, i'm not sure exactly how to convert radians to degrees.
    sql_lall

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Blue Mountains NSW Australia
    Posts
    160
    ahh yes thank you

    but the conversion was one thing i did know
    180/Pi is radians to degrees
    Pi/180 is degrees to radians
    Jack Daniels
    ICQ: 72074030
    VB 6.0

  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking OK

    OK, hope it works.
    I had better try to remember which way round, as i'm sure i'll have to use it soon.

    Anyway, GO AUS!! over 40 Gold medals in Man, UK
    sql_lall

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