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





+1 but please wrap the x in braces such that it compiles: path[name intersections=of=x and a,by=sx]; otherwise the space after x gets eaten and TikZ does not see the and.
– marmot
Aug 24 at 17:27


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



enter image description here



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



intersections


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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)