Michael J. Swart

March 2, 2011

Mandelbrot Revisited.

Filed under: SQLServerPedia Syndication,Technical Articles,Tongue In Cheek — Tags: — Michael J. Swart @ 12:00 pm

So I caught some sort of flu bug recently and that means no new blog post this week. Instead, I’m going to dig through the archives to bring something that you may have missed the first time around.

Today I’m sharing something I first posted two years ago: Something Pretty.

Something Pretty

A T-SQL script I came up with that displays the Mandelbrot set. (Tip: Hit Ctrl-T before executing)

SET NOCOUNT ON;
 
--populate
;WITH Numbers ([row]) AS
(
   SELECT TOP 100 CAST(ROW_NUMBER() OVER (ORDER BY NEWID()) AS FLOAT) [row]
   FROM sys.columns
)
SELECT A.row AS x,
   B.row AS y,
   0 AS iter,
   A.row AS iterx,
   B.row AS itery,
   '.' AS symbol
INTO #GRID
FROM Numbers A, Numbers B
WHERE B.[row] <= 24
GO
 
-- scale
UPDATE #GRID
SET x = x * 3.0 / 100.0 - 2,
   y = y * 2.0 / 24.0 - 1,
   iterx = x * 3.0 / 100.0 - 2,
   itery = y * 2.0 / 24.0 - 1
GO
 
--iterate
UPDATE #GRID
SET iterx = iterx*iterx - itery*itery + x,
    itery = 2*iterx*itery + y,
    iter = iter+1
WHERE iterx*iterx+itery*itery <= 2*2
GO 257
 
UPDATE #GRID SET symbol = CHAR(64+(iter%26)) WHERE NOT iter = 257
GO
 
--print
WITH concatenated (y, c) AS
(
   SELECT G2.y,
       (SELECT SUBSTRING(G.symbol, 1, 1) AS [data()] FROM #GRID G WHERE G.y = G2.y FOR XML PATH('')) c
   FROM (SELECT DISTINCT y FROM #GRID) AS G2
)
SELECT REPLACE(c, ' ', '') FROM concatenated ORDER BY y
GO
 
DROP TABLE #GRID

You’ll get something like the following:

@@@@@@@AAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCDDDDDEFFGILWLIGFEDDDCCCCCCCBBBBBBBBAAAAAAAAAAAAAA
@@@@@@AAAABBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCDDDDEEEFJMESD..NXMNHEDDDDDDCCCCCBBBBBBBBBAAAAAAAAAAA
@@@@AAAABBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCDDDEEEEEFFFGHO.......GQHGFEEEEDDDDDCCCBBBBBBBBBAAAAAAAAA
@@@AAABBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCDDDDDEFNMQ.JHHIAPLEOPV....VPQMLJPQGFFFFHNEDCCBBBBBBBBBBAAAAAAA
@@AABBBBBBBBBBBBBBBBBBBBBBCCCCCCDDDDDDDDEEEFFHJR..HU...................XJLICTUAIEDDCCBBBBBBBBBBAAAAA
@@ABBBBBBBBBBBBBBBBBBBCCCDDDDDDDDDDDDEEEEEFHMJMQR............................OHFEDDCCCBBBBBBBBBBAAAA
@ABBBBBBBBBBBBBCCCCCDDEPHFEEEEEEEEEFFFFFGGILR.................................JGGFDDCCCBBBBBBBBBBAAA
ABBBBBBCCCCCCCCCDDDDEEFHQJJLIHIGMIHHGGGHHJR.....................................SKEDCCCCBBBBBBBBBBAA
BBBCCCCCCCCCCDDDDDDDEFFGHLW..WF...T.RPKJKNL....................................MJFEDCCCCBBBBBBBBBBBA
BCCCCCCCCCCDDDDDDEFGGGHKG..............UQG.....................................KGEDDCCCCCBBBBBBBBBBA
CCDDCCCDEEEEEEFFFGHJ@PRPC.....................................................KFEDDDCCCCCBBBBBBBBBBA
...........................................................................OJHFFEDDDCCCCCBBBBBBBBBBB
CCDDCCCDEEEEEEFFFGHJ@PRPC.....................................................KFEDDDCCCCCBBBBBBBBBBA
BCCCCCCCCCCDDDDDDEFGGGHKG..............UQG.....................................KGEDDCCCCCBBBBBBBBBBA
BBBCCCCCCCCCCDDDDDDDEFFGHLW..WF...T.RPKJKNL....................................MJFEDCCCCBBBBBBBBBBBA
ABBBBBBCCCCCCCCCDDDDEEFHQJJLIHIGMIHHGGGHHJR.....................................SKEDCCCCBBBBBBBBBBAA
@ABBBBBBBBBBBBBCCCCCDDEPHFEEEEEEEEEFFFFFGGILR.................................JGGFDDCCCBBBBBBBBBBAAA
@@ABBBBBBBBBBBBBBBBBBBCCCDDDDDDDDDDDDEEEEEFHMJMQR............................OHFEDDCCCBBBBBBBBBBAAAA
@@AABBBBBBBBBBBBBBBBBBBBBBCCCCCCDDDDDDDDEEEFFHJR..HU...................XJLICTUAIEDDCCBBBBBBBBBBAAAAA
@@@AAABBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCDDDDDEFNMQ.JHHIAPLEOPV....VPQMLJPQGFFFFHNEDCCBBBBBBBBBBAAAAAAA
@@@@AAAABBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCDDDEEEEEFFFGHO.......GQHGFEEEEDDDDDCCCBBBBBBBBBAAAAAAAAA
@@@@@@AAAABBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCDDDDEEEFJMESD..NXMNHEDDDDDDCCCCCBBBBBBBBBAAAAAAAAAAA
@@@@@@@AAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCDDDDDEFFGILWLIGFEDDDCCCCCCCBBBBBBBBAAAAAAAAAAAAAA
@@@@@@@@AAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCDDDDEEFHNIHJREDCCCCCCCBBBBBBBBAAAAAAAAAAAAAAAAA

I’ll be back next week with something new, you have my word.

Gimli

... AND MY AXE!

Powered by WordPress