Schnittpunkte in TikZ Grafik

Tabellen und Grafiken erstellen und anordnen


harper
Forum-Newbie
Forum-Newbie
Beiträge: 2
Registriert: Mi 28. Apr 2010, 21:50

Schnittpunkte in TikZ Grafik

Beitrag von harper »

Hallo,
ich möchte mit TikZ eine Beziehung über ein System von linearen Gleichungen visualisieren. Das ist mir als TikZ Anfänger soweit auch gelungen. Dabei habe ich die Koordinaten von einer Grafikvorlage übernommen.

Während in der Vorlage alle Koordinaten manuell eingegeben wurde, möchte ich sie in TikZ aber möglichst ausrechnen lassen. Dazu habe ich das Konstrukt mit dem -| bzw. |- benutzt, sowie xshift.

Leider find ich keinen Weg, die Schnittpunkte in den Geraden elegant anzugeben. Da für die Linien A1-A2 und B1-B2 identische x1 bzw. x2 gelten, konnte ich die beiden Punkte mit ($ (A1) ! 0.4 ! (A2) $) berechnen, musste aber beide male den Faktor 0.4 angeben. Besser wäre, von y ausgehend die Punkte zu bestimmen.
Gibt es da einen Weg?

Wie erwähnt, bin ich TikZ Anfänger. Kann man das übersichtlicher aufschreiben?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, arrows.meta}

\begin{document}
	\noindent
	\begin{tikzpicture}[scale=1.0, >=Stealth]
	
	% The position of the cartesian coordination system
	\coordinate (O) at (2.740,1.172);
	\coordinate (X) at (18.634,1.172);
	\coordinate (Y) at (2.740,12.776);
	
	\draw [->] (O) -- (X);
	\draw [->] (O) -- (Y);
	\node [label=left:$y$] at (Y) {};
	\node [label=below:$x$] at (X) {};

	% The line endpoints
	\coordinate (A1) at (4.555,10.716);
	\coordinate (A2) at (15.214,9.487);
	\coordinate (B1) at (4.555,6.709);
	\coordinate (B2) at (15.214,4.328);

	% draw lines with dots		
	\draw [blue,thick] (A1)--(A2);
	\draw [blue, fill] (A1) circle [radius=3pt];
	\draw [blue, fill] (A2) circle [radius=3pt];
	\draw [red,thick] (B1)--(B2);
	\draw [red, fill] (B1) circle [radius=3pt];
	\draw [red, fill] (B2) circle [radius=3pt];
	
	% example point at the functions. Better input would be a value of Y
	\coordinate (A curr) at ($ (A1) ! 0.4 ! (A2) $);
	\coordinate (B curr) at ($ (B1) ! 0.4 ! (B2) $);

	\draw [blue, fill] (A curr) circle [radius=3pt];
	\draw [red, fill] (B curr) circle [radius=3pt];
	
	\node (y in) at ([xshift=-2cm]A curr -| Y) {$y$};		
	\node (y out) at ([xshift=-2cm]B curr -| Y) {$\tilde{y}$};
	
	% draw dependcy from Y to first function getting ~X, to second function, getting ~y
	\draw [-{Stealth[length=3mm]}] (y in) -- (A curr);
	\draw [-{Stealth[length=3mm]}] (A curr) -- (B curr);
	\draw [-{Stealth[length=3mm]}] (B curr) -- (y out);
	
	% draw supplemental lines from points to axis
	\draw [dashed] (A1) -- (A1 -| Y) coordinate (a1);
	\draw [dashed] (A2) -- (A2 -| Y) coordinate (a2);
	\draw [dashed] (B1) -- (B1 -| Y) coordinate (b1);
	\draw [dashed] (B2) -- (B2 -| Y) coordinate (b2);
	\draw [dashed] (A1) -- (B1 |- X) coordinate (x1);
	\draw [dashed] (A2) -- (B2 |- X) coordinate (x2);
	\draw [dashed] (B curr) -- (B curr |- X) coordinate (x);

	% draw coordinate's names at the axis
	\draw [very thick] (x2) ++(0,0.25) -- ++(0,-0.5) node[label=below:$x_2$] {};
	\draw [very thick] (x1) ++(0,0.25) -- ++(0,-0.5) node[label=below:$x_1$] {};
	\draw [very thick] (x)  ++(0,0.25) -- ++(0,-0.5) node[label=below:$\bar{x}$] {};
	\draw [very thick] (a1) ++(0.25,0) -- ++(-0.5,0) node[label=left:$a_1$] {};
	\draw [very thick] (a2) ++(0.25,0) -- ++(-0.5,0) node[label=left:$a_2$] {};
	\draw [very thick] (b1) ++(0.25,0) -- ++(-0.5,0) node[label=left:$b_1$] {};
	\draw [very thick] (b2) ++(0.25,0) -- ++(-0.5,0) node[label=left:$b_2$] {};
\end{tikzpicture}
\end{document}

Ratgeber

Re: Schnittpunkte in TikZ Grafik

Beitrag von Ratgeber »

Versuche es mit der Bibliothek 'intersections' von »PGF/TikZ« (s. Abschnitt 133.3.2 auf Seite 144 in der Anleitung).


harper
Forum-Newbie
Forum-Newbie
Beiträge: 2
Registriert: Mi 28. Apr 2010, 21:50

Re: Schnittpunkte in TikZ Grafik

Beitrag von harper »

Mit dem Tip von Ratgeber habe ich in Sektion "13.3.2 Intersections of Arbitrary Paths" eine Lösung gefunden. Der Wert y dient nun als Eingangswert. Er ist hier: \path [name path=input] (0, 10.3) -- +(16,0);. Der Wert 10.3 ist hier noch etwas in einem Path versteckt. Kann ich den noch mit einem Makro vorher setzen?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, arrows.meta, intersections}

\begin{document}
	\noindent
	\begin{tikzpicture}[scale=1.0, >=Stealth]
	
	% The position of the cartesian coordination system
	\coordinate (O) at (2.740,1.172);
	\coordinate (X) at (16.634,1.172);
	\coordinate (Y) at (2.740,12.776);
	
	\draw [->] (O) -- (X);
	\draw [->] (O) -- (Y);
	\node [label=left:$y$] at (Y) {};
	\node [label=below:$x$] at (X) {};

	% The line endpoints
	\coordinate (A1) at (4.555,10.716);
	\coordinate (A2) at (15.214,9.487);
	\coordinate (B1) at (4.555,6.709);
	\coordinate (B2) at (15.214,4.328);

	% draw lines with dots		
	\draw [name path=blau, blue, thick] (A1)--(A2);
	\draw [blue, fill] (A1) circle [radius=3pt];
	\draw [blue, fill] (A2) circle [radius=3pt];
	\draw [name path=rot,red,thick] (B1)--(B2);
	\draw [red, fill] (B1) circle [radius=3pt];
	\draw [red, fill] (B2) circle [radius=3pt];
	
	% example crossing point at the functions. 
	\path [name path=input] (0, 10.3) -- +(16,0);
	\fill [name intersections={of=blau and input}] (intersection-1) coordinate (A curr);
	\path [name path=xpath] (A curr) |- (X);
	\fill [name intersections={of=rot and xpath}] (intersection-1) coordinate (B curr);
	
	\draw [blue, fill] (A curr) circle [radius=3pt];
	\draw [red, fill] (B curr) circle [radius=3pt];
	
	\node (y in) at ([xshift=-2cm]A curr -| Y) {$y$};		
	\node (y out) at ([xshift=-2cm]B curr -| Y) {$\tilde{y}$};
	
	% draw dependcy from Y to first function getting ~X, to second function, getting ~y
	\draw [-{Stealth[length=3mm]}] (y in) -- (A curr);
	\draw [-{Stealth[length=3mm]}] (A curr) -- (B curr);
	\draw [-{Stealth[length=3mm]}] (B curr) -- (y out);
	
	% draw supplemental lines from points to axis
	\draw [dashed] (A1) -- (A1 -| Y) coordinate (a1);
	\draw [dashed] (A2) -- (A2 -| Y) coordinate (a2);
	\draw [dashed] (B1) -- (B1 -| Y) coordinate (b1);
	\draw [dashed] (B2) -- (B2 -| Y) coordinate (b2);
	\draw [dashed] (A1) -- (B1 |- X) coordinate (x1);
	\draw [dashed] (A2) -- (B2 |- X) coordinate (x2);
	\draw [dashed] (B curr) -- (B curr |- X) coordinate (x);

	% draw coordinate's names at the axis
	\draw [very thick] (x2) ++(0,0.25) -- ++(0,-0.5) node[label=below:$x_2$] {};
	\draw [very thick] (x1) ++(0,0.25) -- ++(0,-0.5) node[label=below:$x_1$] {};
	\draw [very thick] (x)  ++(0,0.25) -- ++(0,-0.5) node[label=below:$\bar{x}$] {};
	\draw [very thick] (a1) ++(0.25,0) -- ++(-0.5,0) node[label=left:$a_1$] {};
	\draw [very thick] (a2) ++(0.25,0) -- ++(-0.5,0) node[label=left:$a_2$] {};
	\draw [very thick] (b1) ++(0.25,0) -- ++(-0.5,0) node[label=left:$b_1$] {};
	\draw [very thick] (b2) ++(0.25,0) -- ++(-0.5,0) node[label=left:$b_2$] {};
\end{tikzpicture}
\end{document}

Antworten