CCVertex.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2011 ForzeField Studios S.L
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "math/CCVertex.h"
  24. #include "base/ccMacros.h"
  25. NS_CC_BEGIN
  26. void ccVertexLineToPolygon(Vec2 *points, float stroke, Vec2 *vertices, unsigned int offset, unsigned int nuPoints)
  27. {
  28. nuPoints += offset;
  29. if(nuPoints<=1) return;
  30. stroke *= 0.5f;
  31. unsigned int idx;
  32. unsigned int nuPointsMinus = nuPoints-1;
  33. for(unsigned int i = offset; i<nuPoints; i++)
  34. {
  35. idx = i*2;
  36. Vec2 p1 = points[i];
  37. Vec2 perpVector;
  38. if(i == 0)
  39. perpVector = (p1 - points[i+1]).getNormalized().getPerp();
  40. else if(i == nuPointsMinus)
  41. perpVector = (points[i-1] - p1).getNormalized().getPerp();
  42. else
  43. {
  44. Vec2 p2 = points[i+1];
  45. Vec2 p0 = points[i-1];
  46. Vec2 p2p1 = (p2 - p1).getNormalized();
  47. Vec2 p0p1 = (p0 - p1).getNormalized();
  48. // Calculate angle between vectors
  49. float angle = acosf(p2p1.dot(p0p1));
  50. if(angle < CC_DEGREES_TO_RADIANS(70))
  51. perpVector = p2p1.getMidpoint(p0p1).getNormalized().getPerp();
  52. else if(angle < CC_DEGREES_TO_RADIANS(170))
  53. perpVector = p2p1.getMidpoint(p0p1).getNormalized();
  54. else
  55. perpVector = (p2 - p0).getNormalized().getPerp();
  56. }
  57. perpVector = perpVector * stroke;
  58. vertices[idx].set(p1.x + perpVector.x, p1.y + perpVector.y);
  59. vertices[idx + 1].set(p1.x - perpVector.x, p1.y - perpVector.y);
  60. }
  61. // Validate vertexes
  62. offset = (offset==0) ? 0 : offset-1;
  63. for(unsigned int i = offset; i<nuPointsMinus; i++)
  64. {
  65. idx = i*2;
  66. const unsigned int idx1 = idx+2;
  67. Vec2 p1 = vertices[idx];
  68. Vec2 p2 = vertices[idx+1];
  69. Vec2 p3 = vertices[idx1];
  70. Vec2 p4 = vertices[idx1+1];
  71. float s;
  72. //BOOL fixVertex = !ccpLineIntersect(Vec2(p1.x, p1.y), Vec2(p4.x, p4.y), Vec2(p2.x, p2.y), Vec2(p3.x, p3.y), &s, &t);
  73. bool fixVertex = !ccVertexLineIntersect(p1.x, p1.y, p4.x, p4.y, p2.x, p2.y, p3.x, p3.y, &s);
  74. if(!fixVertex)
  75. if (s<0.0f || s>1.0f)
  76. fixVertex = true;
  77. if(fixVertex)
  78. {
  79. vertices[idx1] = p4;
  80. vertices[idx1+1] = p3;
  81. }
  82. }
  83. }
  84. bool ccVertexLineIntersect(float Ax, float Ay,
  85. float Bx, float By,
  86. float Cx, float Cy,
  87. float Dx, float Dy, float *T)
  88. {
  89. float distAB, theCos, theSin, newX;
  90. // FAIL: Line undefined
  91. if ((Ax==Bx && Ay==By) || (Cx==Dx && Cy==Dy)) return false;
  92. // Translate system to make A the origin
  93. Bx-=Ax; By-=Ay;
  94. Cx-=Ax; Cy-=Ay;
  95. Dx-=Ax; Dy-=Ay;
  96. // Length of segment AB
  97. distAB = sqrtf(Bx*Bx+By*By);
  98. // Rotate the system so that point B is on the positive X axis.
  99. theCos = Bx/distAB;
  100. theSin = By/distAB;
  101. newX = Cx*theCos+Cy*theSin;
  102. Cy = Cy*theCos-Cx*theSin; Cx = newX;
  103. newX = Dx*theCos+Dy*theSin;
  104. Dy = Dy*theCos-Dx*theSin; Dx = newX;
  105. // FAIL: Lines are parallel.
  106. if (Cy == Dy) return false;
  107. // Discover the relative position of the intersection in the line AB
  108. *T = (Dx+(Cx-Dx)*Dy/(Dy-Cy))/distAB;
  109. // Success.
  110. return true;
  111. }
  112. NS_CC_END