I am trying to draw a cube that can be inscribed within a right circular cone (cone of radius r and perpendicular height h). I see at here https://www.geeksforgeeks.org/largest-cube-that-can-be-inscribed-within-a-right-circular-cone/ I can find the side length of cube. But I cann't draw it. I tried
\documentclass[border=3.14mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,backgrounds}
\begin{document}
%polar coordinates of visibility
\pgfmathsetmacro\th{65}
\pgfmathsetmacro\az{110}
\tdplotsetmaincoords{\th}{\az}
%parameters of the cone
\pgfmathsetmacro\R{4} %radius of base
\pgfmathsetmacro\v{5} %hight of cone
\begin{tikzpicture} [scale=1, tdplot_main_coords, axis/.style={blue,thick}]
\path
coordinate (O) at (0,0,0)
coordinate (A) at (\R,0,0)
coordinate (B) at (0,\R,0)
coordinate (C) at ($ 2*(O) - (A) $)
coordinate (D) at ($ 2*(O) - (B) $)
coordinate (S) at (0,0,\v)
;
\fill (S) circle[radius=1pt] node[above] {$S$};
\fill (A) circle[radius=1pt] node[below] {$A$};
\fill (B) circle[radius=1pt] node[below] {$B$};
\fill (C) circle[radius=1pt] node[above] {$C$};
\fill (D) circle[radius=1pt] node[above] {$D$};
\fill (O) circle[radius=1pt] node[below] {$O$};
\draw[thick] (A) -- (S) (S) -- (B);
\draw[dashed] (A) -- (B) -- (C) -- (D) -- cycle (S) -- (C) (A) -- (C) (B) -- (D) (S) -- (O);
\pgfmathsetmacro\cott{{cot(\th)}}
\pgfmathsetmacro\fraction{\R*\cott/\v}
\pgfmathsetmacro\fraction{\fraction<1 ? \fraction : 1}
\pgfmathsetmacro\angle{{acos(\fraction)}}
% % angles for transformed lines
\pgfmathsetmacro\PhiOne{180+(\az-90)+\angle}
\pgfmathsetmacro\PhiTwo{180+(\az-90)-\angle}
% % coordinates for transformed surface lines
\pgfmathsetmacro\sinPhiOne{{sin(\PhiOne)}}
\pgfmathsetmacro\cosPhiOne{{cos(\PhiOne)}}
\pgfmathsetmacro\sinPhiTwo{{sin(\PhiTwo)}}
\pgfmathsetmacro\cosPhiTwo{{cos(\PhiTwo)}}
% % angles for original surface lines
\pgfmathsetmacro\sinazp{{sin(\az-90)}}
\pgfmathsetmacro\cosazp{{cos(\az-90)}}
\pgfmathsetmacro\sinazm{{sin(90-\az)}}
\pgfmathsetmacro\cosazm{{cos(90-\az)}}
% % draw basis circle
\tdplotdrawarc[tdplot_main_coords,thick]{(O)}{\R}{\PhiOne}{360+\PhiTwo}{anchor=north}{}
\tdplotdrawarc[tdplot_main_coords,dashed]{(O)}{\R}{\PhiTwo}{\PhiOne}{anchor=north}{}
% % displaying tranformed surface of the cone (rotated)
\draw[thick] (0,0,\v) -- (\R*\cosPhiOne,\R*\sinPhiOne,0);
\draw[thick] (0,0,\v) -- (\R*\cosPhiTwo,\R*\sinPhiTwo,0);
\end{tikzpicture}
\end{document}