Trying to construct a rectangle inside a rectangle

Discussion in 'AutoCAD' started by Alan Henderson @ A'cad Solutions, Jan 7, 2005.

  1. It's Friday and I can't think today...

    I am trying to programmable construct an angled rectangle with 1 given wide inside another rectangle from corner to corner.
    All four corners of the inner rectangle must touch the four edges of the outer rectangle.
    The only restraint is that the inner rectangle is at a user defined width.

    Program questions -
    Pick 1st Corner of Box?
    Pick 2nd Opposite Corner of Box?
    Input Width of Stud to place in Box?

    I know how to write the program, I just can't figure out the math to determine the four corners of the inner rectangle.

    Any help would be greatly appreciated (in advance!!)

    Alan
     
    Alan Henderson @ A'cad Solutions, Jan 7, 2005
    #1
  2. Alan Henderson @ A'cad Solutions

    BillZ Guest

    All four corners of the inner rectangle must touch the four edges of the outer rectangle.
    <<<

    Which would mean that the inside rectangle is actually a trapezoid?

    Bill
     
    BillZ, Jan 7, 2005
    #2
  3. Alan Henderson @ A'cad Solutions

    David Kozina Guest

    Picture might be worth a thousand words here?


    in message It's Friday and I can't think today...

    I am trying to programmable construct an angled rectangle with 1 given wide
    inside another rectangle from corner to corner.
    All four corners of the inner rectangle must touch the four edges of the
    outer rectangle.
    The only restraint is that the inner rectangle is at a user defined width.

    Program questions -
    Pick 1st Corner of Box?
    Pick 2nd Opposite Corner of Box?
    Input Width of Stud to place in Box?

    I know how to write the program, I just can't figure out the math to
    determine the four corners of the inner rectangle.

    Any help would be greatly appreciated (in advance!!)

    Alan
     
    David Kozina, Jan 7, 2005
    #3
  4. Alan Henderson @ A'cad Solutions

    BillZ Guest

    Oh,
    If the outside box is "square".

    Distance between corners, minus the user distance, divided by 2 would be the offset distance of the diagonal line to create the "stud".

    Bill
     
    BillZ, Jan 7, 2005
    #4
  5. Please, send (atach) a image to (of?) you need.

    Thanks,

    Rogerio
     
    Rogerio_Brazil, Jan 7, 2005
    #5
  6. Well, I wouldn't say that there's necessarily "a simple Trig solution." There's simple Trig in the relationships between lines in the "end result" that you want, but getting to there from what you're starting with might not be so simple. I've tried a little to solve for a or b using only W, X & Y, but haven't found a way to do it yet. I wonder whether it might require calculus, but if it does, I don't think you can program it in Lisp. But I'll certainly ponder it more, and get my son to, over the weekend.
    --
    Kent Cooper, AIA

    ...
    Thanks Kent,
    I had spent a hour looking on the internet and couldn't locate anything close.
    I knew there was a simple Trig solution.
    Alan
     
    Kent Cooper, AIA, Jan 7, 2005
    #6
  7. Alan Henderson @ A'cad Solutions

    Dann Guest

    Just a thought here:
    You know the length from one corner to the opposite corner of the outside rectangle.
    You can draw a Construction line from one corner to the other, offset it the width of the stud in each direction, get the intersectWith points,
    Then draw the inside rectangle.
    ??
    or am I off track here?
    It's Friday and I can't think today...

    I am trying to programmable construct an angled rectangle with 1 given wide inside another rectangle from corner to corner.
    All four corners of the inner rectangle must touch the four edges of the outer rectangle.
    The only restraint is that the inner rectangle is at a user defined width.

    Program questions -
    Pick 1st Corner of Box?
    Pick 2nd Opposite Corner of Box?
    Input Width of Stud to place in Box?

    I know how to write the program, I just can't figure out the math to determine the four corners of the inner rectangle.

    Any help would be greatly appreciated (in advance!!)

    Alan
     
    Dann, Jan 10, 2005
    #7
  8. The angle of the interior rectangle is not the same angle as from corner to corner. See Kent Cooper's reply for example.
    "Dann" <NoneSpecified> wrote in message Just a thought here:
    You know the length from one corner to the opposite corner of the outside rectangle.
    You can draw a Construction line from one corner to the other, offset it the width of the stud in each direction, get the intersectWith points,
    Then draw the inside rectangle.
    ??
    or am I off track here?
    It's Friday and I can't think today...

    I am trying to programmable construct an angled rectangle with 1 given wide inside another rectangle from corner to corner.
    All four corners of the inner rectangle must touch the four edges of the outer rectangle.
    The only restraint is that the inner rectangle is at a user defined width.

    Program questions -
    Pick 1st Corner of Box?
    Pick 2nd Opposite Corner of Box?
    Input Width of Stud to place in Box?

    I know how to write the program, I just can't figure out the math to determine the four corners of the inner rectangle.

    Any help would be greatly appreciated (in advance!!)

    Alan
     
    Alan Henderson @ A'cad Solutions, Jan 10, 2005
    #8
  9. Alan Henderson @ A'cad Solutions

    Dann Guest

    O.K. I get it....My bad
    The angle of the interior rectangle is not the same angle as from corner to corner. See Kent Cooper's reply for example.
    "Dann" <NoneSpecified> wrote in message Just a thought here:
    You know the length from one corner to the opposite corner of the outside rectangle.
    You can draw a Construction line from one corner to the other, offset it the width of the stud in each direction, get the intersectWith points,
    Then draw the inside rectangle.
    ??
    or am I off track here?
    It's Friday and I can't think today...

    I am trying to programmable construct an angled rectangle with 1 given wide inside another rectangle from corner to corner.
    All four corners of the inner rectangle must touch the four edges of the outer rectangle.
    The only restraint is that the inner rectangle is at a user defined width.

    Program questions -
    Pick 1st Corner of Box?
    Pick 2nd Opposite Corner of Box?
    Input Width of Stud to place in Box?

    I know how to write the program, I just can't figure out the math to determine the four corners of the inner rectangle.

    Any help would be greatly appreciated (in advance!!)

    Alan
     
    Dann, Jan 10, 2005
    #9
  10. Alan Henderson @ A'cad Solutions

    Joe Burke Guest

    Alan,

    This was a thought which turned into a beast. For what it's worth...

    Joe Burke

    ;notes: JB 1/10/2005
    ;doesn't work with rotated rectanges due to use of bounding box
    ;new pline is slightly out of square about 0.01 degrees

    (defun c:RectInRect ( / wdth rect lay ll lr ur ul horizdist vertdist midpt
    mindist rotang rotdir ray rayobj1 rayobj2 p1 p2 dist coord ptlst1 ptlst2
    ptlst *Error* MakeInvisibleRay MakeClosedPline GetLL GetUR GetUL GetLR )

    (defun *Error* (Msg)
    (cond
    ((or (not Msg)
    (member Msg '("console break"
    "Function cancelled"
    "quit / exit abort"))))
    ((princ (strcat "\nError: " Msg)))
    )
    (setvar "cmdecho" 1)
    (princ)
    ) ;end

    ;arguments: two points
    (defun MakeInvisibleRay ( p1 p2 / vec )
    (setq vec
    (list
    (cos (angle p1 p2))
    (sin (angle p1 p2))
    '0.0
    )
    )
    (entmakex
    (list
    '(0 . "RAY") '(100 . "AcDbEntity") '(100 . "AcDbRay")
    '(8 . "0") '(60 . 1) (cons 10 p1) (cons 11 vec)
    )
    )
    ) ;end

    (defun MakeClosedPline (ptlst lay)
    (entmake
    (append
    (list
    '(0 . "LWPOLYLINE")
    '(100 . "AcDbEntity")
    '(100 . "AcDbPolyline")
    (cons 8 lay)
    (cons 90 (length ptlst))
    '(70 . 1)
    )
    (mapcar '(lambda (x) (cons 10 x)) ptlst)
    )
    )
    ) ;end

    (defun GetLL (vobj / mn mx)
    (vla-getboundingbox vobj 'mn 'mx)
    (vlax-safearray->list mn)
    ) ;end
    (defun GetUR (vobj / mn mx)
    (vla-getboundingbox vobj 'mn 'mx)
    (vlax-safearray->list mx)
    ) ;end
    (defun GetUL (vobj / mn mx)
    (vla-getboundingbox vobj 'mn 'mx)
    (list
    (car (vlax-safearray->list mn))
    (cadr (vlax-safearray->list mx))
    '0.0
    )
    ) ;end
    (defun GetLR (vobj / mn mx)
    (vla-getboundingbox vobj 'mn 'mx)
    (list
    (car (vlax-safearray->list mx))
    (cadr (vlax-safearray->list mn))
    '0.0
    )
    ) ;end

    (while
    (or
    (not (setq rect (car (entsel "\nSelect rectangle: "))))
    (not (setq rect (vlax-ename->vla-object rect)))
    (not (equal "AcDbPolyline" (vlax-get rect 'ObjectName)))
    )
    (princ "\nMissed pick or wrong object type selected. ")
    )

    (setq lay (vlax-get rect 'Layer))
    (setq ll (GetLL rect))
    (setq lr (GetLR rect))
    (setq ur (GetUR rect))
    (setq ul (GetUL rect))
    (setq midpt (mapcar '* (mapcar '+ ll ur) '(0.5 0.5 0.5)))
    (setq horizdist (distance ll lr))
    (setq vertdist (distance ll ul))
    (setq rotang (* pi 0.00001))

    (if (< horizdist vertdist)
    (progn
    (setq mindist horizdist)
    (setq rotdir 1) ;ccw
    )
    (progn
    (setq mindist vertdist)
    (setq rotdir -1) ;cw
    (setq rotang (* rotang rotdir))
    )
    )

    (princ (strcat "\nMaximum width = " (rtos mindist 2 2)))
    (initget 7)
    (setq wdth (getdist "\nEnter width: "))

    (if (<= mindist wdth)
    (progn
    (princ "\nWidth is too large to fit. ")
    (exit)
    )
    )

    ;initial rotation for faster operation when width
    ;is more than one third of smallest rectangle dimension
    (cond
    ((>= (/ wdth mindist) 0.85)
    (setq rotfirst (* (/ 30.0 180) pi rotdir)))
    ((>= (/ wdth mindist) 0.75)
    (setq rotfirst (* (/ 23.0 180) pi rotdir)))
    ((>= (/ wdth mindist) 0.66)
    (setq rotfirst (* (/ 17.0 180) pi rotdir)))
    ((>= (/ wdth mindist) 0.5)
    (setq rotfirst (* (/ 7.0 180) pi rotdir)))
    ((>= (/ wdth mindist) 0.33)
    (setq rotfirst (* (/ 3.5 180) pi rotdir)))
    (T (setq rotfirst 0.0))
    )

    (setvar "cmdecho" 0)
    (setq ray (MakeInvisibleRay midpt ll))
    (command "offset" (* wdth 0.5) ray ul "")
    (setq rayobj1 (vlax-ename->vla-object (entlast)))
    (command "offset" (* wdth 0.5) ray lr "")
    (setq rayobj2 (vlax-ename->vla-object (entlast)))
    (entdel ray)
    (vlax-invoke rayobj1 'Rotate midpt rotfirst)
    (setq p1 (vlax-invoke rayobj1 'IntersectWith rect acExtendNone))
    (setq p1 (list (car p1) (cadr p1) (caddr p1)))
    (vlax-invoke rayobj2 'Rotate midpt rotfirst)
    (setq p2 (vlax-invoke rayobj2 'IntersectWith rect acExtendNone))
    (setq p2 (list (car p2) (cadr p2) (caddr p2)))
    (setq dist (distance p1 p2))

    (while (not (equal dist wdth 0.000002))
    (vlax-invoke rayobj1 'Rotate midpt rotang)
    (setq p1 (vlax-invoke rayobj1 'IntersectWith rect acExtendNone))
    (setq p1 (list (car p1) (cadr p1) (caddr p1)))
    (vlax-invoke rayobj2 'Rotate midpt rotang)
    (setq p2 (vlax-invoke rayobj2 'IntersectWith rect acExtendNone))
    (setq p2 (list (car p2) (cadr p2) (caddr p2)))
    (setq dist (distance p1 p2))
    )

    (setq coord (vlax-invoke rayobj1 'IntersectWith rect acExtendThisEntity))
    (repeat 2
    (setq ptlst1 (cons (list (car coord) (cadr coord) (caddr coord)) ptlst1))
    (setq coord (cdddr coord))
    )
    (setq coord (vlax-invoke rayobj2 'IntersectWith rect acExtendThisEntity))
    (repeat 2
    (setq ptlst2 (cons (list (car coord) (cadr coord) (caddr coord)) ptlst2))
    (setq coord (cdddr coord))
    )
    (vla-delete rayobj1)
    (vla-delete rayobj2)
    (if (< (distance (car ptlst1) (cadr ptlst1))
    (distance (car ptlst1) (car ptlst2)))
    (setq ptlst (append ptlst1 ptlst2))
    (setq ptlst (append ptlst1 (reverse ptlst2)))
    )
    (MakeClosedPline ptlst lay)
    (*Error* nil)
    )

    ;; option: square a four item point list about its midpoint
    (defun SquarePtlst (ptlst mp / dist1 dist2 avgdist)
    (setq dist1 (distance (car ptlst) mp))
    (setq dist2 (distance (cadr ptlst) mp))
    (setq avgdist (* (+ dist1 dist2) 0.5))
    (list
    (polar mp (angle mp (car ptlst)) avgdist)
    (polar mp (angle mp (cadr ptlst)) avgdist)
    (polar mp (angle mp (caddr ptlst)) avgdist)
    (polar mp (angle mp (cadddr ptlst)) avgdist)
    )
    )
     
    Joe Burke, Jan 10, 2005
    #10
  11. Alan Henderson @ A'cad Solutions

    GaryDF Guest

    Works great...

    autocad 2005 windows 2000

    Gary

     
    GaryDF, Jan 10, 2005
    #11
  12. Joe Burke,

    Very, very good routine.

    Congratulations,

    Rogerio
     
    Rogerio_Brazil, Jan 10, 2005
    #12
  13. Joe,
    Thanks for the great program, but the interior rectangle must be with 90.0
    degree angles.
    This "simple" problem is definitely "not simple".
    Alan

    <snip......
    .......snip>
     
    Alan Henderson @ A'cad Solutions, Jan 10, 2005
    #13
  14. Alan Henderson @ A'cad Solutions

    David Kozina Guest

    If I understand Joe's routine, he is refining the coordinate calculations
    until a (specified) certain degree of accuracy is attained. Perhaps you can
    simply adjust the 'fuzz' factor numbers to be smaller (and thus more
    accurate. (Calculations would take longer depending on how fine you go.)

    I don't know how else you would be able to solve this.

    Seems to be sort of like calculating the square root of 2 by hand or PI -
    you take things out for as long as you wish, and get closer and closer to
    the correct value, but you will never get THE actual value, since you are
    dealing with irrational numbers. (Trying to attain light speed might be
    another metaphor.)

    Best regards,
    David Kozina
     
    David Kozina, Jan 10, 2005
    #14
  15. Alan Henderson @ A'cad Solutions

    Joe Burke Guest

    Alan,

    You're welcome. I'm well aware it's not a simple problem. Thinking it might be, may
    be a problem in itself.

    I looked at what Kent posted, as well as other possible geometric solutions. I don't
    see a way to solve it, other than what I posted. Of course that doesn't mean I'm
    right. It simply demonstrates my knowledge of the issue, or lack of.

    You can "square" the point list using the optional function I posted. You can also
    center the new squared pline in the selected rectangle. But this is still not the
    perfect geometric solution you are looking for.

    Joe Burke
     
    Joe Burke, Jan 10, 2005
    #15
  16. Alan Henderson @ A'cad Solutions

    David Kozina Guest

    Joe,

    Thinking about my reply to Alan, this seems to me to be an interesting
    calculus problem - which does let you actually work out the otherwise
    uncalculable (assuming you have the right equations).
    Would there be a way to use the vl*derivative functions to obtain a true
    calculable value?
    Have to crack open my Calculus book again someday.

    Best regards,
    David Kozina
     
    David Kozina, Jan 10, 2005
    #16
  17. Alan Henderson @ A'cad Solutions

    Fatfreek Guest

    Someone once said, "The proof is in the pudding". That set of routines of
    yours, Joe, is one of the best examples of that expression I've seen.

    Autocad 2005.
    Len Miller
     
    Fatfreek, Jan 11, 2005
    #17
  18. Alan Henderson @ A'cad Solutions

    Joe Burke Guest

    David,

    Right, that's what it does. And you're correct. A smaller fuzz factor, with smaller
    rotation increments, would produce a more accurate result. But as you said, you'd pay
    for it speedwise. I set a limit of one second on how long is too long. In most cases
    it will draw the pline in less than a half second or faster, given my P4 3GHz machine
    running 2004.

    You may have noticed, it does a preliminary rotation of the rays to get them close to
    where they want to be. Then it goes into fine rotation mode. I think another layer of
    more refined tweaking could be added, which might produce an acceptable result.
    Keeping in mind, what's acceptable is open to question.

    BTW, one thing I noticed while working on this. Sometimes the IntersectWith method
    returns a list of six numbers (two identical points) rather than a list of three
    numbers (one point). It occurs when I'm not asking for a ray to be extended in terms
    of intersection, i.e., acExtendNone. In this case the ray can only intersect the
    selected rectangle once. So I don't know why it returns two points. That's the reason
    I make a point list when shouldn't have to, within the while loop.

    I mention this because it seemed to occur more often when I screwed things down
    tighter in terms of the fuzz factor and rotation angle increment.

    To all who liked my half-baked solution, thank you. :)

    Regards
    Joe Burke
     
    Joe Burke, Jan 11, 2005
    #18
  19. Alan Henderson @ A'cad Solutions

    Joe Burke Guest

    David,

    I don't know calculus, or what the derivative functions do.

    Crack that book and let us know if there's a real solution to Alan's question. I'd
    love to see it.

    I'm reminded of John Uhden's statement in his @cv_inside function. "LOOK, MA'... NO
    RAYS!"

    Joe Burke
     
    Joe Burke, Jan 11, 2005
    #19
  20. Alan Henderson @ A'cad Solutions

    David Kozina Guest

    Kent, what do you think about this? (based on your last geometrical thesis.
    ;)

    Let: x,y represent width,height of Outer Rectangle (known)
    Let: Pc = Center Point of Rectangles (known) = (x/2,y/2)
    Let: w represent width of interior rectangle (known)
    Let: a represent distance in X direction to corner of interior rectangle
    (unknown)
    Let: b represent distance in Y direction to corner of interior rectangle
    (unknown)
    Let: D(P1,P2) represent distance from Point P1 to Point P2

    a^2 + b^2 = w^2 => a = Sqrt(w^2 - b^2)

    D(Pc,(0,b)) = D(Pc,(a,0)) (isosceles triangle, correct?)
    (I didn't discern this earlier.)

    Substituting values, and cleaning things up, I get, finally:

    4b^4 + w^4 - 4w^2b^2 + b^2y^2 - 4b^3y + 2w^2by - w^2x^2 + b^2x^2 = 0

    Which can now be solved for b, which will lead to a.
    What do you get?


    Best regards,
    David Kozina


    Tilting at windmills here, in the hope of finding a "perfect" (purely
    mathematical rather than trial-and-error) solution, I offer the following,
    in case it might trigger anyone to see the way to formulate an answer.
     
    David Kozina, Jan 11, 2005
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.