views:

746

answers:

4

Learning a little about T-SQL, and thought an interesting exercise would be to generate a Mandelbrot set with it.

Turns out someone already has (and recently, it appears). I'll let someone else post it as an answer, but I'm curious what optimizations can be made.

Alternately, what would you do to make the code more readable?

I'll select the most readable (yet reasonably compact) version as the accepted answer (too bad we don't have rep bounties yet!) unless someone really comes along with a great optimization.

Bonus points to those answers that teach me a little something about T-SQL.

+9  A: 
Elie
Best viewed in text mode of course..
GregD
Sorry, meant to include the picture of what it looks like.
Elie
That's just plain evil ;-)
toolkit
Reminds me of the time in the 1987-1992 timeframe when somebody wrote a Mandelbrot generator in PostScript, and some idiot kept sending it to our companies one and only LaserWriter.
Paul Tomblin
@paul - Hilarious! Laser printer hijinks at my workplace were limited to changing the status message on the HP printers.
Adam Davis
+6  A: 
Create PROCEDURE dbo.mandlebrot
@left float,
@right float,
@Top float,
@Bottom float,
@Res float,
@MaxIterations Integer = 500
As
Set NoCount On

Declare @Grid Table (
    X float Not Null, 
    Y float Not Null,
    InSet Bit
   Primary Key (X, Y))

Declare @Xo float, @Yo float, @Abs float
Declare @PtX Float, @PtY Float
Declare @Iteration Integer Set @Iteration = 0
Select @Xo = @Left, @Yo = @Bottom

While @Yo <= @Top Begin
    While @Xo <= @Right Begin
     Select @PtX = @Xo, @PtY = @Yo
     While @Iteration < @MaxIterations 
      And (Square(@PtX) + Square(@PtY)) < 4.0 Begin
      Select @PtX = Square(@PtX) - Square(@PtY) + @Xo,
          @PtY = 2* @PtX * @PtY + @Yo
      Select @Iteration, @PtX, @PtY
      Set @Iteration = @Iteration + 1
     End
     Insert @Grid(X, Y, InSet) 
     Values(@Xo, @Yo, Case 
      When @Iteration < @MaxIterations
        Then 1 Else 0 End)
     Set @Xo = @Xo + @res
     Set @Iteration = 0
    End
    Select @Xo = @Left, 
        @Yo = @Yo + @Res
End

Select * From @Grid
Charles Bretana
A: 

As a fractal artist, I'll say "cool." But really it seems more useful to use a fractal program to generate it. After all it's not much good unless you can zoom in, right?

HLGEM
it looks the same zoomed in anyway so it wont matter =)
Chii
+3  A: 

Hopefully, this teaches a bit about T-SQL as well, It does everything in a set based approach which is TSQL's strength (i.e. no while loops) or variables:

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