TikZ: Startpunkt von Verbindungslinie ändern Thema ist als GELÖST markiert

Tabellen und Grafiken erstellen und anordnen


Yoschi95
Forum-Newbie
Forum-Newbie
Beiträge: 2
Registriert: Mi 15. Jun 2022, 14:42

TikZ: Startpunkt von Verbindungslinie ändern

Beitrag von Yoschi95 »

Hallo Community!

Ich habe hier ein Minimalbeispiel, in dem sogenannte Parallelepiped (geometrischer Körper) definiert werden. Bei der Verwendung dieser startet die Verbindungslinie von Block 1 zu Block 2 bei der vorderen rechten Linie des Block 1. Allerdings möchte ich, dass die Linie bei der hinteren rechten Linie des Blocks startet.

Kann mir jemand helfen?
\documentclass{article}

\usepackage{tikz}
\usepackage{xcolor}

\definecolor{grey}{RGB}{200,200,200}

\makeatletter
\pgfkeys{/pgf/.cd,
  parallelepiped offset x/.initial=2mm,
  parallelepiped offset y/.initial=2mm
}
\pgfdeclareshape{parallelepiped}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/parallelepiped offset x}}
    \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/parallelepiped offset y}}
    \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathclose
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@ya}}{\ppd@offset}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}

\node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm,minimum height=1.5cm, align=center] 
(1) at (0,0) {Block 1};
\node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm, minimum height=1.5cm, align=center] 
(2) at (4,0) {Block 2};

\draw[thin, -] (1) -- (2);

\end{tikzpicture}
\end{document}

Benutzeravatar
Stefan Kottwitz
Admin
Admin
Beiträge: 2489
Registriert: Di 8. Jul 2008, 00:39
Kontaktdaten:

Re: TikZ: Startpunkt von Verbindungslinie ändern

Beitrag von Stefan Kottwitz »

Hallo Yoschi,

willkommen im Forum!

Ich finde es super, dass Du gleich ein komplettes Codebeispiel postest. Das probiere ich doch gern aus und hier ist mein Vorschlag.

Zeichne doch einfach die Linie zusammen mit den Nodes, denn die werden erst nach dem gezeichneten Pfad drauf platziert und damit ist die Linie überdeckt:
\draw (0,0) node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm,
      minimum height=1.5cm, align=center] (1){Block 1}
    -- (4,0) node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm,
      minimum height=1.5cm, align=center] (2) at (4,0) {Block 2};
quader.png
Testbares Komplettbeispiel, einfach auf "LaTeX-Ausgabe" klicken:
\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usepackage{xcolor}

\definecolor{grey}{RGB}{200,200,200}

\makeatletter
\pgfkeys{/pgf/.cd,
  parallelepiped offset x/.initial=2mm,
  parallelepiped offset y/.initial=2mm
}
\pgfdeclareshape{parallelepiped}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/parallelepiped offset x}}
    \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/parallelepiped offset y}}
    \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathclose
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@ya}}{\ppd@offset}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}

\draw (0,0) node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm,
      minimum height=1.5cm, align=center] (1){Block 1}
    -- (4,0) node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm,
      minimum height=1.5cm, align=center] (2) at (4,0) {Block 2};

\end{tikzpicture}
\end{document}
Stefan

Yoschi95
Forum-Newbie
Forum-Newbie
Beiträge: 2
Registriert: Mi 15. Jun 2022, 14:42

Re: TikZ: Startpunkt von Verbindungslinie ändern

Beitrag von Yoschi95 »

Hallo Stefan,

das ist eine echt simple und zugleich effektive Lösung!

Besten Dank!!!

Noah1989

Re: TikZ: Startpunkt von Verbindungslinie ändern

Beitrag von Noah1989 »

Zusätzliche Ankerpunkte definiert man so:
\documentclass{article}

\usepackage{tikz}
\usepackage{xcolor}

\definecolor{grey}{RGB}{200,200,200}

\makeatletter
\pgfkeys{/pgf/.cd,
  parallelepiped offset x/.initial=2mm,
  parallelepiped offset y/.initial=2mm
}
\pgfdeclareshape{parallelepiped}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  
  %add additional anchor points at the outer side of the box
  	\anchor{north outer}{    
		% store lower right in xa/ya and upper right in xb/yb
		\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
		\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
		\pgf@x=0.5\pgf@xa \advance \pgf@x by 0.5\pgf@xb
		\pgf@y=\pgf@yb \advance \pgf@y by \pgfkeysvalueof{/pgf/box offset y}
	}
	\anchor{east outer}{    
		% store lower right in xa/ya and upper right in xb/yb
		\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
		\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
		\pgf@x=\pgf@xb \advance \pgf@x by \pgfkeysvalueof{/pgf/box offset x}
		\pgf@y=0.5\pgf@ya \advance \pgf@y by 0.5\pgf@yb
	}
	\anchor{north east outer}{    
		\northeast
		\advance \pgf@x by \pgfkeysvalueof{/pgf/box offset x}
		\advance \pgf@y by \pgfkeysvalueof{/pgf/box offset y}
	}
  
  \backgroundpath{
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/parallelepiped offset x}}
    \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/parallelepiped offset y}}
    \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathclose
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@ya}}{\ppd@offset}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}

\node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm,minimum height=1.5cm, align=center] 
(1) at (0,0) {Block 1};
\node[parallelepiped,draw=black,fill=grey, minimum width=2.5cm, minimum height=1.5cm, align=center] 
(2) at (4,0) {Block 2};

\draw[thin, -] (1.east outer) -- (2);

\end{tikzpicture}
\end{document}

Noah1989

Re: TikZ: Startpunkt von Verbindungslinie ändern

Beitrag von Noah1989 »

ups.. in dem Beispiel muss bei den neuen Ankerpunkten noch "box" durch "parallelepiped" ersetzt werden, da ich die shape bei mir anders genannt hatte :roll:

Antworten