图像相交

| 我怎么知道我的两个图像何时相交?     
已邀请:
如果我说对了
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;
    
以下代码进行了全面检查。 下一步:通常,位图不是矩形。 假设您将颜色分配为\'transparent \',例如黑色(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;
    

要回复问题请先登录注册