NoPaste

zufall2

von detix

SNIPPET_TEXT:
  1. #! /usr/bin/python
  2. import inkex
  3. import simplestyle, sys
  4. from math import *
  5. from simplestyle import *
  6. from random import uniform
  7.  
  8. class Zufallspunkte_im_Kreis(inkex.Effect):
  9.     def __init__(self):
  10.         inkex.Effect.__init__(self)
  11.         self.OptionParser.add_option("--x",
  12.                         action="store", type="int",
  13.                         dest="x", default=0.0,
  14.                         help="Zentrum X")
  15.         self.OptionParser.add_option("--y",
  16.                         action="store", type="int",
  17.                         dest="y", default=0.0,
  18.                         help="Zentrum Y")
  19.         self.OptionParser.add_option("--radius",
  20.                         action="store", type="int",
  21.                         dest="radius", default=1000,
  22.                         help="Radius")
  23.         self.OptionParser.add_option("--punkte",
  24.                         action="store", type="int",
  25.                         dest="punkte", default=100,
  26.                         help="Punkte")
  27.         self.OptionParser.add_option("--punktgroesse",
  28.                         action="store", type="float",
  29.                         dest="punktgroesse", default=1,
  30.                         help="Punktgroesse")
  31.  
  32.     def draw_circle(self, radius, parent):
  33.  
  34.         style = {   'stroke'        : '#000000',
  35.                     'stroke-width'  : '0.1',
  36.                     'fill'          : 'none'            }
  37.         ell_attribs = {'style':simplestyle.formatStyle(style),
  38.             inkex.addNS('cx','sodipodi')        :str(self.options.x),
  39.             inkex.addNS('cy','sodipodi')        :str(self.options.y),
  40.             inkex.addNS('rx','sodipodi')        :str(radius),
  41.             inkex.addNS('ry','sodipodi')        :str(radius),
  42.             inkex.addNS('type','sodipodi')      :'arc'
  43.             }
  44.         ell = inkex.etree.SubElement(parent, inkex.addNS('path','svg'), ell_attribs )
  45.  
  46.     def draw_points(self, x, y, radius, parent):
  47.  
  48.         style = {   'stroke'        : 'none',
  49.                     'stroke-width'  : str(radius),
  50.                     'fill'          : '#ff0000'            }
  51.         ell_attribs = {'style':simplestyle.formatStyle(style),
  52.             inkex.addNS('cx','sodipodi')        :str(x),
  53.             inkex.addNS('cy','sodipodi')        :str(y),
  54.             inkex.addNS('rx','sodipodi')        :str(radius),
  55.             inkex.addNS('ry','sodipodi')        :str(radius),
  56.             inkex.addNS('type','sodipodi')      :'arc'
  57.             }
  58.         ell = inkex.etree.SubElement(parent, inkex.addNS('path','svg'), ell_attribs )
  59.  
  60.     def effect(self):
  61.         parent = self.current_layer
  62.         radius = self.unittouu(str(self.options.radius))
  63.         self.x_offset = self.unittouu(str(self.options.x))
  64.         self.y_offset = self.unittouu(str(self.options.y))
  65.         self.draw_circle(self.options.radius, parent)
  66.         for i in range(1,self.options.punkte + 1):
  67.             x = uniform(-1, 1)
  68.             y = uniform(-1, 1)
  69.             if sqrt(x*x+y*y) <= 1:
  70.                 x1 = x * radius * 96 / 25.4 + self.options.x
  71.                 y1 = y * radius * 96 / 25.4 + self.options.y
  72.                 self.draw_points(x1, y1, str(self.options.punktgroesse/100.0), parent)
  73.  
  74.  
  75. if __name__ == '__main__':
  76.     e = Zufallspunkte_im_Kreis()
  77.     e.affect()

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN