from fractions import Fraction as Q
from pathlib import Path
H = Q(16) # half the block width
stair = [-H+sum(H/i for i in range(k,5)) for k in range(4,0,-1)]
base, left, right, cap = map(Q, (1, '-25/2', 20, -10))
p, q = Q('-35/2'), Q(5)
L, R = Q(2,3), Q(1,3)
l, r = (left+L*p)/(1+L), (right+R*q)/(1+R)
b = (base+(1+L)*l+(1+R)*r)/4
def check():
def in_contact(c,d,f):
assert max(c,d)-H < f < min(c,d)+H
assert L+R == 1 and L*p+R*q == cap
for a, c, f in [(cap,left,p),(cap,right,q),(left,base,l),(right,base,r)]:
in_contact(a,c,f)
assert right-left > 2*H and base-H < b < 0
assert b == (base+left+right+cap)/4
assert right+H == 36 and stair[-1]+H == Q(100,3)
assert [H+H*Q(j-1,j)+sum(H/i for i in range(j+1,5)) for j in range(1,5)] == [Q(100,3), Q(100,3), Q(92,3), Q(28)]
for j,lower in enumerate([0]+[c+H for c in stair[:-1]]):
assert sum(stair[j:])/(4-j) == lower
BG='#f8f1e0'; INK='#382d31'; BLUE='#cad9d5'; GOLD='#e2bd77'; RUST='#963f31'; GUIDE='#7b7163'; TABLE='#e4d4b6'
def txt(x,y,s,size=17,anchor='start',color=INK):
return f'{s}'
def rect(x,y,w,h,fill,stroke=INK):
return f''
def line(x,y,u,v,stroke=GUIDE,dotted=False):
dash=' stroke-dasharray="4 4"' if dotted else ''
return f''
def block(k,o,c,t,hot=False):
return rect(float(o+k*(c-H)), t, float(2*H*k), 35, GOLD if hot else BLUE)
def save(name,w,h,title,desc,parts):
svg=[f'']
Path(name).write_text('\n'.join(svg)+'\n')
def measure(o,end,top,value):
return [line(o,248,o,290,dotted=True),line(end,top,end,290,dotted=True),line(o,286,end,286),line(o,281,o,291),line(end,281,end,291),txt((o+end)/2,314,value,20,'middle',RUST)]
check()
for mode in ['comparison','staircase','counterweight']:
parts=[]
kinds = ['staircase','counterweight'] if mode=='comparison' else [mode]
for i,kind in enumerate(kinds):
o=168+380*i
parts += [txt(o-117,42,'A staircase' if kind=='staircase' else 'Room for a counterweight',24),rect(o-115,248,115,13,TABLE)]
if kind=='staircase':
parts += [block(4,o,c,213-35*j,j==3) for j,c in enumerate(stair)]
parts += measure(o,o+400/3,108,'33⅓ units')
else:
parts += [block(4,o,base,213),block(4,o,left,178),block(4,o,right,178,True),block(4,o,cap,143)]
parts += measure(o,o+144,178,'36 units')
parts += [txt(o-114,281,'table',15,color=GUIDE)]
save(mode+'.svg',760 if len(kinds)==2 else 380,339,'Four blocks at the edge', 'Equal blocks 32 units wide, drawn at the same scale. The staircase reaches 33 and one third units past the table edge. In the second arrangement the cap straddles two blocks, and the right-hand middle block reaches 36 units.',parts)
# Draw reactions on three separated levels so labels never hide the contacts.
o,k=290,7
pts=lambda x:float(o+k*x)
parts=[txt(32,40,'Where the weight lands',26),txt(32,69,'Each block weighs 1. Arrows show downward loads at contacts.',16)]
for yy,c1,c2,f1,f2,labels in [(141,left,right,p,q,('⅔','⅓')),(236,base,None,l,r,('5⁄3','4⁄3')),(331,None,None,b,None,('4',None))]:
if c1 is None: parts.append(rect(108,yy,o-108,12,TABLE))
else: parts.append(block(k,o,c1,yy))
if c2 is not None: parts.append(block(k,o,c2,yy,True))
for f,label in zip((f1,f2),labels):
if f is None: continue
x=pts(f)
parts += [txt(x,yy-34,label,20,'middle',RUST),line(x,yy-26,x,yy-4,RUST),f'']
parts += [line(o,331,o,362,dotted=True),txt(o+8,365,'table edge',15,color=GUIDE)]
save('reactions.svg',590,392,'Where the weight lands','Exploded diagram of the receiving faces: the cap puts two thirds on the left and one third on the right. The middle blocks add their own weights and put five thirds and four thirds on the base. The base adds its own weight and sends four units to the table.',parts)