*** empty log message ***
authorKoichi KAMICHI <kamichi@fonts.jp>
Tue, 25 Aug 2009 12:15:45 +0000 (12:15 +0000)
committerKoichi KAMICHI <kamichi@fonts.jp>
Tue, 25 Aug 2009 12:15:45 +0000 (12:15 +0000)
engine/2d.js [new file with mode: 0644]

diff --git a/engine/2d.js b/engine/2d.js
new file mode 100644 (file)
index 0000000..864f22f
--- /dev/null
@@ -0,0 +1,129 @@
+// Reference : http://www.cam.hi-ho.ne.jp/strong_warriors/teacher/chapter0{4,5}.html\r
+\r
+function point(x, y){\r
+  this.x = x;\r
+  this.y = y;\r
+}\r
+\r
+function getCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22){ // point\r
+  var a1 = y12 - y11;\r
+  var b1 = x11 - x12;\r
+  var c1 = -1 * a1 * x11 - b1 * y11;\r
+  var a2 = y22 - y21;\r
+  var b2 = x21 - x22;\r
+  var c2 = -1 * a2 * x21 - b2 * y21;\r
+  \r
+  var temp = b1 * a2 - b2 * a1;\r
+  if(temp == 0){ // parallel\r
+    return false;\r
+  }\r
+  return new point((c1 * b2 - c2 * b1) / temp, (a1 * c2 - a2 * c1) / temp);\r
+}\r
+\r
+function isCross(x11, y11, x12, y12, x21, y21, x22, y22){ // boolean\r
+  var temp = getCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22);\r
+  if(!temp){ return false; }\r
+  if(x11 < x12 && (temp.x < x11 || x12 < temp.x) ||\r
+     x11 > x12 && (temp.x < x12 || x11 < temp.x) ||\r
+     y11 < y12 && (temp.y < y11 || y12 < temp.y) ||\r
+     y11 > y12 && (temp.y < y12 || y11 < temp.y)\r
+     ){\r
+    return false;\r
+  }\r
+  if(x21 < x22 && (temp.x < x21 || x22 < temp.x) ||\r
+     x21 > x22 && (temp.x < x22 || x21 < temp.x) ||\r
+     y21 < y22 && (temp.y < y21 || y22 < temp.y) ||\r
+     y21 > y22 && (temp.y < y22 || y21 < temp.y)\r
+     ){\r
+    return false;\r
+  }\r
+  return true;\r
+}\r
+\r
+function isCrossBox(x1, y1, x2, y2, bx1, by1, bx2, by2){ // boolean\r
+  if(isCross(x1, y1, x2, y2, bx1, by1, bx2, by1)){ return true; }\r
+  if(isCross(x1, y1, x2, y2, bx2, by1, bx2, by2)){ return true; }\r
+  if(isCross(x1, y1, x2, y2, bx1, by2, bx2, by2)){ return true; }\r
+  if(isCross(x1, y1, x2, y2, bx1, by1, bx1, by2)){ return true; }\r
+  return false;\r
+}\r
+\r
+function isCrossBoxWithOthers(strokesArray, i, bx1, by1, bx2, by2){ // boolean\r
+  for(var j = 0; j < strokesArray.length; j++){\r
+    if(i == j){ continue; }\r
+    switch(strokesArray[j][0]){\r
+    case 0:\r
+    case 8:\r
+    case 9:\r
+      break;\r
+    case 6:\r
+    case 7:\r
+      if(isCrossBox(strokesArray[j][7],\r
+                    strokesArray[j][8],\r
+                    strokesArray[j][9],\r
+                    strokesArray[j][10],\r
+                    bx1, by1, bx2, by2)){\r
+        return true;\r
+      }\r
+    case 2:\r
+    case 12:\r
+    case 3:\r
+      if(isCrossBox(strokesArray[j][5],\r
+                    strokesArray[j][6],\r
+                    strokesArray[j][7],\r
+                    strokesArray[j][8],\r
+                    bx1, by1, bx2, by2)){\r
+        return true;\r
+      }\r
+    default:\r
+      if(isCrossBox(strokesArray[j][3],\r
+                    strokesArray[j][4],\r
+                    strokesArray[j][5],\r
+                    strokesArray[j][6],\r
+                    bx1, by1, bx2, by2)){\r
+        return true;\r
+      }\r
+    }\r
+  }\r
+  return false;\r
+}\r
+\r
+function isCrossWithOthers(strokesArray, i, bx1, by1, bx2, by2){ // boolean\r
+  for(var j = 0; j < strokesArray.length; j++){\r
+    if(i == j){ continue; }\r
+    switch(strokesArray[j][0]){\r
+    case 0:\r
+    case 8:\r
+    case 9:\r
+      break;\r
+    case 6:\r
+    case 7:\r
+      if(isCross(strokesArray[j][7],\r
+                 strokesArray[j][8],\r
+                 strokesArray[j][9],\r
+                 strokesArray[j][10],\r
+                 bx1, by1, bx2, by2)){\r
+        return true;\r
+      }\r
+    case 2:\r
+    case 12:\r
+    case 3:\r
+      if(isCross(strokesArray[j][5],\r
+                 strokesArray[j][6],\r
+                 strokesArray[j][7],\r
+                 strokesArray[j][8],\r
+                 bx1, by1, bx2, by2)){\r
+        return true;\r
+      }\r
+    default:\r
+      if(isCross(strokesArray[j][3],\r
+                 strokesArray[j][4],\r
+                 strokesArray[j][5],\r
+                 strokesArray[j][6],\r
+                 bx1, by1, bx2, by2)){\r
+        return true;\r
+      }\r
+    }\r
+  }\r
+  return false;\r
+}\r