PeterT Posted October 13, 2004 Share Posted October 13, 2004 Is there a simple way to tell in a VectorScript if the bounding boxes of two objects are intersecting? The way that I have been doing it assuming two rectangles with the coordinates b1x,b1y,b2x,b2y and r1x,r1y,r2x,r2y is awfully wordy: code: IF ((((b1X<=r2X) AND (b2X>r2X)) AND ((b1Y<=r1Y) AND (b2Y>=r2Y))) OR (((b1X<r1X) AND (b2X>=r1X)) AND ((b1Y<=r1Y) AND (b2Y>=r2Y))) OR (((b1Y>r1Y) AND (b2Y<=r1Y)) AND ((b1X>=r1X) AND (b2X<=r2X))) OR (((b1Y>=r2Y) AND (b2Y<r2Y)) AND ((b1X>=r1X) AND (b2X<=r2X)))) THEN (--BBoxes are intersecting--)[/code] 1. Is there an easier way than this? 2. This test does not seem to work if the objects being tested are "dimension" objects. Do dimensions have bounding boxes at all? If I group a dimension, then I seem to get its bounding box, but then it is not a dimension anymore, it is a group. Thanks for any input, Peter Quote Link to comment
MullinRJ Posted October 13, 2004 Share Posted October 13, 2004 Hi Peter, I use: code: Intersect : Boolean; L1, T1, R1, B1 : REAL; { Left, Top, Right, Bottom } L2, T2, R2, B2 : REAL; Intersect := (((L1 <= L2) & (R1 >= R2)) & ((T1 <= T2) & (B1 >= B2))) | (((L1 >= L2) & (R1 <= R2)) & ((T1 >= T2) & (B1 <= B2))) | PtInRect(L1, T1, L2, T2, R2, B2) | PtInRect(R1, T1, L2, T2, R2, B2) | PtInRect(R1, B1, L2, T2, R2, B2) | PtInRect(L1, B1, L2, T2, R2, B2) | PtInRect(L2, T2, L1, T1, R1, B1) | PtInRect(R2, T2, L1, T1, R1, B1) | PtInRect(R2, B2, L1, T1, R1, B1) | PtInRect(L2, B2, L1, T1, R1, B1);[/code] This will also catch overlapping dimensions. Raymond [ 10-13-2004, 02:55 PM: Message edited by: MullinRJ ] Quote Link to comment
PeterT Posted October 13, 2004 Author Share Posted October 13, 2004 Thanks, Ramond, I will have a look at your code more closely, but on first glance it appears that your code will also find objects which are completely enclosed inside of another object's bounding box. I wish to exclude these objects, and only find the objects which are actually touching or crossing the bounding box. For example, if I were to draw a large rectangle across the middle of a busy drawing, I would like to get handles to all the objects touching the rectangle, but not to any objects completely inside or completely outside the rectangle. I think the first line of your code would get the objects inside the rectangle even if not touching it. I have never used PtInRect before so I will also look at that. And I just discovered the problem with dimensions in my script. I had previously comment delimited the Type 63 line in my code for testing, and forgot to remove the delimiters. Now the dimensions are being recognized just fine. I am just a beginner at this, and sometimes the simple things get me. Regards, Peter Quote Link to comment
MullinRJ Posted October 14, 2004 Share Posted October 14, 2004 quote: I wish to exclude these objects, and only find the objects which are actually touching or crossing the bounding box.Ah, but you did not say that explicitly. Try: code: A := PtInRect(L1, T1, L2, T2, R2, B2); B := PtInRect(R1, T1, L2, T2, R2, B2); C := PtInRect(R1, B1, L2, T2, R2, B2); D := PtInRect(L1, B1, L2, T2, R2, B2); E := PtInRect(L2, T2, L1, T1, R1, B1); F := PtInRect(R2, T2, L1, T1, R1, B1); G := PtInRect(R2, B2, L1, T1, R1, B1); H := PtInRect(L2, B2, L1, T1, R1, B1); Intersect := (((L1 <= L2) & (R1 >= R2)) & ((T1 <= T2) & (B1 >= B2))) | (((L1 >= L2) & (R1 <= R2)) & ((T1 >= T2) & (B1 <= B2))) | ((A | B | C | D | E | F | G | H) & not ((A & B & C & D) | (E & F & G & H)));[/code] The first two lines find tall skinny Rects and short wide Rects that overlap with no corners inside the other Rect. The third line finds Rects where at least one corner is inside the other Rect, and the last line excludes all corners of one from being inside the other. Of course, A through H are declared as booleans. As written, if the two Rects are equal, Intersect will return TRUE. If you want to filter equal Rects, add "& not EqualRect(L1, T1, R1, B1, L2, T2, R2, B2)" to the expression. To answer your question, I don't think there is a simple way to express what you want. However, once you get it working to your satisfaction, you can define it as a Function and and make a single line VS call for future invocations. HTH, Raymond Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.