Arrows at intersection in TikZ
Arrows at intersection in TikZ
I want to have the arrow tips on the intersection between the rectangle and the lines. Here I've got a M(N)WE:
documentclass[border=5pt,tikz]standalone
usetikzlibraryintersections
begindocument
begintikzpicture
clip[draw] (0,0) rectangle (3,3);
path[name path=a] (0,0) rectangle (3,3);
% draw[name path=b] (1.5,1.5) --+ (-2,2);
% path[name intersections=of=a and b,by=s];
% node[fill=red,circle,inner sep=1pt] at (s) ;
foreach x in 0,10,...,350
draw[name path=x,shift=(1.5,1.5)] (0,0) -- (x:2);
% pgfmathsetmacrobx
path[name intersections=of=x and a,by=sx];
endtikzpicture
enddocument
Of course I could manually type the intersetions, but can I use the comman in a foreach loop
, too?
foreach loop
x
path[name intersections=of=x and a,by=sx];
x
and
@marmot Alternative:
of=a and x
, so you don't need these ugly braces ;)– TeXnician
Aug 24 at 17:28
of=a and x
@TeXnician: Oh God, thank you! xD
– current_user
Aug 24 at 17:30
3 Answers
3
I usually name paths in such a way that they do not end with a macro. That way I am save if I compute the intersection between two paths with a macro.
documentclass[border=5pt,tikz]standalone
usetikzlibraryintersections
begindocument
begintikzpicture
clip[draw] (0,0) rectangle (3,3);
path[name path=a] (0,0) rectangle (3,3);
draw[name path=b] (1.5,1.5) --+ (-2,2);
path[name intersections=of=a and b,by=s];
node[fill=red,circle,inner sep=1pt] at (s) ;
foreach x in 0,10,...,350
draw[name path=x-path,shift=(1.5,1.5)] (0,0) -- (x:2);
% pgfmathsetmacrobx
path[name intersections=of=x-path and a,by=sx];
endtikzpicture
enddocument
Without any intersections:
documentclass[border=5pt,tikz]standalone
begindocument
begintikzpicture
draw (0,0) rectangle (3,3);
foreach rot in 0,90,180,270
foreach x in -40,-30,...,40
draw[->] (1.5,1.5) --++([rotate=rot]x : 1.5/cos(x));
endtikzpicture
enddocument
Arrows touching the right side of the rectangle can be easily drawn as draw[->] (1.5,1.5) --++(x : 1.5/cos(x))
, where each arrow at angle x
has length 1.5/cos(x)
. Then, we should repeat this for the remaining upper, left, and bottom sides of the rectangle, the outer foreach
loop does this by rotating arrows at the respective angles.
draw[->] (1.5,1.5) --++(x : 1.5/cos(x))
x
1.5/cos(x)
foreach
You can try the following (switch a
and x
):
a
x
documentclass[border=5pt,tikz]standalone
usetikzlibraryintersections
begindocument
begintikzpicture
clip[draw] (0,0) rectangle (3,3);
path[name path=a] (0,0) rectangle (3,3);
% draw[name path=b] (1.5,1.5) --+ (-2,2);
% path[name intersections=of=a and b,by=s];
% node[fill=red,circle,inner sep=1pt] at (s) ;
foreach x in 0,10,...,350
draw[name path=x,shift=(1.5,1.5)] (0,0) -- (x:2);
% pgfmathsetmacrobx
path[name intersections=of=a and x, by=sx];
draw[->] (1.5,1.5)-- (sx);
endtikzpicture
enddocument
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
+1 but please wrap the
x
in braces such that it compiles:path[name intersections=of=x and a,by=sx];
otherwise the space afterx
gets eaten and TikZ does not see theand
.– marmot
Aug 24 at 17:27