Aiming an arrow at a target - latex

I am writing a carom billard instruction manual in Latex. Illustrations are made with tikz. I need an arrow that always points from the cue ball in the direction to the object ball and is always the same lenght and at the same distance from the cue ball.
I have a solution drawing an arrow from cueball to object ball and shortening the line. This shortening however has to be done manually every time the distance between the balls changes, by trial and error.
Is there a way to automate this so I don't have to change the lenght by hand?
\documentclass[a4paper, 11pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows,arrows.meta}
\begin{document}
\begin{tikzpicture}[x=1mm, y=1mm]
% Units definitions
\def \tb {142} % width of table
\def \tl {2*\tb} % lenght of table
\def \d {\tb/4}; % diamonds
\def \c {\tb/3}; % Cadre
\def \bb {6.15}; % diameter of balls
% Cueball coordinate
\path (\c,\c) coordinate (B1);
% Objectball coordinate
\path (1.5*\bb,\c-\bb) coordinate (B2);
% Arrow
\draw [-Stealth, thick, shorten >=125, shorten <=30]
(B1) --
(B2);
% Cueball
\draw [fill=white] (B1)
node[above left = 5]{\small Cueball} % Beschriftung
circle (\bb/2);
% Objectball
\draw [fill=white] (B2)
node[above left = 5]{\small Objectball} % Beschriftung
circle (\bb/2);
\end{tikzpicture}
\end{document}

Here is how you could draw the cue stick (you can add draw,red to the fake line and circles to see how this is constructed):
\def \distancetoball {6mm};
\def \sticklength {20mm};
% Define fake line that goes through the object ball and the cue ball
\path [name path=direction] ($(B1)!-\distancetoball-\sticklength!(B2)$) -- (B2);
% Define two fake circles around the cueball to place the cue stick
\path [name path=circle1] (B1) circle(\distancetoball);
\path [name path=circle2] (B1) circle(\distancetoball+\sticklength);
% Pick the intersection of the line with the two circles to be the two extremities of the cue stick
\path [name intersections={of=direction and circle1}];
\coordinate (tip) at (intersection-1);
\path [name intersections={of=direction and circle2}];
\coordinate (source) at (intersection-1);
% Draw the cue stick
\draw [-Stealth, thick] (source) -- (tip);

Related

Generating random points then connecting points less than certain distance away

I currently have the code below which produces the later output. I would like to generate the 10 points randomly as I already am. But instead of having blue dashed lines connecting to the the location (0,0), I want them to connect to the other dots if the distance is less than 4cm away.
I attempted things such as storing the data into arrays but updating and accessing the values was not working. I attempted nested for loops but handling the seed became difficult. What is a good way to do this?
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-2.5,-2.5) grid (2.5,2.5);
\pgfmathsetseed{2}
\foreach \x in {1,...,10}
{
\pgfmathrandominteger{\a}{-240}{240}
\pgfmathrandominteger{\b}{-240}{240}
\fill [color=red,anchor=center](\a*0.01,\b*0.01) circle (0.1);
% CHANGE HERE
\draw [color=blue,densely dotted] (\a*0.01,\b*0.01) -- (0.0,0.0);
};
\end{tikzpicture}
\end{document}
I seemed to have figured out a solution (not ideal but works for this case).
The key was to use the \pgfmathparse for performing the if statement to get a 0 or 1 to use in \ifnum.
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-2.5,-2.5) grid (2.5,2.5);
\pgfmathsetseed{2}
\foreach \x in {1,...,10}
{
\pgfmathrandominteger{\a}{-240}{240}
\pgfmathrandominteger{\b}{-240}{240}
\fill [color=red,anchor=center](\a*0.01,\b*0.01) circle (0.1);
\pgfmathsetseed{2}
\foreach \y in {0,...,\x}
{
\pgfmathrandominteger{\c}{-240}{240}
\pgfmathrandominteger{\d}{-240}{240}
\tikzmath{\i=(\a*0.01-\c*0.01)^2;}
\tikzmath{\j=(\b*0.01-\d*0.01)^2;}
\tikzmath{\k=\i+\j;}
\pgfmathparse{\k < 4.0 ? 1 : 0}
\ifnum\pgfmathresult=1
\draw [color=blue,densely dotted] (\a*0.01,\b*0.01) -- (\c*0.01,\d*0.01);
\fi
};
};
\end{tikzpicture}
\end{document}

How to draw a continuous line using two seperate existing paths using tikz

I'm trying to draw a curve using two seperate paths.
The Line I want to draw should use path=\pathX and use path=\pathY
but I don't want it to draw the entire paths which is why I helped myself clipping both paths. I'd also like to join both path segments with lines on either end which I somehow
achieved. Yet I would like all four segments to be drawn as one continuous curve unlike
what you see in the graphic I attached.
My MWE is as follows.
\documentclass[tikz]{standalone}
\usepackage[]{tikz}
\usetikzlibrary{%
calc,%
intersections,%
}%
\begin{document}
\def\dimx{5}
\def\dimy{5}
\begin{tikzpicture}[]
\path [name path=I, save path=\pathI] ($.25*(\dimx,0)$) rectangle ($.75*(\dimx,0) + .85*(0,\dimy)$);
\coordinate [] (p11) at ($0.1*(0,\dimy) + .1*(\dimx,0)$);
\coordinate [] (p12) at ($0.65*(0,\dimy) + .9*(\dimx,0)$);
\coordinate [] (p21) at ($0.2*(0,\dimy) + .1*(\dimx,0)$);
\coordinate [] (p22) at ($0.95*(0,\dimy) + .9*(\dimx,0)$);
\draw [name path=p1, save path=\pathC]
(p11) to [out=25, in=220] node [at end, anchor=west] {$p_1$} (p12);
\draw [name path=p2, save path=\pathA]
(p21) to [out=35, in=230] node [at end, anchor=west] {$p_2$} (p22);
\path [name intersections={of=p1 and I}] coordinate (4) at (intersection-2);
\path [name intersections={of=p1 and I}] coordinate (1) at (intersection-1);
\path [name intersections={of=p2 and I}] coordinate (3) at (intersection-2);
\path [name intersections={of=p2 and I}] coordinate (2) at (intersection-1);
\begin{scope}[]
\path [clip, use path=\pathI];
\path [name path=p1, save path=\pathX]
(p21) to [out=35, in=230]
node [at end, anchor=west] {$p_1$}
coordinate [pos=0.15] (A)
coordinate [pos=0.7] (B)
(p22);
\path [name path=p3, save path=\pathY]
(p11) to [out=25, in=220]
node [at end, anchor=west] {$p_3$}
(p12);
\draw [thick, blue] edge [use path=\pathY] edge [use path=\pathX];
\end{scope}
\draw [thick, blue] (1) -- (2) (3) -- (4);
\end{tikzpicture}
\end{document}
I might be using a completely wrong approach here but that's as far as google and the pgf manual got me.
Any suggestions I'll greatly appreciate. Thanks in advance.

CircuiTikZ and FET drawing

Is there some simple way to avoid shorting the pins of a FET?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0)
to[I,I=$I_s$] (0,2) % The current source
to[short] (2,2);
\draw (2,0)
to node[nigfete]{FET} (2,2) % The FET transistor
to[short] (2,0) to[short] (0,0);
\draw (2,2)
to[short] (4,2)
to[R=$R$] (4,0)
to[short] (2,0);
\draw (4,2);
\draw node[rground]{};
\end{circuitikz}
\end{document}
I am not sure to understand what you want to achieve, but
use short only if you need to add poles or labels, otherwise -- is easier to type;
use the anchors of the components.
tikz is automatically loaded by circuitikz, and that one has a mandatory argument (the voltage direction standard, look at the manual and at the wanings! ;-).
So a first change could be this:
\documentclass[border=4pt] {standalone}
\usepackage[RPvoltages]{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) node[rground]{}
to[I,I=$I_s$] (0,2) -- (2,2)
node[nigfete, anchor=D](F){FET} % The FET transistor
(F.S) to[short, -*] (2,0) -- (0,0);
\draw (2,2) -- (4,2)
to[R=$R$] (4,0) -- (2,0);
\draw node[rground]{};
\end{circuitikz}
\end{document}
which leads to:
then, in my opinion, it would be better to use relative positioning to make the circuit relocatable.
If you need/want the FET centered in the branch it could be better to start with it or use the calc TikZ library (already loaded by circuitikz). In the following circuit all the movement are relative, so you can move it by just changing the first (0,0):
\documentclass[border=4pt] {standalone}
\usepackage[RPvoltages]{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) node[rground](GND){} to[I,I=$I_s$] ++(0,2)
-- ++(2,0) coordinate(top)
-- ++(2,0) to[R=$R$] ++(0,-2)
-- (GND-|top) coordinate(bottom)
-- (GND)
($(top)!0.5!(bottom)$) node[nigfete](F){FET}
(F.D) -- (top) (F.S) -- (bottom);
\end{circuitikz}
\end{document}

How to draw a cross section of an engine in TikZ

I would like to convert the following picture, which is a cross section of an engine, into a TikZ code:
How to start with this picture?
How to draw the walls and pipes in an easy way?
How to draw the oil (the hatched area)?
How to draw the oil (the hatched area)?
One way may be to use pattern=north east lines from patterns library described at page 730 of the PGF Manual:
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\fill [pattern=north east lines] (0,6) -- (0,0) -- (10,0) -- (10,6);
\draw (0,10) -- (0,0) -- (10,0) -- (10,10);
\end{tikzpicture}
\end{document}

How to change arrow tip in tikz

Is there a simple way to increase the size of an arrow tip using something like:
\tikzset{myptr/.style=->, ????}
without designing a new arrow style from scratch?
One solution, very quick, to just scale the arrow head is number %2 in the following:
\documentclass[multi=false,tikz,border=2mm]{standalone}
\usetikzlibrary{arrows,decorations.markings}
\begin{document}
\begin{tikzpicture}
%1
\draw [->,>=stealth] (0,.5) -- (2,.5);
%2
\draw [decoration={markings,mark=at position 1 with
{\arrow[scale=3,>=stealth]{>}}},postaction={decorate}] (0,0) -- (2,0);
\end{tikzpicture}
\end{document}
This produces:
(sorry for excessive zoom).
Much more in the answers to this question and in this answer, that I used as a source.
Addendum
\tikzset approach. This code:
\documentclass[multi=false,tikz,border=2mm]{standalone}
\usetikzlibrary{arrows,decorations.markings}
\begin{document}
\begin{tikzpicture}
\tikzset{myptr/.style={decoration={markings,mark=at position 1 with %
{\arrow[scale=3,>=stealth]{>}}},postaction={decorate}}}
%1
\draw [->,>=stealth] (0,.5) -- (2,.5);
%2
\draw [myptr] (0,0) -- (2,0);
\end{tikzpicture}
\end{document}
produces the same output as the above one (source: PGF Manual, section 2.8).
Obviously you can use -Latex instead of stealth.
There is a new solution, see https://latexdraw.com/exploring-tikz-arrows/#t-1610685307397. It allows changing both the length and width of arrows:
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,arrows}
\begin{document}
\begin{tikzpicture}
\draw [-{Stealth[length=3mm, width=2mm]}] (0,0.5) -- (1,0.5);
\draw [-stealth] (0,0) -- (1,0);
\end{tikzpicture}
\end{document}

Resources