Cleaning out my disk

Discussion in 'XML' started by Magnus Henriksson, Oct 20, 2006.

  1. Hi all,

    While getting ready to get rid of an old computer of mine, I came across
    this transform that I wrote a few years ago (around 2002 I think). I did
    it just to prove that it could be done, so the more mathematically
    inclined will no doubt find better ways to do this.

    I thought some of you might get a kick out of it.

    Do this:

    1) Run it trough your favorite XSLT 1.0 processor, output to file. I
    recommend a really fast processor, such as SAXON. The transform does not
    depend on any input source, so you can use whatever you have lying
    around. Wait for it...

    2) Open the result in a browser. Wait for it...


    Here is the transform:

    -- begin mandel.xsl --

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="html"
    indent="yes"
    encoding="iso-8859-1"/>

    <!-- The area to calculate -->
    <xsl:param name="minX" select="-2"/>
    <xsl:param name="maxX" select="1.25"/>
    <xsl:param name="minY" select="-1.25"/>
    <xsl:param name="maxY" select="1.75"/>

    <!-- Maximum number of iterations -->
    <xsl:param name="maxIterations" select="100"/>

    <!-- Cell size -->
    <xsl:param name="cellSize" select="1"/>

    <!-- Number of rows -->
    <xsl:param name="rowsMax" select="300"/>

    <xsl:variable name="dy" select="($maxY - $minY) div $rowsMax"/>
    <xsl:variable name="cellsMax" select="($maxX - $minX) div $dy"/>
    <xsl:variable name="dx" select="($maxX - $minX) div $cellsMax"/>

    <xsl:template match="/">
    <html>
    <head>
    <title>XSLT Mandelbrot Set</title>
    </head>
    <body>
    <table cellspacing="0"
    cellpadding="0"
    border="0"
    bgcolor="black">
    <xsl:call-template name="Rows"/>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template name="Rows">
    <xsl:param name="rowNumber" select="0"/>
    <xsl:if test="$rowNumber &lt; $rowsMax">
    <xsl:message>
    <xsl:value-of select="concat('Row ', $rowNumber + 1, ' of ',
    $rowsMax)"/>
    </xsl:message>
    <tr>
    <xsl:call-template name="Cells">
    <xsl:with-param name="rowNumber" select="$rowNumber"/>
    </xsl:call-template>
    </tr>
    <xsl:call-template name="Rows">
    <xsl:with-param name="rowNumber" select="$rowNumber + 1"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    <xsl:template name="Cells">
    <xsl:param name="cellNumber" select="0"/>
    <xsl:param name="rowNumber"/>
    <xsl:if test="$cellNumber &lt; $cellsMax">
    <td width="{$cellSize}" height="{$cellSize}">
    <xsl:call-template name="CellColor">
    <xsl:with-param name="cx" select="$minX + ($cellNumber * $dx)"/>
    <xsl:with-param name="cy" select="$minY + ($rowNumber * $dy)"/>
    </xsl:call-template>
    </td>
    <xsl:call-template name="Cells">
    <xsl:with-param name="cellNumber" select="$cellNumber + 1"/>
    <xsl:with-param name="rowNumber" select="$rowNumber"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    <xsl:template name="CellColor">
    <xsl:param name="iteration" select="0"/>
    <xsl:param name="cx"/>
    <xsl:param name="cy"/>
    <xsl:param name="a0" select="0"/>
    <xsl:param name="b0" select="0"/>
    <xsl:variable name="a1" select="($a0 * $a0) - ($b0 * $b0) + $cx"/>
    <xsl:variable name="b1" select="2 * ($a0 * $b0) + $cy"/>
    <xsl:variable name="zLength" select="($a1 * $a1) + ($b1 * $b1)"/>
    <xsl:if test="$zLength &gt; 4">
    <xsl:variable name="color">
    <xsl:call-template name="Dec2Hex">
    <xsl:with-param name="dec" select="round((16777215 div
    $maxIterations) * $iteration)"/>
    </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="padding"
    select="6 - string-length($color)"/>
    <xsl:variable name="paddedColor"
    select="concat(substring('000000',1,$padding), $color)"/>
    <xsl:attribute name="bgcolor">
    <xsl:value-of select="concat('#',$paddedColor)"/>
    </xsl:attribute>
    </xsl:if>
    <xsl:if test="$iteration &lt; $maxIterations and $zLength &lt;= 4">
    <xsl:call-template name="CellColor">
    <xsl:with-param name="iteration" select="$iteration + 1"/>
    <xsl:with-param name="cx" select="$cx"/>
    <xsl:with-param name="cy" select="$cy"/>
    <xsl:with-param name="a0" select="$a1"/>
    <xsl:with-param name="b0" select="$b1"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    <xsl:template name="Dec2Hex">
    <xsl:param name="dec" select="0"/>
    <xsl:variable name="div" select="floor($dec div 16)"/>
    <xsl:variable name="rem" select="$dec - ($div * 16)"/>
    <xsl:choose>
    <xsl:when test="$dec = 0">0</xsl:when>
    <xsl:when test="$dec = 1">1</xsl:when>
    <xsl:when test="$dec = 2">2</xsl:when>
    <xsl:when test="$dec = 3">3</xsl:when>
    <xsl:when test="$dec = 4">4</xsl:when>
    <xsl:when test="$dec = 5">5</xsl:when>
    <xsl:when test="$dec = 6">6</xsl:when>
    <xsl:when test="$dec = 7">7</xsl:when>
    <xsl:when test="$dec = 8">8</xsl:when>
    <xsl:when test="$dec = 9">9</xsl:when>
    <xsl:when test="$dec = 10">A</xsl:when>
    <xsl:when test="$dec = 11">B</xsl:when>
    <xsl:when test="$dec = 12">C</xsl:when>
    <xsl:when test="$dec = 13">D</xsl:when>
    <xsl:when test="$dec = 14">E</xsl:when>
    <xsl:when test="$dec = 15">F</xsl:when>
    <xsl:eek:therwise>
    <xsl:call-template name="Dec2Hex">
    <xsl:with-param name="dec" select="$div"/>
    </xsl:call-template>
    </xsl:eek:therwise>
    </xsl:choose>
    <xsl:if test="$div">
    <xsl:call-template name="Dec2Hex">
    <xsl:with-param name="dec" select="$rem"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    -- end mandel.xsl --

    Other mildly interesting values of the parameters are:

    minX = 0.2
    maxX = 0.4
    minY = 0.5
    maxY = 0.7

    and

    minX = 0.3
    maxX = 0.4
    minY = 0.5
    maxY = 0.6

    and

    minX = 0.32
    maxX = 0.35
    minY = 0.52
    maxY = 0.55

    The smaller the area, the longer it will take. For really small areas it
    seems like the processor gets stuck.


    // Magnus
    Magnus Henriksson, Oct 20, 2006
    #1
    1. Advertising

  2. And I thought merely solving the eight-queens problem in XSLT was a
    terrifying concept... <grin/>

    Are you willing to officially put that into the public domain so we can
    pass it around?
    Joseph Kesselman, Oct 20, 2006
    #2
    1. Advertising

  3. Joseph Kesselman wrote:
    > And I thought merely solving the eight-queens problem in XSLT was a
    > terrifying concept... <grin/>
    >
    > Are you willing to officially put that into the public domain so we can
    > pass it around?


    I hereby abandon any property rights to the XSLT code in message
    eha24h$emp$.

    // Magnus
    Magnus Henriksson, Oct 20, 2006
    #3
  4. Magnus Henriksson

    roy axenov Guest

    Magnus Henriksson wrote:

    [XSLT Mandelbrot Set Explorer]

    > I thought some of you might get a kick out of it.


    I sure did.

    > 1) Run it trough your favorite XSLT 1.0 processor, output
    > to file. I recommend a really fast processor, such as
    > SAXON.


    libxslt-based PHP5 module worked well enough for me. (And I
    didn't even have to juggle the files around - my web-based
    toolkit handles that for me and displays the transformation
    result in an iframe.)

    I guess this post is a bit pointless, but I just wanted to
    say that this is wonderful stuff. Hats off.

    --
    roy axenov
    roy axenov, Oct 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jas Shultz
    Replies:
    0
    Views:
    946
    Jas Shultz
    Dec 3, 2003
  2. Replies:
    12
    Views:
    520
    santosh
    Nov 15, 2006
  3. DR
    Replies:
    0
    Views:
    653
  4. Replies:
    1
    Views:
    272
    Richard Bos
    Apr 10, 2008
  5. Larry Hastings
    Replies:
    0
    Views:
    312
    Larry Hastings
    Apr 23, 2009
Loading...

Share This Page