7

Consider the following:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width = 4]
\coordinate (a) at (0,0);
\coordinate (b) at (1,0);
\coordinate (c) at (1,1);
\draw (a) -- (intersection of a--b and b--c);
\draw (c) -- (intersection of a--b and b--c);
\end{tikzpicture}
\end{document}

Which produces:

enter image description here

How can I fill the corner? Adding [cap=rounded] didn't solve the issue. I know that I could do something like \draw (a) -- (b) -- (c) but I need to obtain the same effect using intersection.

Thanks!

EDIT 1: I try to figure out how to handle two independent line segments and their intersection. For example:

\begin{tikzpicture}[scale=4,line width=7]
\coordinate (a1) at (0,0);
\coordinate (b1) at (1,1);
\coordinate (b2) at (1,-1);
\draw (b1) -- (b2);
\foreach \x in {0,20,30,40,45}
{
\coordinate (a2) at (\x:10);
\draw (a1) -- (intersection of a1--a2 and b1--b2);
}
\end{tikzpicture}

Here, I don't know in advance (well I do, but for the sake of the question I don't), where the two segments intersect and whether or not the intersection point is a common end point.

enter image description here

1
  • As Jake says in the comments to his answer, could you give a slightly more complicated example of what you are trying to do? If your lines meet at right-angles then you could add line cap=rect to make it look right. Commented May 20, 2011 at 7:30

3 Answers 3

12

In order to get the corner right, the path should be drawn in a single \draw command. You can still use the intersection of in this:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width = 4]
\coordinate (a) at (0,0);
\coordinate (b) at (1,0);
\coordinate (c) at (1,1);
\draw (a) -- (intersection of a--b and b--c) -- (c);
\end{tikzpicture}
\end{document}

7
  • This is indeed helpful for my case, but in general I think it is not good enough. If you're plotting something with parameters, then you may not know in advance that the intersection occurs exactly at a corner. It may be some mid point or a proper intersection. Is there some more general approach?
    – Dror
    Commented May 20, 2011 at 6:34
  • 2
    @Dror: Could you edit your example to include a case like the one you mention? I'm not sure I understand what you mean by "proper intersection".
    – Jake
    Commented May 20, 2011 at 6:40
  • @Dror: Ah, do you mean that your two lines would not necessarily use the same intersection as their endpoints?
    – Jake
    Commented May 20, 2011 at 6:46
  • @Jake: Yes - more or less. Consider my edit...
    – Dror
    Commented May 20, 2011 at 7:38
  • 1
    @Jake: Oh my bad, just thought it would work as well. =) Commented May 20, 2011 at 10:27
3

I think that this is most likely best handled by a clip to chop the lines extending from (a1) at the edge of the line they are meant to be drawn to. Getting it right is a bit of a hassle (but would be near impossible without the fantastic calc library) and the following is not guaranteed to be anywhere near robust. I've tried not to assume that your lines are aligned with the axes, but I was only careful at getting the clip path right on the line from (b1) to (b2).

Incidentally, this doesn't interact well with the scale option since that scales coordinates but not line widths. So to enlarge your picture I used the x=4cm,y=4cm syntax which redefines the standard unit.

line ends

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[line width=7,y=4cm,x=4cm]
\coordinate (a1) at (0,0);
\coordinate (b1) at (1,1);
\coordinate (b2) at (1,-1);
\coordinate (ab) at ($($(b1)!(a1)!(b2)$)!-.5\pgflinewidth!(a1)$);
\coordinate (ab1) at ($(b1)!(ab)!90:(b2)$);
\coordinate (ab2) at ($(b2)!(ab)!90:(b1)$);
\draw[red] (b1) -- (b2);
\clip ($(ab1)!-1cm!(ab2)$) -- ($(ab2)!-1cm!(ab1)$) -| (a1) |-  ($(ab1)!-1cm!(ab2)$);
\foreach \x in {0,20,30,40,45}
{
\coordinate (a2) at (\x:10);
\draw (a1) -- ($(a1)!1.1!(intersection of a1--a2 and b1--b2)$);
}
\end{tikzpicture}

\end{document}
2

I think you will have to (re)draw the corners as part of a continuous path:

\documentclass{article}
\usepackage{tikz}

\begin{document}
 \begin{tikzpicture}[scale=4,line width=7]
 \coordinate (a1) at (0,0);
 \coordinate (b1) at (1,1);
 \coordinate (b2) at (1,-1);
 \coordinate (a0) at (0:10);
 \draw (b1) -- (b2) ;
 \foreach \x in {0,20,30,40,45}
 {
 \coordinate (a2) at (\x:10);
 \draw (a1) -- (intersection of a1--a2 and b1--b2) -- (b2);
 }

 \draw (b1) -- (a1) -- (intersection of a1--a0 and b1--b2);
 \end{tikzpicture}
\end{document}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .