getregion(double x1, double y1, double x2, double y2, double dir, double allowshift = 3 * math.pi / 180)
else if (math.abs(dir - math.pi / 2) < allowshift)
else if (math.abs(dir - math.pi) < allowshift)
else if (math.abs(dir - math.pi / 2 * 3) < allowshift)
else
else
double x, y;
if (calcrosspoint(k1, k2, c1, c2, out x, out y))
regionpoints.add(new pointd(math.ceiling(x), math.ceiling(y)));
else
debug.writeline("求取路徑頂點時有誤");
if (calcrosspoint(k1, k2, c3, c2, out x, out y))
regionpoints.add(new pointd(math.floor(x), math.ceiling(y)));
else
debug.writeline("求取路徑頂點時有誤");
if (calcrosspoint(k1, k2, c3, c4, out x, out y))
regionpoints.add(new pointd(math.floor(x), math.floor(y)));
else
debug.writeline("求取路徑頂點時有誤");
if (calcrosspoint(k1, k2, c1, c4, out x, out y))
regionpoints.add(new pointd(math.ceiling(x), math.floor(y)));
else
debug.writeline("求取路徑頂點時有誤");
}if (regionpoints.count > 0)
regionpoints.add(regionpoints[0]);
return new commonregion(0, regionpoints, null, null, 0, null);
}bool calcrosspoint(double k1, double k2, double b1, double b2, out double x, out double y)
return false;
}/// /// 計算兩個區域是否相交,
/// 需要考慮的情形有:
/// 1、乙個區域包含在另乙個區域內,此時可用點在區域內的演算法計算
/// 2、乙個區域與另乙個區域由部分重疊,且重疊區域不包括邊界點
///
///
///
public bool isintersect(listboundarypoints)
for (int i = 0; i < boundarypoints.count - 1; i++)
for (int i = 1; i < boundarypoints.count; i++)
}return false;
}bool isintheregion(listboundarypoints, double x, double y)
else if (pts.count == 2)
else
}else if (pts.count == 3)
else}}
if (!nearpoint(pts[0], pts.last(), offset)) pts.add(pts[0]);
if (pts == null || pts.count == 0)
return false;
double minx = pts[0].x;
double maxx = pts[0].x;
double miny = pts[0].y;
double maxy = pts[0].y;
for (int i = 1; i < pts.count - 1; i++)
rectangle maxrectangle = new rectangle((int)minx, (int)miny, (int)(maxx - minx), (int)(maxy - miny));
if (x < maxrectangle.left || x > maxrectangle.right || y < maxrectangle.top || y > maxrectangle.bottom)
return false;
int index = new int[4];
for (int i = 0; i < pts.count - 1; i++)
if (routemath.checklinecross(x, y, x - maxrectangle.width, y, pts[i].x, pts[i].y, pts[i + 1].x, pts[i + 1].y))
if (checklinecross(x, y, x, y + maxrectangle.height, pts[i].x, pts[i].y, pts[i + 1].x, pts[i + 1].y))
if (checklinecross(x, y, x, y - maxrectangle.height, pts[i].x, pts[i].y, pts[i + 1].x, pts[i + 1].y))
}foreach (int i in index)
return true;
}
這裡計算相交用的是最笨的計算方法,所以計算時間較長,需要考慮優化問題。 兩個矩形是否相交
假定矩形是用一對點表達的 minx,miny maxx,maxy 那麼兩個矩形rect1,rect2 相交的結果一定是個矩形,構成這個相交矩形rect的點對座標是 minx max minx1,minx2 miny max miny1,miny2 maxx min maxx1,maxx2 maxy ...
兩個矩形相交問題 判斷是否相交
最近,面試遇到一道演算法題目如下 兩個矩形,判斷是否相交 如果相交面積大於零,輸出相交部分的左上角以及右下角座標點,否則,輸出 1,1 1,1 沒有給出完善的解決方案,在面試官的細心引導下,解決了兩個線段相交輸出交點的問題。因此下來在網上搜了相關的問題。1 下面是 判斷兩個矩形是否相交的方法 下圖是...
判斷兩個矩形是否相交,相交區域面積
直接上 package companychukongkeji 兩個矩形都是平行於x,y軸,判斷是否相交。兩種方法,都需要檢查特殊情況。public class rectangleintersect if a.top b.bottom a.right b.left a.bottom b.top a.l...