Cos.htm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <html>
  2. <head>
  3. <title>Blitz3D Docs</title>
  4. <link rel=stylesheet href=../css/commands.css type=text/css>
  5. </head>
  6. <body>
  7. <h1>Cos# ( degrees# )</h1>
  8. <h1>Parameters</h1>
  9. <table>
  10. <tr>
  11. <td>
  12. degrees# = angle in degrees.
  13. </td>
  14. </tr>
  15. </table>
  16. <h1>Description</h1>
  17. <table>
  18. <tr>
  19. <td>
  20. For angles between 0 and 90 degrees this is defined by the sides of a right triangle. The cosine is the side adjacent to the angle divided by the hypotenuse. <br />
  21. <br />
  22. Outside of 0 to 90 the definition uses a circle with radius=1. The angle is placed at the center of the circle, with one side on the positive x-axis. The other side hits the circle at some point. The x coordinate of this point is the cosine of the angle. <br />
  23. <br />
  24. The positive y-axis corresonds to +90 degrees. This is a common source of confusion in Blitz. With screen coordinates ( pixels ) the y-axis points downward. But in the 3d world the y-axis typically points upward. <br />
  25. <br />
  26. Another possible snag is the size of the angle. In principle, the cosine function repeats every 360 degrees. So Cos(-360), Cos(0), Cos(360), Cos(720) etc. should all be exactly the same. But in practice the accuracy decreases as the angle gets farther away from zero. <br />
  27. <br />
  28. <br />
  29. See also ASin, Cos, ACos, Tan, Atan, ATan2
  30. </td>
  31. </tr>
  32. </table>
  33. <h1><a href=../2d_examples/Cos.bb>Example</a></h1>
  34. <table>
  35. <tr>
  36. <td>
  37. ; Sin / Cos / Tan example. <br />
  38. <br />
  39. ; Left/Right arrow keys change angle. Escape quits. <br />
  40. <br />
  41. Const width = 640, height = 480 <br />
  42. Const radius# = .2 * height <br />
  43. Const KEY_ESC = 1, KEY_LEFT = 203, KEY_RIGHT = 205 <br />
  44. <br />
  45. Graphics width, height <br />
  46. SetBuffer BackBuffer( ) <br />
  47. Origin width / 3, height / 2 <br />
  48. <br />
  49. angle# = 0.0 <br />
  50. <br />
  51. While Not KeyDown( KEY_ESC ) <br />
  52. <br />
  53. ; NOTE: It is usually best to avoid very large angles. <br />
  54. ; The 'If angle...' lines show one way to do this. <br />
  55. ; Mod is another possibility. <br />
  56. <br />
  57. If KeyDown( KEY_LEFT ) Then angle = angle - .5 <br />
  58. ; If angle < 0.0 Then angle = angle + 360 <br />
  59. <br />
  60. If KeyDown( KEY_RIGHT ) Then angle = angle + .5 <br />
  61. ; If angle >= 360.0 Then angle = angle - 360 <br />
  62. <br />
  63. Cls <br />
  64. <br />
  65. Color 80, 80, 0 ; pale yellow circle <br />
  66. Oval -radius, -radius, 2 * radius, 2 * radius, False <br />
  67. <br />
  68. For a# = 0.0 To Abs( angle Mod 360 ) Step .5 <br />
  69. <br />
  70. x# = radius * Cos( a ) ; (x,y) is a point on the circle <br />
  71. y# = radius * Sin( a ) ; corresponding to angle a. <br />
  72. <br />
  73. If ( angle Mod 360 < 0 ) Then y = -y ; reverse for negative angle <br />
  74. WritePixel x, y, $ffff00 ; bright yellow <br />
  75. <br />
  76. Next <br />
  77. <br />
  78. Color 255, 255, 0 ; yellow <br />
  79. Line 0, 0, radius * Cos( angle ), radius * Sin( angle ) <br />
  80. <br />
  81. Color 0, 255, 0 ; green <br />
  82. Line 0, 0, radius * Cos( angle ), 0 <br />
  83. Text radius * 1.5, 10, "Cos( angle ) = " + Cos( angle ) <br />
  84. <br />
  85. Color 255, 0, 0 ; red <br />
  86. Line radius * Cos( angle ), 0, radius * Cos( angle ), radius * Sin( angle ) <br />
  87. Text radius * 1.5, -10, "Sin( angle ) = " + Sin( angle ) <br />
  88. <br />
  89. Color 255, 255, 255 <br />
  90. Text radius * 1.5, -30, " angle = " + angle <br />
  91. Text radius * 1.5, 30, "Tan( angle ) = " + Tan( angle ) <br />
  92. <br />
  93. Flip <br />
  94. <br />
  95. Wend <br />
  96. <br />
  97. End
  98. </td>
  99. </tr>
  100. </table>
  101. <br>
  102. <a target=_top href=../index.htm>Index</a><br>
  103. <br>
  104. Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=Cos&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
  105. </html>