CIRCLE: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
Tag: Undo
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
The [[CIRCLE]] statement is used in graphic [[SCREEN (statement)|SCREEN]] modes to create circles, arcs or ellipses.
The [[CIRCLE]] statement is used in graphic [[SCREEN]] modes to create circles, arcs or ellipses.




{{PageSyntax}}
{{PageSyntax}}
: [[CIRCLE]] [{{KW|STEP}}]'''('''{{Parameter|column}}''',''' {{Parameter|row}}'''),''' {{Parameter|radius%}}''',''' [{{Parameter|drawColor%}}][, {{Parameter|startRadian!}}, {{Parameter|stopRadian!}}] [, {{Parameter|aspect!}}]
: [[CIRCLE]] [[[STEP]]]'''('''{{Parameter|column}}''',''' {{Parameter|row}}'''),''' {{Parameter|radius%}}''',''' [{{Parameter|drawColor%}}][, {{Parameter|startRadian!}}, {{Parameter|stopRadian!}}] [, {{Parameter|aspect!}}]




{{Parameters}}
{{PageParameters}}
* Can use [[STEP]] for relative coordinate moves from the previous graphic coordinates.
* Can use [[STEP]] for relative coordinate moves from the previous graphic coordinates.
* Coordinates designate the center position of the circle. Can be partially drawn offscreen.
* Coordinates designate the center position of the circle. Can be partially drawn offscreen.
* {{Parameter|radius%}} is an [[INTEGER]] value for half of the total circle diameter.
* {{Parameter|radius%}} is an [[INTEGER]] value for half of the total circle diameter.
* {{Parameter|drawColor%}} is any available color attribute in the [[SCREEN (statement)|SCREEN]] mode used.  
* {{Parameter|drawColor%}} is any available color attribute in the [[SCREEN]] mode used.
* {{Parameter|startRadian!}} and {{Parameter|stopRadian!}} can be any [[SINGLE]] value from 0 to 2 * π to create partial circles or ellipses.
* {{Parameter|startRadian!}} and {{Parameter|stopRadian!}} can be any [[SINGLE]] value from 0 to 2 * π to create partial circles or ellipses.
* {{Parameter|aspect!}} [[SINGLE]] values of 0 to 1 affect the vertical height and values over 1 affect the horizontal width of an ellipse. Aspect = 1 is a normal circle.
* {{Parameter|aspect!}} [[SINGLE]] values of 0 to 1 affect the vertical height and values over 1 affect the horizontal width of an ellipse. Aspect = 1 is a normal circle.




{{PageDescription}}  
{{PageDescription}}
* When using {{Parameter|aspect!}} the {{Parameter|startRadian!}} and {{Parameter|stopRadian!}} commas must be included even if not used.
* When using {{Parameter|aspect!}} the {{Parameter|startRadian!}} and {{Parameter|stopRadian!}} commas must be included even if not used.
* Radians move in a counter clockwise direction from 0 to 2 * π. Zero and 2 * π are the same circle radian at 3 o'clock.
* Radians move in a counter clockwise direction from 0 to 2 * π. Zero and 2 * π are the same circle radian at 3 o'clock.
Line 21: Line 21:
* Commas after the {{Parameter|drawColor%}} parameter are not required when creating a normal circle. {{Parameter|drawColor%}} can also be omitted to use the last color used in a draw statement.
* Commas after the {{Parameter|drawColor%}} parameter are not required when creating a normal circle. {{Parameter|drawColor%}} can also be omitted to use the last color used in a draw statement.
* The graphic cursor is set to the center of the program window on program start for [[STEP]] relative coordinates.
* The graphic cursor is set to the center of the program window on program start for [[STEP]] relative coordinates.
* '''CIRCLE can be used in any graphic screen mode, but cannot be used in the default screen mode 0 as it is text only.'''  
* '''CIRCLE can be used in any graphic screen mode, but cannot be used in the default screen mode 0 as it is text only.'''




{{PageExamples}}
{{PageExamples}}
''Example 1:'' Finding when the mouse is inside of a circular area:
''Example 1:'' Finding when the mouse is inside of a circular area:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12


Line 39: Line 39:
   xy& = ((x& - cx&) ^ 2) + ((y& - cy&) ^ 2) 'Pythagorean theorem
   xy& = ((x& - cx&) ^ 2) + ((y& - cy&) ^ 2) 'Pythagorean theorem
   {{Cl|IF...THEN|IF}} r& ^ 2 >= xy& {{Cl|THEN}} {{Cl|CIRCLE}} (cx&, cy&), r&, 10 {{Cl|ELSE}} {{Cl|CIRCLE}} (cx&, cy&), r&, 12
   {{Cl|IF...THEN|IF}} r& ^ 2 >= xy& {{Cl|THEN}} {{Cl|CIRCLE}} (cx&, cy&), r&, 10 {{Cl|ELSE}} {{Cl|CIRCLE}} (cx&, cy&), r&, 12
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) 'escape key exit '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) 'escape key exit
{{CodeEnd}}
{{CodeEnd}}
: ''Explanation:'' The square of the circle radius will be greater than or equal to the sum of the square of the mouse coordinates minus the center position when the pointer is inside of the circle. In this example the circle color will change from red to green.
: ''Explanation:'' The square of the circle radius will be greater than or equal to the sum of the square of the mouse coordinates minus the center position when the pointer is inside of the circle. In this example the circle color will change from red to green.
Line 45: Line 45:


''Example 2:'' Program illustrates how the CIRCLE command using a negative radian value can be used to create the hands of a clock.
''Example 2:'' Program illustrates how the CIRCLE command using a negative radian value can be used to create the hands of a clock.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|CONST}} PI = 3.141593 'The mathematical value of PI to six places. You can also use QB64's native _PI.
{{Cl|CONST}} PI = 3.141593 'The mathematical value of PI to six places. You can also use QB64's native _PI.
{{Cl|DIM}} clock(60)            'A dimensioned array to hold 60 radian points
{{Cl|DIM}} clock(60)            'A dimensioned array to hold 60 radian points
Line 78: Line 78:
'* Get the current time from the QuickBASIC built in variable {{Cl|TIME$}}
'* Get the current time from the QuickBASIC built in variable {{Cl|TIME$}}
'* Since {{Cl|TIME$}} is a string, we need to extract the hours, minutes and
'* Since {{Cl|TIME$}} is a string, we need to extract the hours, minutes and
'* seconds from it using {{Cl|LEFT$}}, {{Cl|RIGHT$}} and {{Cl|MID$}}. Then, each of these
'* seconds from it using {{Cl|LEFT$}}, {{Cl|RIGHT$}} and {{Cl|MID$ (function)|MID$}}. Then, each of these
'* extractions need to be converted to a numeric value using VAL and
'* extractions need to be converted to a numeric value using VAL and
'* stored in their respective variables.
'* stored in their respective variables.
Line 85: Line 85:
{{Cl|IF...THEN|IF}} seconds% = 0 {{Cl|THEN}} seconds% = 60 'array counts 1 to 60 not 0 to 59
{{Cl|IF...THEN|IF}} seconds% = 0 {{Cl|THEN}} seconds% = 60 'array counts 1 to 60 not 0 to 59
previoussecond% = seconds% 'hold current second for later use
previoussecond% = seconds% 'hold current second for later use
minutes% = {{Cl|INT}}({{Cl|VAL}}({{Cl|MID$}}({{Cl|TIME$}}, 4, 2))) 'extract minutes from {{Cl|TIME$}}
minutes% = {{Cl|INT}}({{Cl|VAL}}({{Cl|MID$ (function)|MID$}}({{Cl|TIME$}}, 4, 2))) 'extract minutes from {{Cl|TIME$}}
{{Cl|IF...THEN|IF}} minutes% = 0 {{Cl|THEN}} minutes% = 60 'array counts 1 to 60 not 0 to 59
{{Cl|IF...THEN|IF}} minutes% = 0 {{Cl|THEN}} minutes% = 60 'array counts 1 to 60 not 0 to 59
previousminute% = minutes% 'hold current minute for later use
previousminute% = minutes% 'hold current minute for later use
Line 101: Line 101:
         '* Since a second has elapsed we need to erase the old second hand
         '* Since a second has elapsed we need to erase the old second hand
         '* position and draw the new position
         '* position and draw the new position
   
 
         {{Cl|CIRCLE}} (160, 100), 100, 0, -clock(previoussecond%), clock(previoussecond%)
         {{Cl|CIRCLE}} (160, 100), 100, 0, -clock(previoussecond%), clock(previoussecond%)
         {{Cl|CIRCLE}} (160, 100), 100, 15, -clock(seconds%), clock(seconds%)
         {{Cl|CIRCLE}} (160, 100), 100, 15, -clock(seconds%), clock(seconds%)
Line 115: Line 115:
         {{Cl|CIRCLE}} (160, 100), 90, 14, -clock(minutes%), clock(minutes%)
         {{Cl|CIRCLE}} (160, 100), 90, 14, -clock(minutes%), clock(minutes%)
         {{Cl|IF...THEN|IF}} hours% <> previoushour% {{Cl|THEN}} 'has an hour elapsed?
         {{Cl|IF...THEN|IF}} hours% <> previoushour% {{Cl|THEN}} 'has an hour elapsed?
             '* Since an hour has elapsed we need to erase the old hour hand position      
             '* Since an hour has elapsed we need to erase the old hour hand position
             {{Cl|CIRCLE}} (160, 100), 75, 0, -clock(previoushour% * 5), clock(previoushour% * 5)
             {{Cl|CIRCLE}} (160, 100), 75, 0, -clock(previoushour% * 5), clock(previoushour% * 5)
             previoushour% = hours% 'hold current hour for later use
             previoushour% = hours% 'hold current hour for later use
Line 126: Line 126:
     seconds% = {{Cl|VAL}}({{Cl|RIGHT$}}({{Cl|TIME$}}, 2)) 'extract time again and do all over
     seconds% = {{Cl|VAL}}({{Cl|RIGHT$}}({{Cl|TIME$}}, 2)) 'extract time again and do all over
     {{Cl|IF...THEN|IF}} seconds% = 0 {{Cl|THEN}} seconds% = 60
     {{Cl|IF...THEN|IF}} seconds% = 0 {{Cl|THEN}} seconds% = 60
     minutes% = {{Cl|VAL}}({{Cl|MID$}}({{Cl|TIME$}}, 4, 2))
     minutes% = {{Cl|VAL}}({{Cl|MID$ (function)|MID$}}({{Cl|TIME$}}, 4, 2))
     {{Cl|IF...THEN|IF}} minutes% = 0 {{Cl|THEN}} minutes% = 60
     {{Cl|IF...THEN|IF}} minutes% = 0 {{Cl|THEN}} minutes% = 60
     hours% = {{Cl|VAL}}({{Cl|LEFT$}}({{Cl|TIME$}}, 2))
     hours% = {{Cl|VAL}}({{Cl|LEFT$}}({{Cl|TIME$}}, 2))
     {{Cl|IF...THEN|IF}} hours% >= 12 {{Cl|THEN}} hours% = hours% - 12
     {{Cl|IF...THEN|IF}} hours% >= 12 {{Cl|THEN}} hours% = hours% - 12
     {{Cl|IF...THEN|IF}} hours% = 0 {{Cl|THEN}} hours% = 12
     {{Cl|IF...THEN|IF}} hours% = 0 {{Cl|THEN}} hours% = 12
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> "" 'stop program if user presses a key '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> "" 'stop program if user presses a key
 
{{CodeEnd}}
{{CodeEnd}}
{{small|code by Terry Ritchie}}
{{Small|code by Terry Ritchie}}




Line 141: Line 140:
* [[LINE]], [[PSET]], [[PRESET]]
* [[LINE]], [[PSET]], [[PRESET]]
* [[SCREEN]], [[SCREEN (function)]]
* [[SCREEN]], [[SCREEN (function)]]
* [[Alternative circle routine]] {{text|(member-contributed program)}}
* [[Alternative circle routine]] {{Text|(member-contributed program)}}
 


{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:31, 26 February 2023

The CIRCLE statement is used in graphic SCREEN modes to create circles, arcs or ellipses.


Syntax

CIRCLE [[[STEP]]](column, row), radius%, [drawColor%][, startRadian!, stopRadian!] [, aspect!]


Parameters

  • Can use STEP for relative coordinate moves from the previous graphic coordinates.
  • Coordinates designate the center position of the circle. Can be partially drawn offscreen.
  • radius% is an INTEGER value for half of the total circle diameter.
  • drawColor% is any available color attribute in the SCREEN mode used.
  • startRadian! and stopRadian! can be any SINGLE value from 0 to 2 * π to create partial circles or ellipses.
  • aspect! SINGLE values of 0 to 1 affect the vertical height and values over 1 affect the horizontal width of an ellipse. Aspect = 1 is a normal circle.


Description

  • When using aspect! the startRadian! and stopRadian! commas must be included even if not used.
  • Radians move in a counter clockwise direction from 0 to 2 * π. Zero and 2 * π are the same circle radian at 3 o'clock.
  • Negative radian values can be used to draw lines from the end of an arc or partial ellipse to the circle center.
  • Commas after the drawColor% parameter are not required when creating a normal circle. drawColor% can also be omitted to use the last color used in a draw statement.
  • The graphic cursor is set to the center of the program window on program start for STEP relative coordinates.
  • CIRCLE can be used in any graphic screen mode, but cannot be used in the default screen mode 0 as it is text only.


Examples

Example 1: Finding when the mouse is inside of a circular area:

SCREEN 12

r& = 200 'radius    change circle size and position here
cx& = 320 'center x horizontal
cy& = 240 'center y vertical

DO
  i = _MOUSEINPUT
  x& = _MOUSEX
  y& = _MOUSEY
  xy& = ((x& - cx&) ^ 2) + ((y& - cy&) ^ 2) 'Pythagorean theorem
  IF r& ^ 2 >= xy& THEN CIRCLE (cx&, cy&), r&, 10 ELSE CIRCLE (cx&, cy&), r&, 12
LOOP UNTIL INKEY$ = CHR$(27) 'escape key exit
Explanation: The square of the circle radius will be greater than or equal to the sum of the square of the mouse coordinates minus the center position when the pointer is inside of the circle. In this example the circle color will change from red to green.


Example 2: Program illustrates how the CIRCLE command using a negative radian value can be used to create the hands of a clock.

CONST PI = 3.141593 'The mathematical value of PI to six places. You can also use QB64's native _PI.
DIM clock(60)             'A dimensioned array to hold 60 radian points
clockcount% = 15          'A counter to keep track of the radians

'* Start at radian 2*PI and continue clockwise to radian 0
'* Since radian 2*PI points directly right, we need to start clockcount%
'* at 15 (for 15 seconds).  The FOR/NEXT loop counts backwards in increments
'* of 60 giving us the 60 second clock points.  These points are then stored
'* in the dimensioned array clock() to be used later.
'*
FOR radian = 2 * PI TO 0 STEP -(2 * PI) / 60
    clock(clockcount%) = radian
    clockcount% = clockcount% + 1
    IF clockcount% = 61 THEN clockcount% = 1
NEXT radian
'* Change to a graphics screen and draw the clock face
SCREEN 7
CLS
LOCATE 1, 1
COLOR 14, 0
PRINT "Ritchie's Clock"
COLOR 9, 0
PRINT "Uses CIRCLE to"
PRINT "draw hands!"
COLOR 8, 0
CIRCLE (160, 100), 110, 8 'circle with radius of 110 and dark gray
CIRCLE (160, 100), 102, 8 'circle with radius of 102 and dark gray
PAINT (265, 100), 8, 8 'fill between the two dark gray circles with gray
CIRCLE (160, 100), 110, 7 'circle with radius of 110 and light gray
'*
'* Get the current time from the QuickBASIC built in variable TIME$
'* Since TIME$ is a string, we need to extract the hours, minutes and
'* seconds from it using LEFT$, RIGHT$ and MID$. Then, each of these
'* extractions need to be converted to a numeric value using VAL and
'* stored in their respective variables.
'*
seconds% = INT(VAL(RIGHT$(TIME$, 2))) 'extract seconds from TIME$
IF seconds% = 0 THEN seconds% = 60 'array counts 1 to 60 not 0 to 59
previoussecond% = seconds% 'hold current second for later use
minutes% = INT(VAL(MID$(TIME$, 4, 2))) 'extract minutes from TIME$
IF minutes% = 0 THEN minutes% = 60 'array counts 1 to 60 not 0 to 59
previousminute% = minutes% 'hold current minute for later use
hours% = INT(VAL(LEFT$(TIME$, 2))) 'extract hour from TIME$
IF hours% >= 12 THEN hours% = hours% - 12 'convert from military time
IF hours% = 0 THEN hours% = 12 'count from 1 to 12 not 0 to 11
previoushour% = hours% 'hold current hour for later use
'*
'* Start of main program loop
'*
DO
    IF seconds% <> previoussecond% THEN 'has a second elapsed?
        LOCATE 22, 17 'print the time on the screen at
        PRINT TIME$; 'position 22, 17
        '* Since a second has elapsed we need to erase the old second hand
        '* position and draw the new position

        CIRCLE (160, 100), 100, 0, -clock(previoussecond%), clock(previoussecond%)
        CIRCLE (160, 100), 100, 15, -clock(seconds%), clock(seconds%)
        previoussecond% = seconds% 'hold current second for later use
        IF minutes% <> previousminute% THEN 'has a minute elapsed?
            '* Since a minute has elapsed we need to erase the old hour hand position
            CIRCLE (160, 100), 90, 0, -clock(previousminute%), clock(previousminute%)
            previousminute% = minutes% 'hold current minute for later use
        END IF
        '*
        '* Draw the current minute hand position
        '*
        CIRCLE (160, 100), 90, 14, -clock(minutes%), clock(minutes%)
        IF hours% <> previoushour% THEN 'has an hour elapsed?
            '* Since an hour has elapsed we need to erase the old hour hand position
            CIRCLE (160, 100), 75, 0, -clock(previoushour% * 5), clock(previoushour% * 5)
            previoushour% = hours% 'hold current hour for later use
        END IF
        '*
        '* Draw the current hour hand position
        '*
        CIRCLE (160, 100), 75, 12, -clock(hours% * 5), clock(hours% * 5)
    END IF
    seconds% = VAL(RIGHT$(TIME$, 2)) 'extract time again and do all over
    IF seconds% = 0 THEN seconds% = 60
    minutes% = VAL(MID$(TIME$, 4, 2))
    IF minutes% = 0 THEN minutes% = 60
    hours% = VAL(LEFT$(TIME$, 2))
    IF hours% >= 12 THEN hours% = hours% - 12
    IF hours% = 0 THEN hours% = 12
LOOP UNTIL INKEY$ <> "" 'stop program if user presses a key
code by Terry Ritchie


See also



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