Matrix.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System;
  24. using System.Collections.Generic;
  25. namespace DragonBones
  26. {
  27. /// <summary>
  28. /// - 2D Transform matrix.
  29. /// </summary>
  30. /// <version>DragonBones 3.0</version>
  31. /// <language>en_US</language>
  32. /// <summary>
  33. /// - 2D 转换矩阵。
  34. /// </summary>
  35. /// <version>DragonBones 3.0</version>
  36. /// <language>zh_CN</language>
  37. public class Matrix
  38. {
  39. /// <summary>
  40. /// - The value that affects the positioning of pixels along the x axis when scaling or rotating an image.
  41. /// </summary>
  42. /// <default>1.0</default>
  43. /// <version>DragonBones 3.0</version>
  44. /// <language>en_US</language>
  45. /// <summary>
  46. /// - 缩放或旋转图像时影响像素沿 x 轴定位的值。
  47. /// </summary>
  48. /// <default>1.0</default>
  49. /// <version>DragonBones 3.0</version>
  50. /// <language>zh_CN</language>
  51. public float a = 1.0f;
  52. /// <summary>
  53. /// - The value that affects the positioning of pixels along the y axis when rotating or skewing an image.
  54. /// </summary>
  55. /// <default>0.0</default>
  56. /// <version>DragonBones 3.0</version>
  57. /// <language>en_US</language>
  58. /// <summary>
  59. /// - 旋转或倾斜图像时影响像素沿 y 轴定位的值。
  60. /// </summary>
  61. /// <default>0.0</default>
  62. /// <version>DragonBones 3.0</version>
  63. /// <language>zh_CN</language>
  64. public float b = 0.0f;
  65. /// <summary>
  66. /// - The value that affects the positioning of pixels along the x axis when rotating or skewing an image.
  67. /// </summary>
  68. /// <default>0.0</default>
  69. /// <version>DragonBones 3.0</version>
  70. /// <language>en_US</language>
  71. /// <summary>
  72. /// - 旋转或倾斜图像时影响像素沿 x 轴定位的值。
  73. /// </summary>
  74. /// <default>0.0</default>
  75. /// <version>DragonBones 3.0</version>
  76. /// <language>zh_CN</language>
  77. public float c = 0.0f;
  78. /// <summary>
  79. /// - The value that affects the positioning of pixels along the y axis when scaling or rotating an image.
  80. /// </summary>
  81. /// <default>1.0</default>
  82. /// <version>DragonBones 3.0</version>
  83. /// <language>en_US</language>
  84. /// <summary>
  85. /// - 缩放或旋转图像时影响像素沿 y 轴定位的值。
  86. /// </summary>
  87. /// <default>1.0</default>
  88. /// <version>DragonBones 3.0</version>
  89. /// <language>zh_CN</language>
  90. public float d = 1.0f;
  91. /// <summary>
  92. /// - The distance by which to translate each point along the x axis.
  93. /// </summary>
  94. /// <default>0.0</default>
  95. /// <version>DragonBones 3.0</version>
  96. /// <language>en_US</language>
  97. /// <summary>
  98. /// - 沿 x 轴平移每个点的距离。
  99. /// </summary>
  100. /// <default>0.0</default>
  101. /// <version>DragonBones 3.0</version>
  102. /// <language>zh_CN</language>
  103. public float tx = 0.0f;
  104. /// <summary>
  105. /// - The distance by which to translate each point along the y axis.
  106. /// </summary>
  107. /// <default>0.0</default>
  108. /// <version>DragonBones 3.0</version>
  109. /// <language>en_US</language>
  110. /// <summary>
  111. /// - 沿 y 轴平移每个点的距离。
  112. /// </summary>
  113. /// <default>0.0</default>
  114. /// <version>DragonBones 3.0</version>
  115. /// <language>zh_CN</language>
  116. public float ty = 0.0f;
  117. /// <private/>
  118. public Matrix()
  119. {
  120. }
  121. public override string ToString()
  122. {
  123. return "[object DragonBones.Matrix] a:" + a + " b:" + b + " c:" + c + " d:" + d + " tx:" + tx + " ty:" + ty;
  124. }
  125. /// <private/>
  126. public Matrix CopyFrom(Matrix value)
  127. {
  128. this.a = value.a;
  129. this.b = value.b;
  130. this.c = value.c;
  131. this.d = value.d;
  132. this.tx = value.tx;
  133. this.ty = value.ty;
  134. return this;
  135. }
  136. /// <private/>
  137. public Matrix CopyFromArray(List<float> value, int offset = 0)
  138. {
  139. this.a = value[offset];
  140. this.b = value[offset + 1];
  141. this.c = value[offset + 2];
  142. this.d = value[offset + 3];
  143. this.tx = value[offset + 4];
  144. this.ty = value[offset + 5];
  145. return this;
  146. }
  147. /// <summary>
  148. /// - Convert to unit matrix.
  149. /// The resulting matrix has the following properties: a=1, b=0, c=0, d=1, tx=0, ty=0.
  150. /// </summary>
  151. /// <version>DragonBones 3.0</version>
  152. /// <language>en_US</language>
  153. /// <summary>
  154. /// - 转换为单位矩阵。
  155. /// 该矩阵具有以下属性:a=1、b=0、c=0、d=1、tx=0、ty=0。
  156. /// </summary>
  157. /// <version>DragonBones 3.0</version>
  158. /// <language>zh_CN</language>
  159. public Matrix Identity()
  160. {
  161. this.a = this.d = 1.0f;
  162. this.b = this.c = 0.0f;
  163. this.tx = this.ty = 0.0f;
  164. return this;
  165. }
  166. /// <summary>
  167. /// - Multiplies the current matrix with another matrix.
  168. /// </summary>
  169. /// <param name="value">- The matrix that needs to be multiplied.</param>
  170. /// <version>DragonBones 3.0</version>
  171. /// <language>en_US</language>
  172. /// <summary>
  173. /// - 将当前矩阵与另一个矩阵相乘。
  174. /// </summary>
  175. /// <param name="value">- 需要相乘的矩阵。</param>
  176. /// <version>DragonBones 3.0</version>
  177. /// <language>zh_CN</language>
  178. public Matrix Concat(Matrix value)
  179. {
  180. var aA = this.a * value.a;
  181. var bA = 0.0f;
  182. var cA = 0.0f;
  183. var dA = this.d * value.d;
  184. var txA = this.tx * value.a + value.tx;
  185. var tyA = this.ty * value.d + value.ty;
  186. if (this.b != 0.0f || this.c != 0.0f)
  187. {
  188. aA += this.b * value.c;
  189. bA += this.b * value.d;
  190. cA += this.c * value.a;
  191. dA += this.c * value.b;
  192. }
  193. if (value.b != 0.0f || value.c != 0.0f)
  194. {
  195. bA += this.a * value.b;
  196. cA += this.d * value.c;
  197. txA += this.ty * value.c;
  198. tyA += this.tx * value.b;
  199. }
  200. this.a = aA;
  201. this.b = bA;
  202. this.c = cA;
  203. this.d = dA;
  204. this.tx = txA;
  205. this.ty = tyA;
  206. return this;
  207. }
  208. /// <summary>
  209. /// - Convert to inverse matrix.
  210. /// </summary>
  211. /// <version>DragonBones 3.0</version>
  212. /// <language>en_US</language>
  213. /// <summary>
  214. /// - 转换为逆矩阵。
  215. /// </summary>
  216. /// <version>DragonBones 3.0</version>
  217. /// <language>zh_CN</language>
  218. public Matrix Invert()
  219. {
  220. var aA = this.a;
  221. var bA = this.b;
  222. var cA = this.c;
  223. var dA = this.d;
  224. var txA = this.tx;
  225. var tyA = this.ty;
  226. if (bA == 0.0f && cA == 0.0f)
  227. {
  228. this.b = this.c = 0.0f;
  229. if (aA == 0.0f || dA == 0.0f)
  230. {
  231. this.a = this.b = this.tx = this.ty = 0.0f;
  232. }
  233. else
  234. {
  235. aA = this.a = 1.0f / aA;
  236. dA = this.d = 1.0f / dA;
  237. this.tx = -aA * txA;
  238. this.ty = -dA * tyA;
  239. }
  240. return this;
  241. }
  242. var determinant = aA * dA - bA * cA;
  243. if (determinant == 0.0f)
  244. {
  245. this.a = this.d = 1.0f;
  246. this.b = this.c = 0.0f;
  247. this.tx = this.ty = 0.0f;
  248. return this;
  249. }
  250. determinant = 1.0f / determinant;
  251. var k = this.a = dA * determinant;
  252. bA = this.b = -bA * determinant;
  253. cA = this.c = -cA * determinant;
  254. dA = this.d = aA * determinant;
  255. this.tx = -(k * txA + cA * tyA);
  256. this.ty = -(bA * txA + dA * tyA);
  257. return this;
  258. }
  259. /// <summary>
  260. /// - Apply a matrix transformation to a specific point.
  261. /// </summary>
  262. /// <param name="x">- X coordinate.</param>
  263. /// <param name="y">- Y coordinate.</param>
  264. /// <param name="result">- The point after the transformation is applied.</param>
  265. /// <param name="delta">- Whether to ignore tx, ty's conversion to point.</param>
  266. /// <version>DragonBones 3.0</version>
  267. /// <language>en_US</language>
  268. /// <summary>
  269. /// - 将矩阵转换应用于特定点。
  270. /// </summary>
  271. /// <param name="x">- 横坐标。</param>
  272. /// <param name="y">- 纵坐标。</param>
  273. /// <param name="result">- 应用转换之后的点。</param>
  274. /// <param name="delta">- 是否忽略 tx,ty 对点的转换。</param>
  275. /// <version>DragonBones 3.0</version>
  276. /// <language>zh_CN</language>
  277. public void TransformPoint(float x, float y, Point result, bool delta = false)
  278. {
  279. result.x = this.a * x + this.c * y;
  280. result.y = this.b * x + this.d * y;
  281. if (!delta)
  282. {
  283. result.x += this.tx;
  284. result.y += this.ty;
  285. }
  286. }
  287. /// <private/>
  288. public void TransformRectangle(Rectangle rectangle, bool delta = false)
  289. {
  290. var a = this.a;
  291. var b = this.b;
  292. var c = this.c;
  293. var d = this.d;
  294. var tx = delta ? 0.0f : this.tx;
  295. var ty = delta ? 0.0f : this.ty;
  296. var x = rectangle.x;
  297. var y = rectangle.y;
  298. var xMax = x + rectangle.width;
  299. var yMax = y + rectangle.height;
  300. var x0 = a * x + c * y + tx;
  301. var y0 = b * x + d * y + ty;
  302. var x1 = a * xMax + c * y + tx;
  303. var y1 = b * xMax + d * y + ty;
  304. var x2 = a * xMax + c * yMax + tx;
  305. var y2 = b * xMax + d * yMax + ty;
  306. var x3 = a * x + c * yMax + tx;
  307. var y3 = b * x + d * yMax + ty;
  308. var tmp = 0.0f;
  309. if (x0 > x1)
  310. {
  311. tmp = x0;
  312. x0 = x1;
  313. x1 = tmp;
  314. }
  315. if (x2 > x3)
  316. {
  317. tmp = x2;
  318. x2 = x3;
  319. x3 = tmp;
  320. }
  321. rectangle.x = (float)Math.Floor(x0 < x2 ? x0 : x2);
  322. rectangle.width = (float)Math.Ceiling((x1 > x3 ? x1 : x3) - rectangle.x);
  323. if (y0 > y1)
  324. {
  325. tmp = y0;
  326. y0 = y1;
  327. y1 = tmp;
  328. }
  329. if (y2 > y3)
  330. {
  331. tmp = y2;
  332. y2 = y3;
  333. y3 = tmp;
  334. }
  335. rectangle.y = (float)Math.Floor(y0 < y2 ? y0 : y2);
  336. rectangle.height = (float)Math.Ceiling((y1 > y3 ? y1 : y3) - rectangle.y);
  337. }
  338. }
  339. }