Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- 2009may03
-- (find-LATEX "2009apr29-C1.tex")
-- (find-angg "LUA/vectors.tcl")
-- (find-es "lua5" "vectors")
-- (find-anggfile "DAVINCI/intro.lua")
-- (find-LATEX "diaglib.014")
-- «.start-gui»	(to "start-gui")


vector = function (x, y) return Vector.new(x, y) end
Vector = {
  __add = function(v, w) return vector(v[1]+w[1], v[2]+w[2]) end,
  __sub = function(v, w) return vector(v[1]-w[1], v[2]-w[2]) end,
  __mul = function(x, v) return vector(x*v[1], x*v[2]) end,
  __div = function(v, x) return vector(v[1]/x, v[2]/x) end,
  __unm = function(v)    return vector(-v[1], -v[2]) end,
  __call = function(...) return PP(...) end,
  __tostring_lua = function(v) return "["..tostring(v[1])..", "..tostring(v[2]).."]" end,
  __tostring     = function(v) return "["..tostring(v[1])..", "..tostring(v[2]).."]" end,
  __type = "vector",
  new = function (x, y) return setmetatable({x, y}, Vector) end,
  --
  -- Special methods:
  __scale = function (v) return Vector(v[1]*150+250, v[2]*-150+175) end,
  __scale = function (v) return Vector(v[1]*120+250, v[2]*-120+175) end,
  __scale = function (v) return Vector(v[1]*100+135, v[2]*-100+135) end,
  __tostring_tcl = function (v) local w = v:__scale(); return w[1].." "..w[2] end,
}
Vector.__index = Vector
setmetatable(Vector, {__call = function(_, x, y) return Vector.new(x, y) end})

gui_now = function (tclcode, noerr)
    writefile("/tmp/ee.tcmd.tcl", tclcode)
    local output = getoutput("kill -USR2 $(cat /tmp/ee.tcmd.pid)")
    if output ~= "" then
      if noerr then return output
      else error(output)
      end
    end
  end
gui_buffer = {}
gui_push = function (tclcode) tinsert(gui_buffer, tclcode) end
gui_flush = function (noerr)
    local err = gui_now(table.concat(gui_buffer, "\n"), noerr)
    gui_buffer = {}
    return err
  end

gui = gui_now
-- gui = gui_push
-- gui_flush()

pi = math.pi
sqrt = math.sqrt
x1 = function (t) return 1 end
y1 = function (t) return t end
x2 = function (t) return math.cos(t) end
y2 = function (t) return math.sin(t) end
x3 = function (t) return (t - math.pi)^2 - 1 end
y3 = function (t) return -(t - math.pi) end
p1 = function (t) return vector(x1(t), y1(t)) end
p2 = function (t) return vector(x2(t), y2(t)) end
p3 = function (t) return vector(x3(t), y3(t)) end

x1_t = function (t) return 0 end
y1_t = function (t) return 1 end
x2_t = function (t) return -math.sin(t) end
y2_t = function (t) return  math.cos(t) end
x3_t = function (t) return (t - math.pi)*2 end
y3_t = function (t) return -1 end
p1_t = function (t) return vector(x1_t(t), y1_t(t)) end
p2_t = function (t) return vector(x2_t(t), y2_t(t)) end
p3_t = function (t) return vector(x3_t(t), y3_t(t)) end

tchoose = function (t, f1, f2, f3)
    if t <= 0 then return f1 end
    if t < pi then return f2 end
    return f3
  end
x123 = function (t) return tchoose(t, x1, x2, x3)(t) end
y123 = function (t) return tchoose(t, y1, y2, y3)(t) end
p123 = function (t) return tchoose(t, p1, p2, p3)(t) end

x123_t = function (t) return tchoose(t, x1_t, x2_t, x3_t)(t) end
y123_t = function (t) return tchoose(t, y1_t, y2_t, y3_t)(t) end
p123_t = function (t) return tchoose(t, p1_t, p2_t, p3_t)(t) end

x = x123
y = y123
p = p123
x_t = x123_t
y_t = y123_t
p_t = p123_t

-- Drawing commands
-- (find-man "3tk canvas")
-- (find-man "3tk canvas" "\nLINE ITEMS")
-- (find-man "3tk canvas" "\nOVAL ITEMS")
-- (find-LATEX "diaglib.014" "arrow_objects")

clearcommand = ".c delete all"
clear = function () gui(clearcommand) end

dotradius = Vector(.01, .01)
dotoptions = "-fill sienna -outline {}"
dotcommand = function (v, opts)
    return ".c create oval "..tostring(v-dotradius).." "..tostring(v+dotradius)..
                         " "..(opts or dotoptions)
  end
dot = function (v) gui(dotcommand(v)) end

lineoptions = ""
linecommand = function (vs, opts)
    return ".c create line "..table.concat(map(tostring, vs), " ")..
                         " "..(opts or lineoptions)
  end
line = function (vs, opts) gui(linecommand(vs, opts)) end

arrowoptions = "-arrow last -arrowshape {6 7 2} -width 2 -fill orange"
arrowcommand = function (v0, v1, opts)
    return ".c create line "..tostring(v0).." "..tostring(v1)..
                         " "..(opts or arrowoptions)
  end
arrow = function (v0, v1, opts) gui(arrowcommand(v0, v1, opts)) end

arrowv = function (p, v, opts) gui(arrowcommand(p, p+v, opts)) end

-- (find-man "3tk canvas")
-- (find-LATEX "diaglib.014" "postscript")
-- (find-man "3tk canvas")
-- (find-man "3tk canvas" "pathName postscript")
postscriptcommand = function (v, w, fname)
    local sv, sw = v:__scale(), w:__scale()
    local x1, y1 = sv[1], sv[2]
    local x2, y2 = sw[1], sw[2]
    local xl, xwid = min(x1, x2), max(x1, x2) - min(x1, x2)
    local yu, yhei = min(y1, y2), max(y1, y2) - min(y1, y2)
    return ".c postscript "..
           " -x "..xl..
           " -y "..yu..
           " -width "..xwid..
           " -height "..yhei..
           " -file "..fname..
           " -pageanchor nw"
  end
postscript = function (v, w, fname)
    print(postscriptcommand(v, w, fname))
    gui  (postscriptcommand(v, w, fname))
  end

xminxmaxyminymax = function (x0, x1, y0, y1)
    if type(x0) ~= "number" then
      local v, w = x0, x1
      x0, x1, y0, y1 = v[1], w[1], v[2], w[2]
    end
    return min(x0, x1), max(x0, x1), min(y0, y1), max(y0, y1)
  end
BOUNDS = function (x0, x1, y0, y1)
    local xmin, xmax, ymin, ymax = xminxmaxyminymax(x0, x1, y0, y1)
    BOUNDS_v = vector(xmin, ymin)
    BOUNDS_w = vector(xmax, ymax)
    BOUNDS_xaxis = {vector(xmin, 0), vector(xmax, 0)}
    BOUNDS_yaxis = {vector(0, ymin), vector(0, ymax)}
  end
SAVE = function (fname)
    postscript(BOUNDS_v, BOUNDS_w, fname or "/tmp/o.eps")
    print("# (find-pspage \""..fname.."\")")
  end

-- (find-anggfile "LUA/lua50init.lua" "seq = function (a, b, c)")
append = function (...)
    local B = {}
    for _,A in ipairs({...}) do
      for _,a in ipairs(A) do
        tinsert(B, a)
      end
    end
    return B
  end
reverse = function (A)
    local B = {}
    for i=#A,1,-1 do tinsert(B, A[i]) end
    return B
  end
ab_as_x0xn = function (a, b, n)
    local A = {}
    for i=0,n do tinsert(A, a + (i/n)*(b-a)) end
    return A
  end
withx = function (f) return function (x) return vector(x, f(x)) end end

BOUNDS(-1.5, 3,   -2, 1.5)


TCL    = function () Vector.__tostring = Vector.__tostring_tcl end
LUA    = function () Vector.__tostring = Vector.__tostring_lua end
CLEAR  = function () clear() end
IMMED  = function () gui_flush(); gui = gui_now end
BUFFER = function () gui = gui_push end
FLUSH  = function () gui_flush() end

function AXES ()
    -- line({vector(-1.5, 0), vector(3, 0)})
    -- line({vector(0, -2), vector(0, 1.5)})
    line(BOUNDS_xaxis)
    line(BOUNDS_yaxis)
  end
function CURVE ()
    for t=-2,0,.05 do dot(p1(t)) end
    for t=0,pi,pi/60 do dot(p2(t)) end
    for t=pi,pi+2,.05 do dot(p3(t)) end
  end
function BASE () BUFFER() AXES() CURVE() IMMED() end

-- tcmd("setfile fib.lua")
-- tcmd('setvars "n = 4\nN = 1"')
-- tcmd("setline 5")
-- tcmd("setline 6")




--[[
* ;; «start-gui»  (to ".start-gui")
* (eepitch-tcl)
* (eepitch-kill)
* (find-sh0 "killall tclsh")
* (find-sh0 "kill $(cat /tmp/ee.tcmd.pid)")
* (eepitch-tcl)
* (fvwm-sloppy-focus)
source ~/LUA/vectors.tcl
* (fvwm-click-to-focus)
canvas .c -width 500 -height 350 -relief sunken -borderwidth 2
pack .c -expand yes -fill both -side top
.c create line 10 20 30 40


* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "~/LUA/vectors.lua"
TCL()
CLEAR()
BASE()

arrowptt  = function (t0, t1) arrow(p(t0), p(t1)) end
arrowpt_t = function (t0, t1) arrowv(p(t0), (p(t1)-p(t0))/(t1-t0)) end
arrowpt_  = function (t0)     arrowv(p(t0), p_t(t0)) end

arrowptt(0, 3*pi/6)
arrowptt(0, 2*pi/6)
arrowptt(0, 1*pi/6)
arrowpt_t(0, 3*pi/6)
arrowpt_t(0, 2*pi/6)
arrowpt_t(0, 1*pi/6)
arrowpt_ (0)
arrowpt_ (-2)

arrowpt_(1*pi/6)
arrowpt_(2*pi/6)
arrowpt_(3*pi/6)
arrowpt_(4*pi/6)
arrowpt_(5*pi/6)

arrowpt_t(pi, pi+1)
arrowpt_t(pi, pi+1/2)
arrowpt_ (pi)

arrowpt_t(pi+1/2, pi+2)
arrowpt_t(pi+1/2, pi+1)
arrowpt_ (pi+1/2)

arrowpt_ (pi+1)

witht = function (f) return
    function (t) return vector(t, f(t)) end
  end
CLEAR()
AXES()
BUFFER()
    for t=-2,0,.05    do dot(witht(x1)(t)) end
    for t=0,pi,pi/60  do dot(witht(x2)(t)) end
    for t=pi,pi+2,.05 do dot(witht(x3)(t)) end
IMMED()

function dots (vs) for _,v in ipairs(vs) do dot(v) end end
BUFFER()
dots(map(p, seq(-2, pi+2, .05)))
IMMED()
line(map(p, seq(-2, pi+2, .05)))

line(map(p1, seq(-2, pi+2, .05)))
line(map(p2, seq(-2, pi+2, .05)))
line(map(p3, seq(-2, pi+2, .05)))

line(map(witht(x1), seq(-2, pi+2, .05)))
line(map(witht(x2), seq(-2, pi+2, .05)))
line(map(witht(x3), seq(-2, pi+2, .05)))
line(map(p2, seq(-2, pi+2, .05)))
line(map(p3, seq(-2, pi+2, .05)))

postscript(vector(-1.5, -2), vector(3, 1.5), "/tmp/o.ps")
-- (find-pspage "/tmp/o.ps")
-- (find-fline  "/tmp/o.ps")

-- Garbage:
LUA()
t0, t1 = 0, pi/2 
= (p(t1)-p(t0))/(t1-t0)
TCL()

arrow(p(0), p(3*pi/6))
arrow(p(0), p(2*pi/6))
arrow(p(0), p(1*pi/6))
arrowv(p(0), p_t(0))

arrowv(p(0), (p(3*pi/6)-p(0)))
arrowv(p(0), (p(2*pi/6)-p(0))/(pi/6))
arrowv(p(0), (p(1*pi/6)-p(0)))

-- Garbage:
= pi/60
= Vector(99, 200)
= p1(-1)
= p1(0)
-- gui "canvas .c -width 500 -height 350 -relief sunken -borderwidth 2"
-- gui "pack .c -expand yes -fill both -side top"
ts = seq(-2, 0, .2)
PP(map(p1, ts))
PP(ts)
gui ".c create oval 10 20 30 40"
gui ".c create oval 10 20 30 40 -fill sienna"
gui ".c create oval 10 20 30 40 -fill sienna -outline {}"
gui ".c create oval 110.1 120.1 130.1 140.1 -fill sienna -outline {}"
dotoptions = "-fill sienna"
= dotcommand(p1(0))
dc = dotcommand(p1(0))
= dc
gui(dc)
for t=-2,0,.1 do print(t) end
for t=-2,0,.1 do print(p1(t)) end
gui ".c delete all"
gui ".c create line 10 20 30 40"
dotoptions = "-fill sienna"
= vector(2, 3) + vector(4, 5)
= vector(2, 3) + 45
=           23 + vector(4, 5)
=              - vector(4, 5)
= vector(2, 3).__type
Vector(99, 200)

--]]
-- Local variables:
-- coding: raw-text-unix
-- End: