# Polar Coordinate Grapher

A simple polar coordinate Grapher. If there is interest, I will add a GUI, and extend its use to Cartesian coordinates including function (graphs of y with respect to x), as well as graphs of x with respect to y (inverse functions).
It comes pre-loaded with one of my favorite graphs in it, and it is clearly marked what values you can change (there are also a couple values you can change outside this area, but they’re a sort of Easter egg because graphing calculators are boring and I needed to hide secrets). ‘Theta’ serves as ‘x’ in polar coordinates as you can see in the" func" line. I used the word theta as opposed to a symbol to make it easier to recognize and type, and because autocomplete.

``````-- Polar Grapher
supportedOrientations(LANDSCAPE_ANY)
displayMode(OVERLAY)
function setup()
DOT = 0
LINE = 1
BLACK = 0
COLOR = 1
pi = math.pi
print("Hello World!")
drawxaxis = false
drawyaxis = false
origin = vec2(WIDTH/2,HEIGHT/2)
xmi = 0
xma = 0
rangex = 0
ymi = 0
yma = 0
rangey = 0
--------------------------
--CHANGE THE ITEMS BELOW--
grid(-140,140,-140,140)
thmin = 0
theta = thmin
thmax = 12000
thstep = .01
thstepstep = 0
func = "theta+12*sin(12*theta)-12*cos(12*theta)"
--CHANGE THE ITEMS ABOVE--
--------------------------
background(255)
calcing = true
img = image(HEIGHT,HEIGHT)
r = 0
rrate = 0
perframe = 100
drawmode = LINE
drawcol = BLACK
end
function draw()
background(255)
if calcing then
for i=0,perframe do
if not calcing then
break
end
calcpoint()
end
output.clear()
print(1/DeltaTime)
end
stroke(0)
strokeWidth(2)
if drawxaxis then
line(origin.x,0,origin.x,HEIGHT)
end
if drawyaxis then
line(0,origin.y,HEIGHT,origin.y)
end
fill(0,0,0,255)
rect(HEIGHT,0-1,WIDTH-HEIGHT+2,HEIGHT+2)

pushMatrix()
translate(HEIGHT/2,HEIGHT/2)
rotate(r)
r = r + rrate
sprite(img,0,0)
--sprite(img,HEIGHT/2,HEIGHT/2)
popMatrix()
end
function calcpoint()
local x = dist*math.cos(theta)
local y = dist*math.sin(theta)
local lastpoi = poi
convertRawToScreenCoords(x,y)
--drawpoint
setContext(img)
smooth()
if drawcol == BLACK then
stroke(0, 0, 0, 255)
elseif drawcol == COLOR then
stroke(math.random(0,255),math.random(0,255),math.random(0,255))
end
fill(stroke())
strokeWidth(1)
if drawmode == LINE then
if lastpoi then
line(lastpoi.x,lastpoi.y,poi.x,poi.y)
end
else
ellipse(poi.x,poi.y,3)
end
setContext()
--draw finished
theta = theta + thstep
thstep = thstep + thstepstep
if theta > thmax+thstep then
calcing = false
end
end
function grid(xmin,xmax,ymin,ymax)
if xmin<0 and 0<xmax then
drawxaxis = true
xmi = xmin
xma = xmax
rangex = xmax - xmin
local xaxisloc = 0-xmin
origin.x = (xaxisloc/rangex)*HEIGHT
else
drawxaxis = false
xmi = xmin
xma = xmax
rangex = xmax - xmin
local xaxisloc = 0-xmin
origin.x = (xaxisloc/rangex)*HEIGHT
end
if ymin<0 and 0<ymax then
drawyaxis = true
ymi = ymin
yma = ymax
rangey = ymax-ymin
local yaxisloc = 0-ymin
origin.y = (yaxisloc/rangey)*HEIGHT
else
drawyaxis = false
ymi = ymin
yma = ymax
rangey = ymax-ymin
local yaxisloc = 0-ymin
origin.y = (yaxisloc/rangey)*HEIGHT
end
end
function touched(t)
--output.clear()
--print(math.abs(origin.x-t.x))
end
function convertRawToScreenCoords(x,y)
local xp = x-xmi
local coord = vec2((xp/rangex)*HEIGHT,0)
local yp = y-ymi
coord.y = (yp/rangey)*HEIGHT
drawpoint = true
poi = coord
return coord
end
function sin(x)
return math.sin(x)
end
function cos(x)
return math.cos(x)
end
function tan(x)
return math.tan(x)
end
function asin(x)
return math.asin(x)
end
function acos(x)
return math.acos(x)
end
function atan(x)
return math.atan(x)
end
function abs(x)
return math.abs(x)
end
function ln(x)
return math.log(x)
end
function log(x)
return math.log10(x)
end
function exp(x,y)
local power = y or 2
local startnum = x or 1
return startnum^power
end
function sqrt(x)
return math.sqrt(x)
end
function rt(x,root)
local rt = root or 2
return x^(1/rt)
end
``````