10

I'm currently creating lecture notes for "Geometry and Topology". As we now arrived at part about euclidean geometry, I quite often have the situation that I need to visualize lines (not line segments, but lines).

For example, I have the following image (the code is at the end)

enter image description here

But PQ should be a line, so I need to "enlarge" the line segment PQ. Usually, I would calculate the equation of PQ like this

m = (P.y - Q.y)/(P.x - Q.x) -- mind the special case of P.x = Q.x
P.y = m * P.x + t
<=> t = P.y - m*P.x

Then I would add two helping points A and B:

(enlarge by 0.5)
A.x := P.x - 0.5 
B.x := Q.x + 0.5
A.y = m*A.x + t
B.y = m*B.x + t

And finally I would draw the line A - P - Q - B which is my "enlarged line 'PQ'".

But this seems to be quite complicated to me for a task that could be done automatically. So is there a way in TikZ to enlarge lines?

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{point}=[circle,thick,draw=black,fill=black,inner sep=0pt,minimum width=4pt,minimum height=4pt]
    \node (P)[point,label={[label distance=0cm]-90:$P$}] at (0,0) {};
    \node (Q)[point,label={[label distance=0cm]-90:$Q$}] at (5,1) {};
    \node (A)[point,label={[label distance=0cm]180:$\varphi_1(R)$}] at (2,2) {};
    \node (B)[point,label={[label distance=0cm]190:$\varphi_2(R)$}] at (1,3) {};

    \draw[very thick, enlarge] (P) edge node  {} (Q);
    \draw[very thick, red] (P) edge node {} (A);
    \draw[very thick, red] (P) edge node {} (B);
    \draw[very thick, green] (Q) edge node {} (A);
    \draw[very thick, green] (Q) edge node {} (B);
\end{tikzpicture}
\end{document}
8
  • 1
  • You might also want to look at the tkz-euclide package for drawings like this. The syntax takes a bit of getting used to, but once you've got the hang of it, it makes drawing diagrams like this much easier.
    – Jake
    Jan 14, 2014 at 12:36
  • @Jake: thanks, it works with \draw ($(P)!-1cm!(Q)$) -- ($(Q)!-1cm!(P)$); :-) Jan 14, 2014 at 12:49
  • Could you add that as an answer?
    – Jake
    Jan 14, 2014 at 12:51
  • 1
    The line drawn by \tkzDrawLine extends over the points by 20 % of the distance between the two points. You can adjust that amount using \tkzDrawLine[add=0.1 and 0.4](P,Q) to make the line extend by 10 % and 40 % of the distance, for example.
    – Jake
    Jan 14, 2014 at 13:04

4 Answers 4

14

When using tkz-euclide to construct the drawing, you can indicate a line passing through two points using \tkzDrawLine(P,Q), while the line segment would be drawn using \tkzDrawSegment(P,Q):

\documentclass{standalone}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}
\begin{tikzpicture}
\tkzDefPoints{0/0/P, 5/1/Q, 2/2/Phi1, 1/3/Phi2}

\tkzDrawSegments[red](P,Phi1 P,Phi2)
\tkzDrawSegments[blue](Q,Phi1 Q,Phi2)

\tkzDrawLine(P,Q)

\tkzDrawPoints(P,Q,Phi1,Phi2)
\tkzLabelPoints[below](P,Q)
\tkzLabelPoint[above left](Phi1){$\varphi_1(R)$}
\tkzLabelPoint[above left](Phi2){$\varphi_2(R)$}
\end{tikzpicture}
\end{document}
4
  • 1
    This solution looks very much the same as my solution when you use \tkzSetUpPoint[shape=circle,size=10,color=black,fill=black]. Jan 14, 2014 at 13:14
  • What does \usetkzobj{all} do? Removing it seems to have no effect. Jan 14, 2014 at 13:27
  • 1
    @moose: That command loads tools for drawing polygons, arcs and circles. It's actually not necessary for this code.
    – Jake
    Jan 14, 2014 at 13:28
  • 1
    You cad just write \tkzDrawLine[add=0.1 and 0.2](P,Q) and play with 0.1 and 0.2. Thee are the percentages of the segment PQ this segment will be extended beyond the points. The first number (0.1) is for the extension beyond the first point, the second number (0.2) is for the extension beyond the second point Nov 10, 2016 at 14:14
10

Solution

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{point}=[circle,thick,draw=black,fill=black,inner sep=0pt,minimum width=4pt,minimum height=4pt]
    \node (P)[point,label={[label distance=0cm]-90:$P$}] at (0,0) {};
    \node (Q)[point,label={[label distance=0cm]-90:$Q$}] at (5,1) {};
    \node (A)[point,label={[label distance=0cm]180:$\varphi_1(R)$}] at (2,2) {};
    \node (B)[point,label={[label distance=0cm]190:$\varphi_2(R)$}] at (1,3) {};

    \draw[very thick] (P) edge node  {} (Q);
    \draw[very thick, red] (P) edge node {} (A);
    \draw[very thick, red] (P) edge node {} (B);
    \draw[very thick, blue] (Q) edge node {} (A);
    \draw[very thick, blue] (Q) edge node {} (B);

    \draw[very thick] ($(P)!-1cm!(Q)$) -- ($(Q)!-1cm!(P)$);
\end{tikzpicture}
\end{document}

Explanation

The key line is

\draw[very thick] ($(P)!-1cm!(Q)$) -- ($(Q)!-1cm!(P)$);

$(P)!-1cm!(Q)$ defines a point that is 1cm more in direction P when you look at the line PQ. The same for ($(Q)!-1cm!(P)$). This way, you have "enlarged" the line PQ by 1cm in both directions.

Image

enter image description here

10

Another solution would be to just use shorten > and shorten <:

\documentclass[varwidth=true, border=2pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \tikzstyle{point}=[circle,thick,draw=black,fill=black,inner sep=0pt,minimum width=4pt,minimum height=4pt]
    \node (P)[point,label={[label distance=0cm]-90:$P$}] at (0,0) {};
    \node (Q)[point,label={[label distance=0cm]-90:$Q$}] at (5,1) {};
    \node (A)[point,label={[label distance=0cm]180:$\varphi_1(R)$}] at (2,2) {};
    \node (B)[point,label={[label distance=0cm]190:$\varphi_2(R)$}] at (1,3) {};

    \draw[very thick, shorten >=-1cm, shorten <=-1cm] (P) edge node  {} (Q);
    \draw[very thick, red] (P) edge node {} (A);
    \draw[very thick, red] (P) edge node {} (B);
    \draw[very thick, green] (Q) edge node {} (A);
    \draw[very thick, green] (Q) edge node {} (B);

    \path[use as bounding box] (-1,1) rectangle (6, 3.3);
\end{tikzpicture}
\end{document}

Note, however, that TikZ will not extend the bounding box automatically, which is why I had to add \path[use as bounding box] ...;

4

With PSTricks.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}[linejoin=1,linecap=1](8,6)
    \pstGeonode
    [
        PointName={\varphi_2(R),\varphi_1(R),default},
        PosAngle={180,180,-90},
        PointNameSep={24pt,24pt,12pt},
    ](3,5.5){T}(4,3){B}(1,1){P}(7,2){Q}
    \psline[linecolor=red](B)(P)(T)
    \psline[linecolor=blue](B)(Q)(T)
    \pcline[nodesep=-1](P)(Q)
\end{pspicture}
\end{document}

enter image description here

6
  • Can the lines be drawn behind the circles?
    – Jake
    Jan 14, 2014 at 15:13
  • @Jake: Yes. Redraw the circles after drawing the lines. The circles given above are automatically provided by \pstGeonode as a useful bonus as long as linecolor=black. Jan 14, 2014 at 15:17
  • Thank you for answering. It is sad that redrawing the circles is the only solution, since it is not best practice and unnecessarily takes up space in the resulting file.
    – Jake
    Jan 14, 2014 at 15:24
  • @Jake: Then prevent \pstGeonode from providing bonus circles when constructing nodes via PointSymbol=none. So we draw (rather than redraw) the circles manually after constructing the lines. It will save the data storage which in turn reduce the carbon emission. Jan 14, 2014 at 15:27
  • That's a much better idea, thank you. +1
    – Jake
    Jan 14, 2014 at 15:29

You must log in to answer this question.

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