Programming Challenge No. 12

Programming Challenge No. 12

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

Programming Challenge No. 12

john.uhden
Mentor
Mentor

That's right boys and girls.

We are bypassing Challenge Nos. 5, 6, 7, 8 ,9 , 10, and 11 in favor of the 12th month of the year because that's when Christmas comes!

Digging back into history, it was in 1997 that the great Dietmar Rudolph challenged us to build a Christmas tree using AutoLisp.  Of course he included some rule about it being no more that 47 somethings long, which only his own was that short.  And his function created a very artistic tree.  I'm ashamed that I never kept a copy.  But I have kept a copy of my own entry, to which he gave honorable mention.  I have attached the file to inspire you all to do a better job than I.

Unlike Dietmar's rule, there is no limit to the amount of code you may create in your function, but everything must be created within the code.  No references to external anythings are allowed.  In fact each entry will consist of just one (1) AutoLisp file.  The use of Visual Lisp is just fine.  There will be no time trials.  In fact you might want to throw in some delays to slow down your creation to enjoy it more  (I should have done that).

Let's try to have all the entries in by Sunday, Dec. 19, so we can enjoy them all before Christmas day.  Maybe you can teach me how to make one of those video thingies that I can send to my children and theirs.

CAUTION:  Run my code only in an empty drawing because it erases everything.

John F. Uhden

2,083 Views
36 Replies
Replies (36)
Message 21 of 37

Sea-Haven
Mentor
Mentor

Sounds yummy, we have lots of choices here for shrimp/prawns if real keen go to end of my street at night and get some I am on a saltwater lake. Yes beef is creeping in price lamb is way more expensive, but we are lucky that steaks can be purchased at affordable price. Its more rock lobster is $75/Kg and plenty gets purchased for christmas lunch. A bite each.

0 Likes
Message 22 of 37

Sea-Haven
Mentor
Mentor
0 Likes
Message 23 of 37

john.uhden
Mentor
Mentor

Okay, Y'all

As I had hoped, I "spruced" up my tree to slow down its growth and to flicker the flames.

I did not add any Christmas spheres as that would be plagiarism.

Nor did I add any crashing Rudolphs (sorry, Alan).

You can read the revision notes inside the code.

;;AUTOCAD PROGRAMMING CHALLENGE NO. 2
;;C:XMASTREE
;;John F. Uhden, CADvantage Custom Utilities
;;CADvantage@compuserve.com
;;12-11-97
;;Note (12-08-02):
;;  Jim Fisher recalls better than I the rules of this
;;  programming challenge on the old ACAD Forum.
;;  Technically, this exceeded limitations and should
;;  have been disqualified.  Dietmar Rudolph's was
;;  technically correct and pretty darn nifty.  Too
;;  bad I don't have a copy to share.  In fact, we can
;;  thank Jim for retrieving this from his archives.
;;  Anyway, it's still cute.
;;Revised (12-08-02) for R15+
;;  Added vl-cmdf, UCSICON=0, and SHADE command only.
;;  Try setting SHADEMODE to different values before running,
;;  and then use the 3DORBIT command after.
;;Revised 12-19-2021 for Programming Challenge No. 12...
;;  a) added delay so that the tree can get formed more slowly
;;  b) improved size and shape of flames
;;  c) added flickering of flames until user hits Escape.
;;  d) added *error* function so alert shows up upon cancellation.
;;  The @shuffle function came to me from playing cards,
;;    which of course requires @cutting the deck.
;;  Yes, I know it is stupid to flicker flames that are behind the tree,
;;    but so what.
;;
;;  HAPPY HOLIDAYS!
(defun c:XMASTREE (/ *error* @ss2list @Anonymous @Anonymous @shuffle @Anonymous
                     rad R N Z dZ a da d H I J K +- s c)
(defun *error* (msg)
  (setvar "cmdecho" 1)
  (alert "\nMerry Christmas, OR ELSE!")
  (princ)
)
(vl-load-com)
(setq c (if (>= (getvar "acadver") "15") vl-cmdf command))
(setq d distance s setvar)(s "CMDECHO" 0)(s "UCSICON" 0)
(s "TILEMODE" 1)(s "UCSFOLLOW" 0)(s "HIGHLIGHT" 0)
(s "REGENMODE" 1)(c "_.LINE""0,0""1,1""")(c "_.ZOOM""_E")
(C"_.LAYER""_U""*""")(C"_.ERASE"(ssget"X")"")(c"_.UCS""_W")
(C "_.VPOINT""-3,-3,1.2")(C "_.ZOOM""_C""0,0,7" 15)
(defun rad (Z)(* 0.75 (sqrt (- 12.0 Z))))
(defun @ss2list (ss / i lst)
  (repeat (setq i (sslength ss))
    (setq lst (cons (ssname ss (setq i (1- i))) lst))
  )
)
(defun @Anonymous (n / stop)
  (setq stop (+ (getvar "millisecs") n))
  (while (> stop (getvar "millisecs")))
)
(defun @Anonymous (lst / n a b)
  (setq n (fix (* 0.5 (length lst))))
  (setq b (member (nth n lst) lst))
  (setq a (vl-remove-if '(lambda (x)(vl-position x b)) lst))
  (if (> (length b)(length a))
    (list b a)
    (list a b)
  )
)
(defun @shuffle (lst n / cut a b)
  (repeat n
    (setq cut (@cut lst) a (reverse (car cut)) b (reverse (cadr cut)) lst nil)
    (while a
      (setq lst (cons (car a) lst)
            lst (cons (car b) lst)
            a (cdr a)
            b (cdr b)
      )
    )
  )
  (vl-remove nil lst)
)
(defun @Anonymous (e / obj)
  (setq obj (vlax-ename->vla-object e))
  (repeat 1
    (@delay 100)
    (vlax-put obj 'color 23)
	(vla-update obj)
    (@delay 50)
    (vlax-put obj 'color 2)
	(vla-update obj)
  )
)
(setq N 25 Z 1.0 dZ 0.5 a 0.0 da (/ pi N) hpi (* pi 0.5))
(while (< Z 12.0)(setq R (rad Z))(repeat N
  (setq +- (if (= +- -) + -)
        H (list 0.0 0.0 (+- Z (* dz 0.25)))
        I (polar H a R) a (+ a da)
        I (mapcar '+ I (list 0.0 0.0 (* dZ 0.5)))
        J (polar H a (+ R R)) a (+ a da)
        J (mapcar '+ J (list 0.0 0.0 dZ))
        K (polar H a R)
        K (mapcar '+ K (list 0.0 0.0 (* dZ 0.1))))
  (entmake (list '(0 . "3DFACE")'(62 . 3)'(70 . 0)
  (cons 10 H)(cons 11 I)(cons 12 J)(cons 13 K)))
  (if (and (zerop (rem (1- Z) 1))(zerop (rem N 5)))(progn
    (entmake (list '(0 . "CIRCLE")(cons 10 J)'(40 . 0.07)
   '(39 . 0.35)'(62 . 255)))(@delay 50)(prompt " HO")
    (setq J (mapcar '+ J (list 0.0 0.0 0.4)))
    (entmake (list '(0 . "3DFACE")'(62 . 2)'(70 . 0)
	(cons 10 J)
    (cons 11 (polar (mapcar '+ J (list 0 0 0.15))(+ a hpi)(* (d I K) 0.3)))
    (cons 12 (mapcar '+ J (list 0.0 0.0 (* dZ 1.0))))
    (cons 13 (polar (mapcar '+ J (list 0 0 0.15)) (- a hpi)(* (d I K) 0.3)))))
  )))(terpri)(setq Z (+ Z dZ) a (+ a (/ da 2))))
(entmake '((0 . "3DFACE")(62 . 7)(10 -0.536 0.57073 13.225)
(11 0.5123 -0.545566 13.225)(12 -0.011815 0.012582 12.6687)
(13 -0.0118153 0.012582 12.6687)(70 . 0)))
(entmake '((0 . "3DFACE")(62 . 7)(10 -0.3357 0.3575 12.325)
(11 -0.0118 0.01258 13.7813) (12 0.18839 -0.200611 12.8812)
(13 0.188387 -0.200611 12.8812) (70 . 0)))
(entmake '((0 . "3DFACE")(62 . 7)(10 -0.536 0.57073 13.225)
(11 0.31212 -0.33237 12.3249) (12 0.111916 -0.11918 13.225)
(13 0.111916 -0.119179 13.225) (70 . 0)))
(c "_.SHADEMODE" "O")
(setq flames (@shuffle (@ss2list (ssget "x" '((0 . "3DFACE")(62 . 2)))) 7))
(while T
  (mapcar '@flicker (setq flames (@shuffle flames 3)))
)
(princ)
)

John F. Uhden

Message 24 of 37

john.uhden
Mentor
Mentor

@rgrainer 

That is truly a work of mechanical art!

I love it and am very jealous.

I'll have to study what you did as it all seemed impossible to me.

John F. Uhden

0 Likes
Message 25 of 37

john.uhden
Mentor
Mentor

@Sea-Haven 

CRAP.

ACAD 2002 doesn't have a HELIX command.

I'll have to run it at work tomorrow.

John F. Uhden

0 Likes
Message 26 of 37

rgrainer
Collaborator
Collaborator

John,
Please re-read my original post; I did not create the function, I just posted it and it was done by someone else (C J Follmer). I haven't looked at it for a long time and can't remember exactly what is did, but I recall it was pretty slick and full of animations and mechanical art. 

0 Likes
Message 27 of 37

john.uhden
Mentor
Mentor
Well, Mr./Ms. Follmer deserves some accolades.

John F. Uhden

0 Likes
Message 28 of 37

john.uhden
Mentor
Mentor

@Sea-Haven 

Alright!  Got to run it at work.

It seems as though you stole borrowed @Kent1Cooper 's tree, but you made the ornaments blink.  Never seen blinking ornaments before but they are pretty cool.  Just one slight problem... tree trunks in the Northern Hemisphere are more like color 37, not 94.  :]

BTW, do you use North arrows down there or South arrows?

Are your point.txt files in the format PSWZD?

John F. Uhden

0 Likes
Message 29 of 37

Sea-Haven
Mentor
Mentor

Santa gets here earlier as we are closer to the south pole so arrow points down.

0 Likes
Message 30 of 37

john.uhden
Mentor
Mentor

I want to give my sincere thanks to @Kent1Cooper , @Sea-Haven , and @rgrainer for providing us their skill and imagination to turn what I think of as a hobby into some Christmas enjoyment.  🎅  You are all winners!

😀👍👑

John F. Uhden

0 Likes
Message 31 of 37

Michiel.Valcke
Advisor
Advisor

You are all amazing ^^ Happy holidays to everyone ...

0 Likes
Message 32 of 37

arperezinf
Advocate
Advocate

Hello @john.uhden @Sea-Haven @Kent1Cooper @rgrainer 

The only thing missing from the Christmas tree is to throw snowballs like this Christmas Tree Throwing Snow. LOL

Can it be programmed to do the same?

 

Thank you very much for reading my message.
Greetings.

0 Likes
Message 33 of 37

john.uhden
Mentor
Mentor

Just wait 'til next year!

John F. Uhden

Message 34 of 37

Michiel.Valcke
Advisor
Advisor
Accepted solution

You all inspired me to make an Autolisp version of a New Years card. It isn't perfect (it's way over 49 lines as a starter) but I want to thank you all for the inspiration. Especially @john.uhden, I hope you don't mind, but I reused your shuffler idea for this. I also leaned heavily on Lee Mac's random number generator.

I'll try to participate in the christmas challenge next year.

0 Likes
Message 35 of 37

john.uhden
Mentor
Mentor
@Michiel.Valcke
I am in love with your snowflakes. They are even multidenominational.
Then again, there's one that looks like Covid-19. It's #3.
I am stealing them for next Christmas. Just gotta figure out how to have
them land on my tree boughs.
Hmm. Now that I look again, there aren't any 5-pointers. Are real
snowflakes always symmetrical?

John F. Uhden

0 Likes
Message 36 of 37

Michiel.Valcke
Advisor
Advisor

@john.uhden thank you very much, I'm in love with them as well.

Yes, real snowflakes are always based on hexagonal shapes (although some are more lenticular of cylindrical)

Btw, if you restart the program you'll get 40 different snowflakes, so I have no idea how #3 looks like for you ^^
I'm really proud of them, you could make them a lot larger or play with the configuration, but the code generates 'random' snowflake blocks when initialized so each time I tested, there were some that I really liked and others were a bit 'meh'.

Originally I had set it up to play with 10 different blocks each time the command was run, but through testing I found that even my old laptop could handle more, so I upped the count to make each experience more unique.

To make them land on a tree, what I do is once their y-value equals or is less than 0 I remove them from the list of snowflakes that need to fall and delete them. If you don't delete them, but just remove them from the list they would start to heap up at the X-axis. If you can detect if their position is close to a branch you can just do the same and remove them from the list at that point. they will stop moving and stay there.

I was also playing with the idea to draw a snow 'polyline' at the bottom like a ref cloud and each time a snowflake would fall I could move a random vertex of the snow 'polyline' some units up, so the snow at the bottom would slowly grow.

I wanted to do some color change effects in the text, but the code became very messy very quickly and it interfered with the timing of the snowflakes falling, so I left it out instead of trying to fix that. It's just a pity that I cannot force the text to the background, after every x iterations the snowflakes dissappear behind the text 😕 

0 Likes
Message 37 of 37

john.uhden
Mentor
Mentor
@Michiel.Valcke
Random snowflakes; I love it!
I'd have to study the code, but I don't see why the text doesn't stay in
back.

John F. Uhden

0 Likes