Why does this tikz code produce two separate lines? - pdflatex

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] grid (4,3);
\coordinate (X) at (3,3);
\draw[red ,thick,xshift=-10] (1,1 -| X) -- (2,2 -| X);
\draw[blue,thick,xshift=-10] (1,1 -| 3,3) -- (2,2 -| 3,3);
\end{tikzpicture}
\end{document}
It seems that xshift=-10 does not have any effect when the coordinate is specified by name. Why is that?

the answer lies in that X is defined outside the scope of the xshift=-10 transformation - see here for details as well as possibly here -, i.e. the node X is defined and has a fixed position before the beginning of the scope of the transformation which then is only applied to nodes newly defined within the scope.
an example may clarify things: extending the scope to include the definition of X shows the expected behavior with only one line:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines] grid (4,3);
\begin{scope}[xshift=-10]
\coordinate (X) at (3,3);
\draw[red ,thick] (1,1 -| X) -- (2,2 -| X);
\draw[blue,thick] (1,1 -| 3,3) -- (2,2 -| 3,3);
\end{scope}
\end{tikzpicture}
\end{document}
produces:

Related

Check if a point is inside an arbitrary hexahedron

I am working on a 3D finite element code, where i face the following problem:
If I take an arbitrary point (say x), how do I figure out, which element it is in?
This can be simplified to: How do I check if an arbitrary point (x) lies inside or outside of an (hexahedral) element?
What I already found:
Limited to cubes: How to determine a point is inside or outside a cube?
Limited to rectangular shapes: https://math.stackexchange.com/questions/1472049/check-if-a-point-is-inside-a-rectangular-shaped-area-3d
Contrary to the two approaches above, my problem does not assume right angles nor parallel faces.
Problem sketch:
Notation: (again: though the sketch shows a regular shape, our hexahedron is assumed to be of general shape)
8-node hexahedron topology, nodes: 0,..,7
axis: r,s,t
t
|
4--------|-------------7
/| | /|
/ | | / |
/ | | / |
/ | | / |
/ | | / |
/ | | / |
5----------------------6 |
| | | | |
| | o------|---------s
| | / | |
| 0------/--------|------3
| / / | /
| / / | /
| / / | /
| / / | /
| / r | /
|/ |/
1----------------------2
Data that we have available:
coordinates of the nodes (vectors P0 to P7)
coordinates of the point we want to check (lets say Px)
Additionaly we assume the nodes are ordered as sketched above.
My approach/solution so far:
calculate the surface (outward) normal vectors
Use cross products, eg. for the r_pos_normal_vec (pointing out of the plane)
r_pos_normvec = (P2-P1) x (P5-P1)
and for the r_neg_normal_vec
r_neg_normvec = (P4-P0) x (P3-P0)
similarly for the s and t directions
check two opposite corner nodes (I chose node0 and node 6)
For node0
calculate vector from P0 to Px:
P0x = Px - P0
calculate inner prodcut of P0x and surfaces adjacent to node 0
<P0x, r_neg_normal_vec>
<P0x, s_neg_normal_vec>
<P0x, t_neg_normal_vec>
For node1
same scheme as for node 0, whereas P1 instead of P0 and the positive counterparts of the normal vectors are used
Iff all 6 (3 from node0 and 3 from node1) inner products result in negative value -> the point is inside the hexahedron.
Question:
I implemented the functionality described above in my code and ran some tests.
It seems to work, from the math side I am quite confident.
Please discuss my approach, I am happy for any hints/clues/recommendations/bug fixes ...
Is there some way to make this faster?
Alternative solutions?
Note:
To speed up the algorithm a box check can be done first:
Construct a rectangular box around the hexahedron:
Get the min and max values of the node coordinates in each direction.
If the point to check (x) is outside this (larger) box, it cannot be inside the hexahedron.
For any convex polyhedron, establish the implicit equations of the faces (f.i. plane by three points), of the form ax+by+cz+d=0.
When you plug the coordinates of a known point inside the volume (such as the center) in the expression ax+by+cz+d, you will get a set of signs. An arbitrary point is inside if it yields the same signs.
Update:
For maximum performance, you can consider also using an axis-aligned bounding box for quick rejection. This only makes sense if many of the points are outside. Make sur to use a shortcut evaluation so that early rejection can happen.
Note that a rejection test such as X<Xmin is nothing but the above sign test against the plane of equation X-Xmin=0.
I personnally prefer your method, however there also is a way to approach the problem if the hexahedral restricted to parallelepiped. So you can transfer the coordinate of P in the frame $(0; e_1; e_2; e_3)$ to $(P_0, P_0P_1,P_0P_3,P_0P_4)$. We call it $(a,b,c)$, then the point is in that parallelepiped if $a,b,c > 0 a \in [0,1], a+b+c \in [0,1]$.
Because you mentioned that you want to be able to handle arbitrary hexahedrons, I think that your process might be improved if you adjust your s, r, and t normals to account for having slightly warped faces. I would do this by making the following change to r normals (and similar for s and t):
r_pos_normvec = (P6-P1) x (P5-P2)
r_neg_normvec = (P7-P0) x (P4-P3)
This would be important for a case where you shifted node 6 towards node 7 (say 0.9xP6) and had a point at 0.95xP6. Without the warping correction, I believe you would erroneously determine the point as inside the hexahedron.
Here is a python example :
def point_is_in_hexa(point,centers,normals):
vect=[]
prod=[]
for i in range(6):
vect.append(point-centers[i])
vect= np.array(vect)
for i in range(6):
prod.append(np.dot(vect[i],normals[i]))
prod=np.array(prod)
if all(prod <= 0):
is_in_hexa = 1
else:
is_in_hexa = -1
return is_in_hexa
https://github.com/fgomez03/hexacheck

How do I make to creat a circle of radius equals to 1/\sqrt{5}?

The image that I want to do.
\begin{figure}[H]
\centering
\begin{tikzpicture}
\tkzDefPoints{-4/0/A, 4/0/B, 4/4/C, -4/4/D, 0/0/O}
\tkzDrawPolygon(A,B,C,D)
\draw [pattern={hatch[hatch size=10pt, hatch linewidth=.4pt, hatch angle=15]}, pattern color=black] (A) -- (B) -- (C) -- (D) -- cycle;
\tkzDefMidPoint(A,B)
\tkzGetPoint{E}
\tkzDefMidPoint(B,C)
\tkzGetPoint{F}
\tkzDefMidPoint(C,D)
\tkzGetPoint{G}
\tkzDefMidPoint(D,A)
\tkzGetPoint{H}
\tkzFillPolygon(E,F,G,H)
%\tkzDrawCircle Uncomment this part.
%\tkzLabelPoints[auto](A,B,C,D,E,F,G,H)
\end{tikzpicture}
\end{figure}
Left a circle of radius of \dfrac{1}{\sqrt{5}}.

gnuplot: variable values and definitions in plot command

I just stumbled across the following:
According to the gnuplot manual a plot element may contain a definition.
Syntax:
plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>}
Each plot element consists of a definition, a function, or a data source
together with optional properties or modifiers:
plot-element:
{<iteration>}
<definition> | {sampling-range} <function> | <data source>
| keyentry
{axes <axes>} {<title-spec>}
{with <style>}
Check the following example:
For the first graph y=x+1 is plotted because a=1 was defined earlier. As expected.
For the second graph and the first plot command it should be the same but y=2*x+1 is plotted instead (twice).
In the third graph when a=1 is explicitely specified it is plotted as expected.
Why is gnuplot ignoring a=1 for the second graph?
Have I misunderstood something?
Code:
### definitions in plot command
reset session
a = 1
b = 1
f(x) = a*x + b
set yrange[-40:40]
set multiplot layout 1,3
plot f(x)
plot f(x), a=2 f(x), a=3 f(x)
plot a=1 f(x), a=2 f(x), a=3 f(x)
unset multiplot
### end of code
Result:
Your diagnosis is slightly off. In the second panel the first, purple plot is superimposed with the a=3 plot rather than the a=2 plot.
Why? Because gnuplot accumulates all elements of the full plot before actually drawing any of them. This involves making two passes over the command line. One pass to parse and load data from any data sources mentioned (needed for example for autoscaling), then a second pass to evaluate any functions over the range (which might have determined by autoscaling). During the first pass here, a gets set to 2 and then to 3. At the start of the second pass a is still 3 and in the absence of an initial definition to change it that is what is used when f(x) is evaluated.

How is the full adder's carry out term derived?

I'm reading the section of the full adder in Digital Design by Morris Mano and I can't seem to figure out how it got from equation A to equation B.
From a full adder's truth table and k-map using inputs x, y, and z, the carry out term, C, is defined as:
C = xy + xz + yz (equation A)
I could understand the above, but in order to leverage the xor already used by the summation term of x, y, and z, the book redefines C as:
C = z(xy' + x'y) + xy = xy'z + x'yz + xy (equation B)
How are these two equivalent? I've tried to derive one from the other on paper but I'm not able to come up with the steps in between.
Sorry my comment (which I removed) was hastily stated.
Consider the following logic table (I'm using ^ to represent XOR for brevity):
The results of xy + xz + yz are the same as xy + (x ^ y)z because, for the first 6 cases, the value of x + y and x ^ y are the same. For the last two cases where they are different, the xy term being OR'ed in is 1 which makes their difference irrelevant to the final value.

Draw a graph in using latex

I used \includegraphics[height=1.91in,width=5in]{L1F2.jpg} to insert in the latex. but I would like to draw similar picture in latex and add my own text on it similar to what included and change the little in the arrows. What is the best way to do so.
You'll want to \usepackage{tikz}. Examples of graphics made with TikZ and PGF can be found at texample.net/tikz.
A quick example:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[-] (0,0) -- (0,4) node[below left] {$y$}; % Draw the y-axis
\draw[-] (0,0) -- (4,0) node[below left] {$x$}; % Draw the x-axis
\foreach \x in {1,2,3} % Iterate through {1,2,3} to assist the next line
\draw[shift={(\x,0)}] (0,3pt) -- (0,0pt) node[below] {$x_\x$}; % Mark the intervals
\clip (0,0) rectangle (4,4); % Limit the domain and range of the graph
\draw (0,0) plot ({\x},{\x^2}); % Draw a function of \x
\end{tikzpicture}
\end{document}
(I recognize that this is a simple linear regression model, but having just begun college calculus, I don't yet understand how to write this function in gnuplot without knowing the values of $beta_0$ and $beta_1$.)

Resources