fork download
  1. function [axes] = getAxes(platformVertices)
  2. for i = 1:size(platformVertices,1)
  3. %Get the normals of the edges
  4. axes(i, 1) = (platformVertices(mod(i, size(platformVertices,1)) + 1, 2) - platformVertices(i,2));
  5. axes(i, 2) = -(platformVertices(mod(i, size(platformVertices,1)) + 1, 1) - platformVertices(i,1));
  6. axes(i, :) = axes(i, :) / norm(axes(i, :));
  7. end
  8. end
  9.  
  10. function [projection] = project2D(vertices, ax)
  11. min = dot(ax, vertices(1, :));
  12. max = min;
  13.  
  14. for i = 2:size(vertices,1)
  15. temp = dot(ax, vertices(i, :));
  16. if temp < min
  17. min = temp;
  18. elseif temp > max
  19. max = temp;
  20. end
  21. end
  22.  
  23. projection = [min, max];
  24. end
  25.  
  26. function [overlap, minOverlap, maxOverlap] = findOverlap(projectionA, projectionB)
  27.  
  28. if projectionB(2) <= projectionA(1) || ...
  29. projectionA(2) <= projectionB(1)
  30. %Separating Axis Found
  31. overlap = 0;
  32. minOverlap = 0;
  33. maxOverlap = 0;
  34. else
  35. overlap = 1;
  36. minOverlap = max([projectionA(1), projectionB(1)]); %Get the biggest min
  37. maxOverlap = min([projectionA(2), projectionB(2)]); %Get the smallest max
  38. end
  39. end
  40. %% The start scipt is below
  41. %Squares
  42. plat = [-1 1; -1 -1; 1 -1; 1 1];
  43. offset = 2 * ones(4,2);
  44. plat1 = plat + .75 * [1 0; 1 0; 1 0; 1 0] + offset;
  45. plat2 = plat - .5 * [1 1; 1 1; 1 1; 1 1] + offset;
  46. plat3 = plat + .5 * [1 1; 1 1; 1 1; 1 1] + offset;
  47.  
  48. %Plotting stuff
  49. figure
  50. axis equal
  51. %plat1
  52. line([plat1(1,1) plat1(2,1)], [plat1(1,2) plat1(2,2)], 'Color', [0 1 0]);
  53. line([plat1(2,1) plat1(3,1)], [plat1(2,2) plat1(3,2)], 'Color', [0 1 0]);
  54. line([plat1(3,1) plat1(4,1)], [plat1(3,2) plat1(4,2)], 'Color', [0 1 0]);
  55. line([plat1(4,1) plat1(1,1)], [plat1(4,2) plat1(1,2)], 'Color', [0 1 0]);
  56. %plat2
  57. line([plat2(1,1) plat2(2,1)], [plat2(1,2) plat2(2,2)], 'Color', [1 0 0]);
  58. line([plat2(2,1) plat2(3,1)], [plat2(2,2) plat2(3,2)], 'Color', [1 0 0]);
  59. line([plat2(3,1) plat2(4,1)], [plat2(3,2) plat2(4,2)], 'Color', [1 0 0]);
  60. line([plat2(4,1) plat2(1,1)], [plat2(4,2) plat2(1,2)], 'Color', [1 0 0]);
  61. %plat3
  62. line([plat3(1,1) plat3(2,1)], [plat3(1,2) plat3(2,2)], 'Color', [0 0 1]);
  63. line([plat3(2,1) plat3(3,1)], [plat3(2,2) plat3(3,2)], 'Color', [0 0 1]);
  64. line([plat3(3,1) plat3(4,1)], [plat3(3,2) plat3(4,2)], 'Color', [0 0 1]);
  65. line([plat3(4,1) plat3(1,1)], [plat3(4,2) plat3(1,2)], 'Color', [0 0 1]);
  66. %End Plotting stuff
  67.  
  68. wedgeX = [plat1(:,1) plat2(:,1) plat3(:,1)];
  69. wedgeY = [plat1(:,2) plat2(:,2) plat3(:,2)];
  70. NumFaces = max([size(plat1, 1), size(plat2, 1), size(plat3, 1)]);
  71. NumDim = 2;
  72. NumPlatforms = 3;
  73.  
  74. axes = NaN(NumFaces, NumDim, NumPlatforms);
  75. %For a wedge we only have to find the normals of 3 sides since 2 sides are
  76. %parallel
  77. for i = 1:NumPlatforms
  78. axes(:, :, i) = getAxes([wedgeX(1:4, i) wedgeY(1:4, i)]);
  79. end
  80.  
  81. overlaps = NaN(NumFaces * NumPlatforms, 4);
  82. for i = 1:NumPlatforms
  83. for j = 1:NumFaces
  84. projA = project2D([wedgeX(:, i), wedgeY(:, i)], axes(j, :, i));
  85. projB = project2D([wedgeX(:, mod(i, NumPlatforms) + 1), wedgeY(:, mod(i, NumPlatforms) + 1)], axes(j, :, i));
  86. [overlap, minOverlap, maxOverlap] = findOverlap(projA, projB);
  87. line([axes(j, 1, i) * -3, axes(j, 1, i) * 3], [axes(j, 2, i) * -3, axes(j, 2, i) * 3]);
  88. if overlap == 0
  89. 'Not overlapping!'
  90. return
  91. else
  92. % [overlaps((i-1) * NumFaces + j, :)] = project2D([minOverlap, maxOverlap], [axes(j, 2, i), -axes(j, 1, i)]);
  93. %startOfLine
  94. overlaps((i - 1) * NumFaces + j, 1) = minOverlap * axes(j, 1, i);
  95. overlaps((i - 1) * NumFaces + j, 2) = minOverlap * axes(j, 2, i);
  96. %endOfLine
  97. overlaps((i - 1) * NumFaces + j, 3) = maxOverlap * axes(j, 1, i);
  98. overlaps((i - 1) * NumFaces + j, 4) = maxOverlap * axes(j, 2, i);
  99. hold on
  100. % plot(mean(overlaps(:, 1)), mean(overlaps(:, 2)), '*r');
  101. plot(overlaps(:, 1), overlaps(:, 2), 'xr');
  102. plot(overlaps(:, 3), overlaps(:, 4), 'og');
  103. midpoint = [overlaps(:,1) + (overlaps(:, 3) - overlaps(:,1)) / 2, overlaps(:,2) + (overlaps(:, 4) - overlaps(:,2)) / 2];
  104. plot(midpoint(:,1), midpoint(:,2), 'dk');
  105. end
  106. end
  107. end
  108.  
  109. hold on
  110. plot(mean(midpoint((midpoint(:,1) ~= 0),1)), mean(midpoint((midpoint(:,2) ~= 0),2)), 'm*');
  111.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/py_compile.py", line 117, in compile
    raise py_exc
py_compile.PyCompileError: Sorry: IndentationError: ('unexpected indent', ('prog.py', 2, 4, '    for i = 1:size(platformVertices,1)\n'))
stdout
Standard output is empty