Retrieving the Lengths of the Sides of a Rectangle

Retrieving the Lengths of the Sides of a Rectangle

Anonymous
Not applicable
1,384 Views
8 Replies
Message 1 of 9

Retrieving the Lengths of the Sides of a Rectangle

Anonymous
Not applicable

So the goal is to determine the specific length of the left and right sides of a rectangle. So I can figure out how to get the four different points of the polygon. Then by comparison I can determine which points are upper right, lower left, etc. From there I can work out lengths....but it is tedious and I kinda get lost in the numbers. I'm thinking there must be a more elegant solution....anybody know it?

0 Likes
Accepted solutions (1)
1,385 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

If it will always left- and right-side vertical  edges, that is, if the rectangle will always be orthogonally oriented, try something like this [minimally tested]:

 

(vl-load-com)

(defun C:PRSL ; = Polyline Rectangle Side Length
  (/ pl p1 p0)
  (setq pl (car (entsel "\nPolyline rectangle to get side length of: ")))
  (distance
    (setq p1 (vlax-curve-getPointAtParam pl 1)); 2nd vertex
    (if (equal (rem (angle (setq p0 (vlax-curve-getStartPoint pl)) p1) pi) (/ pi 2) 1e-4)

      ;; i.e. is the first segment vertical?
      p0 ; then [use the first segment, i.e. distance to the start point]
      (vlax-curve-getPointAtParam pl 2); else [use the next segment, i.e. to the 3rd vertex]
    ); if
  ); distance
); defun

 

That will return a decimal number of drawing units.  It can be converted to report in whatever your current Units settings are, if desired.

 

It assumes the rectangle is "clean," i.e. doesn't have anything like doubled-up coincident vertices at any of the early corners, and it's really a rectangle so that the length is the same for both sides, etc.

Kent Cooper, AIA
Message 3 of 9

Anonymous
Not applicable

Thanks for the reply....I guess I failed in my description. The two sides WILL always be vertical....however, they will often be different lengths....so it wouldn't be a true rectangle then....what would that be? I've forgotten my Geometry....=) So i'd like it to return the length of the left side and the length of the right side independently.

0 Likes
Message 4 of 9

doaiena
Collaborator
Collaborator

I think the geometric shape you are referring to is a trapezoid. @Kent1Cooper already gave you code to find the length of one of the sides. With a few simple edits you can adjust the code to output both vertical sides.

0 Likes
Message 5 of 9

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... i'd like it to return the length of the left side and the length of the right side independently.


If you only need two lengths reported, that's not difficult.  If you need them specified as to which is the left side and which is the right, there's more to figure out -- do you need that distinction made?

Kent Cooper, AIA
0 Likes
Message 6 of 9

Anonymous
Not applicable

Ah.....i see what it is doing....took me awhile. I've never really used those "v-lax" commands....i've only used the vanilla lisp. But I think I understand. Thanks guys!

0 Likes
Message 7 of 9

john.uhden
Mentor
Mentor

I'm just having a tough time grasping that there are at least 5878 other Paul Hellers.

John F. Uhden

Message 8 of 9

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

Ah.....i see what it is doing....took me awhile. I've never really used those "v-lax" commands....i've only used the vanilla lisp...


Well, some groups of functions are very useful and you should get know them even if you use just AutoLISP (yet).

- Curve Measurement Functions, see HERE 

- String-handling Functions, see HERE

 

Message 9 of 9

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

@Anonymous wrote:

.... i'd like it to return the length of the left side and the length of the right side independently.


If you only need two lengths reported, that's not difficult.  If you need them specified as to which is the left side and which is the right, there's more to figure out -- do you need that distinction made?


Here's one way to do that, if you haven't already worked something out yourself.  It even verifies that a four-vertex closed Polyline was selected, and  checks whether it has two opposite vertical sides.  Lightly tested.

(defun C:PQVSL ; = Polyline Quadrilateral Vertical Side Lengths
  (/ vpt vs rpt pl dL dR)
  (defun vpt (n) (vlax-curve-getPointAtParam pl n))
  (defun vs (vA vB); vertical segment
    (equal (rem (angle vA vB) pi) (/ pi 2) 1e-3)
  ); defun
  (defun rpt (); report lengths
    (prompt (strcat "\nLeft side length is " (rtos dL) "; Right side length is " (rtos dR) "."))
  ); defun
  (if
    (and
      (setq pl (car (entsel "\nPolyline quadrilateral to get vertical-side lengths of: ")))
      (member '(0 . "LWPOLYLINE") (entget pl))
      (member '(90 . 4) (entget pl))
      (vlax-curve-isClosed pl)
    ); and -- closed quadrilateral Polyline
    (progn ; then -- continue
      (cond
        ( (and (vs (vpt 0) (vpt 1)) (vs (vpt 2) (vpt 3))); 1st & 3rd segments vertical
          (if (< (car (vpt 0)) (car (vpt 2))); start to left of half-way point?
            (setq dL (distance (vpt 0) (vpt 1)) dR (distance (vpt 2) (vpt 3))); then
            (setq dL (distance (vpt 2) (vpt 3)) dR (distance (vpt 0) (vpt 1))); else
          ); if
          (rpt)
        ); 1st & 3rd vertical condition
        ( (and (vs (vpt 0) (vpt 3)) (vs (vpt 1) (vpt 2))); 2nd & 4th segments vertical
          (if (< (car (vpt 0)) (car (vpt 2))); start to left of half-way point?
            (setq dL (distance (vpt 0) (vpt 3)) dR (distance (vpt 1) (vpt 2))); then
            (setq dL (distance (vpt 1) (vpt 2)) dR (distance (vpt 0) (vpt 3))); else
          ); if
          (rpt)
        ); 2nd & 4th vertical condition
        ((prompt "\nDoes not have two opposite vertical sides."))
      ); cond
    ); progn
    (prompt "\nDid not select a closed quadrilateral Polyline."); else
  ); if
  (princ)
); defun

It doesn't deal with the possibility of other-than-World Coordinate Systems, but could be made to.

Kent Cooper, AIA
0 Likes