# Defining a point on a path by direction of another path

Is it possible to define a point when I have two paths such as:

``````beginfig(1);
u := 1cm;
path p[];
p0 := (1u,3u)--(2u,2u);
p1 := (2u,0)--(3u,2u);
for i=p0,p1: draw i; endfor;
endfig;
end
``````

so that if continuing `p0` to the `p1`, the wanted point would be there.

I've tried the `dir`* commands, but all the examples I've found seem to either be overly complex for my understanding, or use them in curve definitions.

-
 Where should the point be? Can you be more precise? Did you see the `direction` operator? Section 9.2 of the Metapost manual. – egreg Jan 14 at 18:13 @egreg: yes, that was actually exactly the part I was referring to with "overly complex for me"; I tried to use the example `z[i]-(x[i+1],0) = whatever*direction t[i] of fun;` as a basis: `z0 = whatever*direction p0 of p1;`, but that gave me `Not implemented: postcontrol(path)of(path).`. The manual says that the first argument should be `numeric`, but I don't know how to extract the direction of the path as a `numeric`. – morbusg Jan 14 at 19:29

## 2 Answers

Is this what you're after? The `whatever` command represents an arbitrary point along the line that connects the two points given as args so that `z5 = whatever[z1,z2]=whatever[z3,z4];` solves the resulting equations and produces the point where the lines would intersect.

``````beginfig(1);
u := 1cm;
z1=(1u,3u);
z2=(2u,2u);
z3=(2u,0);
z4=(3u,2u);
draw z1--z2;
draw z3--z4;
z5 = whatever[z1,z2]=whatever[z3,z4];
dotlabel.lrt("z5",z5);
endfig;
end
``````
-
 Yes! This works nicely as well, thank you! I was hoping something to which I could give `path`s, but I think I can work around that. – morbusg Jan 14 at 19:31 Ah, sorry looks like I didn't read the title very well. I don't know Metapost very well so I'm not sure if there's an analogous construction for paths. – Scott H. Jan 14 at 20:25 It looks like using your answer as a `def` works on `path`s just fine: ```secondarydef p on q = whatever[point 0 of p,point 1 of p]=whatever[point 0 of q,point 1 of q] enddef; ```, after which `z5 = p0 on p1;` yields the `point`. Nice, thanks again! :-) – morbusg Jan 14 at 21:03
``````beginfig(1);
u := 1cm;
path p[];
p0 := (1u,3u)..(.5u,2u)..(u,u);
p1 := (2u,0)--(3u,2u);
for i=p0,p1: draw i; endfor;
z1-(u,u)=100*direction infinity of p0;
z2-(u,u)=-100*direction infinity of p0;
p2 := z1--z2;
pickup pencircle scaled 4pt;
drawdot p1 intersectionpoint p2;
endfig;
end
``````

`direction` requires a "time"; since `p0` is an open path, the point at time `infinity` is the terminal point. So I compute two points on the tangent line and define the tangent as a path, finding where it intersects the path `p1`.

Here is your original picture:

``````beginfig(1);
u := 1cm;
path p[];
p0 := (1u,3u)--(2u,2u);
p1 := (2u,0)--(3u,2u);
for i=p0,p1: draw i; endfor;
z1-(2u,2u)=100*direction infinity of p0;
z2-(2u,2u)=-100*direction infinity of p0;
p2 := z1--z2;
pickup pencircle scaled 4pt;
drawdot p1 intersectionpoint p2;
endfig;
end
``````

-
 Thanks, this is nice since it works on curves. Could you tell me what is the multiplier `100` for the `direction`? – morbusg Jan 15 at 10:13