Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Challenge: PointInRect and RectInRect

  1. #11

    Challenge: PointInRect and RectInRect

    All right. Here is another version that works for all cases including JSoftware's one.
    [pascal]
    function RectInRect(R1,R2 : TRect) : boolean;
    var
    w,h : integer;
    x1,x2,y1,y2 : integer;
    begin
    w := (R1.Right - R1.Left) + (R2.Right - R2.Left);
    h := (R1.Bottom - R1.Top) + (R2.Bottom - R2.Top);

    x1 := min(R1.Left, R2.Left);
    x2 := max(R1.Right, R2.Right);
    y1 := min(R1.Top, R2.Top);
    y2 := max(R1.Bottom, R2.Bottom);

    Result := ((x2-x1) < w) and ((y2-y1) < h);
    end;
    [/pascal]

    This function uses the fact that if two rects overlap then the difference
    between smallest left and biggest right coordinate must be smaller then the sum of their widths. Same with Top and Bottom values.
    What is your opinion? Any thoughts on improving?

  2. #12

    Challenge: PointInRect and RectInRect

    You could have early-outs so the function doesnt have to compute needless information:

    [pascal]function RectInRect(R1,R2 : TRect) : boolean;
    var
    w,h : integer;
    x1,x2,y1,y2 : integer;
    begin
    w := (R1.Right - R1.Left) + (R2.Right - R2.Left);

    x1 := min(R1.Left, R2.Left);
    x2 := max(R1.Right, R2.Right);

    if ((x2-x1) < w) then begin result := false; exit; end;

    h := (R1.Bottom - R1.Top) + (R2.Bottom - R2.Top);

    y1 := min(R1.Top, R2.Top);
    y2 := max(R1.Bottom, R2.Bottom);

    Result := ((y2-y1) < h);
    end;[/pascal]
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

  3. #13

    Challenge: PointInRect and RectInRect

    Well, JSoftware points out a situation that should work. I have to admit that my code doesn't work in that situation . Course, the idea is to post a common problem and hopefully find a fast solution for anyone to use, idealy the sample code I provide would work, but in this case, seems I goofed.

  4. #14

    Challenge: PointInRect and RectInRect

    Here's my versions:

    Makes check for rectangle first and turn it around if needed... reading above solutions this may not be fastest :s

    // Switches 2 any variables
    procedure SwitchVar(p1,p2: Pointer; Size: Integer);
    var temp: pointer;
    begin
    getmem(temp,size);
    try
    movememory(temp,p1,Size);
    movememory(p1,p2,Size);
    movememory(p2,temp,Size);
    finally
    freemem(temp);
    end;
    end;

    function PointInRect2(p: TPoint; r: TRect): boolean;
    begin
    if r.Left>r.Right then switchvar(@r.left,@r.right,4);
    if r.Top>r.Bottom then switchvar(@r.top,@r.bottom,4);
    result:=(p.x>=r.Left) and (p.x<=r.Right) and
    (p.y>=r.Top) and (p.y<=r.Bottom);
    end;


    I found this function in other language who knows where.. anyway i just tested every possible situation with it to success. Only exception for fail is if rectangles are not made normally; top left to bottom right.

    function RectCollide(x0,y0,x1,y1,x2,y2,x3,y3: single): boolean;
    begin
    result:=not( ((x0<x2) and (x1<x2)) or
    ((x0>x3) and (x1>x3)) or
    ((y0<y2) and (y1<y2)) or
    ((y0>y3) and (y1>y3)) );
    end;

    Edit: Had to remove all BBCode etc. because they removed parts of code...

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •