Skip to content

Main content

3D Shape Surface Area Calculator Source Code

Browse the implementation files for 3D Shape Surface Area Calculator.
@tool.io/tools/geometry/surface-area source ↗
tool.ts typescript
import {
  calculateSurfaceArea,
  type SurfaceShape,
} from '@tool.io/tools/geometry/surface-area';

const isMissing = (input: unknown): boolean =>
  input === undefined ||
  input === null ||
  (typeof input === 'string' && input.trim() === '');

const parseOptionalNumber = (input: unknown): number | undefined | null => {
  if (isMissing(input)) return undefined;
  if (typeof input !== 'string' && typeof input !== 'number') return null;
  const value = Number(input);
  return Number.isFinite(value) ? value : null;
};

export const surfaceArea = (
  shapeInput: unknown,
  dimensionAInput: unknown,
  dimensionBInput: unknown,
  dimensionCInput: unknown,
): string => {
  if (typeof shapeInput !== 'string') return '';

  const dimensions = [dimensionAInput, dimensionBInput, dimensionCInput].map(
    parseOptionalNumber,
  );
  if (dimensions.includes(null)) return '';
  const [dimensionA, dimensionB, dimensionC] = dimensions as Array<
    number | undefined
  >;
  if (dimensionA === undefined) return '';
  const result = calculateSurfaceArea(
    shapeInput as SurfaceShape,
    dimensionA,
    dimensionB,
    dimensionC,
  );
  return result === null ? '' : result.toFixed(5);
};

export const Tool: TypeTool = {
  name: '3D Shape Surface Area Calculator',
  slug: 'geometry/surface-area',
  intro: 'Calculate the total surface area of common three-dimensional shapes.',
  tool: surfaceArea,
  i: [
    {
      label: 'Shape',
      placeholder: 'Select a 3D shape',
      type: 'select',
      options: [
        {
          value: 'sphere',
          text: 'Sphere — A: radius',
          selected: true,
        },
        {
          value: 'cone',
          text: 'Cone — A: radius, B: height',
        },
        {
          value: 'cube',
          text: 'Cube — A: edge length',
        },
        {
          value: 'cylinder',
          text: 'Cylinder — A: radius, B: height',
        },
        {
          value: 'rectangular-prism',
          text: 'Rectangular prism — A: length, B: width, C: height',
        },
        {
          value: 'capsule',
          text: 'Capsule — A: radius, B: cylinder length',
        },
        {
          value: 'spherical-cap',
          text: 'Spherical cap — A: sphere radius, B: cap height',
        },
        {
          value: 'conical-frustum',
          text: 'Conical frustum — A: large radius, B: small radius, C: height',
        },
        {
          value: 'ellipsoid',
          text: 'Ellipsoid approximation — A, B, C: semiaxes',
        },
        {
          value: 'square-pyramid',
          text: 'Square pyramid — A: base edge, B: vertical height',
        },
      ],
    },
    {
      example: '1',
      help: 'Use the selected shape description to identify dimension A.',
      label: 'Dimension A',
      placeholder: 'Dimension A',
      type: 'number',
    },
    {
      example: '1',
      help: 'Use the selected shape description to identify dimension B.',
      label: 'Dimension B',
      placeholder: 'Dimension B when required',
      type: 'number',
    },
    {
      example: '1',
      help: 'Use the selected shape description to identify dimension C.',
      label: 'Dimension C',
      placeholder: 'Dimension C when required',
      type: 'number',
    },
  ],
  o: [
    {
      example: '12.56637',
      help: 'Total surface area. Ellipsoid results use the Knud Thomsen approximation.',
      label: 'Surface Area',
      placeholder: 'Surface area will appear here...',
      type: 'number',
      render: 'textarea',
    },
  ],
  equation: [
    { name: 'Sphere', eq: '$$S=4\\pi r^2$$' },
    { name: 'Cone', eq: '$$S=\\pi r(r+\\sqrt{r^2+h^2})$$' },
    { name: 'Cube', eq: '$$S=6a^2$$' },
    { name: 'Cylinder', eq: '$$S=2\\pi r(r+h)$$' },
    { name: 'Rectangular prism', eq: '$$S=2(lw+lh+wh)$$' },
    { name: 'Capsule', eq: '$$S=4\\pi r^2+2\\pi rL$$' },
    { name: 'Spherical cap including base', eq: '$$S=\\pi h(4R-h)$$' },
    {
      name: 'Conical frustum',
      eq: '$$S=\\pi(R^2+r^2)+\\pi(R+r)\\sqrt{(R-r)^2+h^2}$$',
    },
    {
      name: 'Ellipsoid — Knud Thomsen approximation, p=1.6075',
      eq: '$$S\\approx4\\pi\\left(\\frac{(ab)^p+(ac)^p+(bc)^p}{3}\\right)^{1/p}$$',
    },
    {
      name: 'Square pyramid',
      eq: '$$S=a^2+2a\\sqrt{(a/2)^2+h^2}$$',
    },
  ],
  cite: [
    {
      title: 'Triaxial ellipsoid surface area — NIST DLMF',
      url: 'https://dlmf.nist.gov/19.33',
    },
    {
      title: 'Knud Thomsen ellipsoid approximation — CSUN',
      url: 'https://www.csun.edu/~hcmth008/150a/bccalclt03_0606.pdf',
    },
  ],
};

export default Tool;

Description

Browse the implementation files for 3D Shape Surface Area Calculator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools