检测旋转的矩形是否在画布弧中

时间:2015-10-25 18:54:25

标签: javascript canvas

我有一个通过ctx.rotate和圆弧旋转的矩形。我想检查矩形的任何部分是否在所述弧内。例如,这应该是真的:

Arc overlaps rectangle

我尝试使用isPointInPath,但我正在寻找的“点”不是矩形的实际坐标,而是非旋转的坐标(实际为绿色,"Feb 2015" "Jun 2015" "Jul 2015" "Aug 2015" "Sep 2015" "Oct 2015" "Nov 2015" "73.98" "1013.21" "3209.38" "4168.41" "5233.48" "1313.83" "622.78" 0 0 0 "1391.72" "761.15" "107.58" 0 0 0 0 0 0 0 0 为检查蓝色):

isPointInPath misses the point

这是JS:

isPointInPath

JSBin for live code fun

1 个答案:

答案 0 :(得分:4)

您可以简单地使用坐标旋转来将矩形与系统对齐,然后只需使用多个轴对齐的算法中的一个:

var dx = arc.x - ship.x,
    dy = arc.y - ship.y;

ctx.rotate( -ship.rot );

ctx.beginPath();
ctx.moveTo( 0, 0 );
ctx.arc( 0, 0, arc.r, arc.sRot, arc.eRot );

if( ctx.isPointInPath( dx, dy ) ||
    ctx.isPointInPath( dx + rect.w, dy ) ||
    ctx.isPointInPath( dx, dy + rect.h ) ||
    ctx.isPointInPath( dx + rect.w, dy + rect.h ) )

    collisionIsTrue();