%!TEX lualatex
\documentclass[border=1pt]{standalone}
\usepackage{luamplib}
\begin{document}
%https://tex.stackexchange.com/questions/648153/how-to-create-triangular-pyramid-of-oranges-using-tikz
\begin{mplibcode}
% first set up the isometric projection
numeric alpha, beta, ipca, ipsa, ipcb, ipsb, ipscale;
alpha = -20;
beta = 10;
ipca = cosd(alpha); ipsa = sind(alpha);
ipcb = cosd(beta); ipsb = sind(beta);
ipscale := 16;
% this macro projects 3D to 2D (isometrically)
vardef p(expr x, y, z) =
(x * ipcb - z * ipsb, y * ipca + x * ipsa * ipsb + z * ipsa * ipcb) scaled ipscale
enddef;
% now make an orange picture
color a, b;
b = (0.99608,0.90196,0.80784);
a = (0.54902,0.17647,0.015686);
path C, c; numeric n; n = 16;
C = fullcircle scaled ipscale;
c = fullcircle scaled 1/2 shifted (-3,3);
picture orange;
orange = image(for i=0 upto n:
fill interpath(i/n, C, c) withcolor (i/n)[a,b];
endfor);
beginfig(1);
for n = 6 : % draw four stacks of oranges...
picture stack; stack = image(
for k = n-1 downto 0:
for j = k downto 0:
for i = 0 upto j:
draw orange shifted p(i - 0.5 j, -0.866 k, -0.866 j + 0.5 k);
endfor
endfor
endfor
);
numeric x; x = 42n * sqrt(n);
draw stack shifted (x, 8n);
endfor
endfig;
\end{mplibcode}
\end{document}