Author Topic: -={ Challenge }=- Enclose lines  (Read 6219 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8805
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #45 on: July 03, 2023, 08:42:25 AM »
the first problem is: "what is the problem?"

is this maybe ?

"given a set of lines in the XY plane,
however oriented and of any length,
determine the smallest polygon
containing them"


I think the original intent was for the lines to be parallel, or with some relaxed tolerance

zak26

  • Newt
  • Posts: 33
Re: -={ Challenge }=- Enclose lines
« Reply #46 on: July 03, 2023, 12:16:18 PM »
Quote
I think the original intent was for the lines to be parallel, or with some relaxed tolerance
Not quite, What about this?

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: -={ Challenge }=- Enclose lines
« Reply #47 on: July 03, 2023, 12:37:47 PM »
What the? ...I'm out. This "challenge" can't make up its mind.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: -={ Challenge }=- Enclose lines
« Reply #48 on: July 03, 2023, 12:49:13 PM »
... it looks like you want to reconstruct the outline of an exploded hatch

"given a set of lines in the XY plane,
however oriented and of any length,
determine the smallest polygon
containing them"

may be that the answer to this question
might fix it at all ...

ScottMC

  • Newt
  • Posts: 194
Re: -={ Challenge }=- Enclose lines
« Reply #49 on: July 03, 2023, 01:07:41 PM »
just needs get the points 'end' coords of all the entities
and then use "" Gile's or Lee.Mac's "" tools!
It's just putting those two together..

    LM:ConvexHull strange behavior at:

https://www.theswamp.org/index.php?topic=53116.msg578882#msg578882

and Lee.Mac's tool to connect points:
https://www.lee-mac.com/entitytopointlist.html

;;-----------------------------------------------------
  LEFT THE WHILE SELECT LOOP IN IT..

Code: [Select]
;; 
;; Entity to Point List  -  Lee Mac
;; Returns a list of WCS points describing or approximating the supplied entity, else nil if the entity is not supported.
;; ent - [ent] Entity name to be described by point list (POINT/LINE/ARC/CIRCLE/LWPOLYLINE/POLYLINE/ELLIPSE/SPLINE)
;; acc - [num] Positive number determining the point density for non-linear objects

(defun LM:ent->pts ( ent acc / ang bul cen cls di1 di2 enx inc itm lst num ocs rad tot typ vt1 vt2 vtl )
    (setq enx (entget ent)
          typ (cdr (assoc 0 enx))
          acc 4.0   ;; number mods circ,arc point amt
    )
    (cond
        (   (= "POINT" typ)
            (list (cdr (assoc 10 enx)))
        )
        (   (= "LINE" typ)
            (mapcar '(lambda ( x ) (cdr (assoc x enx))) '(10 11))
        )
        (   (or (= "ARC" typ) (= "CIRCLE" typ))
            (if (= "ARC" typ)
                (setq ang (cdr (assoc 50 enx))
                      tot (rem (+ pi pi (- (cdr (assoc 51 enx)) ang)) (+ pi pi))
                      num (fix (+ 1.0 1e-8 (* acc (/ tot (+ pi pi)))))
                      inc (/ tot (float num))
                      num (1+ num)
                )
                (setq ang 0.0
                      tot (+ pi pi)
                      num (fix (+ 1e-8 acc))
                      inc (/ tot (float num))
                )
            )
            (setq cen (cdr (assoc 010 enx))
                  rad (cdr (assoc 040 enx))
                  ocs (cdr (assoc 210 enx))
            )
            (repeat num
                (setq lst (cons (trans (polar cen ang rad) ocs 0) lst)
                      ang (+ ang inc)
                )
            )
            (reverse lst)
        )
        (   (or (= "LWPOLYLINE" typ)
                (and (= "POLYLINE" typ) (zerop (logand (logior 16 64) (cdr (assoc 70 enx)))))
            )
            (if (= "LWPOLYLINE" typ)
                (setq vtl (LM:ent->pts:lwpolyvertices enx))
                (setq vtl (LM:ent->pts:polyvertices   ent))
            )
            (if (setq ocs (cdr (assoc 210 enx))
                      cls (= 1 (logand 1 (cdr (assoc 70 enx))))
                )
                (setq vtl (append vtl (list (cons (caar vtl) 0.0))))
            )
            (while (setq itm (car vtl))
                (setq vtl (cdr vtl)
                      vt1 (car itm)
                      bul (cdr itm)
                      lst (cons (trans vt1 ocs 0) lst)
                )
                (if (and (not (equal 0.0 bul 1e-8)) (setq vt2 (caar vtl)))
                    (progn
                        (setq rad (/ (* (distance vt1 vt2) (1+ (* bul bul))) 4.0 bul)
                              cen (polar vt1 (+ (angle vt1 vt2) (- (/ pi 2.0) (* 2.0 (atan bul)))) rad)
                              rad (abs rad)                           
                              tot (* 4.0 (atan bul))
                              num (fix (+ 1.0 1e-8 (* acc (/ (abs tot) (+ pi pi)))))
                              inc (/ tot (float num))
                              ang (+ (angle cen vt1) inc)
                        )                       
                        (repeat (1- num)
                            (setq lst (cons (trans (polar cen ang rad) ocs 0) lst)
                                  ang (+ ang inc)
                            )
                        )
                    )
                )
            )
            (reverse (if cls (cdr lst) lst))
        )
        (   (= "ELLIPSE" typ)
            (setq di1 (vlax-curve-getdistatparam ent (vlax-curve-getstartparam ent))
                  di2 (vlax-curve-getdistatparam ent (vlax-curve-getendparam   ent))
                  di2 (- di2 1e-8)
            )
            (while (< di1 di2)
                (setq lst (cons (vlax-curve-getpointatdist ent di1) lst)
                      rad (distance '(0.0 0.0) (vlax-curve-getfirstderiv ent (vlax-curve-getparamatdist ent di1)))
                      di1 (+ di1 (/ di2 (1+ (fix (* acc (/ di2 rad (+ pi pi)))))))
                )
            )
            (reverse (if (vlax-curve-isclosed ent) lst (cons (vlax-curve-getendpoint ent) lst)))
        )
    )
)

(defun LM:ent->pts:lwpolyvertices ( enx / elv lst vtx )
    (setq elv (list (cdr (assoc 38 enx))))
    (while (setq vtx (assoc 10 enx))
        (setq enx (cdr (member vtx enx))
              lst (cons (cons (append (cdr vtx) elv) (cdr (assoc 42 enx))) lst)
        )
    )
    (reverse lst)
)

(defun LM:ent->pts:polyvertices ( ent / lst vte vtx )
    (setq vte (entnext ent)
          vtx (entget  vte)
    )   
    (while (= "VERTEX" (cdr (assoc 0 vtx)))
        (setq lst (cons (cons (cdr (assoc 10 vtx)) (cdr (assoc 42 vtx))) lst)
              vte (entnext vte)
              vtx (entget  vte)
        )
    )
    (reverse lst)
)
;;// -----------------------------------------------------------------------------------------------------------
   
(defun os3 ( / dist ename vobj )
    (setq dist 0.0000001) ;; puts point close enough to vertex
  ;(setq ent (car (entsel ent))) ;; "\nSelect object to offset: ")
  (setq vobj (vlax-ename->vla-object ent))
  (if (vlax-method-applicable-p vobj 'Offset)
    (progn
     (vlax-invoke vobj 'Offset dist) ; INSIDE
     ;; (vlax-invoke vobj 'Offset (- dist))
    )
    (princ "\nCannot offset selected object type ")
  )
  (princ)
) ;end
;;// -----------------------------------------------------------------------------------------------------------
   
(defun c:p/ ( / *error* ss ent entx dvn ) ;;  ent dvn
(princ "  Places Points On Ends/Arcs/Quads <undo or oops to un-erase..>");;  and Makes Block < *SELECT* >
(setvar 'cmdecho 0)
(setq tot 0)
  (defun *error* ( msg )
    (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
    (if qaf (setvar 'qaflags qaf))
    (if msg (prompt msg))
    (setvar 'cmdecho 1)  ;; (getvar 'cmdecho)
    ;  (setvar "nomutt" 0)  ;; <--works to restore var on pre-finish esc/error..
    (princ)
  )
 
(while
 
   (setq ss (ssget ":S" '((0 . "LINE,ARC,CIRCLE,SPLINE,LWPOLYLINE"))));;filter selection
    (setq ent (ssname ss 0));;grab ename
    (setq dvn 4.0) ;; default
 
        (os3)                   ;;  offset dist: -l 136

   (setq ent (entlast))
  (setq entx ent) ;; make selset with.. to erase
            (command)
              (foreach pnt (LM:ent->pts ent dvn)  ;; 12.0 prefered
              (setq ent (entmake (list '(0 . "POINT") (cons 10 pnt))))
          )
    (setq tot (1+ tot))
   (vl-cmdf "erase" entx "")
   (princ (strcat " Total: " (rtos tot)))
) ;; end of while loop -l 152
(setvar 'cmdecho 1)
;(princ "\n 'O2B' to Make Block<..anonymous>")
  (princ)
)
(vl-load-com) (princ)

« Last Edit: July 03, 2023, 01:14:24 PM by ScottMC »

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: -={ Challenge }=- Enclose lines
« Reply #50 on: July 03, 2023, 01:27:25 PM »
just needs get the points 'end' coords of all the entities
and then use "" Gile's or Lee.Mac's "" tools!
It's just putting those two together..

Doesn't that seem weird (-i.e. how is that fair to Gile or Lee--or anyone else for that matter)? I'm all for using the tools if they save you time but you're just pasting two functions together. What happens if I paste the same functions (written by two different people together); who learns anything from that?

This whole thread seems like it's morphing into a fishing expedition.


Lee,
I just clicked that link mentioned in the post above and it looks like your website is getting a certificate (SSL) error.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: -={ Challenge }=- Enclose lines
« Reply #51 on: July 03, 2023, 01:37:09 PM »
and the polygon could also be concave

ScottMC

  • Newt
  • Posts: 194
Re: -={ Challenge }=- Enclose lines
« Reply #52 on: July 03, 2023, 01:48:32 PM »
"....Cast the net on the other side of the boat."

zak26

  • Newt
  • Posts: 33
Re: -={ Challenge }=- Enclose lines
« Reply #53 on: July 03, 2023, 01:57:57 PM »
... it looks like you want to reconstruct the outline of an exploded hatch

"given a set of lines in the XY plane,
however oriented and of any length,
determine the smallest polygon
containing them"
The problem started just by joining the endpoints, didn't know where they came from, probably from an exploded hatch, that looks like.

What the? ...I'm out. This "challenge" can't make up its mind.
It's your right to do so, thanks for your participation

This whole thread seems like it's morphing into a fishing expedition.
What for?, I think there's no need for those kind of things.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: -={ Challenge }=- Enclose lines
« Reply #54 on: July 03, 2023, 03:57:05 PM »
and the polygon could also be concave
yep :)
they can have a C-shape or S-shape or even an O-shape

it is a very complicated task

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: -={ Challenge }=- Enclose lines
« Reply #55 on: July 03, 2023, 04:22:49 PM »
Not sure I follow the net casting remark but I'm a big believer in learning from others code; can you clean up/out some sub-logic from their routines, or do they need to be used as is? And does that spark some alterations you can make to their routine(s)? I can totally see using someone else's function--and I have quite a few in my library from others--but I also modify them to suite my needs if I can. For example, they (Gile or Lee) may have accounted for more conditions than you'd need in this case can you trim/alter the code?

My point was more along the lines of: "Who is to maintain those functions (you or them if their use doesn't give the expected results)?".

I totally understand that some functions are just become "standard" (-i.e. there isn't really a way to streamline because they're so generic or etc.). But some function algorithms play better/faster for certain types of conditions. For a dumb example, there are a TON of "nthremover" functions; it is up to the programmer to know which is better/worse/"the same as" in a given situation.

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:RemoveNth ( n l )
  2.   ;;  Removes the item at the nth index in a supplied list
  3.   ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com
  4.   ;;
  5.   ;;  Arguments:
  6.   ;;  n - index of item to remove (zero based)
  7.   ;;  l - list from which item is to be removed
  8.   ;;
  9.   ;;  Returns:  List with item at index n removed
  10.   (if (and l (< 0 n))
  11.     (cons (car l) (LM:RemoveNth (1- n) (cdr l)))
  12.     (cdr l)
  13.     )
  14.   )
  15.  
  16. (defun LM:RemoveNth ( n l / i )
  17.   ;;  Removes the item at the nth index in a supplied list
  18.   ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com
  19.   ;;
  20.   ;;  Arguments:
  21.   ;;  n - index of item to remove (zero based)
  22.   ;;  l - list from which item is to be removed
  23.   ;;
  24.   ;;  Returns:  List with item at index n removed
  25.   (setq i -1)
  26.   (vl-remove-if '(lambda ( x ) (= (setq i (1+ i)) n)) l)
  27.  )
  28.  
  29.  
  30. (defun cool (lst i / a)
  31.   ;; By: SMadsen
  32.   (setq a -1)
  33.   (vl-remove-if (function (lambda (n)(= (setq a (1+ a)) i))) lst)
  34. )
  35.  
  36. (defun nthRemove (lst i)
  37.   ;; By: SMadsen
  38.   (and (atom i)(setq i (cons (length lst) i)))
  39.   (cond ((null lst) nil)
  40.         ((/= (car i) (cdr i))
  41.          (cons (car lst) (nthRemove (cdr lst) (cons (1- (car i)) (cdr i)))))
  42.         ((cons (car lst) (nthRemove (cddr lst) (cons (1- (car i)) (cdr i)))))
  43.   )
  44. )
  45.  
  46. (defun nthRemover (lst item / nlst cntr)
  47.   ;; By: John Kaul
  48.   (defun Nested-nthRemover (lst item)
  49.     (if (/= (if (not cntr) (setq cntr 0) cntr) item)
  50.       (setq nlst (cons (car lst) nlst)))
  51.     (setq cntr (1+ cntr))
  52.     (if (> (length (cdr lst)) 0)
  53.       (Nested-nthRemover (cdr lst) item)
  54.       (setq cntr nil))
  55.     (reverse nlst))
  56.   (Nested-nthRemover lst item))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: -={ Challenge }=- Enclose lines
« Reply #56 on: July 03, 2023, 04:29:03 PM »
and the polygon could also be concave
yep :)
they can have a C-shape or S-shape or even an O-shape

it is a very complicated task

Yeah, you're not kidding! I started to fix some logic and was starting to put in some sorting stuff into my dumb function and it blew up (after 5 minutes, it was "back to the drawing board" status). ...now, mine doesn't even work on Lbracket-Rbracket-shapes. :)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2151
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Enclose lines
« Reply #57 on: July 03, 2023, 05:28:24 PM »
the first problem is: "what is the problem?"

is this maybe ?

"given a set of lines in the XY plane,
however oriented and of any length,
determine the smallest polygon
containing them"


Yes, That's the way to ask a question.
A lot of people seem to believe 'we' are mind readers

Sometimes we can't build a cage to hold a rabbit and expect it to hold an elephant , or a snake or a whale as well.

. . . and then there is this :

« Last Edit: July 03, 2023, 05:33:48 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8805
  • AKA Daniel
Re: -={ Challenge }=- Enclose lines
« Reply #58 on: July 03, 2023, 07:22:43 PM »
my first routine seems to work, my second definitely won't
 

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: -={ Challenge }=- Enclose lines
« Reply #59 on: July 04, 2023, 05:52:46 AM »
It is worth noting that, for an arbitrary point list, the problem becomes this -

https://www.theswamp.org/index.php?topic=30434.0