2 % De http://tex.stackexchange.com/questions/19972/draw-a-plane-in-space-and-a-coordinate-system-using-tikz
7 plane max x/.initial=
2,
8 plane max y/.initial=
2,
12 \tikzset{plane/.style=
{fill opacity=
0.5}}
15 % #1 = name of the plane
16 % #2*x + #3*y + #4*z = #5 is the equation of the plane
17 \newcommand*
\definePlaneByEquation[5]{
18 \expandafter\gdef\csname tsx@plane@
#1\endcsname{
19 \def\tsx@plane@xcoeff
{#2}
20 \def\tsx@plane@ycoeff
{#3}
21 \def\tsx@plane@zcoeff
{#4}
22 \def\tsx@plane@scalar
{#5}
27 % The optional first argument is passed as options to TikZ.
28 % The mandatory second argument is the name of the plane.
29 \newcommand\drawPlane[2][]{
30 \tikzset{plane max x/.get=
\tsx@plane@maxx
}
31 \tikzset{plane max y/.get=
\tsx@plane@maxy
}
32 \tikzset{plane max z/.get=
\tsx@plane@maxz
}
33 \csname tsx@plane@
#2\endcsname
35 \ifdim\tsx@plane@xcoeff pt=
0pt
36 \ifdim\tsx@plane@ycoeff pt=
0pt
37 \ifdim\tsx@plane@zcoeff pt=
0pt
40 \filldraw[plane,
#1,shift=
{(
0,
0,
\tsx@plane@scalar/
\tsx@plane@zcoeff)
}]
42 (
\tsx@plane@maxx,
0,
0) --
43 (
\tsx@plane@maxx,
\tsx@plane@maxy,
0) --
44 (
0,
\tsx@plane@maxy,
0) --
48 \ifdim\tsx@plane@zcoeff pt=
0pt
% x=0, z=0
49 \filldraw[plane,
#1,shift=
{(
0,
\tsx@plane@scalar/
\tsx@plane@ycoeff,
0)
}]
51 (
\tsx@plane@maxx,
0,
0) --
52 (
\tsx@plane@maxx,
0,
\tsx@plane@maxz) --
53 (
0,
0,
\tsx@plane@maxz) --
57 (
0,
\tsx@plane@scalar/
\tsx@plane@ycoeff,
0) --
58 (
0,
0,
\tsx@plane@scalar/
\tsx@plane@zcoeff) --
59 (
\tsx@plane@maxx,
0,
\tsx@plane@scalar/
\tsx@plane@zcoeff) --
60 (
\tsx@plane@maxx,
\tsx@plane@scalar/
\tsx@plane@ycoeff,
0) --
65 \ifdim\tsx@plane@ycoeff pt=
0pt
% x!=0,y=0
66 \ifdim\tsx@plane@zcoeff pt=
0pt
% x!=0,y=0,z=0
67 \filldraw[plane,
#1,shift=
{(
\tsx@plane@scalar/
\tsx@plane@xcoeff,
0,
0)
}]
69 (
0,
0,
\tsx@plane@maxz) --
70 (
0,
\tsx@plane@maxy,
\tsx@plane@maxz) --
71 (
0,
\tsx@plane@maxy,
0) --
75 (
\tsx@plane@scalar/
\tsx@plane@xcoeff,
0) --
76 (
0,
0,
\tsx@plane@scalar/
\tsx@plane@zcoeff) --
77 (
0,
\tsx@plane@maxy,
\tsx@plane@scalar/
\tsx@plane@zcoeff) --
78 (
\tsx@plane@scalar/
\tsx@plane@xcoeff,
\tsx@plane@maxy,
0) --
82 \ifdim\tsx@plane@zcoeff pt=
0pt
% x!=0,y!=0,z=0
84 (
\tsx@plane@scalar/
\tsx@plane@xcoeff,
0) --
85 (
0,
\tsx@plane@scalar/
\tsx@plane@ycoeff,
0) --
86 (
0,
\tsx@plane@scalar/
\tsx@plane@ycoeff,
\tsx@plane@maxz) --
87 (
\tsx@plane@scalar/
\tsx@plane@xcoeff,
0,
\tsx@plane@maxz) --
89 \else % x!=0,y!=0,z!=0
91 (
\tsx@plane@scalar/
\tsx@plane@xcoeff,
0,
0) --
92 (
0,
\tsx@plane@scalar/
\tsx@plane@ycoeff,
0) --
93 (
0,
0,
\tsx@plane@scalar/
\tsx@plane@zcoeff) --
101 % #1 = name of the point
102 % (#2,#3,#4) is the location.
103 % Also creates a coordinate node of name #1 at the location.
104 \newcommand\definePointByXYZ[4]{
105 \coordinate (
#1) at (
#2,
#3,
#4);
106 \expandafter\gdef\csname tsx@point@
#1\endcsname{
113 % Project a point to a plane.
114 % #1 = name of the new point
115 % #2 = name of old point
117 \newcommand\projectPointToPlane[3]{{
118 \csname tsx@point@
#2\endcsname
119 \csname tsx@plane@
#3\endcsname
121 % square of norm of the normal vector
122 \pgfmathparse{\tsx@plane@xcoeff*
\tsx@plane@xcoeff +
\tsx@plane@ycoeff*
\tsx@plane@ycoeff +
\tsx@plane@zcoeff*
\tsx@plane@zcoeff
}
123 \let\nnormsq\pgfmathresult
125 % Calculate distance in terms of the (non-normalized) normal vector
126 \pgfmathparse{(
\tsx@point@x*
\tsx@plane@xcoeff +
\tsx@point@y*
\tsx@plane@ycoeff +
\tsx@point@z*
\tsx@plane@zcoeff -
\tsx@plane@scalar) /
\nnormsq}
127 \let\distance\pgfmathresult
130 \pgfmathparse{\tsx@point@x -
\distance*
\tsx@plane@xcoeff
}
132 \pgfmathparse{\tsx@point@y -
\distance*
\tsx@plane@ycoeff
}
134 \pgfmathparse{\tsx@point@z -
\distance*
\tsx@plane@zcoeff
}
137 \definePointByXYZ{#1}{\x}{\y}{\z}