CCAffineTransform.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "math/CCAffineTransform.h"
  23. #include <algorithm>
  24. #include <math.h>
  25. using namespace std;
  26. NS_CC_BEGIN
  27. AffineTransform __CCAffineTransformMake(float a, float b, float c, float d, float tx, float ty)
  28. {
  29. AffineTransform t;
  30. t.a = a; t.b = b; t.c = c; t.d = d; t.tx = tx; t.ty = ty;
  31. return t;
  32. }
  33. Vec2 __CCPointApplyAffineTransform(const Vec2& point, const AffineTransform& t)
  34. {
  35. Vec2 p;
  36. p.x = (float)((double)t.a * point.x + (double)t.c * point.y + t.tx);
  37. p.y = (float)((double)t.b * point.x + (double)t.d * point.y + t.ty);
  38. return p;
  39. }
  40. Vec2 PointApplyTransform(const Vec2& point, const Mat4& transform)
  41. {
  42. Vec3 vec(point.x, point.y, 0);
  43. transform.transformPoint(&vec);
  44. return Vec2(vec.x, vec.y);
  45. }
  46. Size __CCSizeApplyAffineTransform(const Size& size, const AffineTransform& t)
  47. {
  48. Size s;
  49. s.width = (float)((double)t.a * size.width + (double)t.c * size.height);
  50. s.height = (float)((double)t.b * size.width + (double)t.d * size.height);
  51. return s;
  52. }
  53. AffineTransform AffineTransformMakeIdentity()
  54. {
  55. return __CCAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
  56. }
  57. extern const AffineTransform AffineTransformIdentity = AffineTransformMakeIdentity();
  58. const AffineTransform AffineTransform::IDENTITY = AffineTransformMakeIdentity();
  59. Rect RectApplyAffineTransform(const Rect& rect, const AffineTransform& anAffineTransform)
  60. {
  61. float top = rect.getMinY();
  62. float left = rect.getMinX();
  63. float right = rect.getMaxX();
  64. float bottom = rect.getMaxY();
  65. Vec2 topLeft = PointApplyAffineTransform(Vec2(left, top), anAffineTransform);
  66. Vec2 topRight = PointApplyAffineTransform(Vec2(right, top), anAffineTransform);
  67. Vec2 bottomLeft = PointApplyAffineTransform(Vec2(left, bottom), anAffineTransform);
  68. Vec2 bottomRight = PointApplyAffineTransform(Vec2(right, bottom), anAffineTransform);
  69. float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
  70. float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
  71. float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
  72. float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
  73. return Rect(minX, minY, (maxX - minX), (maxY - minY));
  74. }
  75. Rect RectApplyTransform(const Rect& rect, const Mat4& transform)
  76. {
  77. float top = rect.getMinY();
  78. float left = rect.getMinX();
  79. float right = rect.getMaxX();
  80. float bottom = rect.getMaxY();
  81. Vec3 topLeft(left, top, 0);
  82. Vec3 topRight(right, top, 0);
  83. Vec3 bottomLeft(left, bottom, 0);
  84. Vec3 bottomRight(right, bottom, 0);
  85. transform.transformPoint(&topLeft);
  86. transform.transformPoint(&topRight);
  87. transform.transformPoint(&bottomLeft);
  88. transform.transformPoint(&bottomRight);
  89. float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
  90. float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
  91. float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
  92. float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
  93. return Rect(minX, minY, (maxX - minX), (maxY - minY));
  94. }
  95. AffineTransform AffineTransformTranslate(const AffineTransform& t, float tx, float ty)
  96. {
  97. return __CCAffineTransformMake(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty);
  98. }
  99. AffineTransform AffineTransformScale(const AffineTransform& t, float sx, float sy)
  100. {
  101. return __CCAffineTransformMake(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty);
  102. }
  103. AffineTransform AffineTransformRotate(const AffineTransform& t, float anAngle)
  104. {
  105. float sine = sinf(anAngle);
  106. float cosine = cosf(anAngle);
  107. return __CCAffineTransformMake( t.a * cosine + t.c * sine,
  108. t.b * cosine + t.d * sine,
  109. t.c * cosine - t.a * sine,
  110. t.d * cosine - t.b * sine,
  111. t.tx,
  112. t.ty);
  113. }
  114. /* Concatenate `t2' to `t1' and return the result:
  115. t' = t1 * t2 */
  116. AffineTransform AffineTransformConcat(const AffineTransform& t1, const AffineTransform& t2)
  117. {
  118. return __CCAffineTransformMake( t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b
  119. t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d
  120. t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx
  121. t1.tx * t2.b + t1.ty * t2.d + t2.ty); //ty
  122. }
  123. Mat4 TransformConcat(const Mat4& t1, const Mat4& t2)
  124. {
  125. return t1 * t2;
  126. }
  127. /* Return true if `t1' and `t2' are equal, false otherwise. */
  128. bool AffineTransformEqualToTransform(const AffineTransform& t1, const AffineTransform& t2)
  129. {
  130. return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty);
  131. }
  132. AffineTransform AffineTransformInvert(const AffineTransform& t)
  133. {
  134. float determinant = 1 / (t.a * t.d - t.b * t.c);
  135. return __CCAffineTransformMake(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a,
  136. determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty) );
  137. }
  138. NS_CC_END