Benutzer:Chricho/draw-cantorset.py

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Zum Erstellen von Datei:Homeomorphism_between_Cantor_Spaces.svg benutzt.

Lizenz (zusätzlich zur vorgeschriebenen Creative Commons Attr. SA Lizenz in der Wikipedia): GNU General Public License Version 3 oder jede spätere von der FSF bestätigte Version der Lizenz.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#n = 3 # in wie viele Teile
n = 2
p = 2.0/3 # wie viel „Nutzlast“
w = 800 # initiale Breite
h = 400 # Höhe
hp = 2.0/3 # Vertikale Nutzlast
#d = 4 # Tiefe
d = 5

print '<?xml version="1.0" encoding="UTF-8"?>'
print '<svg xmlns="http://www.w3.org/2000/svg" width="' + str(w) + '" height="' + str(h) + '">'

def svgCodeForColour(col):
	return 'rgb(' + str(100*col[0]) + '%,' + str(100*col[1]) + '%,' + str(100*col[2]) + '%)'

lcoulourdata = [[1.0,0.2,0.2], [0.2,0.2,1.0], [0.2,1.0,0.2], [0.9,0.5,0.0], [0.5,0.0,0.9]]
colourscaling = 0.7
def layerColour(key, seckey):
	global lcoulourdata
	global colourscaling
	c = lcoulourdata[key]
	s = colourscaling**seckey
	return [s*c[0], s*c[1], s*c[2]]

toembed = 3
def cantorEmbedding(pos):
	global toembed
	k = 0
	atEndOfLayer = True
	tmpcnt = 0
	seccnt = 0
	for x in pos:
		atEndOfLayer = False
		tmpcnt += 1
		if x == "1":
			atEndOfLayer = True
			k += 1
			seccnt = tmpcnt-1
			tmpcnt = 0
		elif tmpcnt == toembed-1:
			atEndOfLayer = True
			k += 1
			seccnt = tmpcnt
			tmpcnt = 0
	if atEndOfLayer:
		return 'fill="' + svgCodeForColour(layerColour(k, seccnt)) + '"'
	return ""

def layerColouring(pos):
	return 'fill="' + svgCodeForColour(layerColour(len(pos), int(0 if len(pos) == 0 else pos[-1]))) + '"'

def nop(pos):
	return ""

colouring = cantorEmbedding
#colouring = layerColouring

def cantorSet(x, y, n, p, w, d, singleH, spaceH, pos):
	if d == 0:
		return
	print '<rect x="' + str(x) + '" y="' + str(y) + '" height="' + str(singleH) + '" width="' + str(w) + '" ' + colouring(pos) + ' />'
	y += singleH + spaceH
	stepW = (w-p*w/n)/(n-1)
	for i in xrange(n):
		cantorSet(x, y, n, p, p*w/n, d-1, singleH, spaceH, pos+str(i))
		x += stepW

singleH = hp*h/d
spaceH = (1-hp)*h/(d-1)

cantorSet(0, 0, n, p, w, d, singleH, spaceH, "")

print "</svg>"