Beginner's Programming Challenge

Beginner's Programming Challenge

john.uhden
Mentor Mentor
2,599 Views
36 Replies
Message 1 of 37

Beginner's Programming Challenge

john.uhden
Mentor
Mentor

This challenge is NOT for the omnipresent brainiacs who solve the problems of all the needy.

This challenge is to write a function that takes two (2) entity names of two (2) separate circles to see if they are concentric.  The function should return either T or 1 for true and nil for not concentric.

Your code is limited to no more than 25 lines.  A line that word-wraps will be considered as multiple lines.

No help from the regulars is permitted.

First prize will be a weekend in Chatsworth, NJ.  2nd prize will be 5 days in a row in Chatsworth, NJ.

You will have to bring your own pick-up truck and potable water.

John F. Uhden

0 Likes
Accepted solutions (1)
2,600 Views
36 Replies
Replies (36)
Message 21 of 37

john.uhden
Mentor
Mentor

@john.kaulB9QW2 

It was years ago that Stephan Koster demonstrated his use of a long (and ....), and I became enamored with it.

Instead of only two choices with (if a (b) (c)), you could string together a littany of operations that would cease to be evaluated if any expression returned nil.  If you knew the difference between nil and anything non-nil, you could carry on without an error and retain control of that which followed.  Or STOP without an error.  I don't like errors.

I am jealous of your C capabilities, but like many others, I am comfortable living in the AutoLisp realm, often pushing it to its extremes, and learning from and teaching others to help themselves.  It's fun (to me).

AutoLisp is cool because you can use a simple text editor to create functions, and even just copy lines of code into the AutoCAD command line to test.  I mostly don't bother to save and load because it might not be worth saving (or I can't think of a good name that I haven't already used, or remember later why I created it or what it was supposed to do.).  Though I do use ZTree to search for a function or just words to find something that maybe I've already created that might be what I need for the current pipe dream.  I am extremely impressed by @hak_vz who can find almost anything in the forum history.  I think of him as our Forum Historian.  There are decades of valuable responses and code that can solve your problem of the day... stuff from Tony Tanzillo and Michael Puckett and Owen Wengerd, and Luis Esquivel, and Dietmar Rudolf and Vladimir Nesterovski (sp?), and Joe Burke and Larry Leuallen (sp?) and Steve Johnson, and Doug Broad, to name a few.  Special tribute goes to Peter Petrov, who invented Vital Lisp, which was acquired by Autodesk to become Visual Lisp.  God bless him.

Too much, probably.  What was your question?

John F. Uhden

0 Likes
Message 22 of 37

john.uhden
Mentor
Mentor

Alright, Doug!

I am flattered by your imitation.  😁

BTW, check out my recent response to JohnK about Stephan Koster.

John F. Uhden

0 Likes
Message 23 of 37

ronjonp
Mentor
Mentor

@john.uhden Of course .. now you broke your own rules 😂. My help was sculpting an answer provided by an "amateur' .. which I don't have a problem with.

I would have done similar but adding the fuzz as an argument.

 

(defun concentric (e1 e2 f) (equal (cdr (assoc 10 (entget e1))) (cdr (assoc 10 (entget e2))) f))

 

 

 

 

 

 

0 Likes
Message 24 of 37

calderg1000
Mentor
Mentor

Dear @john.uhden 

Greetings to all, teachers. I very much appreciate your brilliant demos, but sadly none of them do not qualify for the award.
Therefore, if I am not late, I attach the following code, I would like to clarify that in the first and second sentence I request to select a circle, therefore I do not do the check suggested by..., @john.kaulB9QW2, to save me a few lines and with it I exclude the possibility that the user selects another entity that also contains a DXF code = 10
@ronjonp  I gladly accept suggestions for improvements

 

 

 

(defun c:cc4 (/ c1 c2 p1 p2)
  (setq p1 (cdr (assoc 10 (entget(car (entsel "\nSelect Circle 1")))))
        p2 (cdr (assoc 10 (entget(car (entsel "\nSelect Circle 2\n")))))
  )
  (if (equal (mapcar '- p1 p2) '(0. 0. 0.)) t
    )
)

 

 

Here one line less

 

(defun c:cc5 (/ c1 c2 p1 p2)
  (if (equal
        (cdr (assoc 10 (entget (car (entsel "\nSelect Circle 1")))))
             (cdr (assoc 10 (entget (car (entsel "\nSelect Circle 2\n")))))
        )
        t
      )
  )

 

 


Carlos Calderon G
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 25 of 37

ronjonp
Mentor
Mentor

See comments below:

 

 

(defun c:cc4 (/ c1 c2 p1 p2)
  ;; If you're going to select circles like this, you should validate your data
  (if (and (setq p1 (car (entsel "\nSelect Circle 1")))
	   (setq p2 (car (entsel "\nSelect Circle 2")))
	   (setq p1 (cdr (assoc 10 (entget p1))))
	   (setq p2 (cdr (assoc 10 (entget p2))))
      )
    ;; This
;;;  (if (equal (mapcar '- p1 p2) '(0. 0. 0.))
;;;    t
;;;  )
    ;; Is the same as this
    (equal (mapcar '- p1 p2) '(0. 0. 0.))
  )
)
;; Better yet create a function that accepts two ENAMES
(defun cc4 (e1 e2)
  ;; I don't check that the items passed are valid
  ;; because you should take care of that prior
  (equal (mapcar '- (cdr (assoc 10 (entget e1))) (cdr (assoc 10 (entget e2)))) '(0. 0. 0.))
)

 

 

0 Likes
Message 26 of 37

john.uhden
Mentor
Mentor

@calderg1000 

Excuse me, Cal, but I wrote the rules, which you might want to read again.

And, as @Kent1Cooper opined, it's up to the programmer who might use the function to supply what is required (two circle entities, not text or mtext or lines or polylines or etc. or any object selection) in spite of @john.kaulB9QW2 's verbosity.

Why it had to take a master, @dbroad , to provide the best solution to a simple challenge is baffling to me.  Then again, he almost always provides us Grade A++ work.  I was hoping to send out myriad acceptances, but another idea of mine went south instead.

Please accept my apologies for your disappointment.

John F. Uhden

0 Likes
Message 27 of 37

john.uhden
Mentor
Mentor

Yes, @ronjonp , one should validate the entities before calling the function, but the validation was not to be any part of the function.

BTW, I don't think any cdrs are required, well, except for the way your fuzz factor is implemented, which by the way allows no tolerance at all.  I had no idea that a fuzz factor could be a list.  But that could be cool if wanting to compare say only x or y or z values, if it works that way.

John F. Uhden

0 Likes
Message 28 of 37

Kent1Cooper
Consultant
Consultant

@calderg1000 wrote:
....
  (if (equal (mapcar '- p1 p2) '(0. 0. 0.)) t
    )
....

You really don't need the (if) or the (mapcar) there.  The (equal) function can compare point lists, and the result of that can be fed out directly.  The same result can be obtained from just:

....
  (equal p1 p2)
....

thought it's better to include a fuzz factor:

....
  (equal p1 p2 1e-4)
....
Kent Cooper, AIA
0 Likes
Message 29 of 37

ronjonp
Mentor
Mentor

@Kent1Cooper wrote:

@calderg1000 wrote:
....
  (if (equal (mapcar '- p1 p2) '(0. 0. 0.)) t
    )
....

You really don't need the (if) or the (mapcar) there.  The (equal) function can compare point lists, and the result of that can be fed out directly.  The same result can be obtained from just:

....
  (equal p1 p2)
....

thought it's better to include a fuzz factor:

....
  (equal p1 p2 1e-4)
....

Very true. Even in @john.uhden 's simplistic answer.

0 Likes
Message 30 of 37

calderg1000
Mentor
Mentor

Very grateful @ronjop, an excellent demonstration to simplify things.
Regarding the Fuzz, in my limited knowledge of Euclidean geometry, I believe that with an infinitesimal displacement of the center of one of the circles, the Fuzz, would be validating eccentric circles


Carlos Calderon G
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 31 of 37

john.uhden
Mentor
Mentor

@calderg1000 

It's all a matter of perception.

With @dbroad 's offering you can use any fuzz you want, including 0.0  (no, 0.00 is no more accurate than 0.0), but it does have to be a 'REAL.

John F. Uhden

0 Likes
Message 32 of 37

dbroad
Mentor
Mentor

@calderg1000 : With fuzz, you are in charge of what is acceptable.  Life itself is eccentric.  Can a surveyor measure a piece of property to an infinite precision?  Do we know pi to the last digit?  You get the idea.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 33 of 37

calderg1000
Mentor
Mentor

Dear Dbroad
I greatly appreciate your answer and I agree with you in the case of real situations. In ideal situations I would prefer to obtain the maximum degree of accuracy allowed.

Regards.


Carlos Calderon G
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 34 of 37

john.uhden
Mentor
Mentor

@dbroad 

No, the surveyor can not measure to an infinite precision, but he can reduce the measured distance's precision so that the measured offset of 9.96' can be published as 10.0' to meet the ordinance requirement.  Close enough, eh?

We do it all the time.  In fact it's our standard.  It would be sinful for the town to tell the builder to demo his poured reinforced foundation and start from scratch, especially when he has prematurely gotten the first floor framing complete.

Occasionally we will get a small tremor while I have the footprint polyline gripped and the foundation shifts just 3 or 4 hundredths in a beneficial direction.  :]

Why, just yesterday the crew set a property corner nail in the top of a vinyl fence post.  Then they shoot the location.  OMG, it was off by 0.004', which to you is about a sixteenth, right?  But it's debatable as to where the center of the nail is relative to its exposed head.  It's just a matter of perception.

Hey, Cal, would you file a complaint if the neighbor's fence was built 1/16" over the property line?  HELLo, if the wind were blowing in the opposite direction it might be clear.

John F. Uhden

0 Likes
Message 35 of 37

john.uhden
Mentor
Mentor

@calderg1000 

As I said before, feel free to use a fuzz of 0.0000000000000000000000000000,

or if you prefer -1e99.

We just gave you the function.  It's up to you how to use it.

John F. Uhden

0 Likes
Message 36 of 37

calderg1000
Mentor
Mentor

Dear @john.uhden 

I really appreciate your knowledge very much; of course one thing is to draw in CAD and another to take the information to the ground, there the precision is lost due to many factors (theory of errors). I agree with you.
The Fuzz thing, I like it and I find it useful in some cases.
On the other hand, I know it was involuntary, but only for those who may not be able to tell the difference and it could lead to confusion; -1e99 is different than 1e-99

 


Carlos Calderon G
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 37 of 37

john.uhden
Mentor
Mentor

@calderg1000 

Thanks for noticing that and pointing it out.

I had looked at it and thought "That ain't right."  But I knew that some bright soul would recognize the error, so I just let it ride.

And it was really stupid because

Command: (minusp -1e99) T

though I just found out that you can use a negative fuzz (what for, I don't know).

John F. Uhden

0 Likes