Sunday, February 22, 2015

Drawing a Parabola in SSMS

Some asteroids are first and last time visitors in our Solar System.
They travel between stars in Milky Way galaxy and sometimes come so close to our Sun that completely change their orbit.
Usually their orbit is changed in shape of hyperbola, but sometimes they are going by parabola shaped orbit.

The formula I've used is not a conventional one, but it served my needs pretty well:
Where:
X and Y - parabola's coordinates;
A and B - coordinates of parabola's vertex;
L -  parabola's slop;
n - parabola's power;

So, it is a time will draw a Parabola in SQL Server management studio.

Here is a script to draw classical shaped parabola:
 DECLARE @MP VARCHAR(MAX)=''; DECLARE @l DECIMAL(8,4) = 1;    -- Slope DECLARE @bx DECIMAL(8,4) = 0;    -- X-bottom point DECLARE @by DECIMAL(8,4) = 0;    -- Y-bottom point DECLARE @p DECIMAL(8,4) = 2;    -- Parabola's Power DECLARE @Step DECIMAL(8,4) = 0.01;    -- Step DECLARE @s DECIMAL(8,4) = 5;    -- X-Span of Parabola DECLARE @cx DECIMAL(8,4) = -@s;    -- X-Current point DECLARE @x1 DECIMAL(19,4); DECLARE @y1 DECIMAL(19,4); DECLARE @x2 DECIMAL(19,4) = @cx + @bx; DECLARE @y2 DECIMAL(19,4) = @by + @l * POWER(ABS(@x2 - @bx),@p); WHILE @cx < @s SELECT     @cx += @Step,     @x1 = @x2, @y1 = @y2,     @x2 = @cx + @bx,     @y2 = @by + @l * POWER(ABS(@x2 - @bx),@p),     @MP = @MP + '('         + CAST(@x1 as VARCHAR) +  ' '         + CAST(@y1 as VARCHAR) + ','         + CAST(@x2 as VARCHAR) +  ' '         + CAST(@y2 as VARCHAR) + '),'; SELECT CAST('MULTILINESTRING(' + LEFT(@MP,LEN(@MP)-1) + ')' as geometry);

As the result I've got that beautiful picture:

By changing slope, parabola power and the coordinates of the bottom point you can get very different parabola shapes.

Here is one example of parameter set:
 DECLARE @l DECIMAL(8,4) = -0.5;    -- Slope DECLARE @bx DECIMAL(8,4) = 7;    -- X-bottom point DECLARE @by DECIMAL(8,4) = 3;    -- Y-bottom point DECLARE @p DECIMAL(8,4) = 1.5;    -- Parabola's Power

In this case parabola's bottom point has coordinates (7,3), it has a negative slope and even its power not a whole number.

Try to reshape it by changing variables.