# Fit an rectangle into another rectangle

31 messages
12
Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

 nophead wrote I gave the equation to Wolfram to solve. It did but answer is horrible: https://www.wolframalpha.com/input/?i=solve+sqrt%28w%5E2+-x%5E2%29%2Fx+%3D+%28a-x%29+%2F+%28b+-+sqrt%28w%5E2+-x%5E2%29%29 When you described it as "horrible", I expected something more along the lines of "a bit awkward", but horrible does not give that solution justice. This goes beyond horrible, and into pure terror... Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list To unsubscribe send an email to [hidden email]
Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

 In reply to this post by nophead nophead wrote I gave the equation to Wolfram to solve. It did but answer is horrible: https://www.wolframalpha.com/input/?i=solve+sqrt%28w%5E2+-x%5E2%29%2Fx+%3D+%28a-x%29+%2F+%28b+-+sqrt%28w%5E2+-x%5E2%29%29 I took the horror from Wolfram and collected all the common terms and constants. This is my result ```function fx(a,b,w)= let ( c1=12*2^(1/3), c2=6*2^(2/3), t1 = 1728*w^2*b^10 + 25920*w^4*b^8 + 5184*a^2*w^2*b^8 + 82944*w^6*b^6 - 134784*a^2*w^4*b^6 + 5184*a^4*w^2*b^6 - 110592*w^8*b^4 + 82944*a^2*w^6*b^4 + 25920*a^4*w^4*b^4 + 1728*a^6*w^2*b^4, t2 = 2*a^6 + 6*b^2*a^4 + 48*w^2*a^4 + 6*b^4*a^2 + 384*w^4*a^2 - 120*b^2*w^2*a^2 + 2*b^6 + 1024*w^6 - 1344*b^2*w^4 + 264*b^4*w^2, t3=sqrt3(t2+sqrt(t1)), t4=(a^4+2*b^2*a^2+16*w^2*a^2+b^4+64*w^4-56*b^2*w^2), t5=(a^2+b^2-4*w^2), t6=sqrt( a^2/4 - t5/6 + t3/c1 + t4 / (t3*c2) ), t7=sqrt( a^2/2 - t5/3 - t3/c1 - t4 / (t3*c2) - (a^3-4*w^2*a-t5*a) / (4*t6) ) ) a/4 - t6/2 + t7/2; ```Still looks odd, but it gives me all I want, The only remaining issues are numeric overflows, when 'w' gets near 'a' ¹). But I can live with this, because in my use case 'w' is almost always something about 0.1*a … 0.3*a. Thank you all for your support, regards. ¹) I assume, this is because OpenSCAD is doing arithmetic in float. Using the same formula in lua with double arithmetic let me come much closer to 'a' without numerical overflows PS: I had to invent ```function sqrt3(x)=sign(x)*abs(x)^(1/3); ```because OpenSCAD coult not evaluate cubic root of negative values ( -8^(1/3) := -2) Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list To unsubscribe send an email to [hidden email]
Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

 As adrianv already stated, finding and figuring out the algebraic solution is much more difficult than finding the solution in an iterative fashion. A short analysis of the problem shows that the points [x,0] and [0, y] need to have the same distance from the middle point M=[a/2,b/2] and w from each other. This means we can set up two equations and have two unknowns. However it is not clear, whether there exists some algebraic solution in a closed form, which has already been discussed.   Therefore, we better employ some iteration scheme. We choose some interval [x1 ... x2] that surely contains the solution x and test the middle point (x1+x2)/2. Depending on the sign of the difference delta of the distances from M, we recursively continue with the upper respectivley lower half of the interval, until delta < eps holds. For testing purposes, I added some max iteration depth test, which shows that the code needs 26 iteration steps for eps=1E-7 a = 13; b = 15; w = 9.16399; echo(solveit()); function solveit(M=[a/2,b/2],w=w, x1=a, x2=0, eps=1E-7, n = 100) =   let(x = (x1+x2)/2)       // equation 1   let(y = sqrt(w*w-x*x))   // equation 2   let(delta = norm(M-[x,0])- norm(M-[0,y])) // test condition   abs(delta) < eps || n<=0 ? [x, y, n]:   delta>0? solveit(M, w, x1, x, n=n-1):            solveit(M, w, x, x2, n=n-1); Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list To unsubscribe send an email to [hidden email]
Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

 In reply to this post by bassklampfe I did a little googling and found this discussion: https://www.physicsforums.com/threads/rectangle-inscribed-in-another-rectangle-solutions-for-all-cases.508715/The first attachment is a pretty rigorous write up (https://www.physicsforums.com/attachments/inscribedrectangles-discussion-txt.36642/). Case 3 is the one analogous to the given problem. For his analytical solution, he derives nearly the same quartic as NateTG came up with (except instead of "2 w a x", it's "2 w^2 a x"), which turns into the same result as the mess from Wolfram (and now that I look at it again, the same as adrianv used for the BOSL root finder). He also details an algorithm for determining the inscribed rectangle angle by finding the inflection point of a function of the angle. I thought it was an interesting explanation and covered all of the points raised in this thread. Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list To unsubscribe send an email to [hidden email]
Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

 I think the Wolfram mess is only because it produces the overall formula in one step and with some constant folding. Quartics are solved by making various substitutions of variables to simplify the problem and if you code it the same way the expressions are a lot simpler. Quadratics can be solved by completing the square and when you fully expand it you get the familiar formula that is not too complex but as soon as you go to cubics the fully expanded formula starts to get out of hand.A good explanation here: https://www.youtube.com/watch?v=N-KXStupwscOn Sun, 28 Mar 2021 at 22:53, jsc <[hidden email]> wrote:I did a little googling and found this discussion: https://www.physicsforums.com/threads/rectangle-inscribed-in-another-rectangle-solutions-for-all-cases.508715/The first attachment is a pretty rigorous write up (https://www.physicsforums.com/attachments/inscribedrectangles-discussion-txt.36642/). Case 3 is the one analogous to the given problem. For his analytical solution, he derives nearly the same quartic as NateTG came up with (except instead of "2 w a x", it's "2 w^2 a x"), which turns into the same result as the mess from Wolfram (and now that I look at it again, the same as adrianv used for the BOSL root finder). He also details an algorithm for determining the inscribed rectangle angle by finding the inflection point of a function of the angle. I thought it was an interesting explanation and covered all of the points raised in this thread. Sent from the OpenSCAD mailing list archive at Nabble.com._______________________________________________ OpenSCAD mailing list To unsubscribe send an email to [hidden email] _______________________________________________ OpenSCAD mailing list To unsubscribe send an email to [hidden email]
Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

Open this post in threaded view
|

## Re: Fit an rectangle into another rectangle

Open this post in threaded view
|