Direction function, for longitude or compass

Post Reply
User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Direction function, for longitude or compass

Post by Dutchman »

Ricardobytes made a function to return a string with the compass-direction: viewtopic.php?p=10310#p10310
It is gone now:
It has been boxed and shipped to another Forum.
Furthermore I needed it for the angular geographic coordinate of longitude, which zero-point is "South", rather than "North".
So I made the function in the following test-code. The names are in the DATA-list made by Ricardobytes.
The test-program explains the usage of 'mode' and 'zero' setting:

Code: Select all

'Direction, for longitude or compass
'by Dutchman, march 2017
'Return the names of the 32-point wind compass rose
'http://rosettacode.org/wiki/Box_the_compass
'https://en.m.wikipedia.org/wiki/Points_of_the_compass
OPTION BASE 1
GOSUB Init
CALL Direction$(0) ' initiate table
'-- set direction according to geographic angle
FOR zero=0 TO 16 STEP 16
  Direction$.zero=zero 'geographic 0° = south
  PRINT "************ Zero-direction set to: ";
  PRINT Direction$.Point$(Direction$.zero+1,1);
  PRINT " ************" ! PRINT
  FOR mode=1 TO 2
    PRINT "====== Mode="&mode&" ======"
    Direction$.mode=mode
    FOR n=-16 TO 16
      arc=n/16 'arc is relative phase, range from -1 to 1
      IF n%4=0 THEN PRINT
      PRINT Direction$(arc),
    NEXT n
    PRINT ! PRINT
  NEXT mode
PRINT ! PRINT
NEXT zero
DO ! UNTIL 0
END

'==== Subroutines ====
Init:
CALL SetEditor(82) 'set style and characters per minimum width
CALL SetOutput(100) 'set style and characters per minimum width
RETURN
'
'r'==== Functions ====
DEF Direction$(arcu) 'by Dutchman
'-- returns longitude or compass direction
'   arcu is relative phase in range -1 to 1
'   mode selects the array-column
'     1=name, 2=compact
'   zero is the index to zero direction 
'     0 for North (compass)
'     16 for South (longitude)
'------ table will be loaded if set=0
IF NOT set THEN Load
index=(INT((arcu+1)*16)+zero)%32+1
RETURN Point$(index,mode)
RETURN'from function
Load:
DIM point$(32,5)
FOR i=1 TO 32
  FOR j=1 TO 5
    READ point$(i,j)
  NEXT j
NEXT i
Set=1 ! mode=1
RETURN'from load subroutine
'-- List of names by 'ricardobytes'
DATA "North","N","354.38","0","5.62","North by east","NbE","5.63","11.25","16.87"
DATA "North-northeast","NNE","16.88","22.50","28.12","Northeast by north","NEbN","28.13","33.75","39.37"
DATA "Northeast","NE","39.38","45.00","50.62","Northeast by east","NEbE","50.63","56.25","61.87"
DATA "East-northeast","ENE","61.88","67.50","73.12","East by north","EbN","73.13","78.75","84.37"
DATA "East","E","84.38","90.00","95.62","East by south","EbS","95.63","101.25","106.87"
DATA "East-southeast","ESE","106.88","112.50","118.12","Southeast by east","SEbE","118.13","123.75","129.37"
DATA "Southeast","SE","129.38","135.00","140.62","Southeast by south","SEbS","140.63","146.25","151.87"
DATA "South-southeast","SSE","151.88","157.50","163.12","South by east","SbE","163.13","168.75","174.37"
DATA "South","S","174.38","180.00","185.62","South by west","SbW","185.63","191.25","196.87"
DATA "South-southwest","SSW","196.88","202.50","208.12","Southwest by south","SWbS","208.13","213.75","219.37"
DATA "Southwest","SW","219.38","225.00","230.62","Southwest by west","SWbW","230.63","236.25","241.87"
DATA "West-southwest","WSW","241.88","247.50","253.12","West by south","WbS","253.13","258.75","264.37"
DATA "West","W","264.38","270.00","275.62","West by north","WbN","275.63","281.25","286.87"
DATA "West-northwest","WNW","286.88","292.50","298.12","Northwest by west","NWbW","298.13","303.75","309.37"
DATA "Northwest","NW","309.38","315.00","320.62","Northwest by north","NWbN","320.63","326.25","331.87"
DATA "North-northwest","NNW","331.88","337.50","343.12","North by west","NbW","343.13","348.75","354.37"
END DEF
''
DEF SetEditor(n)
'--- set fontsize
GET SCREEN SIZE sw,sh
fmin=9 'minimum fontsize
fsize=MAX((MIN(sw,sh)-20)/(0.6*n),fmin)
'--- set style
SET EDITOR FONT COLOR 0,0,0
SET EDITOR BACK COLOR 1,.94,.86
SET EDITOR FONT NAME "Menlo-Regular"
SET EDITOR FONT SIZE fsize
'SET EDITOR FONT NAME "Courier"
'SET EDITOR DEFAULT
TEXT CLEAR
END DEF
'
DEF SetOutput(n)
GET SCREEN SIZE sw,sh
fmin=9 'minimum fontsize
fsize=MAX((MIN(sw,sh)-20)/(0.6*n),fmin)
SET OUTPUT FONT COLOR 0,0,0
SET OUTPUT BACK COLOR 1,1,1
SET OUTPUT FONT NAME "Menlo-Regular"
SET OUTPUT FONT SIZE fsize
END DEF
screenshot.PNG
screenshot.PNG (155.99 KiB) Viewed 1923 times
Last edited by Dutchman on Fri Jan 20, 2023 12:12 pm, edited 1 time in total.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Direction function, for longitude or compass

Post by rbytes »

This is a good adaptation. Nice to know that we can create a compass in smart Basic that gives directions twice as precise as our dedicated GPS units. :D
The only thing that gets me down is gravity...

Post Reply