Sin.htm 3.5 KB

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