Author Topic: CHALLENGE : Rectangles  (Read 21593 times)

0 Members and 1 Guest are viewing this topic.

Swift

  • Swamp Rat
  • Posts: 596
Re: CHALLENGE : Rectangles
« Reply #30 on: September 22, 2006, 06:57:20 PM »
It is fast enough Kerry, that was solved in Mathcad. If I was going to write a routine for that it would take me a great deal longer to simplify those equations than it would to write the program. The beauty of that system is that once you have the core matrix functions worked all you have to do is build the matrices for each application and voila!

I think I'll try to write something up this weekend, but the why behind how works is outside the scope of a single book. I think it would be easy enough to write a user manual type article about it.
« Last Edit: September 22, 2006, 06:59:02 PM by Swift »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CHALLENGE : Rectangles
« Reply #31 on: September 23, 2006, 10:13:33 PM »
Here's 2 options ...
The code that does the work is actually quite compact, the rest is fluff ...
.. these seem fairly fast.
The fitRectangle2 result seems to be more in line with David's posted Mathcad solution.

/// kwb

Quote
Command: (fitRectangle)

 After 34 attempts, the solution is 987.1477421877141
 The base angle is 18.31035073198846

Command: (fitRectangle2)

 After 47 attempts, the solution is 987.1477421881225
 The base angle is 18.31035073352091


Command: (benchmark '((fitRectangle  1000.0 500.0 200.0) (fitRectangle2  1000.0 500.0 200.0)))

Elapsed milliseconds for 32768 iteration(s)/ relative Timing :

    (FITRECTANGLE2 1000.0 500.0 200.0).....1797 / 1.009 <slowest>
    (FITRECTANGLE 1000.0 500.0 200.0)......1781 / 1 <fastest>

edit added : I commented out the prompts for the Benchmark testing

Code: [Select]
(DEFUN fitrectangle2
       (width height depth /)
  ;; kwb .. proof of concept code
  ;; based on ( (W+H)/(L+D)^2) + (W-H)/(L-D)^2) = 2
  ;; where W and H is larger rectangle, L and D is smaller inscribed rectangle.
  ;; .. could be optimised ..
  ;;
  (SETQ break   nil
        attempt 0
        fuzz    1.0e-14
        maxlen  (EXPT (+ (EXPT width 2)
                         (EXPT height 2)
                      )
                      0.5
                )
        minlen  0
  )
  (IF
    (> depth
       (EXPT (+ (EXPT (/ height 2.0) 2)
                (EXPT (/ width 2.0) 2)
             )
             0.5
       )
    )
     (PROMPT "\n No solution is possible"
     )
     ;;
     ;; else
     (PROGN
       (WHILE (NOT break)
         (SETQ attempt (1+ attempt)
               ;; set up a halving solution
               leng    (+ (/ (- maxlen minlen) 2)
                          minlen
                       )
               tmp     (- 2.0
                          (+ (EXPT
                               (/ (+ width height)
                                  (+ depth leng)
                               )
                               2
                             )
                             (EXPT
                               (/ (- width height)
                                  (- depth leng)
                               )
                               2
                             )
                          )
                       )
         )
         (IF (< (ABS tmp) fuzz)
           (SETQ break T)
           ;; else     
           (IF (MINUSP tmp)
             (SETQ minlen leng)
             ;; else
             (SETQ maxlen leng)
           )
         )
       )
       (PROMPT
         (STRCAT
           "\n After "
           (ITOA attempt)
           " attempts, the solution is "
           (RTOS leng 2 16)
         )
       )
       (PROMPT
         (STRCAT
           "\n The base angle is "
           (ANGTOS
             (-
               (kdub:asin
                 (/
                   height
                   (EXPT (+ (EXPT leng 2)
                            (EXPT depth 2)
                         )
                         0.5
                   )
                 )
               )
               (ATAN depth leng)
             )
             0
             15
           )
         )
       )
     )
  )
  (PRINC)
)

Code: [Select]
(DEFUN fitrectangle
       (width height depth /)
  ;;
  ;; for MWK.ctsa .. proof of concept ..
  ;; based on algorithm by George C. for TSD < in C++ >
  ;; kwb 1998 apr 16
  ;; note w and h are half rectangle values ..
  (SETQ w       (* width 0.5)
        h       (* height 0.5)
        d       (* depth 1.0)
        maxb    d
        minb    0.0
        exptd   (EXPT d 2)
        fuzz    0.0000001
        attempt 0
        break   nil
  )
  (IF
    (> d
       (EXPT (+ (EXPT h 2) (EXPT w 2))
             0.5
       )
    )
     (PROMPT "\n No solution is possible"
     )
     ;;
     ;; else
     (PROGN
       (WHILE (NOT break)
         (SETQ attempt (1+ attempt)
               b       (+ (/ (- maxb minb) 2) minb)
               exptb   (EXPT b 2)
               c       (EXPT (- exptd exptb) 0.5)
               o       (+ (- exptd (* exptb 2))
                          (* 2 b h)
                       )
               x       (/ o (* c 2))
               e       (- x w)
         )
         (IF (< (ABS e) fuzz)
           (SETQ break T)
           ;; else     
           (IF (> e 0)
             (SETQ maxb b)
             ;; else
             (SETQ minb b)
           )
         )
       )
       (SETQ len
              (* 2
                 (EXPT
                   (- (+ (EXPT (- h b) 2)
                         (EXPT w 2)
                      )
                      (EXPT (* d 0.5) 2)
                   )
                   0.5
                 )
              )
       )
       (PROMPT
         (STRCAT
           "\n After "
           (ITOA attempt)
           " attempts, the solution is "
           (RTOS len 2 15)
         )
       )
       (PROMPT
         (STRCAT "\n The base angle is "
                 (ANGTOS (ATAN c b) 0 15)
         )
       )
     )
  )
  (PRINC)
)


Code: [Select]
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
;;; arcsine (inverse sine) accepts an argument in the range
;;; -1.0 to 1.0 inclusive, and returns an angle in radians in
;;; the range -pi/2 to pi/2 inclusive.
(defun kdub:asin (num)
  (cond ((> (abs num) 1.0)
         (alert (strcat " Arc-sine error in (KDUB:asin ." "\n Spitting the dummy"))
         (exit)
        )
        ((zerop num) 0.0)
        ((= num 1.0) (* pi 0.50))
        ((= num -1.0) (- (* pi 0.50)))
        (t (atan num (sqrt (- 1.0 (* num num)))))
  )
)
« Last Edit: September 23, 2006, 10:32:30 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Swift

  • Swamp Rat
  • Posts: 596
Re: CHALLENGE : Rectangles
« Reply #32 on: September 25, 2006, 06:45:58 AM »
Kerry your code is certainly more compact than mine would be!

I got started on explaining mine but didn't get finished this weekend, hopefully I'll have something up in a couple of days.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: CHALLENGE : Rectangles
« Reply #33 on: September 25, 2006, 11:06:13 PM »
Love this thread.
Swamp pride.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: CHALLENGE : Rectangles
« Reply #34 on: September 26, 2006, 08:55:38 AM »
Very well done Kerry, as usual.  :-) :-) :-)
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.

whdjr

  • Guest
Re: CHALLENGE : Rectangles
« Reply #35 on: September 26, 2006, 04:34:53 PM »
I am the only one that thinks Swift's inner box is not a rectangle?  It looks more like a parallelogram.

Quote from: Kerry Brown
CHALLENGE : Rectangle pegs in Rectangular holes ..

Provide geometry proof or code to determine the length of a rectangular shape to fit into a rectangular hole as shown

Maverick®

  • Seagull
  • Posts: 14778
Re: CHALLENGE : Rectangles
« Reply #36 on: September 26, 2006, 04:35:50 PM »
  I'm a little warped so it looks right to me.   :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CHALLENGE : Rectangles
« Reply #37 on: September 26, 2006, 04:46:10 PM »
Maverick's proclivity aside ;

Will,
David's solution has the values for A , B and length correct, he just has a wonky drawing .. so I'd assumed he was drawing in architectural mode, not civil ...




« Last Edit: September 26, 2006, 04:56:13 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Swift

  • Swamp Rat
  • Posts: 596
Re: CHALLENGE : Rectangles
« Reply #38 on: September 26, 2006, 05:50:14 PM »
Will: it's a scaling issue with the picture, check the numbers.

Kerry: that hurts.

LE

  • Guest
Re: CHALLENGE : Rectangles
« Reply #39 on: September 26, 2006, 05:53:37 PM »
Interesting solutions and challenge- you guys rock! -

Very nice, approach you got in the George C. algorithm - Kerry.

Btw, who is he? - just out of curiosity.


And to master Swift, great post.


Thanks.


Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: CHALLENGE : Rectangles
« Reply #40 on: September 26, 2006, 06:00:45 PM »
Very well done Kerry, as usual.  :-) :-) :-)

You know he posted the challenge right?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CHALLENGE : Rectangles
« Reply #41 on: September 26, 2006, 06:03:19 PM »
Sorry David ... I was feeling frivolous :-)

Luis,
George is a C/C++ Programmer acquaintance/associate here in Brisbane.
The fitRectangle is a translation of some C code he wrote years ago.
The fitrectangle2 is based on a pure math formulae I found on the net < didn't record the source >  

( ((W+H)/(L+D))^2) + ((W-H)/(L-D))^2) = 2

getting it into lisp wasn't too hard, once I understood what was happening.
« Last Edit: September 26, 2006, 06:27:18 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CHALLENGE : Rectangles
« Reply #42 on: September 26, 2006, 06:05:23 PM »
Very well done Kerry, as usual.  :-) :-) :-)

You know he posted the challenge right?

I think he may know that, Greg. He probably knows I've had a few years to work on a solution as well. :lol:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: CHALLENGE : Rectangles
« Reply #43 on: September 26, 2006, 07:14:47 PM »
Great thread, even better challenge! Kudos to all .... and you too Kerry. :-)
TheSwamp.org  (serving the CAD community since 2003)

Maverick®

  • Seagull
  • Posts: 14778
Re: CHALLENGE : Rectangles
« Reply #44 on: September 26, 2006, 09:58:55 PM »
  Hopefully Swift you knew I was jabbing ya.  The only thing I could do like that is drive a square peg into a round hole with a big hammer.

  I'm still a little burned over you blowing me up all the time in UT.  :-D