CCVertex.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2009 Valentin Milea
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * converts a line to a polygon
  23. * @param {Float32Array} points
  24. * @param {Number} stroke
  25. * @param {Float32Array} vertices
  26. * @param {Number} offset
  27. * @param {Number} nuPoints
  28. */
  29. cc.vertexLineToPolygon = function (points, stroke, vertices, offset, nuPoints) {
  30. nuPoints += offset;
  31. if (nuPoints <= 1)
  32. return;
  33. stroke *= 0.5;
  34. var idx;
  35. var nuPointsMinus = nuPoints - 1;
  36. for (var i = offset; i < nuPoints; i++) {
  37. idx = i * 2;
  38. var p1 = cc.p(points[i * 2], points[i * 2 + 1]);
  39. var perpVector;
  40. if (i === 0)
  41. perpVector = cc.pPerp(cc.pNormalize(cc.pSub(p1, cc.p(points[(i + 1) * 2], points[(i + 1) * 2 + 1]))));
  42. else if (i === nuPointsMinus)
  43. perpVector = cc.pPerp(cc.pNormalize(cc.pSub(cc.p(points[(i - 1) * 2], points[(i - 1) * 2 + 1]), p1)));
  44. else {
  45. var p0 = cc.p(points[(i - 1) * 2], points[(i - 1) * 2 + 1]);
  46. var p2 = cc.p(points[(i + 1) * 2], points[(i + 1) * 2 + 1]);
  47. var p2p1 = cc.pNormalize(cc.pSub(p2, p1));
  48. var p0p1 = cc.pNormalize(cc.pSub(p0, p1));
  49. // Calculate angle between vectors
  50. var angle = Math.acos(cc.pDot(p2p1, p0p1));
  51. if (angle < cc.DEGREES_TO_RADIANS(70))
  52. perpVector = cc.pPerp(cc.pNormalize(cc.pMidpoint(p2p1, p0p1)));
  53. else if (angle < cc.DEGREES_TO_RADIANS(170))
  54. perpVector = cc.pNormalize(cc.pMidpoint(p2p1, p0p1));
  55. else
  56. perpVector = cc.pPerp(cc.pNormalize(cc.pSub(p2, p0)));
  57. }
  58. perpVector = cc.pMult(perpVector, stroke);
  59. vertices[idx * 2] = p1.x + perpVector.x;
  60. vertices[idx * 2 + 1] = p1.y + perpVector.y;
  61. vertices[(idx + 1) * 2] = p1.x - perpVector.x;
  62. vertices[(idx + 1) * 2 + 1] = p1.y - perpVector.y;
  63. }
  64. // Validate vertexes
  65. offset = (offset == 0) ? 0 : offset - 1;
  66. for (i = offset; i < nuPointsMinus; i++) {
  67. idx = i * 2;
  68. var idx1 = idx + 2;
  69. var v1 = cc.Vertex2(vertices[idx * 2], vertices[idx * 2 + 1]);
  70. var v2 = cc.Vertex2(vertices[(idx + 1) * 2], vertices[(idx + 1) * 2 + 1]);
  71. var v3 = cc.Vertex2(vertices[idx1 * 2], vertices[idx1 * 2]);
  72. var v4 = cc.Vertex2(vertices[(idx1 + 1) * 2], vertices[(idx1 + 1) * 2 + 1]);
  73. //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);
  74. var fixVertexResult = !cc.vertexLineIntersect(v1.x, v1.y, v4.x, v4.y, v2.x, v2.y, v3.x, v3.y);
  75. if (!fixVertexResult.isSuccess)
  76. if (fixVertexResult.value < 0.0 || fixVertexResult.value > 1.0)
  77. fixVertexResult.isSuccess = true;
  78. if (fixVertexResult.isSuccess) {
  79. vertices[idx1 * 2] = v4.x;
  80. vertices[idx1 * 2 + 1] = v4.y;
  81. vertices[(idx1 + 1) * 2] = v3.x;
  82. vertices[(idx1 + 1) * 2 + 1] = v3.y;
  83. }
  84. }
  85. };
  86. /**
  87. * returns wheter or not the line intersects
  88. * @param {Number} Ax
  89. * @param {Number} Ay
  90. * @param {Number} Bx
  91. * @param {Number} By
  92. * @param {Number} Cx
  93. * @param {Number} Cy
  94. * @param {Number} Dx
  95. * @param {Number} Dy
  96. * @return {Object}
  97. */
  98. cc.vertexLineIntersect = function (Ax, Ay, Bx, By, Cx, Cy, Dx, Dy) {
  99. var distAB, theCos, theSin, newX;
  100. // FAIL: Line undefined
  101. if ((Ax == Bx && Ay == By) || (Cx == Dx && Cy == Dy))
  102. return {isSuccess:false, value:0};
  103. // Translate system to make A the origin
  104. Bx -= Ax;
  105. By -= Ay;
  106. Cx -= Ax;
  107. Cy -= Ay;
  108. Dx -= Ax;
  109. Dy -= Ay;
  110. // Length of segment AB
  111. distAB = Math.sqrt(Bx * Bx + By * By);
  112. // Rotate the system so that point B is on the positive X axis.
  113. theCos = Bx / distAB;
  114. theSin = By / distAB;
  115. newX = Cx * theCos + Cy * theSin;
  116. Cy = Cy * theCos - Cx * theSin;
  117. Cx = newX;
  118. newX = Dx * theCos + Dy * theSin;
  119. Dy = Dy * theCos - Dx * theSin;
  120. Dx = newX;
  121. // FAIL: Lines are parallel.
  122. if (Cy == Dy) return {isSuccess:false, value:0};
  123. // Discover the relative position of the intersection in the line AB
  124. var t = (Dx + (Cx - Dx) * Dy / (Dy - Cy)) / distAB;
  125. // Success.
  126. return {isSuccess:true, value:t};
  127. };