COS: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The COS function returns the horizontal component or the cosine of an angle measured in radians. {{PageSyntax}} : {{Parameter|value!}} = COS({{Parameter|radianAngle!}}) {{Parameters}} * The {{Parameter|radianAngle!}} must be measured in radians. {{PageDescription}} * To convert from degrees to radians, multiply degrees * π / 180. * COSINE is the horizontal component of a unit vector in the direction theta (θ). * COS(x) can be calculated in ei...")
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:




{{PageSyntax}}  
{{PageSyntax}}
: {{Parameter|value!}} = [[COS]]({{Parameter|radianAngle!}})
: {{Parameter|value!}} = [[COS]]({{Parameter|radianAngle!}})




{{Parameters}}
{{PageParameters}}
* The {{Parameter|radianAngle!}} must be measured in radians.  
* The {{Parameter|radianAngle!}} must be measured in radians.




Line 13: Line 13:
* To convert from degrees to radians, multiply degrees * π / 180.
* To convert from degrees to radians, multiply degrees * π / 180.
* [[COS]]INE is the horizontal component of a unit vector in the direction theta (θ).
* [[COS]]INE is the horizontal component of a unit vector in the direction theta (θ).
* COS(x) can be calculated in either [[SINGLE]] or [[DOUBLE]] precision depending on its argument.
* COS(x) can be calculated in either [[SINGLE]] or [[DOUBLE]] precision depending on its argument.
::: COS(4) = -.6536436 ...... COS(4#) = -.6536436208636119
::: COS(4) = -.6536436 ...... COS(4#) = -.6536436208636119


Line 19: Line 19:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Converting degree angles to radians for QBasic's trig functions and drawing the line at the angle.
''Example 1:'' Converting degree angles to radians for QBasic's trig functions and drawing the line at the angle.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12
PI = 4 * {{Cl|ATN}}(1)
PI = 4 * {{Cl|ATN}}(1)
Line 36: Line 36:
   DEGREES% = RADIANS * 180 / PI
   DEGREES% = RADIANS * 180 / PI
   {{Cl|PRINT}} "DEGREES% = RADIANS * 180 / PI ="; DEGREES%
   {{Cl|PRINT}} "DEGREES% = RADIANS * 180 / PI ="; DEGREES%
{{Cl|LOOP}} {{Cl|UNTIL}} DEGREES% = 0 '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} DEGREES% = 0
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 53: Line 53:


''Example 2:'' Creating 12 analog clock hour points using [[CIRCLE]]s and [[PAINT]]
''Example 2:'' Creating 12 analog clock hour points using [[CIRCLE]]s and [[PAINT]]
{{CodeStart}} '' ''
{{CodeStart}}
  PI2 = 8 * {{Cl|ATN}}(1)                  '2 * π
  PI2 = 8 * {{Cl|ATN}}(1)                  '2 * π
  arc! = PI2 / 12                          'arc interval between hour circles
  arc! = PI2 / 12                          'arc interval between hour circles
  {{Cl|SCREEN (statement)|SCREEN}} 12
  {{Cl|SCREEN}} 12
  FOR t! = 0 TO PI2 STEP arc!
  FOR t! = 0 TO PI2 STEP arc!
   cx% = {{Cl|CINT}}({{Cl|COS}}(t!) * 70) ' pixel columns (circular radius = 70)
   cx% = {{Cl|CINT}}({{Cl|COS}}(t!) * 70) ' pixel columns (circular radius = 70)
Line 62: Line 62:
   {{Cl|CIRCLE}} (cx% + 320, cy% + 240), 3, 12
   {{Cl|CIRCLE}} (cx% + 320, cy% + 240), 3, 12
   {{Cl|PAINT}} {{Cl|STEP}}(0, 0), 9, 12
   {{Cl|PAINT}} {{Cl|STEP}}(0, 0), 9, 12
  NEXT '' ''
  NEXT
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
''Explanation:'' The 12 circles are placed at radian angles that are 1/12 of 6.28318 or .523598 radians apart.
''Explanation:'' The 12 circles are placed at radian angles that are 1/12 of 6.28318 or .523598 radians apart.




''Example 3:'' Creating a rotating spiral with COS and [[SIN]].
''Example 3:'' Creating a rotating spiral with COS and [[SIN]].
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(640, 480, 32)
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}(640, 480, 32)


Line 90: Line 90:
   {{Cl|_DISPLAY}}
   {{Cl|_DISPLAY}}
   {{Cl|_LIMIT}} 30
   {{Cl|_LIMIT}} 30
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INP}}({{Cl|&H}}60) = 1 'escape exit '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INP}}({{Cl|&H}}60) = 1 'escape exit
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ben}}
{{Small|Code by Ben}}
 


{{PageSeeAlso}}
{{PageSeeAlso}}
* [[_PI]] {{text|(QB64 function)}}
* [[_PI]] {{Text|(QB64 function)}}
* [[SIN]] {{text|(sine)}}
* [[SIN]] {{Text|(sine)}}
* [[ATN]] {{text|(arctangent)}}
* [[ATN]] {{Text|(arctangent)}}
* [[TAN]] {{text|(tangent)}}
* [[TAN]] {{Text|(tangent)}}
*[[Mathematical Operations]]
*[[Mathematical Operations]]
*[[Mathematical_Operations#Derived_Mathematical_Functions|Derived Mathematical Functions]]
*[[Mathematical Operations#Derived_Mathematical_Functions|Derived Mathematical Functions]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 22:20, 11 February 2023

The COS function returns the horizontal component or the cosine of an angle measured in radians.


Syntax

value! = COS(radianAngle!)


Parameters

  • The radianAngle! must be measured in radians.


Description

  • To convert from degrees to radians, multiply degrees * π / 180.
  • COSINE is the horizontal component of a unit vector in the direction theta (θ).
  • COS(x) can be calculated in either SINGLE or DOUBLE precision depending on its argument.
COS(4) = -.6536436 ...... COS(4#) = -.6536436208636119


Examples

Example 1: Converting degree angles to radians for QBasic's trig functions and drawing the line at the angle.

SCREEN 12
PI = 4 * ATN(1)
PRINT "PI = 4 * ATN(1) ="; PI
PRINT "COS(PI) = "; COS(PI)
PRINT "SIN(PI) = "; SIN(PI)
DO
  PRINT
  INPUT "Enter the degree angle (0 quits): ", DEGREES%
  RADIANS = DEGREES% * PI / 180
  PRINT "RADIANS = DEGREES% * PI / 180 = "; RADIANS
  PRINT "X = COS(RADIANS) = "; COS(RADIANS)
  PRINT "Y = SIN(RADIANS) = "; SIN(RADIANS)
  CIRCLE (400, 240), 2, 12
  LINE (400, 240)-(400 + (50 * SIN(RADIANS)), 240 + (50 * COS(RADIANS))), 11
  DEGREES% = RADIANS * 180 / PI
  PRINT "DEGREES% = RADIANS * 180 / PI ="; DEGREES%
LOOP UNTIL DEGREES% = 0
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
Explanation: When 8.742278E-08(.00000008742278) is returned by SIN or COS the value is essentially zero.


Example 2: Creating 12 analog clock hour points using CIRCLEs and PAINT

 PI2 = 8 * ATN(1)                  '2 * π
 arc! = PI2 / 12                          'arc interval between hour circles
 SCREEN 12
 FOR t! = 0 TO PI2 STEP arc!
   cx% = CINT(COS(t!) * 70) ' pixel columns (circular radius = 70)
   cy% = CINT(SIN(t!) * 70) ' pixel rows
   CIRCLE (cx% + 320, cy% + 240), 3, 12
   PAINT STEP(0, 0), 9, 12
 NEXT
Code by Ted Weissgerber

Explanation: The 12 circles are placed at radian angles that are 1/12 of 6.28318 or .523598 radians apart.


Example 3: Creating a rotating spiral with COS and SIN.

SCREEN _NEWIMAGE(640, 480, 32)

DO
  LINE (0, 0)-(640, 480), _RGB(0, 0, 0), BF
  j = j + 1
  PSET (320, 240)
  FOR i = 0 TO 100 STEP .1
    LINE -(.05 * i * i * COS(j + i) + 320, .05 * i * i * SIN(j + i) + 240)
  NEXT
  PSET (320, 240)
  FOR i = 0 TO 100 STEP .1
    LINE -(.05 * i * i * COS(j + i + 10) + 320, .05 * i * i * SIN(j + i + 10) + 240)
  NEXT
  PSET (320, 240)
  FOR i = 0 TO 100 STEP .1
    PAINT (.05 * i * i * COS(j + i + 5) + 320, .05 * i * i * SIN(j + i + 5) + 240)
  NEXT

  _DISPLAY
  _LIMIT 30
LOOP UNTIL INP(&H60) = 1 'escape exit
Code by Ben


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link