It’s my first Codea program. You can zoom and pan with your fingers. The grid can be deactivated. You can write your own functions in “function fn(x)”.
function fn(x)
return math.sin(x*2) + math.sin(x)
end
function setup()
center=vec2(WIDTH/2,HEIGHT/2)
pos=vec2(0,0)
size=100
touches = {}
lineCapMode(2)
--noSmooth()
iparameter("Grid",0,1,1)
--watch("myW")
end
function draw()
local fnI, prevI, prevFnI, move, dist
left=fromScreen(vec2(0,0))
right=fromScreen(vec2(WIDTH,0))
top=fromScreen(vec2(0,HEIGHT))
bottom=fromScreen(vec2(0,0))
background(0, 0, 0, 255)
drawAxis()
--draw function
stroke(101, 239, 18, 255)
strokeWidth(4)
prevI = nil
for i = left.x, right.x, (right.x-left.x)/700 do
fnI = fn(i)
if (prevI~=nil) then myLine(vec2(i,fnI),vec2(prevI,prevFnI)) end
prevI = i
prevFnI = fnI
end
--zooming and moving
n = nbTouches()
if(n==1) then
move = vec2(CurrentTouch.prevX-CurrentTouch.x,CurrentTouch.prevY-CurrentTouch.y)
pos = pos - move/size
zooming=false
elseif(n==2) then
if (zooming==true) then
size=size0*distanceBetweenFingers()/dist0
move = vec2(CurrentTouch.prevX-CurrentTouch.x,CurrentTouch.prevY-CurrentTouch.y)
pos = pos - move/size
else
zooming=true
dist0=distanceBetweenFingers()
size0=size
end
else
zooming=false
end
end
function touched(touch)
if touch.state == ENDED then
touches[touch.id] = nil
else
touches[touch.id] = touch
end
end
function nbTouches()
local n
n=0
for i,j in pairs(touches) do
n = n + 1
end
return n
end
function distanceBetweenFingers()
local v, sign
v=vec2(0,0)
sign=1
for i,j in pairs(touches) do
v=v+vec2(j.x,j.y)*sign
sign=-1
end
return v:len()
end
function toScreen(v)
return center + (v + pos) * size
end
function fromScreen(v)
return (v - center) / size - pos
end
function myLine(va,vb)
local v1,v2
v1 = toScreen(va)
v2 = toScreen(vb)
line(v1.x,v1.y,v2.x,v2.y)
end
function drawAxis()
local v,i
stroke(38, 0, 255, 255)
strokeWidth(4)
i=fromScreen(vec2(60,0)).x - fromScreen(vec2(0,0)).x
i=nearestInterval(i)
for x=math.floor(left.x/i)*i,right.x,i do
v=toScreen(vec2(x,0))
gridVerLine(v.x)
line(v.x,v.y+5,v.x,v.y-5)
text(x,v.x-15,v.y-10)
end
for y=math.floor(bottom.y/i)*i,top.y,i do
v=toScreen(vec2(0,y))
gridHorLine(v.y)
line(v.x+5,v.y,v.x-5,v.y)
text(y,v.x-15,v.y-10)
end
stroke(38, 0, 255, 255)
strokeWidth(4)
myLine(vec2(-1000000,0),vec2(1000000,0))
myLine(vec2(0,-1000000),vec2(0,1000000))
end
function gridVerLine(x)
if (Grid == 0) then return end
pushStyle()
stroke(92, 87, 99, 255)
line(x,0,x,HEIGHT)
popStyle()
end
function gridHorLine(y)
if (Grid == 0) then return end
pushStyle()
stroke(92, 87, 99, 255)
line(0,y,WIDTH,y)
popStyle()
end
function nearestInterval(n)
local i1, i2, i3
i=1
i2=2.5
i3=5
while true do
if(n>i and n<i*10)then break
elseif(n<i) then i=i/10
else i=i*10
end
end
if (n>i and n<i2*i) then return(i)
elseif (n>i2*i and n<i3*i) then return(i2*i)
else return(i3*i)
end
end
BTW, how can I insert images in my posts?