# Draw two perpendiculars (Metapost)

To draw the two perpendiculars from M and N, I have written the following code but I would like to know if there is another way to proceed.

  \documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
defaultfont := "texgyrepagella-regular*default" ;
defaultscale :=0.8 ;
path p, q, circle;
circle = fullcircle scaled 2.4cm;
p = unitsquare xscaled 4cm yscaled 2cm;
z1 = point 0 of p;
z2 = point 1 of p;
z3 = point 0.3 of p;
z4 = point 3.4 of p;
q = z4 -- (5cm,ypart(z4));
z5 = p intersectionpoint q;
draw p;
draw subpath (0,2) of circle;
draw z4 -- z5;
draw z3 -- (xpart(z3),2cm);
drawdblarrow (0,-0.8cm) -- (4cm,-0.8cm);
drawdblarrow (-0.8cm,0) -- (-0.8cm,2cm);
label.bot("4",(2cm,-0.8cm));
label.bot("A",z1);
label.bot("B",z2);
label.bot("M",z3);
label.lft("N",z4);
label.bot("$x$",0.5[z1,z3]);
label.lft("2",(-0.8cm,1cm));
endfig;
\end{mplibcode}
\end{document}


• You can replace xpart z3 with x3 (and ypart z4 with y4). There is a quartercircle macro as well but that doesn't really simplify anything. – Scott H. Nov 13 '19 at 22:38

I call the rectangle vertices A, B, C, D (a, b, c and d in the MetaPost code). I also define points M' and N' so that the segments you are interested in are [MM'] and [NN'] (see the figure below). I propose two other techniques to draw these segments:

1. Tell MetaPost that mprime lies on the (c,d) line and that (m,mprime) is parallel to (a,d):
mprime = whatever[c,d];
mprime - m = whatever*(d-a);

2. Tell MetaPost that nprime lies on the (b,c) line and that (n,nprime) is perpendicular to (b,c):
nprime = whatever[b,c];
(nprime - n) dotprod (c-b) = 0;


I leave out the two double arrows from your figure, as they are irrelevant to the question. Here is a complete example showing the two ideas given above in action, and using a quartercircle of radius AM:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}

\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
defaultfont := "texgyrepagella-regular*default" ;
defaultscale := 0.8 ;
pair a, b, c, d, m, n, mprime, nprime;
path rect, qcircle;
rect = unitsquare xscaled 4cm yscaled 2cm;

a = point 0 of rect;
b = point 1 of rect;
c = point 2 of rect;
d = point 3 of rect;

m = point 0.3 of rect;
qcircle = quartercircle scaled (2*abs(m-a));
n = rect intersectionpoint reverse(qcircle);

mprime = whatever[c,d];
nprime = whatever[b,c];
mprime - m = whatever*(d-a);    % declare parallelism
(nprime - n) dotprod (c-b) = 0; % declare perpendicularity

draw rect;
draw qcircle;
draw m -- mprime;
draw n -- nprime;

label.bot("$A$", a);
label.bot("$B$", b);
label.top("$C$", c);
label.top("$D$", d);
label.bot("$M$", m);
label.lft("$N$", n);
label.top("$M'$", mprime);
label.rt ("$N'$", nprime);

endfig;
\end{mplibcode}
\end{document}


• Thank you for your suggestion. I like the use of whatever I have not used yet ! Can you clarify this instruction mprime - m = whatever*(d-a);? – Fabrice Nov 14 '19 at 15:37
• It means “Let k be a (normally anonymous!) numeric variable such that mprime - m = k*(d-a)”. The k is hidden. So, this adds one unknown and two scalar equations that can be interpreted as the vectorial equality MM' = k.AD. Every time you use whatever, a different k is created. As you probably know, MetaPost gathers the (linear) equations you give it and automatically solves the system. You only need to provide enough independent equations (constraints), that is one of its great features! – frougon Nov 14 '19 at 17:30
• Thank you for all his very clear explanations. – Fabrice Nov 15 '19 at 23:35

For interests sake, I tried to come up with a couple of additional options. Neither is particularly convenient, but if you're just learning metapost (like I am), then maybe something useful is in here.

Option 1:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}

\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(0);
defaultfont := "texgyrepagella-regular*default" ;
defaultscale := 0.8 ;

path p,q,r,s;
picture pic;

p = unitsquare xscaled 4cm yscaled 2cm;
q = quartercircle scaled 2.4cm;
z0 = p intersectionpoint q;
% draw a big cross and chop off the portions outside of the rectangle.
r:=(x0,0)--(x0,infinity) cutafter reverse p;
s:=(0,x0)--(infinity,x0) cutafter p;

draw p;
draw q;
draw s;
draw r;

label.bot("M",z0);
label.lft("N",(0,x0));
label.llft("O",origin);
label.bot("x",.5[origin,z0]);
label.bot("B", point 1 of p);
endfig;
\end{mplibcode}
\end{document}


Option 2:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}

\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(0);
defaultfont := "texgyrepagella-regular*default" ;
defaultscale := 0.8 ;

path p,q,r;
picture pic;
p = unitsquare xscaled 4cm yscaled 2cm;
q = quartercircle scaled 2.4cm;

z0 = p intersectionpoint q;
r = (x0,-infinity)--(x0,infinity);
% store big cross
pic := image(
draw r;
draw r rotated 90;
);
% and chop off portion outside of the rectangles bounding box.
% this was unexpected, the bounding box provides extra space, so didn't clip exactly.
begingroup;
interim bboxmargin := 0;
clip pic to bbox p;
endgroup;

draw pic;
draw p;
draw q;

label.bot("M",z0);
label.lft("N",(0,x0));
label.llft("O",origin);
label.bot("x",.5[origin,z0]);
label.bot("B", point 1 of p);
endfig;
\end{mplibcode}
\end{document}


• Thanks for your suggestion – Fabrice Nov 14 '19 at 15:39

Here's another approach showing off infinity and cutafter...

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
% Get Latex to do the work with the fonts
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
% given a circle, and a rectangle with the llcorner at the center of the circle...
path circle, rectangle;
circle = fullcircle scaled 86;
rectangle = unitsquare xscaled 138 yscaled 69;

% draw the first quarter of the circle, and the rectangle
draw subpath (0, 2) of circle;
draw rectangle;

% draw from the start of the circle to a point vertically above, cutting off when we get to the rectangle
% and then again horizontally
draw point 0 of circle -- point 0 of circle shifted (0, infinity) cutafter subpath (2,3) of rectangle;
draw point 2 of circle -- point 2 of circle shifted (infinity, 0) cutafter subpath (1,2) of rectangle;

label.bot("$A$", point 0 of rectangle);
label.bot("$B$", point 1 of rectangle);

label.bot("$M$", point 0 of circle);
label.lft("$N$", point 2 of circle);

label.bot("$x$", 1/2[point 0 of rectangle, point 0 of circle]);

% add the arrow marker annotations
path a[];
a1 = subpath (0, 1) of rectangle shifted 28 down;
a2 = subpath (3, 4) of rectangle shifted 28 left;

interim ahangle := 30;
drawdblarrow a1 withcolor 2/3 red; label.bot("$4$", point 1/2 of a1);
drawdblarrow a2 withcolor 2/3 red; label.lft("$2$", point 1/2 of a2);

endfig;
\end{mplibcode}
\end{document}

• I continue to learn from people like you, thank you very much ! – Fabrice Nov 15 '19 at 23:38