Here the finished code for the great circle calculator.
viewer.mode=FULLSCREEN
-- great circle distance between 2 points
function setup()
rectMode(CENTER)
fill(255)
kilometers,miles=0,0
str=""
val=""
buttonTab={}
table.insert(buttonTab,buttons(WIDTH/2-100,HEIGHT-140,150,50,"Latitude 1","i"))
table.insert(buttonTab,buttons(WIDTH/2+100,HEIGHT-140,150,50,"Longitude 1","i"))
table.insert(buttonTab,buttons(WIDTH/2-100,HEIGHT-390,150,50,"Latitude 2","i"))
table.insert(buttonTab,buttons(WIDTH/2+100,HEIGHT-390,150,50,"Longitude2","i"))
table.insert(buttonTab,buttons(WIDTH/2,HEIGHT-220,200,50,"Statute Miles","o"))
table.insert(buttonTab,buttons(WIDTH/2,HEIGHT-310,200,50,"Kilometers","o"))
end
function draw()
background(224, 198, 136)
fontSize(20)
for a,b in pairs(buttonTab) do
b:draw()
end
calc()
fill(87, 88, 231)
fontSize(30)
text("Great Circle distance between two points",WIDTH/2,HEIGHT-30)
text("tap lat / long boxes and input values",WIDTH/2,HEIGHT-60)
end
function touched(t)
for a,b in pairs(buttonTab) do
b:touched(t)
end
end
function calc()
rad=0.01745329252
lat1=(tonumber(buttonTab[1].val) or 0)*rad
long1=(tonumber(buttonTab[2].val) or 0)*rad
lat2=(tonumber(buttonTab[3].val) or 0)*rad
long2=(tonumber(buttonTab[4].val) or 0)*rad
dx=(lat2-lat1)/2
dy=(long2-long1)/2
a=math.sin(dx)^2+math.cos(lat1)*math.cos(lat2)*math.sin(dy)^2
buttonTab[5].val=7919.8*math.asin(math.sqrt(a)) -- miles
buttonTab[6].val=12745.6*math.asin(math.sqrt(a)) -- kilometers
end
function keyboard(k)
for a,b in pairs(buttonTab) do
b:keyboard(k)
end
end
buttons=class()
function buttons:init(x,y,w,h,name,t)
self.x=x
self.y=y
self.w=w
self.h=h
self.sel=false
self.val=0
self.str=""
self.name=name
self.type=t
end
function buttons:draw()
stroke(255)
strokeWidth(5)
if self.sel then
stroke(255, 0, 0)
if str~="" then
self.val=str
end
end
fill(200)
rect(self.x,self.y,self.w,self.h)
fill(255)
text(self.name,self.x,self.y+self.h*.8)
fill(0)
text(self.val,self.x,self.y)
end
function buttons:touched(t)
if t.state==BEGAN then
self.sel=false
str=""
showKeyboard()
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
if self.type=="i" then
self.val=""
self.str=""
self.sel=true
end
end
end
end
function buttons:keyboard(k)
if self.sel then
if k==BACKSPACE then
self.str=string.sub(self.str,1,#self.str-1)
else
self.str=self.str..k
end
self.val=self.str
end
end