# paste this code at the end of VectorFieldPlot 1.1
u = 120.0
doc = FieldplotDocument('VFPt_metal_ball_grounded', commons=True,
width=600, height=600, center=[320, 300], unit=u)
# draw symbols
field = Field({'monopoles':[[-1,0,1]]}); doc.draw_charges(field)
field = Field({'monopoles':[[0.5,0,-0.5]]}); doc.draw_charges(field, scale=sqrt(0.5))
ball = doc.draw_object('g', {'id':'metal_ball', 'transform':'translate(1,0)',
'style':'fill:none; stroke:#000;stroke-linecap:square'})
# draw the rod
grad1 = doc.draw_object('linearGradient', {'id':'rod_shade', 'x1':0, 'x2':0,
'y1':0, 'y2':1, 'gradientUnits':'objectBoundingBox'}, group=ball)
for col, of in (('#666', 0), ('#ddd', 0.6), ('#fff', 0.7), ('#ccc', 0.75), ('#888', 1)):
doc.draw_object('stop', {'offset':of, 'stop-color':col}, group=grad1)
doc.draw_object('rect', {'x':0.9, 'width':0.7, 'y':-0.1/1.2+0.01, 'height':0.2/1.2-0.02,
'style':'fill:url(#rod_shade); stroke-width:0.02'}, group=ball)
# draw the metal ball
grad = doc.draw_object('radialGradient', {'id':'metal_spot', 'cx':'0.53', 'cy':'0.54',
'r':'0.55', 'fx':'0.65', 'fy':'0.7', 'gradientUnits':'objectBoundingBox'}, group=ball)
for col, of in (('#fff', 0), ('#e7e7e7', 0.15), ('#ddd', 0.25), ('#aaa', 0.7), ('#888', 0.9), ('#666', 1)):
doc.draw_object('stop', {'offset':of, 'stop-color':col}, group=grad)
doc.draw_object('circle', {'cx':0, 'cy':0, 'r':1,
'style':'fill:url(#metal_spot); stroke-width:0.02'}, group=ball)
ball_charges = doc.draw_object('g', {'style':'stroke-width:0.01667'}, group=ball)
# draw actual field
field = Field({'monopoles':[[-1,0,1], [0.5,0,-0.5]]})
n = 23
for i in range(n):
a = (0.5 + i) / n
phi = 2*pi * a
line = FieldLine(field, [-1,0], start_v=[cos(phi), sin(phi)], directions='forward')
fe = [True, False, False, False]
of = [0.5, 0.5, 0.5, 0.5]
# make little charge signs
if vabs(line.get_position(1) - sc.array([1,0])) < 1:
t = op.brentq(lambda t: vabs(line.get_position(t) - sc.array([1,0]))-1, 0, 1)
alpha = atan2(line.get_position(t)[1], line.get_position(t)[0]-1)
doc.draw_object('path', {'d':'M {0:.5f},{1:.5f} h {2:.5f}'.format(
round((u*(0.9*cos(alpha)))-2)/u,
round(u*(0.9*sin(alpha)))/u, 4/u)}, group=ball_charges)
if abs(i-(n-1.0)/2) < n/2-2:
fe = [True, False, False, True]
of = [0.5, 0.5, 0.5, (0.25+vabs(line.get_position(t)-sc.array([0.5,0]))) / 1.5]
doc.draw_line(line, arrows_style={'dist':1.5, 'min_arrows': 1, 'offsets':of, 'fixed_ends':fe})
doc.write()