# How to get the point of intersection of an arc and triangle in METAPOST?

Wish there was a nice reference for METAPOST for non-mathematicians (or a nice interactive graphical interface)

Trying to work up some paths for a CNC project, and I'm simply not understanding how intersections work in METAPOST.

Thus far I have:

\documentclass{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);

A=100;
B=133;
M=12.35;

%outer rectangle
draw origin--(B, 0)--(B, A)--(B, 0)--cycle;

%triangle
draw origin--(B, 0)--(B/2, A/2)--cycle;

%inner circle
draw fullcircle scaled M shifted (B/2, A/2);

draw (B/2-M/2, A/2) .. (B/2, A/2-M/2){right}withcolor red;

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


How do I get the coordinates for the point of intersection of the triangle and the red arc?

Here's how I would draw the figure that I think the OP wants.

\RequirePackage{luatex85}
\documentclass[margin=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);

path rectangle, triangle, circle;

numeric A, B, M;

A=89;
B=144;
M=21;

rectangle = unitsquare yscaled A xscaled B;
triangle  = subpath (0,1) of rectangle --
center rectangle -- cycle;

circle = fullcircle scaled M shifted center rectangle;

draw rectangle;
draw triangle;
draw circle;

draw subpath (4,6) of circle withcolor red;

pair p;
p = triangle intersectionpoint subpath (4,6) of circle;
dotlabel.lft("$p$", p);

endfig;
\end{mplibcode}

\end{document}


## Notes

• RequirePackage{luatex85} avoids some issues with alignment and the point of origin in some versions of LuaTeX. I think this is solved in the most recent levels.

• \mplibtextextlabel{enable} interprets strings in label commands as TeX input.

• I've defined three path variables for the three shapes I want to draw.

• I've explicitly defined the numeric parameters. This avoids any trouble with scope.

• I've defined the rectangle as a scaled version of the built-in unitsquare path.

• I've defined the triangle in terms of the rectangle (which I think was the OP intent), using the useful center macro, and the subpath (x,y) of p syntax. A unitsquare has four points starting with point 0 at the lower left, so subpath (0,1) of rectangle is the bottom edge.

• I've used subpath again to pick out the red arc. A fullcircle has 8 points starting at 3 o'clock.

• Finally I've defined a pair variable for the intersection between the triangle and the arc.

There are various introductions and other useful learning material available here: http://www.tug.org/metapost.html

## Supplementary ideas

• If you actually want to know the values of the coordinates of the intersection point, you can get these using xpart p and ypart p, where p is a pair variable, as above.

• However there is a more convenient notation for this built into plain MP. Instead of defining pair p; and assigning to p, you can assign to z1, and then the x-part is available as x1 and the y-part as y1. You don't have to declare z-variables.

• If you actually wanted to know how far along the path the intersection is, then you should use the alternative operator intersectiontimes. As the name implies, this returns the "times" along each path. More specifically, if you did this:

(t,u) = circle intersectiontimes triangle;


then the intersection point will be at point t of circle. It will also be at point u of triangle of course, although MP does not guarantee that these two points will be exactly the same.

Notice that the direction of the paths, and the order of the operators matters here. If you had done

(t,u) = triangle intersectiontimes circle;


then point t of triangle would be on the right hand leg rather than the left.

When there is more than one intersection, it's not always obvious which one MP is going to choose. In general it's the earliest intersection on the first path that's chosen. You can force MP's hand, by picking out an appropriate subpath instead of using the whole path, which is what I did in my first example.

• To answer the original question, tweak the code to put decimal(xpart p) and decimal(ypart p) somewhere. @WillAdams will probably want to know about xpart and ypart. – Andrew Kepert Dec 28 '16 at 23:48
• Thanks! I think those are what I want --- I keep reading through the METAFONT and METAPOST tutorial stuff and trying to follow along, the problem is, I don't have much of a math background, so find a lot of things difficult to follow --- I'll go back and try again with that I mind. Thruston's well-written answer helps a lot. – WillAdams Dec 29 '16 at 17:17

Turns out the secret is to assign the datatype path:

\documentclass{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);

A=100;
B=133;
M=12.35;

path p, t, r;

%outer rectangle
draw origin--(B, 0)--(B, A)--(B, 0)--cycle;

%triangle
t := origin--(B/2, A/2);
%draw t;

%inner circle
%draw fullcircle scaled M shifted (B/2, A/2);

r := (B/2-M/2, A/2) .. (B/2, A/2-M/2){right};
%draw r withcolor red;

p := r intersectionpoint t;

draw p;

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

• Err, all this does is draw a dot -- you have assigned a single point to a path variable. This is legal but rarely useful... – Thruston Dec 28 '16 at 22:59
• Yeah, but it's all that I needed for the next data point in drawing the paths which I will be needing. – WillAdams Dec 29 '16 at 17:15