mirror of
https://github.com/QB64Official/qb64.git
synced 2024-08-22 09:05:08 +00:00
119 lines
5.1 KiB
Text
119 lines
5.1 KiB
Text
The {{KW|SIN}} function returns the vertical component or sine of an angle measured in radians.
|
|
|
|
|
|
{{PageSyntax}}
|
|
::: value! = '''SIN('''''radian_angle!''''')'''
|
|
|
|
|
|
{{Parameters}}
|
|
* The ''radian_angle'' must be measured in radians from 0 to 2 * Pi.
|
|
|
|
|
|
{{PageDescription}}
|
|
* To convert from degrees to radians, multiply degrees * π/180.
|
|
* [[SIN]]E is the vertical component of a unit vector in the direction theta (θ).
|
|
* Accuracy can be determined as [[SINGLE]] by default or [[DOUBLE]] by following the parameter value with a # suffix.
|
|
|
|
|
|
''Example 1:'' Converting degree angles to radians for Qbasic's trig functions and drawing the line at the angle.
|
|
{{CodeStart}} '' ''
|
|
{{Cl|SCREEN}} 12
|
|
PI = 4 * {{Cl|ATN}}(1)
|
|
{{Cl|PRINT}} "PI = 4 * {{Cl|ATN}}(1) ="; PI
|
|
{{Cl|PRINT}} "COS(PI) = "; {{Cl|COS}}(PI)
|
|
{{Cl|PRINT}} "SIN(PI) = "; {{Cl|SIN}}(PI)
|
|
DO
|
|
{{Cl|PRINT}}
|
|
{{Cl|INPUT}} "Enter the degree angle (0 quits): ", DEGREES%
|
|
RADIANS = DEGREES% * PI / 180
|
|
{{Cl|PRINT}} "RADIANS = DEGREES% * PI / 180 = "; RADIANS
|
|
{{Cl|PRINT}} "X = COS(RADIANS) = "; {{Cl|COS}}(RADIANS)
|
|
{{Cl|PRINT}} "Y = SIN(RADIANS) = "; {{Cl|SIN}}(RADIANS)
|
|
{{Cl|CIRCLE}} (400, 240), 2, 12
|
|
{{Cl|LINE}} (400, 240)-(400 + (50 * {{Cl|SIN}}(RADIANS)), 240 + (50 * {{Cl|COS}}(RADIANS))), 11
|
|
DEGREES% = RADIANS * 180 / PI
|
|
{{Cl|PRINT}} "DEGREES% = RADIANS * 180 / PI ="; DEGREES%
|
|
{{Cl|LOOP}} {{Cl|UNTIL}} DEGREES% = 0 '' ''
|
|
{{CodeEnd}}
|
|
{{OutputStart}}
|
|
PI = 4 * ATN(1) = 3.141593
|
|
COS(PI) = -1
|
|
SIN(PI) = -8.742278E-08
|
|
|
|
Enter the degree angle (0 quits): 45
|
|
RADIANS = DEGREES% * PI / 180 = .7853982
|
|
X = COS(RADIANS) = .7071068
|
|
Y = SIN(RADIANS) = .7071068
|
|
DEGREES% = RADIANS * 180 / PI = 45
|
|
{{OutputEnd}}
|
|
: ''Explanation:'' When 8.742278E-08(.00000008742278) is returned by SIN or [[COS]] the value is essentially zero.
|
|
|
|
|
|
''Example 2:'' Displays rotating gears made using SIN and [[COS]] to place the teeth lines.
|
|
{{CodeStart}} '' ''
|
|
{{Cl|SCREEN (statement)|SCREEN}} 9
|
|
{{Cl|DIM}} {{Cl|SHARED}} Pi {{Cl|AS}} {{Cl|SINGLE}}
|
|
Pi = 4 * {{Cl|ATN}}(1)
|
|
{{Cl|DO}}
|
|
{{Cl|FOR...NEXT|FOR}} G = 0 {{Cl|TO}} Pi * 2 {{Cl|STEP}} Pi / 100
|
|
{{Cl|CLS}} 'erase previous
|
|
{{Cl|CALL}} GEARZ(160, 60, 40, 20, 4, G, 10)
|
|
{{Cl|CALL}} GEARZ(240, 60, 40, 20, 4, -G, 11)
|
|
{{Cl|CALL}} GEARZ(240, 140, 40, 20, 4, G, 12)
|
|
{{Cl|CALL}} GEARZ(320, 140, 40, 20, 4, -G, 13)
|
|
{{Cl|CALL}} GEARZ(320 + 57, 140 + 57, 40, 20, 4, G, 14)
|
|
{{Cl|CALL}} GEARZ(320 + 100, 140 + 100, 20, 10, 4, -G * 2 - 15, 15)
|
|
{{Cl|_DISPLAY}}
|
|
{{Cl|_LIMIT}} 20 'regulates gear speed and CPU usage
|
|
{{Cl|NEXT}} G
|
|
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
|
|
{{Cl|END}}
|
|
|
|
{{Cl|SUB}} GEARZ (XP, YP, RAD, Teeth, TH, G, CLR)
|
|
t = 0
|
|
x = XP + (RAD + TH * {{Cl|SIN}}(0)) * {{Cl|COS}}(0)
|
|
y = YP + (RAD + TH * {{Cl|SIN}}(0)) * {{Cl|SIN}}(0)
|
|
{{Cl|PRESET}} (x, y)
|
|
m = Teeth * G
|
|
{{Cl|FOR...NEXT|FOR}} t = -Pi / 70 {{Cl|TO}} 2 * Pi {{Cl|STEP}} Pi / 70
|
|
x = XP + (RAD + TH * {{Cl|SIN}}((Teeth * t + m)) ^ 3) * {{Cl|COS}}(t)
|
|
y = YP + (RAD + TH * {{Cl|SIN}}((Teeth * t + m)) ^ 3) * {{Cl|SIN}}(t)
|
|
{{Cl|LINE}} -(x, y), CLR
|
|
IF {{Cl|INKEY$}} <> "" THEN {{Cl|END}}
|
|
{{Cl|NEXT}} t
|
|
{{Cl|PAINT}} (XP, YP), CLR 'gear colors optional
|
|
{{Cl|END SUB}} '' ''
|
|
{{CodeEnd}}
|
|
{{small|Adapted from code by: DarthWho}}
|
|
|
|
|
|
''Example 3:'' Displaying the current seconds for an analog clock. See [[COS]] for the clock face hour markers.
|
|
{{CodeStart}} '' ''
|
|
{{Cl|SCREEN}} 12
|
|
Pi2! = 8 * {{Cl|ATN}}(1): sec! = Pi2! / 60 ' (2 * pi) / 60 movements per rotation
|
|
{{Cl|CIRCLE}} (320, 240), 80, 1
|
|
{{Cl|DO}}
|
|
{{Cl|LOCATE}} 1, 1: {{Cl|PRINT}} {{Cl|TIME$}}
|
|
Seconds% = {{Cl|VAL}}({{Cl|RIGHT$}}({{Cl|TIME$}}, 2)) - 15 ' update seconds
|
|
S! = Seconds% * sec! ' radian from the TIME$ value
|
|
Sx% = {{Cl|CINT}}({{Cl|COS}}(S!) * 60) ' pixel columns (60 = circular radius)
|
|
Sy% = {{Cl|CINT}}({{Cl|SIN}}(S!) * 60) ' pixel rows
|
|
{{Cl|LINE}} (320, 240)-(Sx% + 320, Sy% + 240), 12
|
|
{{Cl|DO}}: Check% = {{Cl|VAL}}({{Cl|RIGHT$}}({{Cl|TIME$}}, 2)) - 15: {{Cl|LOOP}} UNTIL Check% <> Seconds% ' wait loop
|
|
{{Cl|LINE}} (320, 240)-(Sx% + 320, Sy% + 240), 0 ' erase previous line
|
|
{{Cl|LOOP}} UNTIL {{Cl|INKEY$}} = {{Cl|CHR$}}(27) ' escape keypress exits
|
|
{{CodeEnd}}
|
|
{{small|Code by: Ted Weissgerber}}
|
|
The value of 2 &pi; is used to determine the sec! multiplier that determines the radian value as S! The value is divided by 60 second movements. To calculate the seconds the [[TIME$]] function is used and that value is subtracted 15 seconds because the 0 value of pi is actually the 3 hour of the clock (15 seconds fast). SIN and COS will work with negative values the same as positive ones! Then the column and row coordinates for one end of the line are determined using SIN and [[COS]] multiplied by the radius of the circular line movements. The minute and hour hands could use similar procedures to read different parts of TIME$.
|
|
|
|
|
|
{{PageSeeAlso}}
|
|
* [[_PI]] {{text|(QB64 function)}}
|
|
*[[COS]] {{text|(cosine)}}
|
|
*[[ATN]] {{text|(arctangent)}}
|
|
*[[TAN]] {{text|(tangent)}}
|
|
*[[Mathematical Operations]]
|
|
*[[Mathematical_Operations#Derived_Mathematical_Functions|Derived Mathematical Functions]]
|
|
|
|
|
|
{{PageNavigation}}
|