barycentric.tex 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. \documentclass{article}
  2. \usepackage{amsmath}
  3. \usepackage{tikz}
  4. \begin{document}
  5. \section{Barycentric Coordinates}
  6. Barycentric coordinates enable you to evenly interpolate between three values among the verticies of a triangle.
  7. \def\xa{0} \def\ya{0}
  8. \def\xb{1} \def\yb{2}
  9. \def\xc{3} \def\yc{-1}
  10. \def\xp{1} \def\yp{0.25}
  11. \begin{center}
  12. \begin{tikzpicture}
  13. \draw[gray, thick] (\xa,\ya) -- (\xb,\yb);
  14. \draw[gray, thick] (\xb,\yb) -- (\xc,\yc);
  15. \draw[gray, thick] (\xc,\yc) -- (\xa,\ya);
  16. \draw[gray, thick, dotted] (\xa,\ya) -- (\xp,\yp);
  17. \draw[gray, thick, dotted] (\xb,\yb) -- (\xp,\yp);
  18. \draw[gray, thick, dotted] (\xc,\yc) -- (\xp,\yp);
  19. \filldraw[black] (\xa,\ya) circle (2pt) node[anchor=west]{$v_1$};
  20. \filldraw[black] (\xb,\yb) circle (2pt) node[anchor=west]{$v_2$};
  21. \filldraw[black] (\xc,\yc) circle (2pt) node[anchor=west]{$v_3$};
  22. \filldraw[black] (\xp,\yp) circle (2pt) node[anchor=west]{$p$};
  23. \end{tikzpicture}
  24. \end{center}
  25. \begin{align}
  26. U &= (u_1, u_2, u_3) \\
  27. v_1 &= (x_1, y_1) \\
  28. v_2 &= (x_2, y_2) \\
  29. v_3 &= (x_3, y_3) \\
  30. p &= (x_p, y_p)
  31. \end{align}
  32. The Barycentric coordinates can be defined in terms of the following relationships:
  33. \begin{align}
  34. \begin{cases}
  35. & u_1 + u_2 + u_3 = 1 \\
  36. & u_1x_1 + u_2x_2 + u_3x_3 = x_p \\
  37. & u_1y_1 + u_2y_2 + u_3y_3 = y_p
  38. \end{cases}
  39. \end{align}
  40. Let's reduce the amount of variables in these equations:
  41. \begin{align}
  42. & u_3 = 1 - u_1 - u_2 \\
  43. & \begin{cases}
  44. u_1(x_1 - x_3) + u_2(x_2 - x_3) &= x_p - x_3 \\
  45. u_1(y_1 - y_3) + u_2(y_2 - y_3) &= y_p - y_3 \\
  46. \end{cases}
  47. \end{align}
  48. Now we can turn the system of equations into matrix form:
  49. \begin{align}
  50. & T =
  51. \begin{bmatrix}
  52. x_1 - x_3 & x_2 - x_3 \\
  53. y_1 - y_3 & y_2 - y_3 \\
  54. \end{bmatrix} \\
  55. & U = \begin{bmatrix}
  56. u1 \\ u2 \\
  57. \end{bmatrix}\\
  58. & R = \begin{bmatrix}
  59. x_p - x_3 \\
  60. y_p - y_3 \\
  61. \end{bmatrix} \\
  62. & T \cdot U = R
  63. \end{align}
  64. So the solution is
  65. \begin{align}
  66. U = T^{-1} \cdot R
  67. \end{align}
  68. The main effort goes towards finding $T^{-1}$
  69. \begin{align}
  70. & T^{-1} = \frac{adj(T)}{det(T)} \\
  71. & det(T) = (x_1 - x_3)(y_2 - y_3) - (x_2 - x_3)(y_1 - y_3) \\
  72. & adj(T) = \begin{bmatrix}
  73. y_2 - y_3 & x_3 - x_2 \\
  74. y_3 - y_1 & x_1 - x_3 \\
  75. \end{bmatrix} \\
  76. & T^{-1} = \frac{1}{det(T)} \cdot \begin{bmatrix}
  77. y_2 - y_3 & x_3 - x_2 \\
  78. y_3 - y_1 & x_1 - x_3 \\
  79. \end{bmatrix} \\
  80. & T^{-1}\cdot R = \frac{1}{det(T)} \cdot \begin{bmatrix}
  81. (y_2 - y_3)(x_p - x_3) + (x_3 - x_2)(y_p - y_3) \\
  82. (y_3 - y_1)(x_p - x_3) + (x_1 - x_3)(y_p - y_3) \\
  83. \end{bmatrix}
  84. \end{align}
  85. And the final formula you need to find $(u_1, u_2, u_3)$ given the points $v_1, v_2, v_3, p$~is
  86. \begin{align}
  87. u_1 &= \frac{(y_2 - y_3)(x_p - x_3) + (x_3 - x_2)(y_p - y_3)}{(x_1 - x_3)(y_2 - y_3) - (x_2 - x_3)(y_1 - y_3)} \\
  88. u_2 &= \frac{(y_3 - y_1)(x_p - x_3) + (x_1 - x_3)(y_p - y_3)}{(x_1 - x_3)(y_2 - y_3) - (x_2 - x_3)(y_1 - y_3)} \\
  89. u_3 &= 1 - u_2 - u_1
  90. \end{align}
  91. \end{document}