Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

compare two rectangles?

6 REPLIES 6
Reply
Message 1 of 7
vladimir.ninic
351 Views, 6 Replies

compare two rectangles?

how to compare if one rectangle is inside other one?

how to get coordinates from rectangles and compare them?

result should be: A is inside B, A is outside B and A&B are overlapping...
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: vladimir.ninic

I think something like this [untested] should work on orthogonal rectangles with the same Z
extrusion direction [whether or not they're at the same Z elevation]. Otherwise [for example
non-orthogonal, especially if not rotated to the same angle], I'd have to think about what the
complications would be. I haven't really worked with the getBoundingBox or safearray things before,
so there might be a simpler way to go about it, and I hope I got the syntax right. You might need a
(vl-load-com) first, too.

(setq
rectA (vlax-ename->vla-object (car (entsel "Select Rectangle A: ")))
rectB (vlax-ename->vla-object (car (entsel "Select Rectangle B: ")))
); end setq
(vla-getBoundingBox rectA 'minA 'maxA); set corners to safearray variables
(vla-getBoundingBox rectB 'minB 'maxB)
(setq
llA (vlax-safearray->list minA); convert safearrays to regular point lists
urA (vlax-safearray->list maxA)
llB (vlax-safearray->list minB)
urB (vlax-safearray->list maxB)
); ent setq
(cond
((and ; if lower left corner of A is to upper right of lower left corner of B, etc.
(= (cdr (reverse (mapcar '> llA llB))) '(T T)); removes Z to check only X and Y
(= (cdr (reverse (mapcar '< urA urB))) '(T T))
); end and
(alert "Rectangle A is INside Rectangle B.")
); end A-inside-B test
((and
(= (cdr (reverse (mapcar '< llA llB))) '(T T))
(= (cdr (reverse (mapcar '> urA urB))) '(T T))
); end and
(alert "Rectangle A is OUTside Rectangle B.")
); end A-outside-B test
(T (alert "Rectangles A and B overlap or {at least partially} coincide."))
); end cond

If they're always *Polyline* rectangles, and always orthogonal, it might be done more briefly,
calculating lower left and upper right corners from their entity data. The above [assuming it
actually works] has the advantage that it should also work if the rectangles are Blocks or [2D]
Solids or 3DFaces or Traces or Mlines.

--
Kent Cooper


skin wrote...
how to compare if one rectangle is inside other one?
how to get coordinates from rectangles and compare them?
result should be: A is inside B, A is outside B and A&B are overlapping...
Message 3 of 7

thnx, but it always says
"Rectangles A and B overlap or {at least partially} coincide."

i've tested it in all situations it's always the same alert...

another question:
how to get x-coordinate of upper left corner of rectangle as variable x1?
Message 4 of 7
Anonymous
in reply to: vladimir.ninic

I'll have to look into why it's not working, later....

The X coordinates of the upper left corners of orthogonally-oriented rectangles [or bounding boxes
around anything] are the same as those of the lower left corners, namely:

(car llA)
and
(car llB)

So:

(setq x1 (car llA))

--
Kent Cooper


skin wrote...
thnx, but it always says
"Rectangles A and B overlap or {at least partially} coincide."

i've tested it in all situations it's always the same alert...

another question:
how to get x-coordinate of upper left corner of rectangle as variable x1?
Message 5 of 7
Anonymous
in reply to: vladimir.ninic

Wrong function -- should have used (equal) instead of (=):

(defun C:RectComp (/ rectA rectB llA urA llB urB)
(setq
rectA (vlax-ename->vla-object (car (entsel "Select Rectangle A: ")))
rectB (vlax-ename->vla-object (car (entsel "Select Rectangle B: ")))
); end setq
(vla-getBoundingBox rectA 'minA 'maxA); set corners to safearray variables
(vla-getBoundingBox rectB 'minB 'maxB)
(setq
llA (vlax-safearray->list minA); convert safearrays to regular point lists
urA (vlax-safearray->list maxA)
llB (vlax-safearray->list minB)
urB (vlax-safearray->list maxB)
); ent setq
(cond
((and ; if lower left corner of A is to upper right of lower left corner of B, etc.
(equal (cdr (reverse (mapcar '> llA llB))) '(T T)); removes Z to check only X and Y
(equal (cdr (reverse (mapcar '< urA urB))) '(T T))
); end and
(alert "Rectangle A is INside Rectangle B.")
); end A-inside-B test
((and
(equal (cdr (reverse (mapcar '< llA llB))) '(T T))
(equal (cdr (reverse (mapcar '> urA urB))) '(T T))
); end and
(alert "Rectangle A is OUTside Rectangle B.")
); end A-outside-B test
(T (alert "Rectangles A overlaps, is fully outside, or\nat least partially coincides with
Rectangle B."))
); end cond
); end defun
(prompt "Type RectComp to Compare two Rectangles.")

--
Kent Cooper


skin wrote...
thnx, but it always says
"Rectangles A and B overlap or {at least partially} coincide."

i've tested it in all situations it's always the same alert...

another question:
how to get x-coordinate of upper left corner of rectangle as variable x1?
Message 6 of 7

thnx Kent!!!

that's it! it works!
Message 7 of 7
Anonymous
in reply to: vladimir.ninic

Great. And I've gotten more familiar with some functions I hadn't known enough about before. [I
should have known about the difference between (=), which is for numbers and strings, and (eq)
and/or (equal), having looked into it and pointed it out on some other thread not very long ago.]

So now, if the Rectangles are rotated to different angles.... Hmmm [but maybe that's never
something you need to deal with].
--
Kent Cooper


skin wrote...
thnx Kent!!!

that's it! it works!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost