|
|
Courtasy of one of the SQL MVPs (and reposted with his permission)
Here's the Mandelbrot set in T-SQL.
Why? Because we can....
-- ORIGINAL AUTHOR: GRAEME JOB
-- ORIGINALLY CREATED: 12-OCT-2008
-- ADAPTED BY: STEVE KASS
-- BECAUSE: IT'S EVEN SHORTER AND FASTER
WITH GEN(V, IV) AS ( -- DIM GENERATOR
SELECT CAST(0 AS FLOAT) AS V, 0 AS IV UNION ALL
SELECT CAST(V + 0.031 AS FLOAT) AS V, IV + 1 AS IV
FROM GEN
WHERE IV < 100
),XGEN(X, IX) AS ( -- X DIM GENERATOR
SELECT -2.2+V AS X, IV AS IX FROM GEN
),YGEN(Y, IY) AS ( -- Y DIM GENERATOR
SELECT -1.5+V AS Y, IV AS IY FROM GEN
), Z(IX, IY, CX, CY, X, Y, I) AS ( -- Z POINT ITERATOR
SELECT IX, IY, X, Y, X, Y, 0
FROM XGEN, YGEN
UNION ALL
SELECT IX, IY, CX, CY, X * X - Y * Y + CX, Y * X * 2 + CY, I + 1
FROM Z
WHERE I < 26
AND X * X + Y * Y < 16
), ZR(IX, IY, C, R) AS (
SELECT
IX, IY, CASE WHEN I > 25 THEN ' ' ELSE SUBSTRING('
.,,,-----++++%%%%@@@@###',I,1) END,
ROW_NUMBER() OVER (PARTITION BY IX, IY ORDER BY I DESC)
FROM Z
)
SELECT
[0]+[1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[15]+[16]+[17]+[18]+[19]+
[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]+[39]+
[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48]+[49]+[50]+[51]+[52]+[53]+[54]+[55]+[56]+[57]+[58]+[59]+
[60]+[61]+[62]+[63]+[64]+[65]+[66]+[67]+[68]+[69]+[70]+[71]+[72]+[73]+[74]+[75]+[76]+[77]+[78]+[79]+
[80]+[81]+[82]+[83]+[84]+[85]+[86]+[87]+[88]+[89]+[90]+[91]+[92]+[93]+[94]+[95]+[96]+[97]+[98]+[99]+[100]
FROM (
SELECT IX, IY, C FROM ZR WHERE R = 1
) AS P(IX,IY,C) PIVOT (MAX(C) FOR IX IN (
[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],
[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],[33],[34],[35],[36],[37],[38],[39],
[40],[41],[42],[43],[44],[45],[46],[47],[48],[49],[50],[51],[52],[53],[54],[55],[56],[57],[58],[59],
[60],[61],[62],[63],[64],[65],[66],[67],[68],[69],[70],[71],[72],[73],[74],[75],[76],[77],[78],[79],
[80],[81],[82],[83],[84],[85],[86],[87],[88],[89],[90],[91],[92],[93],[94],[95],[96],[97],[98],[99],[100])
) AS Piv
Requires SQL 2005 or higher.
Post a reply to this message
|
|