TheSwamp

CAD Forums => CAD General => Topic started by: Kerry on September 21, 2006, 04:09:49 AM

Title: CHALLENGE : Rectangles
Post by: Kerry on September 21, 2006, 04:09:49 AM
It's been a while since we've had a challenge, so ...

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 :-

Assume
Shape depth = 200
Opening Height = 500
Opening Width = 1000

Enjoy .. :-)

/// kwb
Title: Re: CHALLENGE : Rectangles
Post by: Jeff_M on September 21, 2006, 04:25:28 AM
Sure! Throw this up when I should be sleeping! No code for me tonight, and all the 'other' swampers in other time zones will see it before I wake...<sigh>
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 21, 2006, 04:27:37 AM
Sorry Jeff ... It may still  be unresolved when you wake ...  so don't lose any sleep over it :-)
Title: Re: CHALLENGE : Rectangles
Post by: David Hall on September 21, 2006, 10:01:41 AM
Its to early for TRIG Kerry, neeed moooorrre   coffeeeeeee
Title: Re: CHALLENGE : Rectangles
Post by: T.Willey on September 21, 2006, 11:32:17 AM
Had this challenge over on the Adesk Ng, no one solved it, as far as I remember.  See if I can find the link.
Title: Re: CHALLENGE : Rectangles
Post by: LE on September 21, 2006, 11:34:08 AM
Had this challenge over on the Adesk Ng, no one solved it, as far as I remember.  See if I can find the link.

Asi es.... I think Joe Burke provided a solution.
Title: Re: CHALLENGE : Rectangles
Post by: T.Willey on September 21, 2006, 11:37:35 AM
Had this challenge over on the Adesk Ng, no one solved it, as far as I remember.  See if I can find the link.

Asi es.... I think Joe Burke provided a solution.
I think you might be right Luis.  I think he didn't like it though because it was using a testing method, and not a real math soloution.

Well here is the link. (http://discussion.autodesk.com/thread.jspa?messageID=4235879)
Title: Re: CHALLENGE : Rectangles
Post by: Greg B on September 21, 2006, 12:15:39 PM
x=opening width
y=opening height
z=shape depth

find w = shape width
If x=1000 then
w = x-y
(w = 1000-500)

w = 500


You just asked for it to fit in the rectangluar hole.... :wink:
Title: Re: CHALLENGE : Rectangles
Post by: Jeff_M on September 21, 2006, 03:11:56 PM
You just asked for it to fit in the rectangluar hole.... :wink:
Maybe you missed the "as shown" part? :)
Title: Re: CHALLENGE : Rectangles
Post by: Kate M on September 21, 2006, 03:19:32 PM
I'm pretty sure this requires differential equations to get a real solution -- I wrote a system of equations, and it appears to be a second-order problem. If I feel like dragging out an old textbook maybe I'll give it a shot.  :roll:
Title: Re: CHALLENGE : Rectangles
Post by: Greg B on September 21, 2006, 03:35:27 PM
You just asked for it to fit in the rectangluar hole.... :wink:
Maybe you missed the "as shown" part? :)

First rule of drafting...

Numbers come first, the way the drawing looks comes second.
Title: Re: CHALLENGE : Rectangles
Post by: Bob Garner on September 21, 2006, 04:03:25 PM
I once had to develop equations for moving the largest rectangle between two walls and around a corner, somewhat like this puzzle.  I don't remember the solution but I remember it was based on the diagonal dimension of the inner rectangle.  Find the diagonal in one direction, it has to be equal and opposite in the other direction (mirrored?).  The rest is basic trig.

I'm busy trying to figure out some new concrete design equations today, so I don't want to even try to figgur this out.
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 21, 2006, 05:53:21 PM
I have it sketched out on my board at the office. I think Kate is right it would require calculus to solve explicitly, but I think I have it worked out where it can be solved using linear algebra. I plan on spending more time on it once I get home.
Title: Re: CHALLENGE : Rectangles
Post by: David Hall on September 21, 2006, 11:06:09 PM
I have the equation worked out where y is solved in terms of x .  I'll try and post a pic of the equation.  Tomorrow, I will see if I can get an answer
Title: Re: CHALLENGE : Rectangles
Post by: David Hall on September 21, 2006, 11:13:41 PM
I got
(200)^2 = (500- X1)^2 + (1000 - Y1)^2
40000 = 250000 - 1000X1 + (X1)^2 + (1000 - Y1)^2
-210000 - (1000 - Y1)^2 = -1000X1 + X1^2
X = SQRT(-Y^2 + 1000Y -210000) + 1000
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 22, 2006, 05:07:02 AM
Here's a teaser ...

I'll post my solution on later  ...

Title: Re: CHALLENGE : Rectangles
Post by: CAB on September 22, 2006, 10:51:56 AM
I took a shot at it. See crewed attempt below.

Limitations:
WCS only, Outer Rectangle parallel with X/Y
Side to be found < (/ height 2), I could overcome this with more code.


Method:
Draw a circle & check chord length where it intersects the outer rectangle.
 Keep adjusting radius until Chord length matches desired side.


<edit: Length =987.14773556>

Code: [Select]
(defun c:test (/ ang chord cir dis doc e1 e2 h p1 p2 p3 pc pc1 pc2 pc3 pc4 px rad
               side side2 space step tar w fuzz get_inter group_on3
              )


  ;;  test values
  (setq p1   '(-1000 1000 0) ; Lower Left Corner
        w    1000
        h    500
        side 200
        fuzz 0.0001 ; side match tolerance
  )


  ;;  get the intersect list, then return the closest point to pt
  (defun get_inter (ent1 ent2 pt / plist result)
    (setq plist
           (vl-catch-all-apply
             'vlax-safearray->list
             (list (vlax-variant-value (vla-intersectwith ent1 ent2 acextendnone)))
           )
    )
    (setq plist (group_on3 plist))
    (foreach x plist
      (cond
        ((null result)
         (setq result x)
        )
        ((> (distance pt result) (distance pt x))
         (setq result x)
        )
      )
    )
    result
  )

  (defun group_on3 (inplst / outlst tmp grp idx sub)
    (while inplst
      (setq outlst (cons (list (car inplst) (cadr inplst) (caddr inplst)) outlst))
      (setq inplst (cdddr inplst))
    )
    outlst
  )


  ;;======================================================================

  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (setq space
         (if (zerop (vla-get-activespace doc))
           (if (= (vla-get-mspace doc) :vlax-true)
             (vla-get-modelspace doc) ; active VP
             (vla-get-paperspace doc)
           )
           (vla-get-modelspace doc)
         )
  )

  ;;  draw limit lines
  (setq chord 0
        p2    (polar p1 0 w)
        p3    (polar p2 (/ pi 2) h)
        pc    (polar p1 (angle p1 p3) (/ (distance p1 p3) 2.))
  )
  (setq e1 (vla-addline space (vlax-3d-point p1) (vlax-3d-point p2)))
  (setq e2 (vla-addline space (vlax-3d-point p2) (vlax-3d-point p3)))
  (if (> h w)
    (setq tar e1)
    (setq tar e2)
  )

 
  ;;  get chord location to match side length
  (setq rad  (+ (distance pc (vlax-curve-getclosestpointto tar pc)) 0.1)
        step (- (distance pc p2) rad)
  )
  (while (progn
           (setq cir (vla-addcircle space (vlax-3d-point pc) rad))
           (setq pc1 (get_inter e1 cir p2)
                 pc2 (get_inter e2 cir p2)
           )
           (setq dis  (distance pc1 pc2)
                 step (/ step 2)
           )
           (vla-delete cir)
           (cond
             ((equal dis side fuzz)
              nil
             )
             ((> dis side)
              (setq rad (+ rad step))
             )
             (t
              (setq rad (- rad step))
             )
           )
         )
  )

  ;;  Draw the rectangle
  (setq e1 (vla-addline space (vlax-3d-point pc1) (vlax-3d-point pc2)))
  (setq px (vlax-curve-getclosestpointto e1 pc))
  (setq side2 (* (distance pc px) 2)
        ang   (angle px pc)
  )
  (setq pc3 (polar pc2 ang side2)
        pc4 (polar pc1 ang side2)
  )
  (vla-addline space (vlax-3d-point pc1) (vlax-3d-point pc4))
  (vla-addline space (vlax-3d-point pc2) (vlax-3d-point pc3))
  (vla-addline space (vlax-3d-point pc3) (vlax-3d-point pc4))

 
  (vlax-release-object e1)
  (vlax-release-object cir)
  (vlax-release-object space)
  (vlax-release-object doc)
  (princ)
)
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 22, 2006, 02:50:26 PM
I'm uploading my solution to http://www.survey-this.com/KerryBrownRectangles.htm

I solved it using a mixture of least squares adjustment routines. The math isn't well noted but if anyone is interested I will try to explain it.
Title: Re: CHALLENGE : Rectangles
Post by: T.Willey on September 22, 2006, 03:14:46 PM
I'm uploading my solution to http://www.survey-this.com/KerryBrownRectangles.htm

I solved it using a mixture of least squares adjustment routines. The math isn't well noted but if anyone is interested I will try to explain it.
I tried to see what you did, but all the pictures looked like they had broken links.
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 22, 2006, 03:21:45 PM
They work on my machine, give it another try.

Here is a sketch to accompany the math

(http://www.survey-this.com/KerryBrownRectangles_images/SOLUTION.png)
Title: Re: CHALLENGE : Rectangles
Post by: T.Willey on September 22, 2006, 03:33:27 PM
Still doesn't work for me.  Maybe I'm special??  :-)

See attached.

Edit:  Removed picture as the problem got solved.
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 22, 2006, 03:37:02 PM
Looks like you'll have to use explorer, it wont open in Firefox for me.
Title: Re: CHALLENGE : Rectangles
Post by: T.Willey on September 22, 2006, 03:56:15 PM
Looks like you'll have to use explorer, it wont open in Firefox for me.
You are correct.  I right clicked on it, and used 'Open Link in IE Tab', and it worked with firefox.  Will look at it later today.  Thanks for posting it.
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 22, 2006, 05:02:32 PM
I'm uploading my solution to http://www.survey-this.com/KerryBrownRectangles.htm

I solved it using a mixture of least squares adjustment routines. The math isn't well noted but if anyone is interested I will try to explain it.

Holy equations David !! ....

I knew I should have done more than 3 years high school ... :-)

There is a beauty to that, even if I don't understand it ..
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 22, 2006, 05:07:14 PM
... but it looks like we both have the same solution, out to 7 decimal places
Title: Re: CHALLENGE : Rectangles
Post by: CAB on September 22, 2006, 05:51:06 PM
Wow Swift quite a piece of work. Sorry to say I don't understand it. Maybe in my next life. :-)

I had to change my fuzz factor to get that close.

fuzz = 0.0001
Count = 23
Length =987.14773556

fuzz = 0.00001
Count = 26
Length =987.14774210
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 22, 2006, 05:57:36 PM
Nice construction technique Alan ..
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 22, 2006, 06:05:57 PM
That stuff was brow beat into me over two semesters in college. At the time I thought it was a waste, of course, but I have found many uses for it over the years. I've used it for everything from closing survey loops to adjusting gps observations and like in this case fitting odd geometry.

I used the same method, different equations though, with Cornbread to write some neat coordinate transformation stuff. Sadly though I've never been able to get a routine in vb to work, but I have several c++ routines for this sort of thing.

If the forecast for a soggy weekend holds true I'll add some notes to that, I might even be able to squeeze an article out of it.



Title: Re: CHALLENGE : Rectangles
Post by: T.Willey on September 22, 2006, 06:08:27 PM
If the forecast for a soggy weekend holds true I'll add some notes to that, I might even be able to squeeze an article out of it.
That would be sweet, for us.  But I hope your weekend is nice, and are able to play outside.   :-D
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 22, 2006, 06:17:19 PM
Is it fast David ? There seems to be a lot of heavy duty exponent calcs being done.

... not that speed is really an issue !

Just rechecked my attempt .. it contains a significant quantity of exponent calcs as well ...
... no options when dealing with Pythagoras  :-)
Title: Re: CHALLENGE : Rectangles
Post by: Swift 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.
Title: Re: CHALLENGE : Rectangles
Post by: Kerry 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)))))
  )
)
Title: Re: CHALLENGE : Rectangles
Post by: Swift 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.
Title: Re: CHALLENGE : Rectangles
Post by: Bryco on September 25, 2006, 11:06:13 PM
Love this thread.
Swamp pride.
Title: Re: CHALLENGE : Rectangles
Post by: CAB on September 26, 2006, 08:55:38 AM
Very well done Kerry, as usual.  :-) :-) :-)
Title: Re: CHALLENGE : Rectangles
Post by: whdjr 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
Title: Re: CHALLENGE : Rectangles
Post by: Maverick® on September 26, 2006, 04:35:50 PM
  I'm a little warped so it looks right to me.   :-D
Title: Re: CHALLENGE : Rectangles
Post by: Kerry 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 ...




Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 26, 2006, 05:50:14 PM
Will: it's a scaling issue with the picture, check the numbers.

Kerry: that hurts.
Title: Re: CHALLENGE : Rectangles
Post by: LE 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.

Title: Re: CHALLENGE : Rectangles
Post by: Greg B on September 26, 2006, 06:00:45 PM
Very well done Kerry, as usual.  :-) :-) :-)

You know he posted the challenge right?
Title: Re: CHALLENGE : Rectangles
Post by: Kerry 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.
Title: Re: CHALLENGE : Rectangles
Post by: Kerry 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:
Title: Re: CHALLENGE : Rectangles
Post by: Mark on September 26, 2006, 07:14:47 PM
Great thread, even better challenge! Kudos to all .... and you too Kerry. :-)
Title: Re: CHALLENGE : Rectangles
Post by: Maverick® 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
Title: Re: CHALLENGE : Rectangles
Post by: Maverick® on September 26, 2006, 10:00:26 PM
Maverick's proclivity aside ;


  I had to look it up.  Good one.  :-D
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 27, 2006, 04:43:49 AM
Great thread, even better challenge! Kudos to all ....

Yep, this one turned out fine ...  there may be a couple of other solutions, we'll see ...
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 27, 2006, 07:44:50 AM
  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

No problems Mav.

I've moved on to Half-Life 2 nowadays, you orta get it, I'd enjoy killing you again.
Title: Re: CHALLENGE : Rectangles
Post by: Bryco on September 27, 2006, 09:57:33 AM
I did a vba version as I have needed this to see how big I can build something to still fit through a given door.
This draws the inner rectangle. Since I don't know lisp it took a while to copy Kerry's to vba. This seemed backwards to the lisp "If Temp > 0 Then  then Min = L"
Code: [Select]
Sub BestFit()

    Dim W As Double, H As Double
    Dim L As Double, D As Double
    Dim X As Double, Y As Double
    Dim X1 As Double, Y1 As Double
    Dim Pt(2) As Double
    Dim Ent As AcadEntity
    Dim oP As AcadLWPolyline
   
    Set Ent = EntSel("Pick the rectangular opening:")
    If TypeOf Ent Is AcadLWPolyline Then
    Set oP = Ent
    Else
        MsgBox "The opening must be a rectangle."
        Exit Sub
    End If
    If Not UBound(oP.Coordinates) = 7 Then
        MsgBox "The opening must be a rectangle."
        Exit Sub
    End If
   
    X = oP.Coordinates(0)
    Y = oP.Coordinates(1)
    X1 = oP.Coordinates(2)
    Y1 = oP.Coordinates(3)
     If Abs(X1 - X) < 0.00000000001 Then 'equals
        X1 = oP.Coordinates(4)
    End If
    If X1 < X Then
        X = X1
        X1 = oP.Coordinates(0)
    End If

    If Abs(Y1 - Y) < 0.00000000001 Then 'equals
        Y1 = oP.Coordinates(5)
    End If
    If Y1 < Y Then
        Y = Y1
        Y1 = oP.Coordinates(1)
    End If
   
    Pt(0) = X: Pt(1) = Y
    W = X1 - X: H = Y1 - Y
    D = ThisDrawing.Utility.GetDistance(, "inside rectangle depth:")
       
    RecInRec W, H, D, Pt

End Sub

Function RecInRec(W As Double, H As Double, _
            D As Double, Pt As Variant) As AcadLWPolyline
     ' based on ( (W+H)/(L+D)^2) + (W-H)/(L-D)^2) = 2
     'KerryBrown->vb
    Dim L As Double
    Dim Max As Double, Min As Double
    Dim Temp As Double, fuzz As Double
    Dim Attempt As Integer
   
    fuzz = 0.00000000000001
    Max = Sqr((W * W) + (H * H))
    If D >= W Or D >= H Then
        MsgBox "No go"
        Exit Function
    End If
    Do
        Attempt = Attempt + 1
        If Attempt = 1 Then Min = Max - D 'a square gives the shortest L
        L = Min + ((Max - Min) / 2)  'set up a halving solution
        Temp = ((W + H) / (L + D)) ^ 2 + ((W - H) / (L - D)) ^ 2 - 2
        If Temp > 0 Then
             Min = L
        Else
            Max = L
        End If
        If Abs(Temp) < fuzz Then
        Debug.Print "yes"
        End If
    Loop Until Abs(Temp) < fuzz
   
    Debug.Print Attempt, L
    'Draw the pline
    Dim Ang As Double, A As Double, B As Double
    Dim X As Double, Y As Double
    Dim P(7) As Double
    Dim oP As AcadLWPolyline
    Dim Ang1 As Double
   
    X = Pt(0): Y = Pt(1)
    Ang1 = Atn(D / L)

    If H > W Then
        If Ang1 > 0.25 * Pi Then Ang1 = Atn(L / D)
       Ang = ArcSin(W / (Sqr(D * D + L * L))) - Ang1
        Debug.Print Ang * 180 / Pi
        B = (Sin(Ang) * D)
        A = Abs(Cos(Ang) * D)
    Else
        Ang = ArcSin(H / (Sqr(D * D + L * L))) - Atn(D / L)
        B = Cos(Ang) * D
        A = W - Cos(Ang) * L
    End If
 
    P(0) = X + A: P(1) = Y
    P(2) = X + W: P(3) = Y + H - B
    P(4) = X + (W - A): P(5) = Y + H
    P(6) = X: P(7) = Y + B
    Set oP = ThisDrawing.ModelSpace.AddLightWeightPolyline(P)
    oP.Closed = True
    Set RecInRec = oP

End Function
Title: Re: CHALLENGE : Rectangles
Post by: SomeCallMeDave on September 27, 2006, 10:27:51 AM
Here is my attempt.  It is the least effecient so far (about 900 iterations), but what it lacks in speed it makes up in in-elegance  :)

The 'a' and 'b' are the same as in Swift's sketch in reply #19.

I used the the areas of the rectangles and resulting triangles and the fact that the triangles are similar to work out the equations.

Code: [Select]
(defun KerryQuest2(pOpenHeight pOpenWidth pShapeDepth)
    (defun GetB()  (sqrt (- (* Q Q) (* a a))))
    (setq  Q pShapeDepth
           X pOpenHeight
           Y pOpenWidth
       GuessA 1
       fuzz 0.0000000000001
       Count 0
     )
   
     ;first guess
    (setq a guessA
          b (GetB)
    );setq

    (defun Check() 
          (setq P1 (- X b)
                P2 (- Y a)
                P3 (/ P1 P2)
                P4 (/ a b)
          );setq
          (/ (min P3 P4) (max P3 P4))
    );defun check
   
    (setq Test (Check))
    (while (not (equal Test 1 fuzz))
           (setq a (+ a (- 1 Test))
                b (GetB)
                Count (+ 1 Count)
                Test (Check)
          );setq
    );while
     (setq L (/ (+ (* X Y) (* -1 (- X b) (- Y a) ) (* -1 a b )  )  Q))
     (princ (strcat "\nL=" (rtos L 2 15) " and Count =" (itoa Count)))
     (prin1)
)


The result
Code: [Select]
Command: (kerryquest2 1000 500 200)
L=987.1477421881179 and Count =888
Title: Re: CHALLENGE : Rectangles
Post by: whdjr on September 27, 2006, 11:27:58 AM
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 ...

Hey civil guys are the ones drawing all the curved lines...talk about wonky.
Title: Re: CHALLENGE : Rectangles
Post by: whdjr on September 27, 2006, 11:29:48 AM
Will: it's a scaling issue with the picture, check the numbers.

Kerry: that hurts.

I wasn't doubting your work Swift just commenting on the wonkyness of the picture...if you want to blame it on the swamp for wonkying your picture then so be it but .....

 :lol:


Good Solution.  :-)
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 28, 2006, 08:48:06 AM
Isn't language wonderfull !!

all sorts ..
Title: Re: CHALLENGE : Rectangles
Post by: solo on September 28, 2006, 12:26:33 PM
Hi everybody,

Let me start by saying that i am pretty new to this forum and i didn't realize that you had to be registered and logged in to actually see the attached pictures.  So for a while i was trying to figure out how any of you could come up with any sort of solution to this problem.  I figured you were all geniuses and that i should go back to school.  Now that i can see the pictures i actually understand what is being asked.

I noticed that a few solutions were posted and that they revolved around the use of vb code or autolisp and they seem to use trial and error until the length is determined.  These programs are pretty cool but i was hoping that someone would be able to determine a formula that allows you to plug in the depth, length and width and then returns the exact length of the peg.  I say someone because i have come to the conclusion that my trig skills aren't strong enough to find the answer myself but it seems like it would be possible.

We are given the length and width of the rectangular hole and the depth of the peg. After looking at it it seems that any one peg depth can have only one peg length regardless of the size of the rectangular hole (however, in many cases, a given peg length will have 2 peg depths).  I think if you were to graph this equation you would have some form of hyperbola that illustrates the change in length as the depth changes.

See attachment (if it actually gets attached)

- As we start from the minimum depth of 0 the peg length is at it's maximun length. 
- As the peg depth increases the peg length will begin to decrease.
- At some point the depth will continue to increase but the length will reverse and begin to increase

I've looked at this problem long enough to know that if i don't find an answer soon i will go crazy.  I really believe that there is a direct relationship between the depth of peg, the height and width of the rectangular hole, and ultimately the length of the peg.  If anyone knows for certain that there is not please let me know so i can stop pulling my hair out.  My TI82 graphing calculator isn't working so my last remaining hope has faded.  Perhaps someone can determine the formula from an X/Y list of peg depths and there resultant peg lengths.

I know this has been pretty long winded and i apologize.  Perhaps this wasn't the intended challenge.  If not, please consider this a new one. One formula that solves for any peg depth in any size hole.

Thanks,
solo
Title: Re: CHALLENGE : Rectangles
Post by: solo on September 28, 2006, 12:35:34 PM
     |
     |*
     |    *
 P  |        *
 E  |             *                             LENGTH OF RECTANGULAR HOLE
 G  | ---------------*---------------------------------------------------------------------------------------*
     |                              *                                                                                         *
 L  |                                          *                                                               *
 E  |                                                                *                       *
 N  |
 G  |
 T  |
 H  |
     |
     |
     |________________________________________________________________________________________
                                                                      PEG DEPTH

Shouldn't matter what size peg or what size hole.
Title: Re: CHALLENGE : Rectangles
Post by: deegeecees on September 28, 2006, 12:40:37 PM
See attachment (if it actually gets attached)

Noop, didn't get attached.

Welcome to the Pond. Check http://www.theswamp.org/index.php?board=22.0 (http://www.theswamp.org/index.php?board=22.0) if you haven't already.

May we call you Han?
Title: Re: CHALLENGE : Rectangles
Post by: solo on September 28, 2006, 12:51:02 PM
I won't "force" you to.

The attachment just showed a graph similar to the one that i created with text in my second post.
Title: Re: CHALLENGE : Rectangles
Post by: Maverick® on September 28, 2006, 12:54:19 PM
Bwahahahaha

Welcome!   
Title: Re: CHALLENGE : Rectangles
Post by: deegeecees on September 28, 2006, 12:55:04 PM
Touche!

 :lmao:

"But I wanted to go to the Hodgi Station to pick up some power converters!"

We now return you to our regularly scheduled program...
Title: Re: CHALLENGE : Rectangles
Post by: Greg B on September 28, 2006, 01:24:13 PM
Geeks!

 :grazy:
Title: Re: CHALLENGE : Rectangles
Post by: deegeecees on September 28, 2006, 02:15:06 PM
Geeks!

 :grazy:

Naw, I just think funny is as funny does, and I know from funny. Like this memorable quote:

Squad Leader: "Stay on target"
Porkins: "Their coming in too fast!"
Squad Leader: "Stay on target"
Porkins: "I caaant see em!"
Squad Leader: "Stay on target"
Porkins: "Whaaaaaa......"
Squad Leader: "We lost Porkins"


Poor Porkins.
Title: Re: CHALLENGE : Rectangles
Post by: Greg B on September 28, 2006, 05:14:16 PM
How about?
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 28, 2006, 05:40:17 PM
solo, welcome to the swamp ... ignore the comments from the peanut gallery. :lol:  someone let them out of their cage ..

now that you are logged in you can probably see the place where they usually live.

//// back on topic ..

If 'anyone' < points finger at you> can devise a solution that resolves this in one pass I'd be really interested in seeing it.
 
Title: Re: CHALLENGE : Rectangles
Post by: sinc on September 28, 2006, 06:26:13 PM

If 'anyone' < points finger at you> can devise a solution that resolves this in one pass I'd be really interested in seeing it.
 

I came up with a couple of different formulas for solving the problem in one pass, but they both ended up being complicated differential equations.  If there's a way to solve them symbolically, I either can't remember it or can't see it.  I haven't really done much of this stuff since college, fifteen-odd years ago...  The numerical solution is definitely easier, if you have a computer.
Title: Re: CHALLENGE : Rectangles
Post by: SEANT on September 29, 2006, 07:15:07 AM
I am also "Vexed" by this problem.  In the spirit of collaboration (and in the interest of giving my mind a rest) here's a contribution to a possible solution.

The only concrete thing I have is the equation to find the "Shortest Hypotenuse" rectangle for a given hole (see also attached .xls).  This rectangle has the unique properties of a hypotenuse equal to Hole length, corners at 1/2 height of Hole rectangle, , 1/2 Area of Hole rectangle Area.  Another interesting property is A x B = (1/2H)2.

With any luck this info will shake out some new ideas.

Sean

(http://www.theswamp.org/screens/index.php?dir=_other/&file=Equation.gif)
Title: Re: CHALLENGE : Rectangles
Post by: CADaver on September 29, 2006, 08:07:19 AM
Bear in mind that all the resultant triangles are "similar triangles" whose sides would be ratio related.
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 29, 2006, 08:19:14 AM
Seant Welcome Aboard and thanks for the contribution!

Now:
Your excel sheets and equation give a result which is significantly different than ours. I thought it would have been a unique solution, eh back to the scratchpad
Title: Re: CHALLENGE : Rectangles
Post by: Swift on September 29, 2006, 08:33:17 AM
Seant,

Draw your solution out. Using the side lengths I got from your spreadsheet the interior angles of the interior polygon are not 90.
Title: Re: CHALLENGE : Rectangles
Post by: solo on September 29, 2006, 10:51:55 AM
Hi CADaver,

You're right.  The resultant triangles will be similar but we do not know enough info about them to determine the direct relationship between them.  Only having one side of the triangle and no angle makes it really difficult to determine a sure fire formula that will work for any peg size and hole size combination.

I can create a relationship but it's a relationship of unknowns so i can't derive an answer.  At this point i am trigonometrically bankrupt.  I used to have a TI-82 graphing calculator that i thought was capable of calculating a formula based on an XY list.  I thought i might be able to enter numbers from tested dimensions from differant conditions and create a formula that would be close enough (if not exact) to see what's going on.

Solo
Title: Re: CHALLENGE : Rectangles
Post by: SEANT on September 29, 2006, 05:51:03 PM
I should have taken more time to explain what I was posting. 

In the search to find a general math solution to the original challenge this was one of only two sized pegs for the given size hole I was certain about . A perfect match, and a rectangle that had a hypotenuse equal to the hole length.  It is not a solution for the target depth of 200, but the spreadsheet does give the dimensions of a "Shortest Hypotenuse Rectangle" for any Rectangle Hole size.

I'm unsure if this will have any bearing on an "ultimate" math solution, if there even is one, but it does illustrate some know relationship.  I suspect it will require the use of all of the knowns (there aren't many) to come up with a solution.

I also see that my GIF image was truncated.  Let's hope I get better with subsequent posts.

Sean

Title: Re: CHALLENGE : Rectangles
Post by: SEANT on September 29, 2006, 06:05:36 PM
Apparently the GIF image may or may not be truncated based on the aspect ratio of a monitor.  Imagine rectangles of different size giving me fits like that. :?

If anyone cares to see the full image, or, for that matter, would like to see how this equation was derived, please ask.
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 29, 2006, 06:14:14 PM
SEANT, the gif looks fine to me.



Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 29, 2006, 06:15:44 PM
... and yes, I'd like to see how it was derived.
Title: Re: CHALLENGE : Rectangles
Post by: SEANT on September 29, 2006, 08:56:14 PM
There is a typo in the equation of my first post: Should read -4(H/2)2

I'm living up to my mosquito ranking.  Fortunately, the spreadsheet is working as expected.

See attached PDF for updated scratch sheet. 

Sean
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 29, 2006, 09:16:25 PM
Thanks SEANT,

I'll have a look at that tonight.  I'm having trouble visualising the application of Shortest Hypotenuse derivitaves to this problem ... but thats my issue :-)


You know, of course, that the 'Mosquito' has nothing to do with your presumed talent ... just an indicator of your posting count. 

:lol: < deleted references to the natural reverse corollary of your assumption >
Title: Re: CHALLENGE : Rectangles
Post by: Kerry on September 29, 2006, 09:31:40 PM
seant,

Just had a quick look at your PDF ... I believe your method fails at axiom 3 ..

C^2 + D^2 = W^2  is NOT necessarily correct.

.. unless I misunderstand your calculations.
Title: Re: CHALLENGE : Rectangles
Post by: SEANT on September 29, 2006, 10:16:18 PM
You understand the calculation but the scope of this formula may be more limited than you're expecting.  At this point I'm strictly looking at a "peg" with the shortest possible hypotenuse.  I'm assuming the length of this short hypotenuse is equal to the Width of the "hole".  The calculations and spreadsheet only deal with that particular peg.

I was hoping that the information from that rectangle and the "hole" rectangle would alllow some form of direct interpolation to a target peg size.  In fact, if the two ranges 0 to 258.81905, and 1000 to 1118.034 are assigned to the sine wave 0 to Pi/2, other peg lengths can be derived; but with a regular error pattern, peaking at 2%.  The next step would be to account for that error.

I'll post that sine overlay tomorrow.

Devoting this much thought to a problem of dubious practical value may not be healthy, however.  :ugly:

 
Title: Re: CHALLENGE : Rectangles
Post by: sinc on September 30, 2006, 12:55:45 PM

I can create a relationship but it's a relationship of unknowns so i can't derive an answer.


By using the fact that the triangles are similar, I was able to reduce the equations to a single unknown.

Assume Angle A is the larger angle in the triangles, h = hole height (shorter length), w = hole width (longer length), d = peg depth (short side of peg).  Then the length L (long side of the peg) could be determined by:

L = (h - dsinA)/cosA

It could also be determined by:

L = (w - dcosA)/sinA

The problem is determining the value of A.  Combining the above equations, I get messes like:

tanA = (w - dcosA) / (h - dsinA)

...which I sure can't figure out how to solve for A, except numerically with a computer for any given w, h, and d.
Title: Re: CHALLENGE : Rectangles
Post by: SEANT on October 02, 2006, 05:34:20 AM
Ah yes,

I can see where those equations came from.  But, the claim that only one unknown remains is a bit misleading.  Both magnitude of L and angle A are undetermined.

I also see how the final equation was derived, by setting the first two equal to themselves. Unfortunately I think that only gives you the definition of tangent, i.e. Sin/Cos of any angle.  Though, I may be wrong there.

What would make life easy is if a second, generaly unrelated equation, could be formulated - to fulfill the "2 equations, 2 Unknowns" requirement.  By analyzing the "Peg" rectangle hypotenuse I get:

L = ((H-(2D*SIN(A))2 + W2 - D2)1/2

The major problem is setting it equal to one of your equation.  The reduction process becomes quite a handful. 

 
Title: Re: CHALLENGE : Rectangles
Post by: sinc on October 02, 2006, 10:47:59 PM

What would make life easy is if a second, generaly unrelated equation, could be formulated - to fulfill the "2 equations, 2 Unknowns" requirement.


But, that's exactly what I did.  Here are the two equations, with two unknowns (L and A):

L = (h - dsinA)/cosA
L = (w - dcosA)/sinA

The problem is that there are two "A" terms on the right side in each equation.  That's the part I'm not sure how to handle.  Normally, to solve two equations with two unknowns, we would simply subtract them.  Unfortunately, subtracting the above two equations creates this mess:

(h - dsinA)/cosA = (w - dcosA)/sinA

We know that "A" must be less than 90 degrees, so that could simplify the problem (we can ignore any solutions in other quadrants).  But even taking that into account, I have no idea how to solve that mess for "A".  However, it is very possible to use a computer to keep trying values for A until the left and right sides of the equation are within a specific tolerance of each other.  That gives the value of A, which can be used to find L.
Title: Re: CHALLENGE : Rectangles
Post by: SEANT on October 03, 2006, 07:07:05 AM
Actually, the reason I considered them related was due to my attempt to solve them with the definitions of Sin and Cos:

Sin(A) = Opposite/Hypotenuse
Cos(A) = Adjacent/Hypotenuse

Using that method, with the drawing below, returned something as useful as CosA=CosA.

(http://www.theswamp.org/screens/_other/SinCos.gif)

For that matter, I don't think the "unrelated" equation of my previous post is actually all that unrelated.  More likely, it is just an inefficient method of describing the general relationships you provided.

It is proving quite difficult to isolate either Sin or Cos to some rational value.