Monday, February 23, 2015

Drawing a Hyperbola in SSMS

The last and most common shape of motion in the space is Hyperbola.

Small objects from outer space change their orbits insignificantly under heavy gravitation of bigger bodies.

Here is the formula I've used to draw hyperbolas:
Where:
X and Y - Hyperbola's coordinates;
A and B - imaginary central point;
L - slop.

Here is a classic hyperbola script for SQL Server:

 DECLARE @MP VARCHAR(MAX)=''; DECLARE @l DECIMAL(8,4) = 1;    -- Slope DECLARE @bx DECIMAL(8,4) = 0;    -- X-Center point DECLARE @by DECIMAL(8,4) = 0;    -- Y-Center point DECLARE @s DECIMAL(8,4) = 10;    -- X-Span of Hyperbola DECLARE @Step DECIMAL(8,4) = 0.1;    -- Step DECLARE @k DECIMAL(8,4) = 0; DECLARE @cx DECIMAL(8,4) = -@s;    -- X-Current point DECLARE @x1 DECIMAL(19,9); DECLARE @y1 DECIMAL(19,9); DECLARE @x2 DECIMAL(19,9) = @cx + @bx; DECLARE @y2 DECIMAL(19,9) = @by + @k*@x2 + @l / (@x2 - @bx); WHILE @cx < @s SELECT     @cx += @Step,     @x1 = @x2, @y1 = @y2,     @x2 = @cx + @bx,     @y2 = CASE @cx WHEN 0 THEN @y1                 ELSE @by + @k*@x2 + @l / (@x2 - @bx) END,     @MP = @MP + CASE WHEN @x2 = @bx                   OR (@x1 <= @bx and @x2 >= @bx ) THEN '' ELSE '('         + CAST(@x1 as VARCHAR) +  ' '         + CAST(@y1 as VARCHAR) + ','         + CAST(@x2 as VARCHAR) +  ' '         + CAST(@y2 as VARCHAR) + '),' END ; SELECT CAST('MULTILINESTRING(' + LEFT(@MP,LEN(@MP)-1) + ')' as geometry);

Here is the result of that script in SSMS:

By adjusting variables you can get variety of hyperbola shapes:

 DECLARE @l DECIMAL(8,4) = -2;    -- Slope DECLARE @bx DECIMAL(8,4) = 12;    -- X-Center point DECLARE @by DECIMAL(8,4) = 3;    -- Y-Center point DECLARE @s DECIMAL(8,4) = 25;    -- X-Span of Hyperbola DECLARE @Step DECIMAL(8,4) = 0.1;    -- Step DECLARE @k DECIMAL(8,4) = 0.5;