Llevat la part del manual del pas de l'equació implícita a l'equació paramètrica...
[apunts-acces-uib-majors-25-anys-matematiques.git] / tikz-comandes.tex
blob5ea26c1febee68a2c0e730a41fe8628fb17be7db
1 % Codi per a plans
2 % De http://tex.stackexchange.com/questions/19972/draw-a-plane-in-space-and-a-coordinate-system-using-tikz
3 \makeatletter
5 % Set some defaults
6 \tikzset{
7 plane max x/.initial=2,
8 plane max y/.initial=2,
9 plane max z/.initial=2
12 \tikzset{plane/.style={fill opacity=0.5}}
14 % Define a plane.
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}
26 % Draw a plane.
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
38 %invalid plane
39 \else % x=0, y=0
40 \filldraw[plane,#1,shift={(0,0,\tsx@plane@scalar/\tsx@plane@zcoeff)}]
41 (0,0,0) --
42 (\tsx@plane@maxx,0,0) --
43 (\tsx@plane@maxx,\tsx@plane@maxy,0) --
44 (0,\tsx@plane@maxy,0) --
45 cycle;
46 \fi
47 \else % x=0, y != 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)}]
50 (0,0,0) --
51 (\tsx@plane@maxx,0,0) --
52 (\tsx@plane@maxx,0,\tsx@plane@maxz) --
53 (0,0,\tsx@plane@maxz) --
54 cycle;
55 \else % x=0
56 \filldraw[plane,#1]
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) --
61 cycle;
62 \fi
63 \fi
64 \else % x!=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)}]
68 (0,0,0) --
69 (0,0,\tsx@plane@maxz) --
70 (0,\tsx@plane@maxy,\tsx@plane@maxz) --
71 (0,\tsx@plane@maxy,0) --
72 cycle;
73 \else % x!=0,y=0,z!=0
74 \filldraw[plane,#1]
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) --
79 cycle;
80 \fi
81 \else % x!=0,y!=0
82 \ifdim\tsx@plane@zcoeff pt=0pt % x!=0,y!=0,z=0
83 \filldraw[plane,#1]
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) --
88 cycle;
89 \else % x!=0,y!=0,z!=0
90 \filldraw[plane,#1]
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) --
94 cycle;
95 \fi
96 \fi
97 \fi
100 % Define a point.
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{
107 \def\tsx@point@x{#2}
108 \def\tsx@point@y{#3}
109 \def\tsx@point@z{#4}
113 % Project a point to a plane.
114 % #1 = name of the new point
115 % #2 = name of old point
116 % #3 = name of plane
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
129 % Calculate point
130 \pgfmathparse{\tsx@point@x - \distance*\tsx@plane@xcoeff}
131 \let\x\pgfmathresult
132 \pgfmathparse{\tsx@point@y - \distance*\tsx@plane@ycoeff}
133 \let\y\pgfmathresult
134 \pgfmathparse{\tsx@point@z - \distance*\tsx@plane@zcoeff}
135 \let\z\pgfmathresult
137 \definePointByXYZ{#1}{\x}{\y}{\z}
139 \makeatother