Image intersect
How do I kno开发者_如何学Cw when my two images are intersecting?
If I get you right
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;
The following code does a full check.
Next: usually a bitmap is not a rectangle.
Assuming you have a color assigned as 'transparent' say black (RGB(0,0,0))
You can see if the 2 bitmaps laying on top of each other have non-black pixels at the same x/y coordinate.
The following code demonstrates.
I have not tested the code, so minor issues might be in there
//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;
精彩评论