As a follow-up for my last post hereby I present a Creeper (right) in (Lua)LaTeX.
Image source (left): File:Creeper.png – Minecraft Wiki
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
% Creeper
% Author: István Szántai (szantaii) \documentclass{article} \usepackage{luacode} \usepackage{xcolor} \usepackage{tikz} \usepackage{tikz-3dplot} \usepackage[active, tightpage]{preview} \PreviewEnvironment{tikzpicture} \setlength{\PreviewBorder}{1cm} \definecolor{creeper_white}{HTML}{9AD78E} \definecolor{creeper_lightgreen}{HTML}{5ED04C} \definecolor{creeper_green}{HTML}{00A500} \definecolor{creeper_darkgreen}{HTML}{255522} \begin{luacode*} function draw_coordinate_system() tex.sprint("\\draw[white!50!gray,thick,->] (0,0,0) -- " .. "(3,0,0) node[text=white!50!gray,anchor=north east]{$x$};") tex.sprint("\\draw[white!50!gray,thick,->] (0,0,0) -- " .. "(0,3,0) node[text=white!50!gray,anchor=west]{$y$};") tex.sprint("\\draw[white!50!gray,thick,->] (0,0,0) -- " .. "(0,0,3) node[text=white!50!gray,anchor=south]{$z$};") end function matrix_scalar_multiplication(matrix, scalar) local rows = #matrix local cols = #matrix[1] local tmp_matrix = {} for i = 1, rows do tmp_matrix[i] = {} for j = 1, cols do tmp_matrix[i][j] = matrix[i][j] * scalar end end return tmp_matrix end function shift_coordinates(matrix, array) local matrix_rows = #matrix local matrix_cols = #matrix[1] local array_length = #array local tmp_matrix = {} if matrix_cols == array_length then for i = 1, matrix_rows do tmp_matrix[i] = {} for j = 1, matrix_cols do tmp_matrix[i][j] = matrix[i][j] + array[j] end end return tmp_matrix else return nil end end function tikzcube(x, y, z, color) local side_1 = {{1, 1, -1}, {-1, 1, -1}, {-1, -1, -1}, {1, -1, -1}} local side_2 = {{-1, 1, -1}, {-1, 1, 1}, {-1, -1, 1}, {-1, -1, -1}} local side_3 = {{-1, -1, -1}, {1, -1, -1}, {1, -1, 1}, {-1, -1, 1}} local side_4 = {{1, 1, -1}, {-1, 1, -1}, {-1, 1, 1}, {1, 1, 1}} local side_5 = {{1, -1, -1}, {1, 1, -1}, {1, 1, 1}, {1, -1, 1}} local side_6 = {{1, 1, 1}, {-1, 1, 1}, {-1, -1, 1}, {1, -1, 1}} local cube_sides = {side_1, side_2, side_3, side_4, side_5, side_6} local tex_cube = "" for i = 1, #cube_sides do tex_cube = tex_cube .. "\\draw[ultra thin, fill=" .. color .. "] " local current_side = matrix_scalar_multiplication(cube_sides[i], 0.5) current_side = shift_coordinates(current_side, {x, y, z}) local current_side_rows = #current_side local current_side_cols = #current_side[1] for j = 1, current_side_rows do for k = 1, current_side_cols do if k == 1 then tex_cube = tex_cube .. "(" end tex_cube = tex_cube .. current_side[j][k] if k ~= current_side_cols then tex_cube = tex_cube .. ", " else tex_cube = tex_cube .. ") -- " end end end tex_cube = tex_cube .. "cycle;" end tex.sprint(tex_cube) end function draw_head(x_pos, y_pos, z_pos, colors_array) for x = x_pos, x_pos + 7, 1 do for y = y_pos, y_pos + 7, 1 do for z = z_pos, z_pos + 7, 1 do if x == x_pos or x == x_pos + 7 or y == y_pos or y == y_pos + 7 or z == z_pos or z == z_pos + 7 then local color_index = math.random(1, #colors_array - 1) tikzcube(x, y, z, colors_array[color_index]) if x == x_pos + 7 and (z == z_pos + 4 and (y == y_pos + 2 or y == y_pos + 5)) or (z == z_pos + 2 and (y == y_pos + 3 or y == y_pos + 4)) or (z == z_pos + 1 and (y > 1 and y < 6)) then tikzcube(x, y, z, "black") end if x == x_pos + 7 and ((z == z_pos + 5 and ((y > y_pos and y < y_pos + 3) or (y > y_pos + 4 and y < y_pos + 7))) or (z == z_pos + 4 and (y == y_pos + 1 or y == y_pos + 6)) or (z == z_pos + 3 and (y == y_pos + 3 or y == y_pos + 4)) or (z == z_pos + 2 and (y == y_pos + 2 or y == y_pos + 5)) or (z == z_pos and (y == y_pos + 2 or y == y_pos + 5))) then tikzcube(x, y, z, colors_array[#colors_array]) end end end end end end function draw_leg(x_pos, y_pos, z_pos, colors_array) for x = x_pos, x_pos + 3, 1 do for y = y_pos, y_pos + 7, 1 do for z = z_pos, z_pos + 5, 1 do if x == x_pos or x == x_pos + 3 or y == y_pos or y == y_pos + 7 or z == z_pos or z == z_pos + 5 then local color_index = math.random(1, #colors_array - 1) tikzcube(x, y, z, colors_array[color_index]) if x == x_pos + 3 and ((z == z_pos + 1 and (y == y_pos or y == y_pos + 2 or y == y_pos + 4 or y == y_pos + 6)) or (z == z_pos and (y == y_pos + 1 or y == y_pos + 3 or y == y_pos + 5 or y == y_pos + 7))) then tikzcube(x, y, z, colors_array[#colors_array]) end if x == x_pos + 3 and ((z == z_pos + 1 and (y == y_pos + 1 or y == y_pos + 3 or y == y_pos + 5 or y == y_pos + 7)) or (z == z_pos and (y == y_pos or y == y_pos + 2 or y == y_pos + 4 or y == y_pos + 6))) then tikzcube(x, y, z, "black") end end end end end end function draw_bodypart(x_pos, y_pos, z_pos, x_length, y_length, z_length, colors_array) local color for x = x_pos, x_pos + x_length - 1, 1 do for y = y_pos, y_pos + y_length - 1, 1 do for z = z_pos, z_pos + z_length - 1, 1 do if x == x_pos or x == x_pos + x_length - 1 or y == y_pos or y == y_pos + y_length - 1 or z == z_pos or z == z_pos + z_length - 1 then local color_index = math.random(1, #colors_array) tikzcube(x, y, z, colors_array[color_index]) end end end end end function draw_creeper(x_rotation, z_rotation) local creeper_colors = {"creeper_white", "creeper_lightgreen", "creeper_green", "creeper_darkgreen"} tex.sprint("\\tdplotsetmaincoords{" .. x_rotation .. "}{" .. z_rotation .. "}") tex.sprint("\\begin{tikzpicture}[tdplot_main_coords]") math.randomseed(os.time()) draw_leg(-2, 0, -18, creeper_colors) draw_leg(6, 0, -18, creeper_colors) draw_bodypart(2, 0, -12, 4, 8, 12, creeper_colors) draw_head(0, 0, 0, creeper_colors) -- draw_coordinate_system() tex.sprint("\\end{tikzpicture}") end \end{luacode*} \begin{document} \luadirect{draw_creeper(66, 135)} \end{document} |