How to Find Intersections with Ellipses in PGF/TikZ - latex

I am trying to display a sphere in PGF/TikZ to illustrate the idea of great circles.
The code for my current result is:
\begin{tikzpicture}
\tikzfading[name=fade right,
left color=transparent!20,
right color=transparent!90]
\tikzfading[name=fade out,
inner color=transparent!100,
outer color=transparent!10]
\tikzfading[name=fade right gc,
left color=transparent!0,
right color=transparent!70]
\draw [<->, dashed] (0,-5) -- (0,5); % y-axis
\draw [->, dashed] (0, 0) -- (20:5); % x-axis
\draw [->, dashed] (0, 0) -- (200:5); % x-axis
\draw [->, dashed] (0, 0) -- (340:5); % z-axis
\draw [->, dashed] (0, 0) -- (160:5); % z-axis
\fill [color=cyan, opacity=0.15, path fading=fade out] (0,0) circle (4cm); % bounding circle
\fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90] (0,0) ellipse (4cm and 1cm); % x-y-axis area
% great circle 1
\draw [rotate=-40, color=red, path fading=fade right gc, fading angle=40] (0,0) ellipse (4cm and 1cm);
% great circle 2
\draw[rotate=5, color=red, path fading=fade right gc, fading angle=5] (0,0) ellipse (1.5cm and 4cm);
\end{tikzpicture}
How do I
find the two points of intersection of the two red ellipses (commented as great circle 1 and 2),
find the point of intersection of a line (originating at the center (0,0)) with a ellipse, and
place a little circle or rectangle there?
Placing a little circle or rectangle there is not an issue.
Thank you very much!

Check out section 4.1.4. of the TikZ and PGF manual, titled "The Intersection of the Circles." You need to use the intersections library, which allows you to use the name intersections key, as in \path [name intersections={of=path 1 and path 2}] ;. To use this, you'll need to use the name path key, as in \draw [name path = y axis, <->, dashed] (0,-5) -- (0,5) ; % y-axis. Accessing the intersections seems to vary between versions; my local copy of the manual has different instructions from the one I linked you to. However, at least on my version, you then access the intersections with (intersection-1), (intersection-2), etc. To get circles at each intersection in your example, then, I would change your code to look like the following:
\begin{tikzpicture}
\tikzfading[ name = fade right
, left color = transparent!20
, right color = transparent!90 ]
\tikzfading[name = fade out
, inner color = transparent!100
, outer color = transparent!10 ]
\tikzfading[name = fade right gc
, left color = transparent!0
, right color = transparent!70]
\draw [name path = y axis, <->, dashed] (0,-5) -- (0,5) ; % y-axis
\draw [name path = x- axis, ->, dashed] (0, 0) -- (20:5) ; % x-axis
\draw [name path = x+ axis, ->, dashed] (0, 0) -- (200:5) ; % x-axis
\draw [name path = z+ axis, ->, dashed] (0, 0) -- (340:5) ; % z-axis
\draw [name path = z- axis, ->, dashed] (0, 0) -- (160:5) ; % z-axis
% bounding circle
\fill [color=cyan, opacity=0.15, path fading=fade out]
(0,0) circle (4cm) ;
% x-y-axis area
\fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90]
(0,0) ellipse (4cm and 1cm);
% great circle 1
\draw [ name path = great circle 1
, rotate = -40
, color = red
, path fading = fade right gc
, fading angle = 40]
(0,0) ellipse (4cm and 1cm);
% great circle 2
\draw [ name path = great circle 2
, rotate = 5
, color = red
, path fading = fade right gc
, fading angle = 5]
(0,0) ellipse (1.5cm and 4cm);
% Intersections
\path [name intersections={of=great circle 1 and great circle 2}] ;
\foreach \i in {1,...,4}
\fill [color=red] (intersection-\i) circle (2pt) ;
\path [name intersections={of=y axis and great circle 1}] ;
\fill (intersection-1) circle (2pt) ;
\fill (intersection-2) circle (2pt) ;
\path [name intersections={of=y axis and great circle 2}] ;
\fill (intersection-1) circle (2pt) ;
\fill (intersection-2) circle (2pt) ;
\foreach \a in {x,z} {
\foreach \ss in {+,-} {
\def\s.{\ss} % Otherwise the space in `\a\s axis` would get gobbled.
\path [name intersections={of=\a\s. axis and great circle 1}] ;
\fill (intersection-1) circle (2pt) ;
\path [name intersections={of=\a\s. axis and great circle 2}] ;
\fill (intersection-1) circle (2pt) ;
}
}
\end{tikzpicture}
Other than the reformatting (to avoid the horizontal scroll bar), all I have changed of your existing code is to add the name path key to your axes and great circles. I then added the intersections code, which should be relatively self-explanatory. Remember to \usetikzlibrary{intersections} first, and everything should work.

Related

Fill between function does not account for areas of other curves

In the below graph, the fill below function works great for filling in between, path B and path C. But it is not so good when I do not want to fill anything below path A (that is I do not want the area under blue line shaded, I do not how to get rid of that shaded area). Is there a way to do this?
\usepackage{tikz}
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = $ $,
ymin = 0,
ymax = 6,
xmin = 0,
xmax = 4,
samples = 100,
xticklabels={},
yticklabels={},
]
\addplot[color=blue, domain=0:4,name path =A] {4-x};
\addplot[color=red, domain=0:4, name path =C] {x};
\addplot[color=purple, domain=0:4, name path =B] {1.5*x};
\addlegendentry{$f(x)= a-bx$}
\addlegendentry{$g(x)= \tilde{d}x$}
\addlegendentry{$g(x)= dx$}
\addplot fill between[
of = C and B,
soft clip={domain=1.6:2},
];
\addplot fill between[
of = C and A,
soft clip={domain=1.6:2},
];
\end{axis}
\end{tikzpicture}

How to draw following figure or similar using tikz and pgf

My thesis superviser asks to redraw this picture in order to cite it in my paper. Could someone help how? Maybe there is a software that can make drawn picture a latex code
enter image description here
Perhaps this will get you started. First make a suitable layout of the boxes by editing the foreach loop. After that, make the boxes one-by-one, e.g.
\draw (2,7) node[rectangle,fill=green!20]{$C$};
\draw (3,7) node[rectangle,fill=green!20]{$T_1$};
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.symbols}
\begin{document}
\begin{tikzpicture}[block/.style={signal, draw, signal to=north}]
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\filldraw[fill=blue!30!white,very thick] (1,4) rectangle (9,8);
\foreach \x in {2,3,4,5,6,7,8} {
\draw (\x,3) node[block,fill=yellow!50]{text};
\draw (\x,5) node[rectangle,fill=red!20]{text};
\draw (\x,7) node[rectangle,fill=green!20]{text};
\draw[->,line width=1mm] (\x,3.5) -- (\x,4.5);
}
\begin{scope}[shift={(13,0)}]
% #1, code needed also below
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\end{scope}
\begin{scope}[shift={(15,-0.2)}]
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\end{scope}
\begin{scope}[shift={(17,-0.4)}]
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\draw (0,0) circle (2pt);
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
% copy paste contents from #1 and edit a little
\filldraw[fill=blue!30!white,very thick] (1,4) rectangle (9,8);
\foreach \x in {2,3,4,5,6,7,8} {
\draw (\x,3) node[block,fill=yellow!50]{text};
\draw (\x,5) node[rectangle,fill=red!20]{text};
\draw (\x,7) node[rectangle,fill=green!20]{text};
\draw[->,line width=1mm] (\x,3.5) -- (\x,4.5);
}
\end{scope}
\end{tikzpicture}
\end{document}

How to add subscript in a vertex in tikz-latex?

The problem I am facing is that I can define the label for each vertex, but then when I move to position the vertices south, or east, that just creates a new vertex.
\begin{figure}
\tikzstyle{VertexStyle} = [shape = ellipse,minimum width= 6ex,draw]
\tikzstyle{EdgeStyle} = [->,>=stealth']
\begin{tikzpicture}[scale=0.5, auto=left,every node/.style={circle}]
\SetGraphUnit{4}
\Vertex[L=$x_{1}$]{x};
\Vertex[L=$h_{1}$]{h};
\EA(x){h}
\Edges(x)(h)
\end{tikzpicture}
\end{figure}
Following is the output I get then:
For your code, line-by-line, the following is happening:
\Vertex[L=$x_{1}$]{x};
This creates a vertex (at the origin) with the name x and a Label $x_1$.
\Vertex[L=$h_{1}$]{h};
This also creates a vertex (at the origin) with the name h with a Label $h_1$.
\EA(x){h}
Here you add a vertex h to the EAst of x. The label name is h as well as the label.
\Edges(x)(h)
Now you try to add an edge between x and h.
Perhaps this is something that you might be interested in:
\Vertex[x = 1cm, L = $x_1$]{x};
Place a vertex called x with a Label $x_1$ (1cm east of the origin, just because).
\Vertex[x = 4cm, L = $h_1$]{h};
Place a vertex h with Label $h_1$ (4cm east of the origin, just so it doesn't sit on/close to x).
\Edges(x,h)
Draw an edge between nodes x and h.
\SetVertexMath
Node names and labels will include math content.
\EA(h){h_2}
Create a new vertex h_2 to the EAst of h.
\Edges(h,h_2)
Draw another edge between h and h_2.
Here is a complete minimal example:
\documentclass{article}
\usepackage{tkz-berge}
\usetikzlibrary{shapes}
\begin{document}
\begin{figure}
\tikzstyle{VertexStyle} = [
shape = ellipse,
minimum width = 6ex,
draw]
\tikzstyle{EdgeStyle} = [
->,
> = stealth']
\begin{tikzpicture}[scale=0.5]
\SetGraphUnit{4}
\Vertex[x = 1cm, L = $x_1$]{x};
\Vertex[x = 4cm, L = $h_1$]{h};
\Edges(x,h)
\SetVertexMath
\EA(h){h_2}
\Edges(h,h_2)
\end{tikzpicture}
\end{figure}
\end{document}

LaTex/TikZ - Box alignment

I created following LaTeX/TikZ script to draw a figure:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{arrows, chains}
\usetikzlibrary{positioning,shapes.multipart}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw, fill=yellow!96!black, rectangle,
align=center, inner sep=3ex, font=\sffamily, label=below: Label1] (Box1) {
\begin{tikzpicture}
\node[draw, shape=rectangle split, rectangle split parts=2,
inner sep=1ex, rounded corners=0pt,
fill=white, font=\vphantom{Q}\sffamily] (Innerbox) {One \nodepart{two} Two};
\end{tikzpicture}};
\node[draw, inner sep=1ex, right=of Box1, label=below: Label2] (Box2) {
\begin{tikzpicture}
\end{tikzpicture}
{\begin{tikzpicture}
\node[draw, fill=yellow!96!black, rectangle,
align=center, inner sep=3ex, font=\sffamily, label=below: Label3] (Box3) {Four};
\end{tikzpicture}}};
\draw (Box1) -- (Box2) -- (Box3);
\draw[dashed] (Innerbox.one east) -- (Box2);
\draw[dashed] (Innerbox.two east) -- (Box2);
\end{tikzpicture}
\end{document}
I want to solve following problems:
How can the boxes be aligned equal, so than the label below is always on the same level?
How can box "Four" be added (not within, but as a box like Box1) to the right of Box2, ideally with a dashed line from Box2 to Box3?
Desired structure:
[Box1] -- [Box2] -- [Box3]
My elementary approach to this picture:
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
% left box
\filldraw [fill=yellow, draw=black, ultra thick] (0,0) rectangle (8,-10);
\filldraw [fill=white, draw=black, ultra thick] (2,-2) rectangle (6,-5);
\filldraw [fill=white, draw=black, ultra thick] (2,-5) rectangle (6,-8);
\node at (4,-3.5) [font=\sffamily] {\Huge One};
\node at (4,-6.5) [font=\sffamily] {\Huge Two};
% right box
\filldraw [fill=white, draw=black, ultra thick] (12,0) rectangle (20,-10);
\filldraw [fill=yellow, draw=black, ultra thick] (13,-1) rectangle (19,-6);
\node at (16,-3.5) [font=\sffamily] {\Huge Four};
% labels
\node at (4,-11) {\Huge 1};
\node at (16,-11) {\Huge 2};
\node at (16,-7) {\Huge 3};
% lines
\draw [ultra thick] (8,-5) -- (12,-5);
\draw [loosely dashed, ultra thick] (6,-3.5) -- (12,-4);
\draw [loosely dashed, ultra thick] (6,-6.5) -- (12,-6);
\end{tikzpicture}
\end{document}
And its output:
Here you can play with coordinates to align boxes as you like.
This tries to answer to part 1 of your question while part 2 of it is not well clear to me. Anyway if you like this code, you can develop it and edit box Four just by using \filldraw, \draw and \node as above.

How to get the direction of a marking arrow point to the tangent of decoration point on the curve

I need to decorate a curve with the center of the arrow at the center of the curve. In tikz this can be achieved by setting the value of pre length equal to the length of the arrow, as in the following example
\draw[decoration={ pre length=8pt, markings, mark=at position 0.5 with {\arrow{Stealth[black,length=8pt]}}}, postaction={decorate}] (-0.5, 0.125) to [out=60, in=120] (0.5, 0.125);
However, now the direction of arrow is the tangent of the point where the arrow tip is, which is not symmetric and not nice at all.
Is it possible to get the direction of the arrow the same as the tangent of decoration point (pos=0.5 here) while keeping the center of the arrow at the same position so that the picture looks symmetric ? The following picture is what I need.
Inspired by Jake's answer of getting the local coordinate system of an arbitrary point of a curve through markings, I tweak this out.
\newcommand{\appendarrow}[3]{
\tikzset{
middlearrow/.style args={#1}{
decoration={
markings,
mark= at position 0.5 with
{
\coordinate (exy1) at (#1/-6.0,0pt);
\coordinate (exy2) at (-0.5*#1,#1/3.0); % 3.0 adjustable
\coordinate (exy3) at (0.5*#1,0pt);
\coordinate (exy4) at (-0.5*#1,#1/-3.0); % 3.0 adjustable
},
},
postaction=decorate
},
middlearrow/.default= 3pt
}
\draw[middlearrow=#1] #3;
\fill[#2] (exy1) -- (exy2) -- (exy3) -- (exy4) -- cycle;
}
If we want to draw a path with a middle arrow, just use \appendarrow{arrow size}{arrow color}{path}. For example, \appendarrow{6pt}{black}{(-0.5, 0.125) to [out=60, in=120] (0.5, 0.125);} gives me the following figure
What about this?
\draw[thick, ->] (-0.5, 0.125) to [out=90, in=180] (0, 0.5);
\draw[thick] (0, 0.5) to [out=0, in=90] (0.5, 0.125);
To me, cutting the curve in two parts and putting a regular arrow at the end of the first curve is the best idea.

Resources