Results 1 to 2 of 2

Shape Functions

  1. #1
    Lost in the code... Mikali's Avatar
    Join Date
    Jun 2003
    Location
    %HW2_ROOT%

    Shape Functions

    Here's a library of curve and surface functions in 2D and 3D. These functions randomly generate individual points along the surface--and sometimes the interior--of the given shape. Each function outputs another table containing the coordinates of the randomly generated point.

    Code:
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeCuboid (a, b, c, d, e)
    	local t = random()
    	local l = random3(-a, a)
    	local w = random3(-b, b)
    	local h = random3(-c, c)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local p = randomSign()
    	if (t < 1/3) then
    		l = L * p
    	elseif (t < 2/3) then
    		w = W * p
    	elseif (t <= 1) then
    		h = H * p
    	end
    	return
    	{
    		l,
    		h,
    		w,
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeEllipsoid (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local u = random3(180)
    	local v = random3(360)
    	return
    	{
    		L * cos(v) * sin(u),
    		H * sin(v) * sin(u),
    		W * cos(u),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeCylinder (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local h = random3(-c, c)
    	local u = random3(180)
    	local v = random3(360)
    	return
    	{
    		L * cos(v),
    		h,
    		W * sin(v),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeCone (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		(1 - h / c) * L * cos(v) / 2,
    		h,
    		(1 - h / c) * W * sin(v) / 2,
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the width of the tube, <fD> equals the thickness, and <fE> equals the height of the tube.
    function makeToroid (a, b, c, d, e)
    	local H = random3(c - d, c)
    	local M = random3(e - d, e)
    	local v = random3(360)
    	local o = random3(360)
    	return
    	{
    		(a + M * cos(v)) * cos(o),
    		H * sin(v),
    		(b + M * cos(v)) * sin(o),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, and <fD> equals the width (thickness is not supported), <fE> is the number of revolutions.
    function makeHelicoid (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local t = random()
    	return
    	{
    		L * cos(t * e * 360),
    		c * (2 * t - 1),
    		W * sin(t * e *  360),
    	}
    end
    
    -- <fA> equals the length of axis 1 at a height of 1000 units, <fB> equals the length of axis 2 at a height of 1000 units, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeParaboloid (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		L * sqrt(h / 1000) * cos(v),
    		h,
    		W * sqrt(h / 1000) * sin(v),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeHyperboloid (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local t = random()
    	local v = random3(360)
    	local p = randomSign()
    	return
    	{
    		L * sqrt(1 + (t * p)^2) * cos(v),
    		H * (t * p),
    		W * sqrt(1 + (t * p)^2) * sin(v),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, <fD> equals the thickness, and <fE> is zero.
    function makeAstroid (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local v = random3(360)
    	local o = random3(360)
    	return
    	{
    		L * (cos(o) * cos(v))^3,
    		H * (sin(v))^3,
    		W * (sin(o) * cos(v))^3,
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals roughly the inverse of the length of axis 3 for large numbers of objects, <fD> equals the thickness, and <fE> is zero.
    function makeFunnel (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local t = random()
    	local v = random3(360)
    	return
    	{
    		L * t * cos(v),
    		H * log(t) / 10,
    		W * t * sin(v),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the distance between each twist, <fD> is zero, and <fE> is the number of twists.
    function makeDini (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local v = random3(360)
    	local u = random3(180)
    	return
    	{
    		L * (cos(e * v) * sin(u/2)),
    		H * (cos(u/2) + log(tan(u/4)) + rad(e * v) / (2 * PI)),
    		W * (sin(e * v) * sin(u/2)),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the height of the screw, <fD> is zero, and <fE> is zero.
    function makeCorkscrew (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local v = random3(360)
    	local u = random3(180)
    	return
    	{
    		L * cos(v) * cos(u),
    		H * rad(v) / (2 * PI),
    		W * sin(v) * cos(u),
    	}
    end
    
    -- <fA> equals the length of axis 1 of the tube, <fB> equals the length of axis 2 of the tube, <fC> equals the vertical separation between revolutions, <fD> equals the radius of the center gap, and <fE> equals the number of revolutions.
    function makeSeashell (a, b, c, d, e)
    	local t = random()
    	local o = random3(360)
    	return
    	{
    		(d / e + (1 - t) * (1 + cos(o))) * a * cos(e * t * 360),
    		c * t^(1/2) * (2 * e - 1) + b * sin(o) * (1 - t),
    		(d / e + (1 - t) * (1 + cos(o))) * a * sin(e * t * 360),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the maximum height of the wave, <fD> zero, and <fE> equals the frequency of the wave pattern.
    function makeSineDisc (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local H = random3(c - d, c)
    	local t = random()
    	local v = random3(360)
    	return
    	{
    		L * t * cos(v),
    		H * sin(t * 360 * e),
    		W * t * sin(v),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the maximum height of the wave, <fD> zero, and <fE> equals the frequency of the wave pattern.
    function makeSinePlane (a, b, c, d, e)
    	local s = random()
    	local t = random()
    	return
    	{
    		a * s * 2 - a,
    		c * (sin(s * 360 * e) + sin(t * 360 * e)) / 2,
    		b * t * 2 - b,
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals half the width of the strip, <fD> is zero, and <fE> is zero.
    function makeMoebius (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		L * cos(v) + h * cos(v/2) * cos(v),
    		h * sin(v/2),
    		W * sin(v) + h * cos(v/2) * sin(v),
    	}
    end
    
    -- <fA> equals the length of the bounding cube, <fB> equals the thickenss of the curve, <fD> equals the speed of the second arc relative to the first, <fD> equals the speed of the third arc relative to the first, and <fE> equals the number of revolutions.
    function makeLissajous3D (a, b, c, d, e)
    	local v = random3(360)
    	local o = random3(360)
    	local u = random3(180)
    	return
    	{
    		a * sin(v * e)		+ b * sin(u) * cos(o),
    		a * sin(v * e * c)	+ b * sin(u) * sin(o),
    		a * sin(v * e * d)	+ b * cos(u),
    	}
    end
    
    -- <fA> equals the scaling along the x-axis, <fB> equals the scaling along the z-axis, <fC> equals the scaling along the y-axis, <fD> is zero, and <fE> is zero.
    function makeKlein (a, b, c, d, e)
    	local u = random3(360)
    	local v = random3(180)
    	local X = cos(u) * (cos(u/2) * (sqrt(2) + cos(v)) + sin(u/2) * sin(v) * cos(v))
    	local Y = -1 * sin(u/2) * (sqrt(2) + cos(v)) + cos(u/2) * sin(v) * cos(v)
    	local Z = sin(u) * (cos(u/2) * (sqrt(2) + cos(v)) + sin(u/2) * sin(v) * cos(v))
    	return
    	{
    		X * a,
    		Y * c,
    		Z * b,
    	}
    end
    
    -- <fA> equals the scaling along the x-axis, <fB> equals the scaling along the z-axis, <fC> equals the scaling along the y-axis, <fD> is zero, and <fE> is zero.
    function makeKlein8 (a, b, c, d, e)
    	local u = random3(360)
    	local v = random3(360)
    	local X = (e + cos(u/2) * sin(v) - sin(u/2) * sin(v*2)) * cos(u)
    	local Y = sin(u/2) * sin(v) + cos(u/2) * sin(v*2)
    	local Z = (e + cos(u/2) * sin(v) - sin(u/2) * sin(v*2)) * sin(u)
    	return
    	{
    		X * a,
    		Y * c,
    		Z * b,
    	}
    end
    
    -- description missing
    function makeKuen (a, b, c, d, e)
    	local u = random3(180)
    	local v = random3(360)
    	local X = 2 * (cos(v) + rad(v) * sin(v)) * sin(u) / (1 + rad(v)^2 * sin(u)^2)
    	local Y = log(tan(u/2)) + 2 * cos(u) / (1 + rad(v)^2 * sin(u)^2)
    	local Z = 2 * (sin(v) - rad(v) * cos(v)) * sin(u) / (1 + rad(v)^2 * sin(u)^2)
    	return
    	{
    		X * a,
    		Y * c,
    		Z * b,
    	}
    end
    
    -- <fA> equals the scaling along the x-axis, <fB> equals the scaling along the z-axis, <fC> equals the scaling along the y-axis, <fD> is zero, and <fE> is an integer greater than 2.
    function makeBoy (a, b, c, d, e)
    	local u = random3(360)
    	local v = random3(180)
    	local X = (2/3) * (cos(u) * cos(2 * v) + sqrt(2) * sin(u) * cos(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
    	local Y = sqrt(2) * cos(u)^2 / (sqrt(2) - sin(2 * u) * sin(2 * v))
    	local Z = (2/3) * (cos(u) * sin(2 * v) - sqrt(2) * sin(u) * sin(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
    	return
    	{
    		X * a,
    		Y * c,
    		Z * b,
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, and <fD> equals the thickness.
    function makeRectangle (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local l = random3(-a, a)
    	local w = random3(-b, b)
    	local h = random3(-c, c)
    	local p = randomSign()
    	if (t <= 1/2) then
    		l = L * p
    	elseif (t <= 1) then
    		w = W * p
    	end
    	return
    	{
    		l,
    		h,
    		w,
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the length of axis 3, and <fD> equals the thickness.
    function makeEllipse (a, b, c, d, e)
    	local L = random3(a - d, a)
    	local W = random3(b - d, b)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		L * cos(v),
    		h,
    		W * sin(v),
    	}
    end
    
    -- <fA> equals the distance between the vertex and the focus, <fB> equals the length, <fC> equals the height, and <fD> equals the thickness.
    function makeParabola (a, b, c, d, e)
    	local w = random3(-b, b)
    	local h = random3(-c, c)
    	local p = randomSign()
    	return
    	{
    		sqrt(4 * w * a) * p + random3(-d/2, d/2),
    		h,
    		w + random3(-d/2, d/2),
    	}
    end
    
    -- <fA> equals the length of axis 1, <fB> equals the length of axis 2, <fC> equals the distance from the origin to one of the foci, <fD> equals the thickness.
    function makeHyperbola (a, b, c, d, e)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		a / cos(v) + random3(-d/2, d/2),
    		h,
    		b * tan(v) + random3(-d/2, d/2),
    	}
    end
    
    -- <fA> equals the radius of the greater circle, <fB> equals the radius of the lesser circle, <fC> equals the height above or below the plane, <fD> equals the distance from the center of the lesser circle, and <fE> equals the number of revolutions.
    function makeHypotrochoid (a, b, c, d, e)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		(a - b) * cos(v * e) + d * cos((a - b) / b * v * e),
    		h,
    		(a - b) * sin(v * e) - d * sin((a - b) / b * v * e),
    	}
    end
    
    -- <fA> equals the radius of the greater circle, <fB> equals the radius of the lesser circle, <fC> equals the height above or below the plane, <fD> equals the distance from the center of the lesser circle, and <fE> equals the number of revolutions.
    function makeEpitrochoid (a, b, c, d, e)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		(a + b) * cos(v * e) - d * cos((a + b) / b * v * e),
    		h,
    		(a + b) * sin(v * e) - d * sin((a + b) / b * v * e),
    	}
    end
    
    -- <fA> equals the length of the bounding square, <fB> equals the phase shift, <fC> equals the height of the curve, <fD> equals the speed of the second arc relative to the first, and <fE> equals the number of revolutions.
    function makeLissajous2D (a, b, c, d, e)
    	local h = random3(-c, c)
    	local v = random3(360)
    	return
    	{
    		a * sin(v * e + b),
    		h,
    		a * sin(v * e * d),
    	}
    end
    Download my HW2 mods, maps & tools. link
    Username|SF on Gamespy/Xfire/Hamachi/Gameranger

  2. #2
    Croatians
    Guest

    Shape Functions

    Other variant is possible also

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •