User:Lieven Van Speybroeck/Prototyping/9-Constellate me

From XPUB & Lens-Based wiki

Star Vector Graphics

Find out what your real constellation is...

UPDATE (v0.2)

The constellator has been turned into a cgi-script.
User-friendly and all!


--- code ---

constellation.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Constellator</title>
<link href="constellation.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function validate(){
	var name=document.forms["constellation"]["name"].value;
	var date=document.forms["constellation"]["date"].value;
	
	if (name==null || name==""){
  		alert("The stars need a name");
  		return false;
  	}
  	else if (date==null || date=="") {
  		alert("The stars need a date")
  		return false;
  	}
  	
  	if ((name.match(/[a-z]/))==null){
 		alert('The stars only understand an [a-z] name');
 		return false;
 	}
}

function doDate(){
	date = new Date();
	day = date.getDate();
	if (day < 10) {
		day = "0"+day;
	}
	if (month < 10) {
		month = "0"+month;
	}
	month = date.getMonth();
	year = date.getFullYear();
	document.forms["constellation"]["date"].value = year+"-"+month+"-"+day;
}
</script>
</head>
<body onload="doDate();">
<div class="container">
<form name="constellation" action="../cgi-bin/constellation.cgi" onsubmit="return validate()" method="get">
	<ul>
		<li>name:<br/><input type="text" name="name" size="30" Value="Your name here"></li>
		<li>date of birth:<br/><input type="date" name="date" size="30"></li>
		<li><input type="submit" value="*~*:._constellat0rrr_.:*~*" class="button"></li>
	</ul>
</form>
</div>
</body>
</html>

constellation.cgi

#!/usr/bin/python
#-*- coding:utf-8 -*-

import cgi, cgitb, random, string

alphabet = string.lowercase
form = cgi.FieldStorage()
name = (form.getvalue('name'))
date = (form.getvalue('date')).split("-")
clean = name.replace(" ","")
day = float(date[2])
month = float(date[1])
year = date[0]
year = float(year[2:])
characters = list(clean.lower());

print "Content-type: image/svg+xml"
print 

print """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="100%"
   height="100%"
   id="svg3040"
   version="1.1"
   inkscape:version="0.48.1 r9760"
   sodipodi:docname="New document 3">
  <defs
     id="defs3042" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="1.979899"
     inkscape:cx="335.42751"
     inkscape:cy="427.91605"
     inkscape:document-units="px"
     inkscape:current-layer="layer2"
     showgrid="false"
     inkscape:snap-global="false"
     inkscape:window-width="1918"
     inkscape:window-height="1177"
     inkscape:window-x="0"
     inkscape:window-y="21"
     inkscape:window-maximized="0" />
  <metadata
     id="metadata3045">
    <rdf:RDF>
      <cc:Work
	 rdf:about="">
	<dc:format>image/svg+xml</dc:format>
	<dc:type
	   rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
	<dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="sky"
     inkscape:groupmode="layer"
     id="layer1"
     sodipodi:insensitive="true">
    <rect
       style="fill:#000000;fill-opacity:1;stroke:none"
       id="rect3048"
       width="100%"
       height="100%" />
  </g>
  <g
     inkscape:groupmode="layer"
     id="layer2"
     inkscape:label="constellation">"""

circle = """<circle cx="{0}%" cy="{1}%" r="{2}%"
	fill="{3}" stroke="none" style="opacity:{4};" />"""
line = """<line x1="{0}%" y1="{1}%" x2="{2}%" y2="{3}%" stroke="#ff00ff" stroke-width="0.1%" />"""

text = """<text x="{0}%" y="{1}%" fill="#FF00FF" font-family="Courier"
font-size="13" text-anchor="middle">{2}</text>"""

for x in range(0,800):
	print circle.format(random.random()*100, random.random()*100, random.random()/5, 'grey', random.random())

x = 0
for n in characters:
	pos = ((alphabet.index(n))+1)
	if 1 <= pos <= 4:
		y1 = day*2
		x1 = (pos*10)+day*2
	elif 5 <= pos <= 8:
		y1 = month*2
		x1 = ((pos-4)*10)+day*2
	elif 9 <= pos <= 12:
		y1 = day*3
		x1 = ((pos-8)*10)+day*2
	elif 13 <= pos <= 16:
		y1 = month*3
		x1 = ((pos-12)*10)+month*2
	elif 17 <= pos <= 20:
		y1 = year/2
		x1 = ((pos-16)*10)+month*2
	elif 21 <= pos <= 24:
		y1 = day+month*2
		x1 = ((pos-20)*10)+month*2
	elif 25 <= pos <= 26:
		y1 = 90
		x1 = ((pos-24)*10)+day*2
	if x != 0:
		print line.format(x2, y2, x1, y1)
	else:
		textx = x1
		texty = y1-2
	print circle.format(x1, y1, random.random()/3+0.05, 'white',1)
	x2 = x1
	y2 = y1
	x+=1
print text.format(textx, texty, name)
print"""
</g>
</svg>"""


____DEPRECATED SECTION (v0.1)________

examples

constellate(.py)

#!/usr/bin/python
import sys, string, random

alphabet = string.lowercase
name = sys.argv[1]
clean = sys.argv[1].replace(" ",'')
try:
	characters = list(clean.lower());
	length = len(sys.argv[1]);
	f = open('constellations/'+name+'.svg', 'w')

	f.write("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
	<!-- Created with Inkscape (http://www.inkscape.org/) -->

	<svg
	   xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:cc="http://creativecommons.org/ns#"
	   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	   xmlns:svg="http://www.w3.org/2000/svg"
	   xmlns="http://www.w3.org/2000/svg"
	   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
	   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
	   width="100%"
	   height="100%"
	   id="svg3040"
	   version="1.1"
	   inkscape:version="0.48.1 r9760"
	   sodipodi:docname="New document 3">
	  <defs
	     id="defs3042" />
	  <sodipodi:namedview
	     id="base"
	     pagecolor="#ffffff"
	     bordercolor="#666666"
	     borderopacity="1.0"
	     inkscape:pageopacity="0.0"
	     inkscape:pageshadow="2"
	     inkscape:zoom="1.979899"
	     inkscape:cx="335.42751"
	     inkscape:cy="427.91605"
	     inkscape:document-units="px"
	     inkscape:current-layer="layer2"
	     showgrid="false"
	     inkscape:snap-global="false"
	     inkscape:window-width="1918"
	     inkscape:window-height="1177"
	     inkscape:window-x="0"
	     inkscape:window-y="21"
	     inkscape:window-maximized="0" />
	  <metadata
	     id="metadata3045">
	    <rdf:RDF>
	      <cc:Work
		 rdf:about="">
		<dc:format>image/svg+xml</dc:format>
		<dc:type
		   rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
		<dc:title></dc:title>
	      </cc:Work>
	    </rdf:RDF>
	  </metadata>
	  <g
	     inkscape:label="sky"
	     inkscape:groupmode="layer"
	     id="layer1"
	     sodipodi:insensitive="true">
	    <rect
	       style="fill:#000000;fill-opacity:1;stroke:none"
	       id="rect3048"
	       width="100%"
	       height="100%" />
	  </g>
	  <g
	     inkscape:groupmode="layer"
	     id="layer2"
	     inkscape:label="constellation">""")

	circle = """<circle cx="{0}%" cy="{1}%" r="{2}" fill="{3}" stroke="none" style="opacity:{4};" />"""
	line = """<line x1="{0}%" y1="{1}%" x2="{2}%" y2="{3}%" stroke="#ff00ff" stroke-width="0.1%" />"""
	text = """<text x="{0}%" y="{1}%" fill="#ff00ff" font-family="Courier" font-size="13" text-anchor="middle">{2}</text>"""

	for x in range(0,800):
		f.write(circle.format(random.random()*100, random.random()*100, random.randint(2,5), 'grey', random.random()))

	x = 0
	for n in characters:
		pos = ((alphabet.index(n))+1)
		if 1 <= pos <= 4:
			y1 = 20
			x1 = (pos*10)+20
		elif 5 <= pos <= 8:
			y1 = 30 
			x1 = ((pos-4)*10)+10
		elif 9 <= pos <= 12:
			y1 = 35
			x1 = ((pos-8)*10)+27
		elif 13 <= pos <= 16:
			y1 = 45
			x1 = ((pos-12)*10)+18
		elif 17 <= pos <= 20:
			y1 = 53
			x1 = ((pos-16)*10)+12
		elif 21 <= pos <= 24:
			y1 = 77
			x1 = ((pos-20)*10)+36
		elif 25 <= pos <= 26:
			y1 = 84
			x1 = ((pos-24)*10)+23
		f.write(circle.format(x1, y1, random.randint(2,6), 'white',1))
		if x != 0:
			f.write(line.format(x2, y2, x1, y1))
		else:
			textx = x1
			texty = y1-2
		x2 = x1
		y2 = y1
		x=1
	f.write(text.format(textx, texty, name))
	f.write("""
	</g>
	</svg>""")
except:
	print '\n!-------------------------------!\nOnly characters from a-z allowed\n!-------------------------------!\n'

run

  • $ chmod +x constellate
  • $ cp constellate /usr/local/bin
  • $ constellate 'Whatever you want to write in the stars'