1

我怎么知道我的两个图像何时相交?

4

2 回答 2

5

如果我说对了

function IsIntersertButNotContained(const R1, R2: TRect): Boolean;
var
  R: TRect;

begin
// R1 and R2 intersect
  Result:= IntersectRect(R, R1, R2)
//   R1 is not contained within R2
    and not EqualRect(R, R1)
//   R2 is not contained within R1
    and not EqualRect(R, R2);
end;
于 2011-04-19T18:27:31.163 回答
0

以下代码进行了全面检查。

下一个:通常位图不是矩形。
假设您将颜色指定为“透明”,例如黑色(RGB(0,0,0))
,您可以查看相互重叠的 2 个位图是否在相同的 x/y 坐标处具有非黑色像素。

下面的代码演示。
我没有测试过代码,所以可能存在一些小问题

//This function first test the bounding rects and then goes into the pixels
//inside the overlaping rectangle.
function DoBitmapsOverlap(Bitmap1, Bitmap2: TBitmap; 
                          Bitmap2Offset: TPoint; TPColor: TColor): boolean;
var
  Rect1, Rect2: TRect;
  OverlapRect: TRect;
  x1,y1: integer;
  c1,c2: TColor;
begin
  Result:= false;
  Rect1:= Rect(0,0,Bitmap1.Width, Bitmap1.Height);
  with Bitmap2Offset do Rect2:= Rect(x,y,x+ Bitmap2.Width, y+Bitmap2.Height);
  if not(IntersectRect(OverlapRect, Rect1, Rect2)) then exit;
  for x1:= OverlapRect.Left to OverlapRect.Right do begin
    for y1:= OverlapRect.Top to OverlapRect.Bottom do begin
      c1:= Bitmap1.Canvas.Pixels[x1,y1];
      c2:= Bitmap1.Canvas.Pixels[x1-Bitmap2Offset.x, y1-Bitmap2Offset.y];
      Result:= (c1 <> TPColor) and (c2 <> TPColor);
      if Result then exit;            
    end; {for y1}
  end; {for x1}   
end;
于 2011-04-19T18:46:58.953 回答