Author Topic: line from a point perpendicular to a line  (Read 22996 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28708
line from a point perpendicular to a line
« Reply #15 on: June 07, 2004, 10:18:06 AM »
No I meant the code. <g>
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
line from a point perpendicular to a line
« Reply #16 on: June 07, 2004, 10:18:51 AM »
Stig,
Good point about the 3D aspect, In my old routine I converted to 2D points
but forgot to here. Here is my revised version.
I my testing points p1 & p2 did not need to be transed, not understanding the
entire process I am not sure why. The results were correct so I did not
trans p1 & p2 but I see you did.
Ugh, your math went by me like an express train with no stop here. WTFWT :shock:

CAB


Code: [Select]
;;  this prg draws a vertical line from a picked point
;;    to a picked base line
 
(defun c:LnRay (/ ang intpt ent p1 p2 p3 p4)
  ;;Make 2D point from 3D point
  (defun 3dP->2dP (3dpt)(list (car 3dpt) (cadr 3dpt)))
  (if (setq ent (entsel "\nSelect base line: "))
    (if (= "LINE" (cdr (assoc 0 (setq ent (entget (car ent))))))
      (progn
        (setq p1  (3dP->2dP(cdr (assoc 10 ent)))
              p2  (3dP->2dP(cdr (assoc 11 ent)))
              ang (angle p1 p2)
        )
        (while
          (setq p3
                 (getpoint
                   "\nSelect point to draw perpendicular from, Enter to quit: "
                 )
          )
           (setq p3 (3dP->2dP(trans p3 1 0))) ; adjust for non-World UCS
           (setq p4 (polar p3 (+ ang (/ pi 2)) 100))
           (setq intpt (inters p1 p2 p3 p4 nil))
           (entmake (list '(0 . "LINE")
                          (cons 10 p3)
                          (cons 11 intpt)
                    )
           )
        ) ; end while
      ) ; progn
      (prompt "\nObject is not a LINE.")
    ) ; endif
    (prompt "\nNothing Selected.")
  ) ; endif
  (princ)
)

(prompt "\nElev Lines loaded, type LnRay to run")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #17 on: June 07, 2004, 10:52:18 AM »
Heh Mark  :D

CAB, you don't need to convert the line's points to UCS because you're converting p3 to WCS where p1 and p2 is also located. I did it the other way 'round .. a bit more work but indifferent to the result.

The math behind it is explained here if you're interested. Not very complicated.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
line from a point perpendicular to a line
« Reply #18 on: June 07, 2004, 11:17:51 AM »
Quote from: Stig
CAB, you don't need to convert the line's points to UCS because you're converting
p3 to WCS where p1 and p2 is also located. I did it the other way 'round .. a bit more work but
indifferent to the result.

Thanks for the explanation.


Another nice Link, thanks.

Quote from: Stig
Not very complicated.

Easy for you to say, I couldn't fix my rubix cube either. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

DEVITG

  • Bull Frog
  • Posts: 463
line from a point perpendicular to a line
« Reply #19 on: June 08, 2004, 07:47:37 AM »
Hi Stig , it is a good way to learn about lambda and mapcar,
I shall do !!
I will dig on it, to learn about it.

Thanks for it.

 :)
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #20 on: June 08, 2004, 08:30:26 AM »
My pleasure, DEVITG

I guess it's not a bad example for practicing ones MAPCAR skills.

Take for instance the dot product of two vectors: P1 dot P2. It can be written out as (+ (* x1 x2)(* y1 y2)(* z1 z2)), or without having to assign six different variables:
(+ (* (car p1)(car p2))(* (cadr p1)(cadr p2))(* (caddr p1)(caddr p2)))

That's quite a lot to write. Cos' multiplications happen on same level in two point lists, it can more easily be written as (mapcar '* p1 p2). To add all values in the list of products, simply apply an addition:

(apply '+ (mapcar '* p1 p2))

And so, a dot product function was born.

Of course, in the point-to-perpendicular case we needed to substract vectors before multiplying the coordinates. Written in its entirety it would look something like this:

(+ (* (- (car p3) (car p1))(- (car p2) (car p1))) (* (- (cadr p3) (cadr p1))(- (cadr p2) (cadr p1))) (* (- (caddr p3) (caddr p1)) (- (caddr p2) (caddr p1))))

Like with the simple multiplication above, the exact same things happen on each level. Only thing changing is the operator, which means that '(* p1 p2) should be replaced with '(* (- p3 p1)(- p2 p1)).
If one takes to subtract vectors before multiplying them, it could be written with multiple MAPCAR's:

(apply '+ (mapcar '* (mapcar '- p3 p1)(mapcar '- p2 p1)))


Or it could be stuffed into a LAMBDA function, doing it all in one go:

(apply '+ (mapcar '(lambda (a b c)(* (- c a) (- b a))) p1 p2 p3))

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
line from a point perpendicular to a line
« Reply #21 on: June 08, 2004, 09:28:46 AM »
Say, i have this theory that Mapcar is a recursive function and foreach is actualy the itterative version of mapcar. In fact in an attempt to "prove it" i spent some time rebuilding the "Map" function. *But anyways.... Im babbling now*

Does anyone know if Mapcar is a recrusive function?
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #22 on: June 08, 2004, 10:43:13 AM »
Here's a take on writing your own MAPCAR function. As you can see it is recursive:

Code: [Select]
(defun mapIt (fn lst)
  (cond ((null lst) nil)
        (T (cons (eval (list fn (car lst))) (mapIt fn (cdr lst))))
  )
)


(mapit '(lambda (n)(+ n 5)) '(1 2 3))
(6 7 8 )

(mapit (function (lambda (n)(cons n (chr n)))) '(65 66 97))
((65 . "A") (66 . "B") (97 . "a"))

(mapit 'chr '(65 66 97))
("A" "B" "a")

Most of the functions that we take for granted are recursive .. MEMBER, APPEND, ASSOC, SUBST etc. etc.

For example, here's a simple rebuild of ASSOC:

Code: [Select]
(defun associate (item lst)
  (cond ((equal (caar lst) item) (car lst))
        (T (associate item (cdr lst)))
  )
)


(setq alist '((0 . "A")(1 . "B")(2 . "C")(3 . "D")))

(associate 2 alist)
(2 . "C")

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #23 on: June 08, 2004, 10:46:52 AM »
Quote from: Se7en
In fact in an attempt to "prove it" i spent some time rebuilding the "Map" function.

How much time was it?? :)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
line from a point perpendicular to a line
« Reply #24 on: June 08, 2004, 10:54:00 AM »
Outstanding! Stig you are amazing!

Oh not much. I didnt get very far at all, but i did find this. (I dont know it even works or not ...dosent look like it would. :P)
Code: [Select]
(defun MapItter (pros lst / cntr nlst lstlength)
  (setq cntr 0 lstlength (length lst))
  (while (< cntr lstlength)
    (setq nlst (cons (pros (nth cntr lst)) nlst))
     (setq cntr (1+ cntr)))
  (reverse nlst)
  )


thanx Stig.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #25 on: June 08, 2004, 11:03:28 AM »
Mind if I rename it to MapItcher? :)

Well, it works for non-quoted predefined function names - so you can't pass lambda expressions to it.

I would write it with an evaluater, e.g.
(setq nlst (cons (eval (list pros (nth cntr lst))) nlst))

, or

(setq nlst (cons (apply pros (list (nth cntr lst))) nlst))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
line from a point perpendicular to a line
« Reply #26 on: June 08, 2004, 11:06:20 AM »
Just an observation.
Most of us think in English or our native language about lisp
but it appears to me that Stig has internalized lisp and now
"Thinks in LISP" :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #27 on: June 08, 2004, 11:12:43 AM »
Beware of the Knights who say "Nil!"

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
line from a point perpendicular to a line
« Reply #28 on: June 08, 2004, 11:32:11 AM »
Code: [Select]
(defun Convo2SMadsen ()
  (setq res (how are you?))
  (cond
    ((eq  (eval res) 'T)
     "Good, I'm glad to hear that")
    ((eq (eval res) 'nil)
     "Awe, too bad. Im sorry to hear that."
     )
    )
  )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #29 on: June 08, 2004, 11:40:26 AM »
Nil! Nil!