File:Ideal-triangle hyperbolic tiling.svg

From testwiki
Jump to navigation Jump to search
    Original file (SVG file, nominally 800 × 800 pixels, file size: 33 KB)

    This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

    Summary

    Description
    English: The Poincaré disc model of the hyperbolic plane tiled with ideal triangles. See the SVG source for the Haskell program that generated the image.
    Date
    Source Own work
    Author Saric
    SVG development
    InfoField
     The SVG code is valid.
     This vector image was created with an unknown SVG tool.

    Source code

    The logo of Haskell – purely functional programming language
    The logo of Haskell – purely functional programming language
    This media was created with Haskell (purely functional programming language)
    Here is a listing of the source used to create this file.

    Deutsch ∙ македонски ∙ English ∙ +/−

    import Data.List (intersperse)
    import Text.Printf (printf)
    
    {- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~ Parameters
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -}
    
    output_path = "/tmp/hyperbolic.svg" :: FilePath
    
    width = 800
    background_fill = "white"
    
    (xmin, xmax) = (-1, 1)
    (ymin, ymax) = (-1, 1)
    
    {- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~ Declarations
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -}
    
    type Point = (Double, Double)
    
    data PathPart = PathPart String
     {- For commands in an SVG <path>. -}
    
    header = unlines $
       ["<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
        "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"",
        "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">",
        "<svg",
        "    xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"",
        "    xmlns:xlink=\"http://www.w3.org/1999/xlink\"",
        "    width='" ++ show width ++ "' height='" ++ show height ++ "'>\n"]
    footer = "\n\n</svg>\n"
    
    height = (ymax - ymin) * width/(xmax - xmin) :: Double
    
    {- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~ IO
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -}
    
    main = writeFile output_path svg
    
    {- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~ Geometry
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -}
    
    content = unlines $ [unitcircle, primeTri, subTrisSymbol] ++
                  map subTrisUse [0, 1/6, 1/3, 1/2, 2/3, 5/6]
      where unitcircle = svgcirc (0, 0) 1 "black"
            p θ = polar $ 1/4 - θ
    
            primeTri = hypTriangle (p 0 1) (p (1/3) 1) (p (2/3) 1) "black"
    
            subTrisSymbol :: String
            subTrisSymbol = unlines $
                "<symbol id='subTris'>" :
                (map ("  " ++) $ f recdepth 0 (1/6)) ++
                ["</symbol>"]
              where f 0 θ1 θ2 = [t θ1 θm θ2 $ color 0]
                      where θm = ((θ1 + θ2)/2)
                    f n θ1 θ2 = (if even n then t θ1 θm θ2 (color n) else []) :
                        f (n - 1) θ1 θm ++ f (n - 1) θm θ2
                      where θm = ((θ1 + θ2)/2)
                    t θ1 θ2 θ3 c = hypTriangle (p θ1 1) (p θ2 1) (p θ3 1) c
                    color n | even n == even recdepth = "black"
                            | otherwise               = "white"
                    recdepth = 6
    
            subTrisUse θ = printf "<use xlink:href='#subTris' transform='rotate(%d %g,%g)'/>"
                (floor (θ * 360) :: Int) (svgx 0) (svgy 0)
    
    hypTriangle :: Point -> Point -> Point -> String -> String
    hypTriangle p1 p2 p3 fill = svgpath
       [pathMoveto p1, hypSeg p1 p2, hypSeg p2 p3, hypSeg p3 p1]
       "none" fill
    
    hypSeg :: Point -> Point -> PathPart
    {- The hyperbolic segment connecting the two points in the
    Poincaré disc model. -}
    hypSeg p1' p2' | r > 40    = pathLineto p2'
                   | otherwise = pathArc p2' r sweep
      where (cx, cy) = discCenter p1 p2
            r = dist (cx, cy) p1
            sweep = negateIf flipped $ if midy ≈≈ cy
                  then cx < midx
                  else midy > cy
              where midx = (x1 + x2) / 2
                    midy = (y1 + y2) / 2
    
            ((x1, y1), (x2, y2)) = (p1, p2)
            (p1, p2, flipped) =
                if xa < xb then
                  (p1', p2', False)
                else if xb < xa then
                  (p2', p1', True)
                else if ya > yb then
                  (p1', p2', False)
                else
                  (p2', p1', True)
              where (xa, ya) = p1'
                    (xb, yb) = p2'
    
    discCenter :: Point -> Point -> Point
    {- Given two points in the Poincaré disc model of the hyperbolic
    plane, finds the center of the circle containing the geodesic
    that connects them. -}
    discCenter (x1, y1) (x2, y2) =
       (-(y1*a + y1^2*y2 + x1^2*y2 + y2)/b,
         (x1*a + y1^2*x2 + x1^2*x2 + x2)/b)
      where a = -y2^2 - x2^2 - 1
            b = 2*(x2*y1 - x1*y2)
    
    dist :: Point -> Point -> Double
    {- Euclidean distance. -}
    dist (x1, y1) (x2, y2) = sqrt $ (x1 - x2)^2 + (y1 - y2)^2
    
    polar :: Double -> Double -> Point
    {- Converts a polar point (the angle being given in revolutions)
    to a Cartesian point. -}
    polar θ r = (r * cos (θ * 2 * pi), r * sin (θ * 2 * pi))
    
    {- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~ SVG
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -}
    
    svg = header ++ background ++ "\n" ++ content ++ footer
    
    background = printf
        "<rect x='0' y='0' width='%d' height='%d' fill='%s'/>\n"
        (ceiling width :: Int) (ceiling height :: Int) background_fill
    
    svgcirc :: Point -> Double -> String -> String
    svgcirc (x, y) r s = printf
        "<circle cx='%g' cy='%g' r='%g' stroke='%s' fill='none'/>"
        (svgx x) (svgy y) (svgd r) s
    
    svgpath :: [PathPart] -> String -> String -> String
    svgpath ps = printf "<path d='%s' stroke='%s' fill='%s'/>" d
      where d = concat $ intersperse " " $ map f ps
            f (PathPart s) = s
    
    pathMoveto, pathLineto :: Point -> PathPart
    pathMoveto (x, y) = PathPart $ printf "M%g,%g" (svgx x) (svgy y)
    pathLineto (x, y) = PathPart $ printf "L%g,%g" (svgx x) (svgy y)
    
    pathArc :: Point -> Double -> Bool -> PathPart
    pathArc (x, y) r sweep = PathPart $ printf "A%g,%g 0 0,%d %g,%g"
        (svgd r) (svgd r) (if sweep then 1 else 0 :: Int)
        (svgx x) (svgy y)
    
    svgx x = (x - xmin)/(xmax - xmin) * width
    svgy y = (y - ymin)/(ymin - ymax) * height + height
    svgd d = d / (xmax - xmin) * width
    
    {- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~ Utility
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -}
    
    negateIf True  a = not a
    negateIf False a = a
    
    (≈≈) :: RealFrac a => a -> a -> Bool
    a ≈≈ b = abs (a - b) < 1e-6
    
    infix 4 ≈≈
    

    Licensing

    Public domain This work has been released into the public domain by its author, Saric. This applies worldwide.
    In some countries this may not be legally possible; if so:
    Saric grants anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.

    Captions

    Add a one-line explanation of what this file represents

    Items portrayed in this file

    depicts

    1 March 2010

    File history

    Click on a date/time to view the file as it appeared at that time.

    Date/TimeThumbnailDimensionsUserComment
    current20:47, 2 March 2010Thumbnail for version as of 20:47, 2 March 2010800 × 800 (33 KB)wikimediacommons>Saric{{Information |Description={{en|1=The Poincaré disc model of the hyperbolic plane tiled with ideal triangles. See the SVG source for the Haskell program that generated the image.}} |Source={{own}} |Author=[http://en.wikipedia.org/wiki/User:Saric Saric] |

    There are no pages that use this file.