Programming Challenge 6/22

Programming Challenge 6/22

john.uhden
Mentor Mentor
2,069 Views
47 Replies
Message 1 of 48

Programming Challenge 6/22

john.uhden
Mentor
Mentor

This one has my head spinning.

The challenge is to write a function that returns the point on an ellipse where the tangent direction is equal to the one entered.

The function shall take two (2) arguments:

1.  the ename of the ellipse

2.  the desired tangent angle in radians

No, you will not make your own ellipse, nor have your function select one, nor add any code to confirm that the entity is in fact an ellipse.  If you can't use what I give you below, then tell your boss (if you have one) and tell your family (if you have one) and tell your friend (if you have one) that you are taking a one-way trip to the Ukraine (or Chatsworth, NJ if you prefer).

I am ignorant of any direct way to find the point, so to me it will require iteration to converge on the solution.

You may use a fuzz of 1e-8, nothing larger.  @calderg1000 is the only one entitled to use a fuzz of zero.

This exercise is only for determining the point in space where Tommy Lee Jones must exit the space station to land on the moon.  It's not for like building a geometrically perfect playground or something.

The ellipse is defined as:

(setq ellipse
(entmakex
'((0 . "ELLIPSE")(100 . "AcDbEntity")(67 . 0)(410 . "Model")
(8 . "0")(100 . "AcDbEllipse")(10 36.0 5.0 0.0)(11 -2.0 1.5 0.0)
(210 0.0 0.0 1.0)(40 . 0.5)(41 . 0.0)(42 . 5.0))
)
)

The angle in radians = 5.8

I have no idea as yet what the solution is, nor may I ever find out except by trial and error, or if @LeeMinardi shows me with his vector voodoo.

John F. Uhden

0 Likes
2,070 Views
47 Replies
Replies (47)
Message 2 of 48

devitg
Advisor
Advisor

@john.uhden as I can see , it is not a complete  ellipse , is it be so?

devitg_0-1646586026263.png

 

 

0 Likes
Message 3 of 48

john.uhden
Mentor
Mentor

@devitg 

Yes, it is open on purpose just to fluster some and because I may give you a different angle to solve later.

John F. Uhden

0 Likes
Message 4 of 48

john.uhden
Mentor
Mentor

Okay.  Here's my first attempt.

The convergence method came from just grasping at straws, as they say.

It's got more in it than needed, but that's just to please the eye (mine).

 

(defun @tanpt (ellipse tang / ctr p ang param)
  ;; where ellipse = ellipse ename
  ;;       tang = desired tangent angle in radians
  (setq ctr (vlax-get (setq obj1 (vlax-ename->vla-object ellipse)) 'center)
        p (polar ctr (- tang (* pi 0.5)) 1000) ;; gotta start somewhere
        p (vlax-curve-getclosestpointto ellipse p)   ;; ""
        param (vlax-curve-getparamatpoint ellipse p)
  )
  (while (not (equal tang (setq ang (angle '(0 0 0)(vlax-curve-getfirstderiv ellipse param))) 1e-8))
    (setq param (+ param (* 0.1 (- tang ang))))
    (print param)(prin1 ang)
  )
  (print ang)
  (setq p (vlax-curve-getpointatparam ellipse param))
  (setvar "pdmode" 34)
  (entmakex (list '(0 . "POINT")(cons 10 p)'(62 . 1)))
  p
)

 

 

John F. Uhden

0 Likes
Message 5 of 48

doaiena
Collaborator
Collaborator

I guess we had a similar idea for the approach @john.uhden.

I came up with a quick approximation:

 

(defun TanPtAtAngle ( / ellipse ang ellipseObj tanP1 tanP2)

(if (setq ellipse (car (entsel)))
(progn
(setq ang 5.8)
(setq ellipseObj (vlax-ename->vla-object ellipse))
(setq tanP1 (vlax-curve-getclosestpointto ellipseObj (polar (vlax-get ellipseObj 'Center) (+ ang (/ pi -2)) 100.0)))
(setq tanP2 (vlax-curve-getclosestpointto ellipseObj (polar (vlax-get ellipseObj 'Center) (+ ang (/ pi 2)) 100.0)))

(princ (strcat "\nTanP1: " (vl-princ-to-string tanP1) "\nTanP2: " (vl-princ-to-string tanP2)
"\nDeviation in TanP1 angle: " (vl-princ-to-string (- ang (angle '(0 0 0) (vlax-curve-getFirstDeriv ellipseObj (vlax-curve-getParamAtPoint ellipseObj tanP1)))))
"\nDeviation in TanP2 angle: " (vl-princ-to-string (- ang (angle (vlax-curve-getFirstDeriv ellipseObj (vlax-curve-getParamAtPoint ellipseObj tanP2)) '(0 0 0))))
))
))
(princ)
);defun

 

0 Likes
Message 6 of 48

john.uhden
Mentor
Mentor

@doaiena 

Why do so many of you guys have such an anathema to following directions?

I explicitly said to make a function that takes two arguments.  Albeit yours appears to duplicate what was provided, yours takes no arguments.

I have yet to test it, but I would bet your function provides the correct solution, that is providing that mine is correct.  I'm sure that @dbroad will let me know.

Doesn't matter though because someone will hopefully provide a calculus based solution that requires no convergence mechanism.  I haven't done any calculus since college many decades ago, or maybe it was high school, "when I wore a younger man's clothes."

John F. Uhden

0 Likes
Message 7 of 48

Sea-Haven
Mentor
Mentor

No idea how to put this into practice

SeaHaven_0-1646644006717.png

 

Message 8 of 48

dbroad
Mentor
Mentor

I don't have time to think about it but it would seem a direct solution would be possible if you converted the data for the ellipse into an equation for the ellipse and then used the first derivative of the equation.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 9 of 48

john.uhden
Mentor
Mentor

@dbroad

If I understood it, I would do it.  I think it's a little late to be going back almost 60 years.

At least I think I came up with some kind of answer, albeit my convergence mechanism was more like whittled down from scrap wood than die cast or injection molded.

John F. Uhden

0 Likes
Message 10 of 48

john.uhden
Mentor
Mentor

So, @hak_vz, @ВeekeeCZ, @pbejse, @ronjonp, @leeminardi, @Kent1Cooper, et al,

where are you guys and gals?  This is no fun if I am the only one to answer according to the guidelines with some hippy dippy convergence mechanism that I pulled out of the air.  😩

John F. Uhden

0 Likes
Message 11 of 48

hak_vz
Advisor
Advisor

@john.uhden 

I will post my solution (hopefully) through the weekend. Your request is challenging. I have started with my code, and I'll try to make solution according to your original request where you only have ellipse entity and desired tangent angle. As a result I would like to see tangent line created at tangent point. 

@Sea-Haven  This is something worth to try to code.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 12 of 48

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

... write a function that returns the point on an ellipse where the tangent direction is equal to the one entered.

The function shall take two (2) arguments:

1.  the ename of the ellipse

2.  the desired tangent angle in radians

....


.... and in the case of all closed/full Ellipses and many open ones, will have two solutions where the virtual Line(s) at the tangent angle touch opposite sides of the Ellipse.  Does it matter which point is returned, or should both be when there are two?

 

EDIT:  And, for some partial Ellipses and tangency directions, there can be no solution.  I assume that should be reported in some way.

Kent Cooper, AIA
0 Likes
Message 13 of 48

john.uhden
Mentor
Mentor

@hak_vz 

In my playing around before I figured out how the tangent worked, I drew lines tangent to points just to see which way they were headed.  Then I decided to leave it out.  Sure, add that visual confirmation.  Certainly no deductions.  Just be sure to stick that landing on the triple loop.

John F. Uhden

0 Likes
Message 14 of 48

john.uhden
Mentor
Mentor

@Kent1Cooper 

Whuh?  You lost me there.  Two solutions?  I think of an ellipse as being just an oblong circle, not some amoeba shaped polyline.  We're talking tangent here, not perpendicular.  I can't see how there would be more than one tangent point.

Please enlighten me.

John F. Uhden

0 Likes
Message 15 of 48

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

....  Two solutions?  ....  I can't see how there would be more than one tangent point.

Please enlighten me.


It wouldn't be true in the case of your example Ellipse, because one of its ends doesn't extend far enough to reach the tangency point with the desired angle.  But if that end [the right end of the break] extended farther, or for any full/closed Ellipse, there are these two tangencies with the desired angle [green]:

Kent1Cooper_0-1646923999414.png

Here's an example of the no-solution situation, using your same 5.8-radian direction and a reduced version of your example Ellipse [obviously possible with partial Ellipses only]:

Kent1Cooper_0-1646923744543.png

 

Kent Cooper, AIA
0 Likes
Message 16 of 48

john.uhden
Mentor
Mentor

@Kent1Cooper 

I am sorry to disagree with you.

While they are parallel tangents, their directions are opposite.

But you knew that and were trying to snooker me.  🤓

For real fun, use my function with the same ellipse but a tangent direction of 3.2.  Then you can tell me my function is faulty.

John F. Uhden

0 Likes
Message 17 of 48

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

.... While they are parallel tangents, their directions are opposite. ....


Who says?  The two green Lines run in the same direction.  If you intended that the drawn direction of the Ellipse itself needs to be the same as that of the [virtual] Line, that should have been made clear in Message 1.  To my mind, the wording "the tangent direction is equal to the one entered" is legitimately interpretable as meaning the direction of the [virtual] Line that would be tangent at the desired point.

Kent Cooper, AIA
0 Likes
Message 18 of 48

dbroad
Mentor
Mentor

I think John is looking for a vector, a ray tangent to the ellipse, not an xline.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 19 of 48

Kent1Cooper
Consultant
Consultant

But given that restriction, try this:

 

(defun ETPA (ellipse tang / ctr tangD eobj robj ints foundit) ; = Ellipse Tangent Point for Angle
  (setq
    ctr (getpropertyvalue ellipse "Center")
    tangD (* (/ tang pi) 180); in degrees for Rotate prompts
  ); setq
  (command "_.rotate" ellipse "" "_non" ctr "_reference" tangD 0); tang direction to horizontal
  (vla-getboundingbox (setq eobj (vlax-ename->vla-object ellipse)) 'minpt 'maxpt)
  (command
    "_.rectang" "_non" (vlax-safearray->list minpt) "_non" (vlax-safearray->list maxpt)
      ; rectangle with edges parallel/perpendicular to tang direction
    "_.rotate" ellipse (entlast) "" "_non" ctr "_reference" 0 tangD ; back to original orientation
  ); command
  (setq
    robj (vlax-ename->vla-object (entlast))
    ints (vlax-invoke eobj 'IntersectWith robj acExtendNone)
  ); setq
  (entdel (entlast)); delete rectangle
  (while (and ints (not foundit))
    (setq pt (list (car ints) (cadr ints) (caddr ints))); first [remaining] intersection
    (if
      (equal
        (angle
          '(0 0 0)
          (vlax-curve-getFirstDeriv ellipse (vlax-curve-getParamAtPoint ellipse pt))
        ); angle
        tang
        1e-8
      ); equal
      (setq foundit T); then [stop (while) loop, keep pt variable]
      (setq ints (cdddr ints)); else - remove latest intersection
    ); if
  ); while
  (if foundit
    (progn ; then
      (setvar 'pdmode 35); so point shows
      (command "_.point" "_non" pt)
    ); progn
    (prompt "\nEllipse does not pass at that direction anywhere."); else
  ); if
  (princ)
); defun

 

Kent Cooper, AIA
0 Likes
Message 20 of 48

john.uhden
Mentor
Mentor

@Kent1Cooper 

It doesn't count for much but I said it.

Where do "virtual" lines come into this?

Yes, I can draw a house footprint CW or CCW and it's the same footprint, but here we are talking about directions.  Even a line has one start point and one endpoint, which give it a direction.  And a first derivative is a direction.

So if you're driving up the road and come to an intersection, will a left turn point you in the same direction as a right turn?  And don't try giving me the tangent argument.  A tangent is just a unitless ratio.  A direction has units, in this example radians.  Should I have provided a surveyor's bearing instead?  I hope you won't try to tell me that a bearing of N45°W is the same direction as S45°E.  If you think they're the same "virtually" then you're not getting a job where I work.

BTW, I had said nothing about the drawn direction of the ellipse.  I just provided the entmake list so that everyone started out on the same page.

If you want to throw some mud in my face, tell me my function won't work on a mirrored ellipse (210 0.0 0.0 -1.0).

It doesn't.  I hadn't thought about that at all.

John F. Uhden

0 Likes