Author Topic: CHALLENGE : Rectangles  (Read 22157 times)

0 Members and 1 Guest are viewing this topic.

Maverick®

  • Seagull
  • Posts: 14778
Re: CHALLENGE : Rectangles
« Reply #45 on: September 26, 2006, 10:00:26 PM »
Maverick's proclivity aside ;


  I had to look it up.  Good one.  :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CHALLENGE : Rectangles
« Reply #46 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 ...
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 #47 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.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: CHALLENGE : Rectangles
« Reply #48 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

SomeCallMeDave

  • Guest
Re: CHALLENGE : Rectangles
« Reply #49 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

whdjr

  • Guest
Re: CHALLENGE : Rectangles
« Reply #50 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.

whdjr

  • Guest
Re: CHALLENGE : Rectangles
« Reply #51 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.  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: CHALLENGE : Rectangles
« Reply #52 on: September 28, 2006, 08:48:06 AM »
Isn't language wonderfull !!

all sorts ..
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.

solo

  • Guest
Re: CHALLENGE : Rectangles
« Reply #53 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

solo

  • Guest
Re: CHALLENGE : Rectangles
« Reply #54 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.
« Last Edit: September 28, 2006, 12:58:26 PM by solo »

deegeecees

  • Guest
Re: CHALLENGE : Rectangles
« Reply #55 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 if you haven't already.

May we call you Han?

solo

  • Guest
Re: CHALLENGE : Rectangles
« Reply #56 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.
« Last Edit: September 28, 2006, 12:52:43 PM by solo »

Maverick®

  • Seagull
  • Posts: 14778
Re: CHALLENGE : Rectangles
« Reply #57 on: September 28, 2006, 12:54:19 PM »
Bwahahahaha

Welcome!   

deegeecees

  • Guest
Re: CHALLENGE : Rectangles
« Reply #58 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...

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: CHALLENGE : Rectangles
« Reply #59 on: September 28, 2006, 01:24:13 PM »
Geeks!

 :grazy: