anchor.diff:  .Figure 1 (b)
   image::https://github.com/lxjk/lxjk.github.io/raw/master/images/frustum/fig1b.png[, 300,align="center"]
   
  +.Figure 2
  +image::https://github.com/lxjk/lxjk.github.io/raw/master/images/frustum/fig2.png[, 400,align="center"]
  +
   Now let’s go back to the normal case as shown in figure 2. Intuitively we can guess the minimal bounding sphere should sits on somewhere between near plane center stem:[{N_0}(0,-n)] and far plane center stem:[{F_0}(0,-f)], and it should have the same distance to all 4 vertices of the frustum, that is stem:[\left|\vec{C{N_1}}\right|=\left|\vec{C{N_2}}\right|=\left|\vec{C{F_1}}\right|=\left|\vec{C{F_2}}\right|=R]. Can we actually find such a point? Since frustum is symmetrical along Y axis, if we found a point stem:[C] such that stem:[\left|\vec{C{N_1}}\right|=\left|\vec{C{F_1}}\right|=R], it is guaranteed we will have stem:[\left|\vec{C{N_2}}\right|=\left|\vec{C{F_2}}\right|=R] as well. Now we just need to calculate that point.
   
   Remember our half FOV angle is denoted as stem:[α], we have stem:[\left|\vec{{N_0}{N_1}}\right|=n\tan⁡α, \left|\vec{{F_0}{F_1}}\right|=f\tan⁡α, \left|\vec{{N_0}{F_0}}\right|=f-n]. We denote stem:[x=\left|\vec{C{N_0}}\right|], then stem:[\left|\vec{C{F_0}}\right|=f-n-x], if we solve x we can find out radius stem:[R]:
  
positive.code: \begin{align*}
  R&=\left|\vec{{C}{N_1}}\right|=\sqrt{\left|\vec{{C}{N_0}}\right|^{2}+\left|\vec{{N_0}{N_1}}\right|^{2}}\\
  &=\sqrt{x^{2}+n^{2}\tan^{2}α}\\
  &=\frac{1}{2}\sqrt{(f-n)^{2}+2(f^{2}+n^{2})tan^{2}α+(f+n)^{2}tan^{4}α}\\
  \end{align*}
  ++++
  
  
negative.code: 
  However there is a catch. As shown in figure 3, if the near plane is close to far plane, we might end up with a bounding sphere larger than we need. Sphere stem:[C] is the sphere we calculated, but sphere stem:[C'={F_0}] is the minimal sphere. This is because based on our calculation, we want to fit all 4 vertices on the sphere. In the normal case this will give us the minimal sphere, but when near plane is close to far plane, it is better to give up near plane since it is already inside the bounding sphere based on far plane only.
  
  .Figure 3
  image::https://github.com/lxjk/lxjk.github.io/raw/master/images/frustum/fig3.png[, 400,align="center"]
  
  What is the condition we should give up? Take another look at the sphere center we calculated, we got a larger sphere because our center is farther along Y axis than our far plane, so we can simply make sure our bounding sphere center sits within the frustum, if it is farther than the far plane, clamp it to the far plane. The condition that we should clamp is
  
  [stem]
  ++++
  \begin{align*}
  -\frac{1}{2}(f+n)(1+tan^{2}α)&\leqslant-f\\
  tan^{2}α&\geqslant\frac{f-n}{f+n}
  \end{align*}
  ++++
  
  To rewrite our result
  
  If stem:[tan^{2}α&\geqslant\frac{f-n}{f+n}]
  
  [stem]
  ++++
  \begin{align*}
  C&=(0, -f)\\
  R&=f\tanα
  \end{align*}
  ++++
  
  Else
  
  [stem]
  ++++
  \begin{align*}
  C&=(0, -\frac{1}{2}(f+n)(1+tan^{2}α))\\
  R&=\frac{1}{2}\sqrt{(f-n)^{2}+2(f^{2}+n^{2})tan^{2}α+(f+n)^{2}tan^{4}α}
  \end{align*}
  ++++
  
  We solved 2D situation, going into 3D it is actually no more difficult than 2D. As shown in figure 4, we just need to work on the 2D frustum defined by stem:[{N_1}{N_3}{F_1}{F_3}]. Similarly, based on symmetricity of frustum, if we find a bonding sphere that stem:[N_1] and stem:[F_1] is on the sphere, it is guaranteed that all 8 vertices of frustum will be on the sphere. The only extra thing we need to do here is calculate stem:[\left|\vec{{N_0}{N_1}}\right|] and stem:[\left|\vec{{F_0}{F_1}}\right|]. It depends on which is the major axis of your field of view. I will use X axis as major axis for example, let stem:[w] be viewport width, stem:[h] be viewport height, we have stem:[{N_1}=(-n\tan⁡α,n\frac{h}{w}\tan⁡α), \left|\vec{{N_0}{N_1}}\right|=n\sqrt{1+\frac{h^{2}}{w^{2}}}\tan⁡α], similarly stem:[\left|\vec{{F_0}{F_1}}\right|=f\sqrt{1+\frac{h^{2}}{w^{2}}}\tan⁡α].
  
  .Figure 4
  image::https://github.com/lxjk/lxjk.github.io/raw/master/images/frustum/fig4.png[, 400,align="center"]
  
  Here is our collusion. For 3D frustum with viewport width stem:[w], height stem:[h], near plane stem:[n], far plane stem:[f], X axis field of view angle stem:[fov], let stem:[k=\sqrt{1+\frac{h^{2}}{w^{2}}}\tan⁡{\frac{fov}{2}}], then the minimal bounding sphere is:
  
  If stem:[k^{2}&\geqslant\frac{f-n}{f+n}]
  
  [stem]
  ++++
  \begin{align*}
  C&=(0, -f)\\
  R&=fk
  \end{align*}
  ++++
  
  Else
  
  [stem]
  ++++
  \begin{align*}
  C&=(0, -\frac{1}{2}(f+n)(1+k^{2}))\\
  R&=\frac{1}{2}\sqrt{(f-n)^{2}+2(f^{2}+n^{2})k^{2}+(f+n)^{2}k^{4}}
  \end{align*}
  ++++
  