hyperboloid.lua


NAME
    hyperboloid 

FUNCTION
    hyperboloid(n, r, a, b)

NOTES
    Generate hyperboloid surface based on parametric equations of
    
        x = a sqrt(1 + u^2) cos(v)
        y = a sqrt(1 + u^2) sin(v)
        z = b u

    Example:
        require("plot_simple")
        plot = plot_simple.new()
        plot:add_static(zeGrf.new("light"))
        require("hyperboloid")
        xyz, nor = hyperboloid(32, 1, 1, 1)
        xyz:scale(50, 50, 50)
        shape = zeGrf.new("polygon")
        shape:set{type = "quads", vertex = xyz,
                  vertex_normal = nor, color = {0, .7, .7, 1}}
        plot:add(shape)
        plot:animate()
NPUTS
    n    - slices between 0 and 2pi
    r    - range
    a, b - hyperbolic parameters

OUTPUTS
    Two zeVertex objects containing coordinates and normals of quads.

SOURCE

require("surface_generator")

function hyperboloid(n, r, a, b)
    assert(n >= 8)
    assert(r > 0)
    assert(a > 0)
    assert(b > 0)

    local function sfunc(u, v)
        local r = a*math.sqrt(1 + u*u)
        return r*math.cos(v), r*math.sin(v), b*u
    end

    local function nfunc(u, v)
        local r = math.sqrt(1 + u*u)
        local cosv, sinv = math.cos(v), math.sin(v)
        return zeMake.normal2(0, 0, 0,
                              a*cosv*u/r, a*sinv*u/r, b,
                             -a*r*sinv, a*r*cosv, 0)
    end

    local U, V = zeUtl.new("double", "double")
    U:range(-r, r/n, 2*n+1)
    V:range(0, math.pi/n, 2*n+1)
    
    return surface_generator(U, V, sfunc),
           surface_generator(U, V, nfunc)
end