awk — Geometry

Circle — area and circumference from radius
echo "5" | awk '{
    pi=atan2(0,-1)
    printf "radius=%.1f area=%.4f circumference=%.4f\n", $1, pi*$1^2, 2*pi*$1
}'
Sphere — surface area and volume
echo "3" | awk '{
    pi=atan2(0,-1)
    printf "radius=%.1f\n", $1
    printf "surface area=%.4f\n", 4*pi*$1^2
    printf "volume=%.4f\n", (4.0/3)*pi*$1^3
}'
Cylinder volume — rack unit to volume (server room planning)
# Cable spool: radius=15cm, height=10cm
awk 'BEGIN{
    pi=atan2(0,-1)
    r=15; h=10
    vol=pi*r^2*h
    printf "cylinder: r=%dcm h=%dcm → volume=%.2f cm³ (%.2f liters)\n", r, h, vol, vol/1000
}'
Rectangle and triangle areas
awk 'BEGIN{
    # Rectangle
    w=4; h=7
    printf "rectangle %dx%d = %d\n", w, h, w*h

    # Triangle (base, height)
    b=6; th=5
    printf "triangle b=%d h=%d = %.1f\n", b, th, 0.5*b*th

    # Triangle (three sides — Heron'\''s formula)
    a=3; b=4; c=5
    s=(a+b+c)/2
    area=sqrt(s*(s-a)*(s-b)*(s-c))
    printf "triangle sides=%d,%d,%d = %.4f\n", a, b, c, area
}'
Distance between two GPS coordinates (Haversine formula)
# Los Angeles to New York
awk 'BEGIN{
    pi=atan2(0,-1)
    lat1=34.0522*pi/180; lon1=-118.2437*pi/180
    lat2=40.7128*pi/180; lon2=-74.0060*pi/180
    R=6371  # Earth radius in km

    dlat=lat2-lat1; dlon=lon2-lon1
    a=sin(dlat/2)^2+cos(lat1)*cos(lat2)*sin(dlon/2)^2
    c=2*atan2(sqrt(a),sqrt(1-a))
    printf "LA to NYC: %.1f km (%.1f miles)\n", R*c, R*c*0.621371
}'
Polygon area from vertices (Shoelace formula)
# Data center floor plan: vertices (x,y) in meters
cat <<'EOF' | awk '{x[NR]=$1; y[NR]=$2} END{
    n=NR; sum=0
    for(i=1;i<=n;i++){
        j=(i%n)+1
        sum+=(x[i]*y[j]-x[j]*y[i])
    }
    area=sqrt(sum^2)/2
    printf "polygon area: %.2f m² (%.2f ft²)\n", area, area*10.7639
}'
0 0
10 0
10 8
5 12
0 8
EOF
Cable length — 3D distance between rack positions
# Rack A at (2,3,1) and Rack B at (8,5,4) in meters
awk 'BEGIN{
    x1=2;y1=3;z1=1; x2=8;y2=5;z2=4
    straight=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)
    # Add 20% for slack and routing
    printf "straight: %.2fm\n", straight
    printf "with slack: %.2fm\n", straight*1.2
    printf "recommended: %.0fm cable\n", int(straight*1.2)+1
}'
Pythagorean theorem — is it a right triangle?
cat <<'EOF' | awk '{
    a=$1; b=$2; c=$3
    # Sort so c is largest
    if(a>c){t=a;a=c;c=t}
    if(b>c){t=b;b=c;c=t}
    diff=a^2+b^2-c^2
    printf "(%d,%d,%d): %s\n", $1,$2,$3, (diff==0?"right triangle":"not right")
}'
3 4 5
5 12 13
7 8 10
8 15 17
EOF
Degrees, radians, gradians conversion table
awk 'BEGIN{
    pi=atan2(0,-1)
    printf "%-10s %-12s %-10s\n", "Degrees", "Radians", "Gradians"
    for(d=0;d<=360;d+=30){
        printf "%-10d %-12.6f %-10.4f\n", d, d*pi/180, d*10/9
    }
}'
Midpoint and slope between two points
awk 'BEGIN{
    x1=2; y1=3; x2=8; y2=15
    mx=(x1+x2)/2; my=(y1+y2)/2
    slope=(y2-y1)/(x2-x1)
    printf "points: (%d,%d) and (%d,%d)\n", x1,y1,x2,y2
    printf "midpoint: (%.1f, %.1f)\n", mx, my
    printf "slope: %.4f\n", slope
    printf "y-intercept: %.4f\n", y1-slope*x1
    printf "equation: y = %.4fx + %.4f\n", slope, y1-slope*x1
}'