#!/usr/bin/python3
# -*- coding: utf8 -*-
try:
import svgwrite
except ImportError:
print('requires svgwrite library: https://pypi.org/project/svgwrite/')
# documentation at https://svgwrite.readthedocs.io/
exit(1)
from math import *
# document
size = 768, 580
name = 'bi-elliptic_transfer'
doc = svgwrite.Drawing(name + '.svg', profile='full', size=size)
doc.set_desc(name, name + '''.svg
https://commons.wikimedia.org/wiki/File:''' + name + '.svg')
# background
doc.add(doc.rect(id='background', insert=(0, 0), size=size, fill='white', stroke='none'))
r1 = 109.6
r2 = 146.4
rb = 537.3
g = doc.add(doc.g(transform='translate(559.22, 290)', fill='none'))
sun = g.add(doc.g(id='sun'))
nbeam = 12
rsun, rsun2 = 8.2, 7.2
rbeam = 13.8
p = []
for i in range(nbeam):
phi0, phi1 = 2*pi*i/nbeam, 2*pi*(i+0.5)/nbeam
p += [[rbeam*cos(phi0), rbeam*sin(phi0)], [rsun2*cos(phi1), rsun2*sin(phi1)]]
sun.add(doc.polygon(points=p, stroke='#f89c16', stroke_width=1, fill='#dbf816'))
grad = doc.defs.add(doc.radialGradient(id='grad', center=(0.5, 0.5), r=0.5,
gradientUnits="objectBoundingBox"))
grad.add_stop_color(offset=0, color='#dbf816')
grad.add_stop_color(offset=1, color='#f89c16')
sun.add(doc.circle(center=(0, 0), r=rsun, stroke='#f89c16', stroke_width=1,
fill='url(#grad)'))
arrow_d = 'M 0.3,0 L -0.8,0.5 Q -0.5,0 -0.8,-0.5 Z'
doc.defs.add(doc.marker(id='arrow1', refX=0, refY=0, viewBox='-1 -1 2 2',
orient='auto', markerWidth=18, markerHeight=18)).add(doc.path(
d=arrow_d, stroke='none', fill='#0000c4'))
doc.defs.add(doc.marker(id='arrow2', refX=0, refY=0, viewBox='-1 -1 2 2',
orient='auto', markerWidth=18, markerHeight=18)).add(doc.path(
d=arrow_d, stroke='none', fill='#bc0d0d'))
doc.defs.add(doc.marker(id='arrow3', refX=0, refY=0, viewBox='-1 -1 2 2',
orient='auto', markerWidth=8, markerHeight=8)).add(doc.path(
d=arrow_d, stroke='none', fill='#197810'))
doc.defs.add(doc.marker(id='arrow4', refX=0, refY=0, viewBox='-1 -1 2 2',
orient='auto', markerWidth=8, markerHeight=8)).add(doc.path(
d=arrow_d, stroke='none', fill='#a42d0c'))
g.add(doc.path(d='M {0},0 A {1},{1} 0 0 0 {1},0 A {1},{1} 0 0 0 {0},0'.format(-r1, r1),
stroke='#0000c4', stroke_width=2.5, marker_end='url(#arrow1)'))
g.add(doc.path(d='M {0},0 A {1},{1} 0 0 0 {1},0 A {1},{1} 0 0 0 {0},0'.format(-r2, r2),
stroke='#bc0d0d', stroke_width=2.5, marker_end='url(#arrow2)'))
a1 = (r1 + rb) / 2
b1 = sqrt(a1**2 - (a1 - r1)**2)
a2 = (r2 + rb) / 2
b2 = sqrt(a2**2 - (a2 - r2)**2)
g.add(doc.path(d='M {},0 A {},{} 0 0 0 {},0'.format(-rb, a1, b1, r1),
stroke='#00b996', stroke_width=2, stroke_dasharray='2,4'))
g.add(doc.path(d='M {},0 A {},{} 0 0 0 {},0'.format(r2, a2, b2, -rb),
stroke='#ff991b', stroke_width=2, stroke_dasharray='2,4'))
g.add(doc.path(d='M {},0 A {},{} 0 0 0 {},0'.format(r1, a1, b1, -rb),
stroke='#00b996', stroke_width=5))
g.add(doc.path(d='M {},0 A {},{} 0 0 0 {},0'.format(-rb, a2, b2, r2),
stroke='#ff991b', stroke_width=5))
dv1 = sqrt(2/r1 - 1/a1) - sqrt(1/r1)
dv2 = sqrt(2/rb - 1/a2) - sqrt(2/rb - 1/a1)
dv3 = sqrt(2/r2 - 1/a2) - sqrt(1/r2)
l1 = 160
g.add(doc.line(start=(r1, 0), end=(r1, -l1),
stroke='#197810', stroke_width=3, marker_end='url(#arrow3)'))
g.add(doc.line(start=(-rb, 0), end=(-rb, l1*dv2/dv1),
stroke='#197810', stroke_width=3, marker_end='url(#arrow3)'))
g.add(doc.line(start=(r2, 0), end=(r2, l1*dv3/dv1),
stroke='#a42d0c', stroke_width=3, marker_end='url(#arrow4)'))
# text
g.add(doc.text('1', font_size='48px', stroke='none', fill='black',
text_anchor='middle', transform='translate(84, 18)',
font_family='Bitstream Vera Sans'))
g.add(doc.text('2', font_size='48px', stroke='none', fill='black',
text_anchor='middle', transform='translate(-508, 18)',
font_family='Bitstream Vera Sans'))
g.add(doc.text('3', font_size='48px', stroke='none', fill='black',
text_anchor='middle', transform='translate(181, 18)',
font_family='Bitstream Vera Sans'))
doc.save(pretty=True)