From 4c2fcc8fa9ffbd9349bcbbb63d931ac44b36f7f3 Mon Sep 17 00:00:00 2001 From: kekee000 Date: Tue, 23 Sep 2014 19:21:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B0=8F=E6=95=B0=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E8=AE=A1=E7=AE=97=E8=AF=AF=E5=B7=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/js/segmentCross.js | 2 +- src/graphics/isBezierCross.js | 4 ++-- src/graphics/isBezierLineCross.js | 4 ++-- src/graphics/isBoundingBoxCross.js | 16 ++++++------- src/graphics/isSegmentCross.js | 10 ++++---- src/graphics/util.js | 37 ++++++++++++++++++++++-------- src/render/util/selectShape.js | 2 +- 7 files changed, 47 insertions(+), 28 deletions(-) diff --git a/demo/js/segmentCross.js b/demo/js/segmentCross.js index 8b0af96..33895fd 100644 --- a/demo/js/segmentCross.js +++ b/demo/js/segmentCross.js @@ -22,7 +22,7 @@ define( var width = canvas.offsetWidth; var height = canvas.offsetHeight; - var points = [{"x":398,"y":31,"onCurve":true},{"x":474,"y":201,"onCurve":true},{"x":457,"y":74},{"x":407,"y":74}] ; + var points = [{"x":563.5776234567902,"y":637.5320987654322,"onCurve":true},{"x":563.5776234567902,"y":433.53209876543207,"onCurve":true},{"x":543,"y":478},{"x":10000,"y":478}] ; $('[data-index]').each(function(index, item) { if(!points[index]) { diff --git a/src/graphics/isBezierCross.js b/src/graphics/isBezierCross.js index d0b326a..48a469b 100644 --- a/src/graphics/isBezierCross.js +++ b/src/graphics/isBezierCross.js @@ -14,7 +14,7 @@ define( var bezierQ2Equation = require('../math/bezierQ2Equation'); var bezierQ4Equation = require('../math/bezierQ4Equation'); var bezierCubeEquation = require('../math/bezierCubeEquation'); - var ceil = require('./util').ceil; + var ceilPoint = require('./util').ceilPoint; var matrix = require('./matrix'); var multi = matrix.multi; @@ -187,7 +187,7 @@ define( // 求解x,y坐标 return tResult.length ? tResult.map(function(t) { - return ceil({ + return ceilPoint({ x: p0.x * Math.pow(1 - t, 2) + 2 * p1.x * t * (1-t) + p2.x * Math.pow(t, 2), y: p0.y * Math.pow(1 - t, 2) + 2 * p1.y * t * (1-t) + p2.y * Math.pow(t, 2) }); diff --git a/src/graphics/isBezierLineCross.js b/src/graphics/isBezierLineCross.js index 23cc814..658f409 100644 --- a/src/graphics/isBezierLineCross.js +++ b/src/graphics/isBezierLineCross.js @@ -10,7 +10,7 @@ define( function(require) { var bezierQ2Equation = require('../math/bezierQ2Equation'); - var ceil = require('./util').ceil; + var ceilPoint = require('./util').ceilPoint; /** * 判断贝塞尔曲线与直线相交 @@ -68,7 +68,7 @@ define( if(result) { return result.map(function(t) { - return ceil({ + return ceilPoint({ x: p0.x * Math.pow(1 - t, 2) + 2 * p1.x * t * (1-t) + p2.x * Math.pow(t, 2), y: p0.y * Math.pow(1 - t, 2) + 2 * p1.y * t * (1-t) + p2.y * Math.pow(t, 2) }); diff --git a/src/graphics/isBoundingBoxCross.js b/src/graphics/isBoundingBoxCross.js index 5c0e2a5..0480944 100644 --- a/src/graphics/isBoundingBoxCross.js +++ b/src/graphics/isBoundingBoxCross.js @@ -21,20 +21,20 @@ define( * @return {number} 包含关系 */ function isBoundingBoxCross(b1, b2) { - var b1_lt = isPointInBound(b2, b1); // 左上 - var b1_rt = isPointInBound(b2, {x: b1.x + b1.width, y: b1.y}); // 右上 - var b1_lb = isPointInBound(b2, {x: b1.x, y: b1.y + b1.height}); // 左下 - var b1_rb = isPointInBound(b2, {x: b1.x + b1.width, y: b1.y + b1.height}); //右下 + var b1_lt = isPointInBound(b2, b1, true); // 左上 + var b1_rt = isPointInBound(b2, {x: b1.x + b1.width, y: b1.y}, true); // 右上 + var b1_lb = isPointInBound(b2, {x: b1.x, y: b1.y + b1.height}, true); // 左下 + var b1_rb = isPointInBound(b2, {x: b1.x + b1.width, y: b1.y + b1.height}, true); //右下 // b2 包含 b1 if(b1_lt && b1_rt && b1_lb && b1_rb) { return 2; } - var b2_lt = isPointInBound(b1, b2); // 左上 - var b2_rt = isPointInBound(b1, {x: b2.x + b2.width, y: b2.y}); // 右上 - var b2_lb = isPointInBound(b1, {x: b2.x, y: b2.y + b2.height}); // 左下 - var b2_rb = isPointInBound(b1, {x: b2.x + b2.width, y: b2.y + b2.height}); //右下 + var b2_lt = isPointInBound(b1, b2, true); // 左上 + var b2_rt = isPointInBound(b1, {x: b2.x + b2.width, y: b2.y}, true); // 右上 + var b2_lb = isPointInBound(b1, {x: b2.x, y: b2.y + b2.height}, true); // 左下 + var b2_rb = isPointInBound(b1, {x: b2.x + b2.width, y: b2.y + b2.height}, true); //右下 // b1 包含 b2 if(b2_lt && b2_rt && b2_lb && b2_rb) { diff --git a/src/graphics/isSegmentCross.js b/src/graphics/isSegmentCross.js index de90eb0..8275b18 100644 --- a/src/graphics/isSegmentCross.js +++ b/src/graphics/isSegmentCross.js @@ -10,7 +10,7 @@ define( function(require) { var isBoundingBoxCross = require('./isBoundingBoxCross'); - var ceil = require('./util').ceil; + var ceilPoint = require('./util').ceilPoint; var isPointInBound = require('./util').isPointInBound; var matrix = require('./matrix'); var multi = matrix.multi; @@ -125,13 +125,13 @@ define( x = - (seg2[2] + seg2[1] * y) / seg2[0]; } - p = { + p = ceilPoint({ x: x, y: y - }; + }); - if(isPointInBound(b1, p) && isPointInBound(b2, p)) { - return [ceil(p)]; + if(isPointInBound(b1, p, true) && isPointInBound(b2, p, true)) { + return [p]; } } } diff --git a/src/graphics/util.js b/src/graphics/util.js index e54dfaa..eab993b 100644 --- a/src/graphics/util.js +++ b/src/graphics/util.js @@ -13,14 +13,25 @@ define( /** * 将点进行误差舍入 * + * @param {Object} p 点对象 * @return {Object} 点 */ - function ceil(p) { - p.x = Math.ceil(p.x * 100000) / 100000; - p.y = Math.ceil(p.y * 100000) / 100000 + function ceilPoint(p) { + p.x = Math.round(p.x * 100000) / 100000; + p.y = Math.round(p.y * 100000) / 100000; return p; } - + + /** + * 将数值进行误差舍入 + * + * @param {Object} x 数值 + * @return {number} 点 + */ + function ceil(x) { + return Math.round(x * 100000) / 100000; + } + /** * 判断点是否在bounding box内部 * @param {Object} bound bounding box对象 @@ -28,15 +39,23 @@ define( * @return {boolean} 是否 */ function isPointInBound(bound, p, fixed) { - p = fixed ? ceil(p) : p; - return p.x <= bound.x + bound.width - && p.x >= bound.x - && p.y <= bound.y + bound.height - && p.y >= bound.y + if(fixed) { + return ceil(p.x) <= ceil(bound.x + bound.width) + && ceil(p.x) >= ceil(bound.x) + && ceil(p.y) <= ceil(bound.y + bound.height) + && ceil(p.y) >= ceil(bound.y); + } + else { + return p.x <= bound.x + bound.width + && p.x >= bound.x + && p.y <= bound.y + bound.height + && p.y >= bound.y; + } } return { ceil: ceil, + ceilPoint: ceilPoint, isPointInBound: isPointInBound }; } diff --git a/src/render/util/selectShape.js b/src/render/util/selectShape.js index 903ffa3..d3e19c6 100644 --- a/src/render/util/selectShape.js +++ b/src/render/util/selectShape.js @@ -55,7 +55,7 @@ define( return end; } else { - return shapes[0]; + return start; } }