File:Cylindrical magnet.svg
Summary
{{Information
|description =
The image is created by the following python source-code. Requirements:
|date =2015-05-23 |source =Own work |author =Geek3 |other versions =File:VFPt cylindrical magnet.svg (with field lines) |other fields= {{Igen|Python|+|%v|c1=
- !/usr/bin/python
- -*- coding: utf8 -*-
try:
import svgwrite as svg
except ImportError:
print 'requires svgwrite library: http://pypi.python.org/pypi/svgwrite/' exit(1)
name = 'Cylindrical_magnet'
w, h = 616, 216
lw = 16
l, r = (w - lw) / 2., (h - lw) / 2.
- document
doc = svg.Drawing(name + '.svg', size=(w, h)) doc.set_desc(title=name,
desc='about: http://commons.wikimedia.org/wiki/File:{0}.svg'.format(name))
doc['stroke'] = 'black' doc['stroke-linejoin'] = 'miter'
magnet = doc.add(doc.g(id='magnet', transform='translate({0},{1})'.format(w/2., h/2.)))
- green and red areas
magnet.add(doc.rect(insert=(-l, -r), size=(2*l, 2*r),
fill='#00cc00', stroke='none'))
magnet.add(doc.rect(insert=(0, -r), size=(l, 2*r),
fill='#ff0000', stroke='none'))
- light reflection gradient overlay
grad = doc.defs.add(doc.linearGradient(id='gradient',
x1=0, x2=0, y1=-r, y2=r, gradientUnits='userSpaceOnUse'))
for col, of, opa in [['#000000', '0', '0.125'],
['#ffffff', '0.07', '0.125'], ['#ffffff', '0.25', '0.5'], ['#ffffff', '0.6', '0.2'], ['#000000', '1', '0.33']]: grad.add_stop_color(offset=of, color=col, opacity=opa)
- frame
magnet.add(doc.rect(insert=(-l, -r), size=(2*l, 2*r),
style='fill:url(#gradient); stroke-width:{0}; stroke-linejoin:miter; stroke:#000000'.format(lw)))
- S and N labels
S = doc.text('S', insert=(0, 0),
transform='translate({0},{1}) scale({2},{2})'.format(
-0.65 * l, 0.44 * r, r / 100.))
N = doc.text('N', insert=(0, 0),
transform='translate({0},{1}) scale({2},{2})'.format(
0.65 * l, 0.44 * r, r / 100.))
text_attr = {'text-anchor':'middle',
'style':'fill:#000000; stroke:none; ' + 'font-size:120px; font-family:Bitstream Vera Sans'}
for k, v in text_attr.iteritems():
S[k] = v N[k] = v
magnet.add(S) magnet.add(N)
doc.save()
try:
import xml.dom.minidom
xml = xml.dom.minidom.parse(name + '.svg')
with open(name + '.svg', 'w') as svg_file:
svg_file.write(xml.toprettyxml(indent=' '))
except ImportError: pass }} }}
Licensing
| Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. |
- You are free:
- to share – to copy, distribute and transmit the work
- to remix – to adapt the work
- Under the following conditions:
- attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.