Anzeige:
Ergebnis 1 bis 3 von 3

Thema: Zufällige Anordnung von Kreisen in Tikz

Baum-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    Registrierter Benutzer
    Registriert seit
    12.11.2008
    Beiträge
    219

    Zufällige Anordnung von Kreisen in Tikz

    Hallo,

    ich suche ein Möglichkeit, wie ich eine vorgegebenen Anzahl von Kreisen in einem Viereck zufällig verteilen kann. Dabei soll es möglich sein einen Mindestabstand einzustellen, damit es nicht zu Überlappungen kommt. Ich habe das Paket poisson gefunden, das ziemlich genau das macht, was ich möchte. Leider verstehe ich noch nicht, wie ich da die Anzahl an Kreisen vorgeben kann.

    Ich wäre über Verbesserungen oder auch Tipps zu anderen Ansätzen Dankbar.

    Minimalbeispiel

    Code:
    \documentclass[12pt,listof=totoc,bibliography=totoc,pointlessnumbers]{scrartcl}
    \usepackage[ngerman]{babel}
    \usepackage[obeyspaces]{url}
    \usepackage[utf8]{inputenc}
    \usepackage{lmodern} 
    \usepackage{poisson} 
    \usepackage{tikz}
    \usetikzlibrary{math}
    
    
    \tikzset{plaettchenvoll/.style={line width = 0.2 mm,draw,fill=gray!80,circle,minimum width = 3.25mm},
    }%\\
    \begin{document}
    	\begin{tikzpicture}
    		\xdef\mylist{\poissonpointslist{5}{5}{0.7}{3}}  % Sparse, fast
    		\draw (-0.5,-0.5)--(5.5,-0.5)--(5.5,5.5)--(-0.5,5.5)--cycle;
    		\foreach \x/\y[count=\i from 0]in \mylist {
    				\node[plaettchenvoll,fill=blue!\i] at(\x,\y) {};
    		}
    	\end{tikzpicture}
    \end{document}
    poisson.sty
    Code:
    \directlua{dofile("poisson.lua")}
    \newcommand{\poissonpointslist}[4]{
        \directlua{poisson_points_list(#1,#2,#3,#4)}
    }
    poisson.sty
    Code:
    -- This is a lua implementation of the algorithm described in
    -- http://devmag.org.za/2009/05/03/poisson-disk-sampling/
    --
    -- The structure of the algorithm is exactly the same than in 
    -- the mentioned article. Its pseudo-code snippets were translated
    -- to Lua.
    --
    -- One detail worths an explanation, though. The article uses a 2D matrix
    -- called grid to store coordinates of points. In the article, it is
    -- assumed that grid elements can be accesed via grid[point], being point
    -- some structure with a pair of x and y integers, so grid[point] should
    -- be equivalent to grid[x,y] or grid[x][y]. This grid is assumed to be
    -- initially dimensioned and filled by nils.
    --
    -- In my implementation the grid is dynamic, and it is an associative array
    -- indexed by string keys in the form grid["(x,y)"]. The function gridToString()
    -- can be used to convert a Point to its string form, so the grid is indeed
    -- accesed like this: grid[gridToString(p)] being p a Point with integer
    -- coordinates (which in fact is found via imageToGrid, like in the article)
    
    -- UTILITY FUNCTIONS (used in the article, without giving implementation)
    -- =====================================================================
    
    -- RandomQueue stores values and gives them in random order
    RandomQueue = {}
    function RandomQueue.new ()
        return {last=-1}
    end
    
    function RandomQueue.push(q, item) 
        local last = q.last + 1
        q.last = last
        q[last] = item
    end
    
    function RandomQueue.pop(q)
        if (RandomQueue.empty(q)) then 
            return nil
        end
        local index = math.random(0,q.last) 
        -- A random index is generated. The last element
        -- is swaped with this random item, and the new
        -- last item is popped.
        local last = q.last
        item = q[index]
        q[index] = q[last]
        q[last] = nil
        q.last = last -1
        return item
    end
    
    function RandomQueue.empty(q)
        return q.last==-1
    end
    
    function RandomQueue.print(q)
        -- For debugging. Not used
        local t = {}
        for i=0, q.last do
            table.insert(t, string.format("(%f,%f)", q[i].x, q[i].y))
        end
        print (string.format("RandomQueue %d elem: %s", q.last+1, table.concat(t, " ")))
    end
    
    -- Point stores a coordinate pair
    Point = {}
    
    function Point.new(x,y)
        return {x=x, y=y}
    end
    
    -- Determines if a point is inside the rendered rectangle
    function inRectangle(point, width, height)
      return (point.x>0 and point.y>0 and point.x
    
    Geändert von MC3330 (11-03-2020 um 05:47 Uhr)

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •