Fractal Diamond-Square Terrain Generation in Perl

The title is mostly for search engines for anyone who encounters this in the future.

I have, for no particular reason, decided to implement the fractal diamond-square terrain generation algorithm in Perl. Sometimes it's nice to just play.


    $$$$$$$$$$$$$$$$$$$$$$$####################*********************!**!!!!!!!!!!!!!
    $$$$$$$$$$$$$$$$$$$$$$#####################****************************!!!!!!!!!
    $$$$$$$$$$$$$$$$$$$$$$###########################*#*##*#*####************!!!!!!!
    ####$####$#$#$$$$$$$$$##########################################**********!!!!!!
    ################$$$$$$$$##########################################**********!!!!
    #################$$$$$$$$$##########################################**********!!
    *###############$$$$$$$$$$$#########################$##$#$$$$########**********!
    ****#*############$$#$##$##########################$$$$$$$$$$$########**********
    ********#############################################$$$$$$$$#########**********
    *************#########################################$#$##$########************
    !*!*************###################################################*************
    !!!!!!*!**********####*##*#*#######################################*************
    !!!!!!!!!!!*****************############$##########################*************
    =!!!!!!!!!!!!***************###########$$$##########################**#*#*******
    ====!!!!!!!!!!!!***********############$$$$$#############################*******
    ;=======!!=!!!!!!!**********##**#######$##############################**********
    ;;;;;;========!!!!!!!***************###############################*************
    ;;;;;;;;========!!!!!!!!*!*!**********############*##############************###
    ;;;;;;;;;;;=====!!!!!!!!!!!!!!!********#*#**************######*************#####
    :;;;;;;;;;;======!!!!!!!!!!!!!!!***************************#*#*********#########
    :::;;;;;;;;;======!!!!!!!!!!!!!!!*************************************##########
    ::::::;;;;;;=======!!!!!!!!!!!!!!!*!*******************************#############
    ~:::::;;;;;;;;=========!!=!!!!!!!!!!!!!*!***************************############
    ~~::::::;;;;;;;;=;===========!=!=!!!!!!!!!!!!!!!*********************#*#########
    ~~~~:::::::;;;;;;;;=;===============!!!!!!!!!!!!!!!*********************########
    ~~~~~~:::::::;;;;;;;;;;;;;=;==========!!!!!!!!!!!!!*!*******************########
    -~~~~~~~::::::::::;;;;;;;;;;;;;==========!=!!!!!!!!!!!!*****************########
    ----~~~~~~~::::::::::;;;;;;;;;;;;=============!!!!!!!!!***************##########
    ------~~~~~~~~:~:::::::;;;;;;;;;;;;;============!!!!!!!!**************#######$$$
    ,--------~-~~~~~~~:::::::::::;;;;;;;;;==========!!!!!!!!!***********#########$$$

Note that it's not actually tied to a particular output format, so if you prefer HTML:

                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    

1 Comment

C implementation? Here you go. Straight conversion from Perl, though:

https://gist.github.com/tsee/5461671

It's pretty naively implemented. On top of that, if you use the TCC hack (see test.pl in the gist and https://github.com/tsee/p5-XS-TCC) to compile & run in memory, then the Perl code isn't so much slower than the C. In this case, that's because all the perl API C macros used are expanded and compiled with TCC, which is much less efficient than your system compiler. So just go ahead and use XS to wrap the C instead. That's easy as pie, too.

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/