Rectangle vertexes

Discussion in 'AutoCAD' started by Marc'Antonio Alessi, Jun 28, 2004.

  1. Maybe this is been already solved:

    given a list of four couples of vertexes that represents
    a rectangle or a square (also rotated):

    ((
    (40.0 20.0 0.0) ; P1
    (88.5071 32.1268 0.0) ; P2
    )
    (
    (88.5071 32.1268 0.0) ; P2
    (81.2311 61.2311 0.0) ; P3
    )
    (
    (81.2311 61.2311 0.0) ; P3
    (32.7239 49.1043 0.0) ; P4
    )
    (
    (32.7239 49.1043 0.0) ; P4
    (40.0 20.0 0.0) ; P1
    ))


    the couples in the list can be in every sequence
    as the vertexes inside the couple

    (as in a rectangle formed by 4 lines drawed in
    various directions)


    Given a direction, for example (ang P2 P3),
    I need to find:

    Px = center of the rectangle (or square)
    H = length on given angle
    L = length on (given angle +- 90)

    P4-----------------P3
    | |
    | |
    | Px? | H?
    | |
    | |
    P1-----------------P2
    L?
     
    Marc'Antonio Alessi, Jun 28, 2004
    #1
  2. Thanks Herman.

    Cheers.

    Marco
     
    Marc'Antonio Alessi, Jun 29, 2004
    #2
  3. Marc'Antonio Alessi

    Mark Propst Guest

    I don't know if this is any help or not
    it's part of what i use to test for 'rectangleness'
    ;;; given a list of four points
    ;;; returns list of three distances, shortest, middle, longest
    ;;; between any one point and 3 other points in list
    ;;; if the list is the same measured from any two of the points in the list
    ;;; or nil if the points don't form a rectangle (within fuzz)
    ;;; thus for a rectangle the shortest is height, middle is length, and
    longest is diagonal
    ;;; from this you can easily get center pt etc
    ;;; or at least that's what it's supposed to do...no guarantees
    ;;; i think i seemed to work in limited testing but it's been a while since
    i've looked at it
    ;;; cleaned up a bit to post here so hope i didn't screw it up.
    (defun GetRectDistList (lst / DistList lastDistList Result idx fuzz)
    (setq idx 0 Fuzz 0.00000001)
    (repeat 2
    (setq distlist (get-list-of-Distances lst idx))
    (if LastDistList ; if this isn't the first time
    ;check this list of distances against last list
    (if (equal DistList LastDistList fuzz)
    (setq result DistList);return list of distances
    ;else
    (setq result NIL)
    ) ;_ endif
    ;) ;progn
    ;else its the first time
    (progn
    ;so save the last list of distances to compare to the next one
    (setq LastDistList DistList)
    ;reset counter
    (setq idx (1+ idx))
    ) ;progn
    ) ;if
    ) ;repeat
    result ;return list or nil
    )
    (defun get-list-of-distances (ptlst idx / distlist)
    (setq I idx J -1)
    (repeat (length ptlst)
    (setq j(1+ j))
    (if(not(= i j));don't read the same point twice
    (setq Distlist
    (cons
    (distance
    (nth I ptlst)
    (nth J ptlst)
    ) ;distance
    Distlist
    )
    ) ;setq
    );end if
    );repeat
    (setq distlist (sort-distlist distlist));sort from short to long or
    however..
    ;return distlist
    );end defun
    ;;;;;;-------------------------------------------
    (defun sort-distlist (lst)
    (vl-sort
    lst
    '<
    )
    );end

    hth
    Mark
     
    Mark Propst, Jun 29, 2004
    #3
  4. Marc'Antonio Alessi

    randy benson Guest

    Here's a start (solves for px):

    (setq p1 '(40.0 20.0 0.0)
    p2 '(88.5071 32.1268 0.0)
    p3 '(81.2311 61.2311 0.0)
    p4 '(32.7239 49.1043 0.0)
    )

    (defun ctr (p1 p2 p3 p4 / ex nx px)
    (setq ex (/ (+ (car p1) (car p2) (car p3) (car p4)) 4.0)
    nx (/ (+ (cadr p1)(cadr p2)(cadr p3)(cadr p4)) 4.0)
    zx (/ (+ (caddr p1)(caddr p2)(caddr p3)(caddr p4)) 4.0)
    ; or just set zx to 0.0 for 2d
    px (list ex nx zx)
    )
    )



    ; Command: (ctr p1 p2 p3 p4)
    ; (60.6155 40.6155 0.0)
     
    randy benson, Jun 29, 2004
    #4
  5. Marc'Antonio Alessi

    randy benson Guest

    ; Makes no sense in 3d anyway, so use 0.0:

    (defun rect-ctr (p1 p2 p3 p4 / ex nx px)
    (setq ex (/ (+ (car p1) (car p2) (car p3) (car p4)) 4.0)
    nx (/ (+ (cadr p1)(cadr p2)(cadr p3)(cadr p4)) 4.0)
    px (list ex nx 0.0)
    )
    )
     
    randy benson, Jun 29, 2004
    #5
  6. Marc'Antonio Alessi

    Doug Broad Guest

    Marco,
    Try this for the centroid:

    (setq rect '((
    (40.0 20.0 0.0) ; P1
    (88.5071 32.1268 0.0) ; P2
    )
    (
    (88.5071 32.1268 0.0) ; P2
    (81.2311 61.2311 0.0) ; P3
    )
    (
    (81.2311 61.2311 0.0) ; P3
    (32.7239 49.1043 0.0) ; P4
    )
    (
    (32.7239 49.1043 0.0) ; P4
    (40.0 20.0 0.0) ; P1
    )))

    (defun cntroid (rect) ;D.C.B-2004
    (mapcar '(lambda (x) (/ x 8.0))
    (apply 'mapcar
    (cons '+ (apply 'append rect)))))
     
    Doug Broad, Jun 29, 2004
    #6
  7. Thanks Doug and all.
     
    Marc'Antonio Alessi, Jun 29, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.