From: Koichi KAMICHI Date: Wed, 15 Oct 2008 06:22:28 +0000 (+0000) Subject: Initial revision X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=ad1a1c8a5ef45c1fede2c3e5493c6570323a9a08;p=chise%2Fkage.git Initial revision --- diff --git a/engine/2d.js b/engine/2d.js new file mode 100644 index 0000000..864f22f --- /dev/null +++ b/engine/2d.js @@ -0,0 +1,129 @@ +// Reference : http://www.cam.hi-ho.ne.jp/strong_warriors/teacher/chapter0{4,5}.html + +function point(x, y){ + this.x = x; + this.y = y; +} + +function getCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22){ // point + var a1 = y12 - y11; + var b1 = x11 - x12; + var c1 = -1 * a1 * x11 - b1 * y11; + var a2 = y22 - y21; + var b2 = x21 - x22; + var c2 = -1 * a2 * x21 - b2 * y21; + + var temp = b1 * a2 - b2 * a1; + if(temp == 0){ // parallel + return false; + } + return new point((c1 * b2 - c2 * b1) / temp, (a1 * c2 - a2 * c1) / temp); +} + +function isCross(x11, y11, x12, y12, x21, y21, x22, y22){ // boolean + var temp = getCrossPoint(x11, y11, x12, y12, x21, y21, x22, y22); + if(!temp){ return false; } + if(x11 < x12 && (temp.x < x11 || x12 < temp.x) || + x11 > x12 && (temp.x < x12 || x11 < temp.x) || + y11 < y12 && (temp.y < y11 || y12 < temp.y) || + y11 > y12 && (temp.y < y12 || y11 < temp.y) + ){ + return false; + } + if(x21 < x22 && (temp.x < x21 || x22 < temp.x) || + x21 > x22 && (temp.x < x22 || x21 < temp.x) || + y21 < y22 && (temp.y < y21 || y22 < temp.y) || + y21 > y22 && (temp.y < y22 || y21 < temp.y) + ){ + return false; + } + return true; +} + +function isCrossBox(x1, y1, x2, y2, bx1, by1, bx2, by2){ // boolean + if(isCross(x1, y1, x2, y2, bx1, by1, bx2, by1)){ return true; } + if(isCross(x1, y1, x2, y2, bx2, by1, bx2, by2)){ return true; } + if(isCross(x1, y1, x2, y2, bx1, by2, bx2, by2)){ return true; } + if(isCross(x1, y1, x2, y2, bx1, by1, bx1, by2)){ return true; } + return false; +} + +function isCrossBoxWithOthers(strokesArray, i, bx1, by1, bx2, by2){ // boolean + for(var j = 0; j < strokesArray.length; j++){ + if(i == j){ continue; } + switch(strokesArray[j][0]){ + case 0: + case 8: + case 9: + break; + case 6: + case 7: + if(isCrossBox(strokesArray[j][7], + strokesArray[j][8], + strokesArray[j][9], + strokesArray[j][10], + bx1, by1, bx2, by2)){ + return true; + } + case 2: + case 12: + case 3: + if(isCrossBox(strokesArray[j][5], + strokesArray[j][6], + strokesArray[j][7], + strokesArray[j][8], + bx1, by1, bx2, by2)){ + return true; + } + default: + if(isCrossBox(strokesArray[j][3], + strokesArray[j][4], + strokesArray[j][5], + strokesArray[j][6], + bx1, by1, bx2, by2)){ + return true; + } + } + } + return false; +} + +function isCrossWithOthers(strokesArray, i, bx1, by1, bx2, by2){ // boolean + for(var j = 0; j < strokesArray.length; j++){ + if(i == j){ continue; } + switch(strokesArray[j][0]){ + case 0: + case 8: + case 9: + break; + case 6: + case 7: + if(isCross(strokesArray[j][7], + strokesArray[j][8], + strokesArray[j][9], + strokesArray[j][10], + bx1, by1, bx2, by2)){ + return true; + } + case 2: + case 12: + case 3: + if(isCross(strokesArray[j][5], + strokesArray[j][6], + strokesArray[j][7], + strokesArray[j][8], + bx1, by1, bx2, by2)){ + return true; + } + default: + if(isCross(strokesArray[j][3], + strokesArray[j][4], + strokesArray[j][5], + strokesArray[j][6], + bx1, by1, bx2, by2)){ + return true; + } + } + } + return false; +}