且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 T-SQL 生成 Mandelbrot?

更新时间:2023-02-05 23:24:51

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