Apunts de Matemàtiques per a l'accés a la UIB per a majors de 25 anys
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tikz-comandes.tex 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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
  4. % Set some defaults
  5. \tikzset{
  6. plane max x/.initial=2,
  7. plane max y/.initial=2,
  8. plane max z/.initial=2
  9. }
  10. \tikzset{plane/.style={fill opacity=0.5}}
  11. % Define a plane.
  12. % #1 = name of the plane
  13. % #2*x + #3*y + #4*z = #5 is the equation of the plane
  14. \newcommand*\definePlaneByEquation[5]{
  15. \expandafter\gdef\csname tsx@plane@#1\endcsname{
  16. \def\tsx@plane@xcoeff{#2}
  17. \def\tsx@plane@ycoeff{#3}
  18. \def\tsx@plane@zcoeff{#4}
  19. \def\tsx@plane@scalar{#5}
  20. }
  21. }
  22. % Draw a plane.
  23. % The optional first argument is passed as options to TikZ.
  24. % The mandatory second argument is the name of the plane.
  25. \newcommand\drawPlane[2][]{
  26. \tikzset{plane max x/.get=\tsx@plane@maxx}
  27. \tikzset{plane max y/.get=\tsx@plane@maxy}
  28. \tikzset{plane max z/.get=\tsx@plane@maxz}
  29. \csname tsx@plane@#2\endcsname
  30. \ifdim\tsx@plane@xcoeff pt=0pt
  31. \ifdim\tsx@plane@ycoeff pt=0pt
  32. \ifdim\tsx@plane@zcoeff pt=0pt
  33. %invalid plane
  34. \else % x=0, y=0
  35. \filldraw[plane,#1,shift={(0,0,\tsx@plane@scalar/\tsx@plane@zcoeff)}]
  36. (0,0,0) --
  37. (\tsx@plane@maxx,0,0) --
  38. (\tsx@plane@maxx,\tsx@plane@maxy,0) --
  39. (0,\tsx@plane@maxy,0) --
  40. cycle;
  41. \fi
  42. \else % x=0, y != 0
  43. \ifdim\tsx@plane@zcoeff pt=0pt % x=0, z=0
  44. \filldraw[plane,#1,shift={(0,\tsx@plane@scalar/\tsx@plane@ycoeff,0)}]
  45. (0,0,0) --
  46. (\tsx@plane@maxx,0,0) --
  47. (\tsx@plane@maxx,0,\tsx@plane@maxz) --
  48. (0,0,\tsx@plane@maxz) --
  49. cycle;
  50. \else % x=0
  51. \filldraw[plane,#1]
  52. (0,\tsx@plane@scalar/\tsx@plane@ycoeff,0) --
  53. (0,0,\tsx@plane@scalar/\tsx@plane@zcoeff) --
  54. (\tsx@plane@maxx,0,\tsx@plane@scalar/\tsx@plane@zcoeff) --
  55. (\tsx@plane@maxx,\tsx@plane@scalar/\tsx@plane@ycoeff,0) --
  56. cycle;
  57. \fi
  58. \fi
  59. \else % x!=0
  60. \ifdim\tsx@plane@ycoeff pt=0pt % x!=0,y=0
  61. \ifdim\tsx@plane@zcoeff pt=0pt % x!=0,y=0,z=0
  62. \filldraw[plane,#1,shift={(\tsx@plane@scalar/\tsx@plane@xcoeff,0,0)}]
  63. (0,0,0) --
  64. (0,0,\tsx@plane@maxz) --
  65. (0,\tsx@plane@maxy,\tsx@plane@maxz) --
  66. (0,\tsx@plane@maxy,0) --
  67. cycle;
  68. \else % x!=0,y=0,z!=0
  69. \filldraw[plane,#1]
  70. (\tsx@plane@scalar/\tsx@plane@xcoeff,0) --
  71. (0,0,\tsx@plane@scalar/\tsx@plane@zcoeff) --
  72. (0,\tsx@plane@maxy,\tsx@plane@scalar/\tsx@plane@zcoeff) --
  73. (\tsx@plane@scalar/\tsx@plane@xcoeff,\tsx@plane@maxy,0) --
  74. cycle;
  75. \fi
  76. \else % x!=0,y!=0
  77. \ifdim\tsx@plane@zcoeff pt=0pt % x!=0,y!=0,z=0
  78. \filldraw[plane,#1]
  79. (\tsx@plane@scalar/\tsx@plane@xcoeff,0) --
  80. (0,\tsx@plane@scalar/\tsx@plane@ycoeff,0) --
  81. (0,\tsx@plane@scalar/\tsx@plane@ycoeff,\tsx@plane@maxz) --
  82. (\tsx@plane@scalar/\tsx@plane@xcoeff,0,\tsx@plane@maxz) --
  83. cycle;
  84. \else % x!=0,y!=0,z!=0
  85. \filldraw[plane,#1]
  86. (\tsx@plane@scalar/\tsx@plane@xcoeff,0,0) --
  87. (0,\tsx@plane@scalar/\tsx@plane@ycoeff,0) --
  88. (0,0,\tsx@plane@scalar/\tsx@plane@zcoeff) --
  89. cycle;
  90. \fi
  91. \fi
  92. \fi
  93. }
  94. % Define a point.
  95. % #1 = name of the point
  96. % (#2,#3,#4) is the location.
  97. % Also creates a coordinate node of name #1 at the location.
  98. \newcommand\definePointByXYZ[4]{
  99. \coordinate (#1) at (#2,#3,#4);
  100. \expandafter\gdef\csname tsx@point@#1\endcsname{
  101. \def\tsx@point@x{#2}
  102. \def\tsx@point@y{#3}
  103. \def\tsx@point@z{#4}
  104. }
  105. }
  106. % Project a point to a plane.
  107. % #1 = name of the new point
  108. % #2 = name of old point
  109. % #3 = name of plane
  110. \newcommand\projectPointToPlane[3]{{
  111. \csname tsx@point@#2\endcsname
  112. \csname tsx@plane@#3\endcsname
  113. % square of norm of the normal vector
  114. \pgfmathparse{\tsx@plane@xcoeff*\tsx@plane@xcoeff + \tsx@plane@ycoeff*\tsx@plane@ycoeff + \tsx@plane@zcoeff*\tsx@plane@zcoeff}
  115. \let\nnormsq\pgfmathresult
  116. % Calculate distance in terms of the (non-normalized) normal vector
  117. \pgfmathparse{(\tsx@point@x*\tsx@plane@xcoeff + \tsx@point@y*\tsx@plane@ycoeff + \tsx@point@z*\tsx@plane@zcoeff - \tsx@plane@scalar) / \nnormsq}
  118. \let\distance\pgfmathresult
  119. % Calculate point
  120. \pgfmathparse{\tsx@point@x - \distance*\tsx@plane@xcoeff}
  121. \let\x\pgfmathresult
  122. \pgfmathparse{\tsx@point@y - \distance*\tsx@plane@ycoeff}
  123. \let\y\pgfmathresult
  124. \pgfmathparse{\tsx@point@z - \distance*\tsx@plane@zcoeff}
  125. \let\z\pgfmathresult
  126. \definePointByXYZ{#1}{\x}{\y}{\z}
  127. }}
  128. \makeatother