CCVertex.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. Copyright (c) 2009 Valentin Milea
  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. /**
  24. * converts a line to a polygon
  25. * @param {Float32Array} points
  26. * @param {Number} stroke
  27. * @param {Float32Array} vertices
  28. * @param {Number} offset
  29. * @param {Number} nuPoints
  30. */
  31. cc.vertexLineToPolygon = function (points, stroke, vertices, offset, nuPoints) {
  32. nuPoints += offset;
  33. if (nuPoints <= 1)
  34. return;
  35. stroke *= 0.5;
  36. var idx;
  37. var nuPointsMinus = nuPoints - 1;
  38. for (var i = offset; i < nuPoints; i++) {
  39. idx = i * 2;
  40. var p1 = cc.p(points[i * 2], points[i * 2 + 1]);
  41. var perpVector;
  42. if (i === 0)
  43. perpVector = cc.pPerp(cc.pNormalize(cc.pSub(p1, cc.p(points[(i + 1) * 2], points[(i + 1) * 2 + 1]))));
  44. else if (i === nuPointsMinus)
  45. perpVector = cc.pPerp(cc.pNormalize(cc.pSub(cc.p(points[(i - 1) * 2], points[(i - 1) * 2 + 1]), p1)));
  46. else {
  47. var p0 = cc.p(points[(i - 1) * 2], points[(i - 1) * 2 + 1]);
  48. var p2 = cc.p(points[(i + 1) * 2], points[(i + 1) * 2 + 1]);
  49. var p2p1 = cc.pNormalize(cc.pSub(p2, p1));
  50. var p0p1 = cc.pNormalize(cc.pSub(p0, p1));
  51. // Calculate angle between vectors
  52. var angle = Math.acos(cc.pDot(p2p1, p0p1));
  53. if (angle < cc.degreesToRadians(70))
  54. perpVector = cc.pPerp(cc.pNormalize(cc.pMidpoint(p2p1, p0p1)));
  55. else if (angle < cc.degreesToRadians(170))
  56. perpVector = cc.pNormalize(cc.pMidpoint(p2p1, p0p1));
  57. else
  58. perpVector = cc.pPerp(cc.pNormalize(cc.pSub(p2, p0)));
  59. }
  60. perpVector = cc.pMult(perpVector, stroke);
  61. vertices[idx * 2] = p1.x + perpVector.x;
  62. vertices[idx * 2 + 1] = p1.y + perpVector.y;
  63. vertices[(idx + 1) * 2] = p1.x - perpVector.x;
  64. vertices[(idx + 1) * 2 + 1] = p1.y - perpVector.y;
  65. }
  66. // Validate vertexes
  67. offset = (offset == 0) ? 0 : offset - 1;
  68. for (i = offset; i < nuPointsMinus; i++) {
  69. idx = i * 2;
  70. var idx1 = idx + 2;
  71. var v1 = cc.vertex2(vertices[idx * 2], vertices[idx * 2 + 1]);
  72. var v2 = cc.vertex2(vertices[(idx + 1) * 2], vertices[(idx + 1) * 2 + 1]);
  73. var v3 = cc.vertex2(vertices[idx1 * 2], vertices[idx1 * 2]);
  74. var v4 = cc.vertex2(vertices[(idx1 + 1) * 2], vertices[(idx1 + 1) * 2 + 1]);
  75. //BOOL fixVertex = !ccpLineIntersect(ccp(p1.x, p1.y), ccp(p4.x, p4.y), ccp(p2.x, p2.y), ccp(p3.x, p3.y), &s, &t);
  76. var fixVertexResult = !cc.vertexLineIntersect(v1.x, v1.y, v4.x, v4.y, v2.x, v2.y, v3.x, v3.y);
  77. if (!fixVertexResult.isSuccess)
  78. if (fixVertexResult.value < 0.0 || fixVertexResult.value > 1.0)
  79. fixVertexResult.isSuccess = true;
  80. if (fixVertexResult.isSuccess) {
  81. vertices[idx1 * 2] = v4.x;
  82. vertices[idx1 * 2 + 1] = v4.y;
  83. vertices[(idx1 + 1) * 2] = v3.x;
  84. vertices[(idx1 + 1) * 2 + 1] = v3.y;
  85. }
  86. }
  87. };
  88. /**
  89. * returns whether or not the line intersects
  90. * @param {Number} Ax
  91. * @param {Number} Ay
  92. * @param {Number} Bx
  93. * @param {Number} By
  94. * @param {Number} Cx
  95. * @param {Number} Cy
  96. * @param {Number} Dx
  97. * @param {Number} Dy
  98. * @return {Object}
  99. */
  100. cc.vertexLineIntersect = function (Ax, Ay, Bx, By, Cx, Cy, Dx, Dy) {
  101. var distAB, theCos, theSin, newX;
  102. // FAIL: Line undefined
  103. if ((Ax == Bx && Ay == By) || (Cx == Dx && Cy == Dy))
  104. return {isSuccess:false, value:0};
  105. // Translate system to make A the origin
  106. Bx -= Ax;
  107. By -= Ay;
  108. Cx -= Ax;
  109. Cy -= Ay;
  110. Dx -= Ax;
  111. Dy -= Ay;
  112. // Length of segment AB
  113. distAB = Math.sqrt(Bx * Bx + By * By);
  114. // Rotate the system so that point B is on the positive X axis.
  115. theCos = Bx / distAB;
  116. theSin = By / distAB;
  117. newX = Cx * theCos + Cy * theSin;
  118. Cy = Cy * theCos - Cx * theSin;
  119. Cx = newX;
  120. newX = Dx * theCos + Dy * theSin;
  121. Dy = Dy * theCos - Dx * theSin;
  122. Dx = newX;
  123. // FAIL: Lines are parallel.
  124. if (Cy == Dy) return {isSuccess:false, value:0};
  125. // Discover the relative position of the intersection in the line AB
  126. var t = (Dx + (Cx - Dx) * Dy / (Dy - Cy)) / distAB;
  127. // Success.
  128. return {isSuccess:true, value:t};
  129. };
  130. /**
  131. * returns wheter or not polygon defined by vertex list is clockwise
  132. * @param {Array} verts
  133. * @return {Boolean}
  134. */
  135. cc.vertexListIsClockwise = function(verts) {
  136. for (var i = 0, len = verts.length; i < len; i++) {
  137. var a = verts[i];
  138. var b = verts[(i + 1) % len];
  139. var c = verts[(i + 2) % len];
  140. if (cc.pCross(cc.pSub(b, a), cc.pSub(c, b)) > 0)
  141. return false;
  142. }
  143. return true;
  144. };