$lib graph on
data 0,1 , 200,1 , 440,1 , 680,2 , 880,2 , 1040,1 , 1200,1
data 1360,0 , 1520,0 , 1720,0 , 1880,3 , 1960,3 , 2040,4
defdbl a - z
def fnacs(x) = atn(sqr((1 - x * x) / x))
dim rowdata%(12 , 1)
pi = 3.141592653589
k = 1.33
re = 6371.4 * k
ang = 6
for rowcount% = 0 to 12
	read rowdata%(rowcount% , 0)
	read rowdata%(rowcount% , 1)
next rowcount%
f$ = "heights2.dat"
if dir$(f$) = "" then
	f$ = "d:" + f$
	if dir$(f$) > "" then exit if
	sound 500 , 2
	color 12 + 16
	print "Height database missing"
	end
end if
if command$ > "" then
	baselo$ = command$
	print baselo$
end if
screen 12
open "b" , 1 , f$
'==================================================================
color 14
print tab(20);"VISIBLE GROUND FROM SPECIFIED LOCATION"
color 7
locate 6 , 1
if baselo$ = "" then input; "BASE LOCATION   (NGR ,  Locator   or  Sitename)   " , baselo$
if baselo$ > "" then call locationIn(baselo$ , he , hn , mastheight , basename$) else end
print tab(37);"MAST HEIGHT ";
if mastheight = 0 then
	input"  ", mastheight
else
	print using "### m";mastheight
end if
call GetHeight(he , hn , BaseHeight)
zhorizdist = sqr(2 * re * (baseheight + mastheight) / 1000)
defaultlimit = 10 * int((zhorizdist * 1.4) / 10 + .5)
color 2
print using "Base Height #### m   Zero horizon at #### km";baseHeight , zhorizdist
color 7
print
print "MAXIMUM DISTANCE OUT   [rtn] for ";using$("#### km   " , defaultlimit);
input ;,limit
if limit = 0 then	limit = defaultlimit

bearingincr = round(180 / pi * atn(1 / limit) , 1)
print
input "LIMIT SECTOR           y / N                      ",sect$
if ucase$(sect$) = "Y" then
	input "BEARING RANGE,   clockwise       From  ,  To      ",bearingfrom , bearingto
	if bearingto < bearingfrom then bearingto = bearingto + 360
else
	bearingfrom = 0
	bearingto = 360
end if
data 4,10,63 , 11,13,50 , 18,16,45 , 25,19,40 , 32,22,36 , 39,25,30
data 46,28,24 , 50,31,18 , 53,28,18 , 56,25,18 , 60,22,18
cls
for co% = 1 to 11
	palette co% , co%
'	read r%,g%,b%
	r% = co% * 4 + 19
	g% = 63 - co% * 3
	b% = 63 - co% * 5
	reg 1 , &h1010
	reg 2 , co%
	reg 3 , 256 * g% + b%
	reg 4 , r% * 256
	call interrupt &h10
next co%
reg 1 , &h1010
reg 2 , 15
reg 3 , 256 * 63 + 63
reg 4 , 63 * 256
call interrupt &h10

color 14
locate 19 , 69
print " 'HORVIEW'";
locate 20 , 69
print "G4JNT  1997";
line (538, 285) - (636 , 320) , 15 , b

lowerlimit = -2
window (-12 , -2.5) - (450 , 3)
locate 3 , 72
print "Distance";
locate 4 , 75
print "out"
for co% = 2 to 11
	d = limit * (co% - 2) / 9
	locate 17 - co% , 73
	color 15
	print using "### km "; d;
	color co%
	print "Û";
next co%
line (0 , -2) - (360 , -2) , 15
line (360 , -2) - (360 , 3) , 15
color 15
for y = -2 to 2.5 step 1
	locate 17 - y / 5 * 27 , 66
	print using "+#.##" ; y
next y
for x% = 0 to 360 step 30
	line (x% , -2) - (x% , -2.1) , 15
	locate 30 , x% / 360 * 64 + 1
	print using "###" ; x%;
next x%
print "  Bearing ";
locate 25 , 72 : print "Horizon";
locate 26 , 72 : print "angle"

for bearing = bearingfrom to bearingto step bearingincr
	increment = .5
'	alongcount% = 0
	maxheight = 0
	maxtheta1 = lowerlimit
	angle = (90 - bearing) * pi / 180
	xmult = cos(angle)
	ymult = sin(angle)
	for along = increment to limit step increment
		co% = along / limit * 9 + 2
		ae = along * xmult
		an = along * ymult
		call GetHeight(ae + he , an + hn , aheight)
		theta1 = max(( (aheight - baseheight - mastheight) / 1000 / along - along / 2 / re ) * 180 / pi , lowerlimit)
		if theta1 >= maxtheta1 then
         maxtheta1 = theta1
			pset (bearing , theta1) , co%
		end if
	next along
	color 15
	if inkey$ = chr$(27) then exit for
next bearing
locate 1 , 1
print using " Horizon view from   \      \  \                                \";ucase$(baselo$), basename$;
locate 2 , 1
print using "####m ASL  Mast Height ###m";baseheight , mastheight;
line (0 , -2) - (360 , -2) , 15
line (360 , -2) - (360 , 3) , 15
do
	ik$ = inkey$
	if ik$ = chr$(0 , 59) then
		window
		call DumpVGAtoBMP("HORV")
	end if
loop until ik$ = chr$(27)
palette
end
'==========================================================================
Sub LocationIn(lo$ , e , n , mastheight , basename$)
	lo$ = ucase$(lo$)
	q1$="AA00AA  "
	q2$="RR99XX99"
	q3$="HA000000"
	q4$="YZ999999"
	whichtype%=0:notloc%=0:notngr%=0:found%=0
	for chk%=1 to len(lo$)
		a$=mid$(lo$,chk%,1)
		if a$ < mid$(q1$,chk%,1) or a$ > mid$(q2$,chk%,1) then notloc%=-1
		if a$ < mid$(q3$,chk%,1) or a$ > mid$(q4$,chk%,1) then notngr%=-1
	next chk%
	if (notloc% and notngr%) or len(lo$) < 6 then
		found%=0
		OSITE$=lo$
		searchloop%=0
		close 3             ' In case
		open "i",3,"sites.dat"
		do
			incr searchloop%
			line input #3,readline$
			posn1%=instr(readline$,"~")
			posn2%=instr(posn1%+7,readline$,"~")
			sitename$=left$(readline$,posn1%-1)
			if posn2%=0 then
				stloc$=mid$(readline$,posn1%+1,8)
				mastheight = 0
			else
				stloc$=mid$(readline$,posn1%+1,posn2%-posn1%-1)
				posnhgt% = instr(posn2% , ucase$(readline$) , "H")
				if posnhgt% > 0 then mastheight = val(mid$(readline$ , posnhgt% + 1))_
							else mastheight = 0
			end if

			stloc$=ucase$(stloc$)
			if instr(stloc$,"~")>0 then stloc$=left$(stloc$,6)  ' 6 Digit loc stored
			b%=instr(ucase$(sitename$),osite$)
			IF B%>0 THEN
				found%=-1
				basename$ = sitename$
				exit loop
			end if
			loop until eof(3)
		close 3
		if not found%  and dir$("auxsites.dat") > ""then
			open "i",3,"auxsites.dat"
			searchloop% = 0
			do
				incr searchloop%
				line input #3,readline$
				posn1%=instr(readline$,"~")
				posn2%=instr(posn1%+7,readline$,"~")
					sitename$=left$(readline$,posn1%-1)
				if posn2%=0 then
					stloc$=mid$(readline$,posn1%+1,8)
				else
					stloc$=mid$(readline$,posn1%+1,posn2%-posn1%-1)
					posnhgt% = instr(posn2% , ucase$(readline$) , "H")
					if posnhgt% > 0 then mastheight = val(mid$(readline$ , posnhgt% + 1))_
								else mastheight = 0
				end if

				stloc$=ucase$(stloc$)
				if instr(stloc$,"~")>0 then stloc$=left$(stloc$,6)  ' 6 Digit loc stored
				b%=instr(ucase$(sitename$),osite$)
				IF B%>0 THEN
					found%=-1
					basename$ = sitename$
					exit loop
				end if
			loop until eof(3)
		end if
		lo$ = stloc$

		if found%=0 then
			sound 300,1
			print "Not stored"
			end
		else
      	print " ";lo$
		end if
	end if

	c6% = asc(mid$(lo$ , 6 , 1))
	c5% = asc(mid$(lo$ , 5 , 1))
	if c5% >= 48 and c5% <= 57 then
		HN%=VAL(MID$(lo$,6,3))
		HE%=VAL(MID$(lo$,3,3))
		T1%=ASC(MID$(lo$,1,1))-65:IF T1%>8 THEN T1%=T1%-1
		T2%=ASC(MID$(lo$,2,1))-65:IF T2%>8 THEN T2%=T2%-1
		E = 500 * (T1% MOD 5) + 100 * (T2% MOD 5) - 1000 + HE% / 10
		N = 1900 - 500 * (T1% \ 5) - 100 * (T2% \ 5) + HN% / 10
	else
		if len(lo$) <= 6 then lo$ = lo$ + "55"
		longd=20*(asc(mid$(lo$,1,1))-74)+2*(asc(mid$(lo$,3,1))-48)_
				+(asc(mid$(lo$,5,1))-65)/12_
				+(asc(mid$(lo$,7,1))-48)/120

		latd= 10*(asc(mid$(lo$,2,1))-74)+asc(mid$(lo$,4,1))-48_
				+(asc(mid$(lo$,6,1))-65)/24_
				+(asc(mid$(lo$,8,1))-48)/240

		DR = 180/3.141592654
		LATR = LATD / DR
		LO = LONGD / DR
		Y = SQR(1 - .00667054# * SIN(LATR) * SIN(LATR))
		N = -100 + 111.0793 * (LATD - 49)
		N = N - 31.947 * SIN((LATD - 49) / DR) * COS(LATR + 49 / DR) + .971 * (2 + LONGD) ^ 2 * SIN(LATR) * COS(LATR) / Y
		E = 400 + 111.2651 * (2 + LONGD) * COS(LATR) / Y
		E = E - .0056 * (2 + LONGD) ^ 3 * (COS(LATR)) ^ 3 * ((TAN(LATR)) ^ 2 - 1)
	end if
end sub

sub GetHeight (ein , nin , height)
	shared rowdata%()
	e = ein * 1000
	n = nin * 1000
	n100% = n \ 100000       '100 km row
	e100% = e \ 100000
	n10% = n \ 10000 mod 10
	e10% = e \ 10000 mod 10

	npoint% = n \ 500 mod 20
	epoint% = e \ 500 mod 20

	estart% = rowdata%(n100% , 1)
	filebreak& = rowdata%(n100% , 0) * 1000

	km100sq% = e100% - estart%
	km10sq% = n10% + e10% * 10

	offset10& = km100sq% * 40000  +  km10sq% * 400

	fineoffset% = epoint% + 380 - 20 * npoint%

	offset& = offset10& + fineoffset%
	pointer& = offset& + filebreak&
	if pointer& > 0 then
		seek 1 , pointer&
		get$ 1 , 1 , ch1$
		height = asc(ch1$) ^ 1.32
	else
		height = 0
	end if
end sub


sub ENtoNGR(e,n,ngr$)
	 e1% = e\100
	 n1% = n\100
	 c1%=83-5*(n1%\5)+(e1%\5)
	 if c1%=73 then c1%=72
	 eh%=(e1% mod 5)
	 nh%=(n1% mod 5)
	 c2%=(4-nh%)*5+eh%+65
	 IF c2%>72 then incr c2%
	 e2&=(e-e1%*100)*10
	 n2&=(n-n1%*100)*10
	 ce$=mid$(STR$(e2&),2)
	 cn$=mid$(STR$(n2&),2)
	 while len(ce$)<3
		ce$="0"+ce$
	 wend
	 while len(cn$)<3
		cn$="0"+cn$
	 wend
	 NGR$=CHR$(C1%)+CHR$(C2%)+CE$+CN$
end sub

sub ENtoLoc (e , n , locator$)
	dr = 180 / 3.141593
	T1=(N+5548.79)/6371.28
	T2=2*ATN(EXP((E-400)/6389.7))
	LONG=ATN(-COS(T2)/(COS(T1)*SIN(T2)))-2/DR
	LA=SIN(T1)*SIN(T2)
	LAT=ATN(LA/SQR(1-LA*LA))
	latd = lat*dr
	longd=long*dr
	locator$=chr$(65+int((longd+180)/20))+chr$(65+int((latd+90)/10))+_
	chr$(48+int((longd+180)/2 mod 10))+chr$(48+int((latd+90) mod 10))+_
	chr$(65+int((longd+180)*12 mod 24))+chr$(65+int((latd+90)*24 mod 24))+_
	chr$(48+int((longd+180)*120 mod 10))+chr$(48+int((latd+90)*240 mod 10))
end sub

sub DistBear (de , dn , he , hn , dist , bear)
	dx = de - he
	dy = dn - hn
	dist = sqr(dx * dx + dy * dy)
	if (dx + dist) > 0 then bear = 360 / 3.1415927 * atn(dy / (dx + dist)) else bear = 0
	bear = 90 - bear
	if bear < 0 then bear = bear + 360
end sub

sub NearestSite(me , mn , foundname$)
	found%=0
	nearest=9999
	foundname$=""
	open "i",3,"sites.dat"
	while not eof(3)
		line input #3,l$
		posn1%=instr(l$,"~")
		posn2%=instr(posn1%+7,l$,"~")
		sitename$=left$(l$,posn1%-1)
		if posn2%=0 then
			location$=mid$(l$,posn1%+1,8)
		else
			location$=mid$(l$,posn1%+1,posn2%-posn1%-1)
		end if
		lo$=ucase$(location$)
		c6% = asc(mid$(lo$ , 6 , 1))
		c5% = asc(mid$(lo$ , 5 , 1))
		if c5% >= 48 and c5% <= 57 then
			HN%=VAL(MID$(lo$,6,3))
			HE%=VAL(MID$(lo$,3,3))
			T1%=ASC(MID$(lo$,1,1))-65:IF T1%>8 THEN T1%=T1%-1
			T2%=ASC(MID$(lo$,2,1))-65:IF T2%>8 THEN T2%=T2%-1
			E = 500 * (T1% MOD 5) + 100 * (T2% MOD 5) - 1000 + HE% / 10
			N = 1900 - 500 * (T1% \ 5) - 100 * (T2% \ 5) + HN% / 10
		else
			if len(lo$) <= 6 then lo$ = lo$ + "55"
			longd=20*(asc(mid$(lo$,1,1))-74)+2*(asc(mid$(lo$,3,1))-48)_
					+(asc(mid$(lo$,5,1))-65)/12_
					+(asc(mid$(lo$,7,1))-48)/120

			latd= 10*(asc(mid$(lo$,2,1))-74)+asc(mid$(lo$,4,1))-48_
					+(asc(mid$(lo$,6,1))-65)/24_
					+(asc(mid$(lo$,8,1))-48)/240

			DR = 180/3.141592654
			LATR = LATD / DR
			LO = LONGD / DR
			Y = SQR(1 - .00667054# * SIN(LATR) * SIN(LATR))
			N = -100 + 111.0793 * (LATD - 49)
			N = N - 31.947 * SIN((LATD - 49) / DR) * COS(LATR + 49 / DR) + .971 * (2 + LONGD) ^ 2 * SIN(LATR) * COS(LATR) / Y
			E = 400 + 111.2651 * (2 + LONGD) * COS(LATR) / Y
			E = E - .0056 * (2 + LONGD) ^ 3 * (COS(LATR)) ^ 3 * ((TAN(LATR)) ^ 2 - 1)
		end if
		dist = sqr((e - me) ^ 2 + (n - mn) ^ 2)
		if dist <= nearest then
			nearest = dist
			foundname$=sitename$
			founde = e
			foundn = n
		end if
	wend
	close 3
	me = founde
	mn = foundn               'move mouse to nearest site on return
	sound 2000,1
end sub

sub DumpVGAToBmp(basename$)       'Palette table assumes EGA compatibility
	filename$ = dir$(basename$ + "???.bmp")
	n% = val(mid$(filename$ , 6, 3))
	do
		f$ = dir$
		n% = max(val(mid$(f$ , 6, 3)) , n%)
	loop until f$ = ""
	n$ = mid$(str$(min(n% + 1, 999)) , 2)
	do while len(n$) < 3
		n$ = "0" + n$
	loop
	f$ = basename$ + n$ + ".BMP"
	color 15 : locate 1 , 69 : print f$;
	bwidth& = 640           'previously set by   PALETTE CO%,CO%
	bheight& = 480
	ColoursInTable& = 16
	BitsPerPixel% = 4        '1 / 4 / 8 / 24  only
	NPlanes% = 1             '1 only
	InfoSize& = 40

	ImageSize& = bwidth& * bheight& * BitsperPixel% / 8
	dataoffset& = 54 + ColoursInTable& * 4
	filesize& = dataoffset& + ImageSize&

	t = timer
	if instr(ucase$(f$) , ".BMP") = 0 then f$ = f$ + ".BMP"
	fi% = freefile
	open "b" , fi% , f$
	i1% = 0 : i2% = 0 : compression& = 0
	bm$ = "BM"
	put$ fi% , bm$
	put fi% , 2 , filesize&
	put fi% ,, i1%
	put fi% ,, i2%
	put fi% ,, DataOffset&
	put fi% ,, InfoSize&
	put fi% ,, bwidth&
	put fi% ,, bheight&
	put fi% ,, NPlanes%
	put fi% ,, BitsPerPixel%
	put fi% ,, compression&
	put fi% ,, ImageSize&
	put fi% ,, XPelsPerMetre&
	put fi% ,, YPelsPerMetre&
	put fi% ,, ColoursInTable&
	put fi% ,, ImportantColours&

	seek fi% , 54
	for rgb% = 0 to 15
		reg 1 , &h1015
		reg 2 , rgb%
		call interrupt &h10
		b% = (reg(3) mod 256) * 4
		g% = (reg(3) \ 256) * 4
		r% = (reg(4) \ 256) * 4
		pa$ = chr$(b%) + chr$(g%) + chr$(r%) + chr$(0)
		put$ fi% , pa$
	next rgb%

	seek fi% , dataoffset&         'data starts at bottom left of picture
	nibble% = 0
	for y% = bheight& - 1 to 0 step -1
		dat$ = ""
		for x% = 0 to bwidth& - 2 step 2
				d% = 16 * point (x% , y%) + point (x% + 1 , y%)
				dat$ = dat$ + chr$(d%)
		next x%
      put$ fi% , dat$
	next y%
	close fi%
	sound 1750 , 2
	locate 1 , 69 : print space$(12);
end sub

cl30:
	locate 30 , 1
	print space$(40);
	timer off
return
