如何使用 T-SQL 生成 Mandelbrot? [英] How to generate a Mandelbrot with T-SQL?

查看:27
本文介绍了如何使用 T-SQL 生成 Mandelbrot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习了一点 T-SQL,并认为一个有趣的练习是用它生成 Mandelbrot 集.

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.

那些能教会我一些 T-SQL 知识的答案的加分点.

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

-亚当

推荐答案

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

这篇关于如何使用 T-SQL 生成 Mandelbrot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆