% See Martin Gardner: Mathematical Circus, ISBN 0-88385-506-2, % Chapter 5: Elegant Triangles, pp. 56 prologues := 1; tracingequations := 1; def PutLabel(expr A, B, C, txt) = picture lbl; lbl = thelabel(txt, origin); cw := xpart(lrcorner lbl - llcorner lbl); ch := ypart(ulcorner lbl - llcorner lbl); pair vec; vec := (0.5[B, C] - A); veclen := length vec; pair pos; pos + (cw * xpart vec / veclen, ch * ypart vec / veclen) = A; draw lbl shifted pos withcolor (0, 0, 1); enddef; def PutPointAndLabel(expr A, B, C, txt) = fill fullcircle scaled 2pt shifted A; PutLabel(A, B, C, txt); enddef; def Triangle(suffix \$)(expr Atext, Apoint, Btext, Bpoint, Ctext, Cpoint) = pair \$.A, \$.B, \$.C; \$.A = Apoint; \$.B = Bpoint; \$.C = Cpoint; % midpoints of the three sides pair \$.A.mid, \$.B.mid, \$.C.mid; \$.A.mid = 0.5[\$.B, \$.C]; \$.B.mid = 0.5[\$.A, \$.C]; \$.C.mid = 0.5[\$.A, \$.B]; % feet pair \$A.foot, \$.B.foot, \$.C.foot; \$.A.foot = \$.t.A[\$.B, \$.C]; (\$.A - \$.A.foot) dotprod (\$.B - \$.C) = 0; \$.B.foot = \$.t.B[\$.A, \$.C]; (\$.B - \$.B.foot) dotprod (\$.A - \$.C) = 0; \$.C.foot = \$.t.C[\$.A, \$.B]; (\$.C - \$.C.foot) dotprod (\$.A - \$.B) = 0; % where is the ortho centre? pair \$.O; \$.O = whatever[\$.A.foot, \$.A] = whatever[\$.B.foot, \$.B]; % compute the midpoints m[] forsuffixes \$\$ = A, B, C: pair \$.O.\$\$; \$.O.\$\$ = 0.5[\$.\$\$, \$.O]; endfor; % compute circle pair \$.NC.C; \$.NC.C = 0.5[\$.A.mid, \$.O.A]; % center point \$.NC.r = length(\$.A.mid - \$.O.A); % check outsideness boolean \$.outside; \$.outside := false; forsuffixes \$\$ = A, B, C: boolean \$.outside.\$\$; \$.outside.\$\$ := (\$.t.\$\$ > 1) or (\$.t.\$\$ < 0); if \$.outside.\$\$: \$.outside := true; fi; endfor; vardef \$.Label = PutPointAndLabel(\$.A, \$.B, \$.C, Atext); PutPointAndLabel(\$.B, \$.C, \$.A, Btext); PutPointAndLabel(\$.C, \$.A, \$.B, Ctext); enddef; vardef \$.Draw expr col = draw \$.A -- \$.B -- \$.C -- cycle withcolor col; enddef; vardef \$.DrawAndLabel expr col = \$.Draw col; PutLabel(\$.A, \$.B, \$.C, Atext); PutLabel(\$.B, \$.C, \$.A, Btext); PutLabel(\$.C, \$.A, \$.B, Ctext); enddef; vardef \$.DrawFeet = if \$.outside: forsuffixes \$\$ = A, B, C: if \$.outside.\$\$: draw \$.\$\$ -- \$.O; else: draw \$.\$\$.foot -- \$.O; fi; endfor; else: forsuffixes \$\$ = A, B, C: draw \$.\$\$.foot -- \$.\$\$ dashed evenly; endfor; fi; enddef; vardef \$.DrawNinePointCircle = draw fullcircle scaled \$.NC.r shifted \$.NC.C; enddef; enddef; beginfig(1); u = 50; Triangle(T)(btex \$A\$ etex, (-1u,1u), btex \$B\$ etex, (2u,0), btex \$C\$ etex, (-1.1u,-1u)); T.DrawFeet; T.DrawAndLabel(1,0,0); Triangle(MP)(btex \$AB\$ etex, T.C.mid, btex \$AC\$ etex, T.B.mid, btex \$BC\$ etex, T.A.mid); MP.Label; Triangle(F)(btex \$A_H\$ etex, T.A.foot, btex \$B_H\$ etex, T.B.foot, btex \$C_H\$ etex, T.C.foot); F.Label; PutPointAndLabel(T.O, T.B.foot, T.A, btex \$O\$ etex); Triangle(M)(btex \$A_M\$ etex, T.O.A, btex \$B_M\$ etex, T.O.B, btex \$C_M\$ etex, T.O.C); M.Label; T.DrawNinePointCircle; % forsuffixes \$\$ = A, B, C: % draw T.O.\$\$ -- T.\$\$.mid withcolor (0,1,0); % endfor; MP.Draw(0,1,0); MP.DrawFeet; PutPointAndLabel(MP.O, T.B.mid, T.C.mid, btex \$H\$ etex); draw T.O -- MP.O withcolor (1,0,0); fill fullcircle scaled 2pt shifted 0.5[T.O,MP.O] withcolor (1,0,0); endfig; end.