[NTG-context] Lua code and table

Tomas Hala tomas.hala at mendelu.cz
Wed Feb 17 22:13:14 CET 2021


Hi Fabrice,

see below, I attached two ideas how to do it.

The best,

Tomáš

Wed, Feb 17, 2021 ve 06:40:54PM +0100 Fabrice Couvreur napsal(a):
#    Hi,  The code works, but is it possible to make it shorter ?
#    Thank you
#    Fabrice
#    \startuseMPgraphic{DiagonalRule}
#       rulethickness := \frameddimension{rulethickness};
# 
#       drawoptions(
#         withpen pencircle scaled rulethickness
#         withcolor \MPcolor{\framedparameter{framecolor}});
# 
#       pair leftcorner, rightcorner, Leftcorner, Rightcorner;
#       leftcorner  := (rulethickness, \overlayheight-rulethickness);
#       rightcorner := (\overlaywidth-rulethickness, rulethickness);
#       Leftcorner  := ( rulethickness, rulethickness);
#       Rightcorner :=
#    (\overlaywidth-rulethickness,\overlayheight-rulethickness);
# 
#       draw leftcorner -- rightcorner;
#       draw Leftcorner -- Rightcorner;
#     \stopuseMPgraphic
# 
#    \defineoverlay
#      [DiagonalRule]
#      [\useMPgraphic{DiagonalRule}]
# 
#    \define[2]\DiagonalLabel{%
#      \setuptabulate [after={\blank[\frameddimension{offset}]}]
#      \starttabulate [|p|r|]
#        \NC    \NC \bf #2 \NC\NR
#        \NC \bf #1 \NC    \NC\NR
#      \stoptabulate
#    }
#    \starttext
#    \startluacode
#      local background_one = {
#       align = "middle",
#       style = "type",
#       background = "DiagonalRule",
#      }
# 
#      local letters_1 = { "A", "B", "C", "D", "E", "F", "G", "H" }
#      context.startxtable({"align={middle,lohi},
#    width=0.8cm,offset=0.8ex,bodyfont=9pt,framecolor=cyan"})
#      context.startxrow()
#      context.startxcell({"frame=off"})
#      context()
#      context.stopxcell()
#      for _, letter in ipairs(letters_1) do
#        context.startxcell()
#        context(letter) context.stopxcell()
#      end
#      context.stopxrow()
#       for _, letter in ipairs(letters_1) do
#      context.startxrow()
#        context.startxcell()
#        context(letter)
#        context.stopxcell()
#      for j = 0, 7 do
#      context.startxcell(  j == 0 and letter == "B" and background_one
#      or j == 0 and letter == "C" and background_one
#      or j == 0 and letter == "D" and background_one
#      or j == 0 and letter == "G" and background_one
#      or j == 0 and letter == "H" and background_one
#      or j == 1 and letter == "A" and background_one
#      or j == 1 and letter == "E" and background_one
#      or j == 1 and letter == "F" and background_one
#      or j == 1 and letter == "G" and background_one
#      or j == 2 and letter == "A" and background_one
#      or j == 2 and letter == "D" and background_one
#      or j == 2 and letter == "F" and background_one
#      or j == 2 and letter == "G" and background_one
#      or j == 2 and letter == "H" and background_one
#      or j == 3 and letter == "A" and background_one
#      or j == 3 and letter == "C" and background_one
#      or j == 3 and letter == "E" and background_one
#      or j == 3 and letter == "H" and background_one
#      or j == 4 and letter == "B" and background_one
#      or j == 4 and letter == "D" and background_one
#      or j == 4 and letter == "F" and background_one
#      or j == 4 and letter == "G" and background_one
#      or j == 5 and letter == "B" and background_one
#      or j == 5 and letter == "C" and background_one
#      or j == 5 and letter == "E" and background_one
#      or j == 6 and letter == "A" and background_one
#      or j == 6 and letter == "B" and background_one
#      or j == 6 and letter == "C" and background_one
#      or j == 6 and letter == "E" and background_one
#      or j == 7 and letter == "A" and background_one
#      or j == 7 and letter == "C" and background_one
#      or j == 7 and letter == "D" and background_one
#      )
#      context()
#      context.stopxcell()
#      end
#      context.stopxrow()
#      end
#      context.stopxtable()
#    \stopluacode
#    \stoptext

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=
\starttext
\startluacode

function test()
  for x=0,7 do 
    for y=65,72 do 
      local yy=string.char(y)
      context(x)  
      context(" "..yy)
      context(f1(x,yy)..f2(x,yy)..f3(x,yy))
      context.par()
  end end
end

local background_one = true

function f1 (j,letter)
  local res=
   j == 0  and  letter == "B"  and  background_one  
or j == 0  and  letter == "C"  and  background_one 
or j == 0  and  letter == "D"  and  background_one 
or j == 0  and  letter == "G"  and  background_one 
or j == 0  and  letter == "H"  and  background_one 
or j == 1  and  letter == "A"  and  background_one 
or j == 1  and  letter == "E"  and  background_one 
or j == 1  and  letter == "F"  and  background_one 
or j == 1  and  letter == "G"  and  background_one 
or j == 2  and  letter == "A"  and  background_one 
or j == 2  and  letter == "D"  and  background_one 
or j == 2  and  letter == "F"  and  background_one 
or j == 2  and  letter == "G"  and  background_one 
or j == 2  and  letter == "H"  and  background_one 
or j == 3  and  letter == "A"  and  background_one 
or j == 3  and  letter == "C"  and  background_one 
or j == 3  and  letter == "E"  and  background_one 
or j == 3  and  letter == "H"  and  background_one 
or j == 4  and  letter == "B"  and  background_one 
or j == 4  and  letter == "D"  and  background_one 
or j == 4  and  letter == "F"  and  background_one 
or j == 4  and  letter == "G"  and  background_one 
or j == 5  and  letter == "B"  and  background_one 
or j == 5  and  letter == "C"  and  background_one 
or j == 5  and  letter == "E"  and  background_one 
or j == 6  and  letter == "A"  and  background_one 
or j == 6  and  letter == "B"  and  background_one 
or j == 6  and  letter == "C"  and  background_one 
or j == 6  and  letter == "E"  and  background_one 
or j == 7  and  letter == "A"  and  background_one 
or j == 7  and  letter == "C"  and  background_one 
or j == 7  and  letter == "D"  and  background_one 
  return ":"..tostring(res)
end

function f2 (j,letter)
  local t = { 
    { ["B"]=true, ["C"]=true, ["D"]=true, ["G"]=true, ["H"]=true, },
    { ["A"]=true, ["E"]=true, ["F"]=true, ["G"]=true, },
    { ["A"]=true, ["D"]=true, ["F"]=true, ["G"]=true, ["H"]=true, },
    { ["A"]=true, ["C"]=true, ["E"]=true, ["H"]=true, },
    { ["B"]=true, ["D"]=true, ["F"]=true, ["G"]=true, },
    { ["B"]=true, ["C"]=true, ["E"]=true, },
    { ["A"]=true, ["B"]=true, ["C"]=true, ["E"]=true, },
    { ["A"]=true, ["C"]=true, ["D"]=true, }
  }
  local res = t[j+1][letter] or false
  return ":"..tostring(res)
end

function f3 (j,letter)
  local t = { "BCDGH", "AEFG", "ADFGH", "ACEH", "BDFG", "BCE", "ABCE", "ACD" }
  local res = t[j+1]:find(letter) or 0
  return ":"..tostring(res>0)
end

test()

\stopluacode
\stoptext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=


More information about the ntg-context mailing list