5

I'm trying to get the maximum possible width and height of a rectangle inside a pie chart. All fields have the same angle. $\alpha$ is never bigger than $90^{\circ}$.

I have the variables $\alpha$, $r$, $b$ and I know that $w = 3h$.

I'm searching for $w$, $h$ and $P_1 (x_1, y_1)$.

I'm a programmer, so I'm not that good with math and I have to translate this to Code afterwards. Thanks for your help!

enter image description here

Edit:

I'm using Javascript. Thanks to Paul. He explained me, that I have to use radians instead of degree using Math.tan. In addition, there is no Math.cot in Javascript. That's why I had to create two more functions.

const tan = (deg) => Math.tan(deg * Math.PI / 180);
const cot = (value) => 1 / tan(value);
  • So you have a fixed aspect-ratio rectangle (1:3) - is it always aligned to the axes, as you have drawn it? and is the pie-chart sector always symmetrically downwards, again as drawn? – Joffan Mar 10 '17 at 22:11
  • I'm not sure the "maximum" in your question makes sense. The aspect ratio determines $P_1$ - when that point is near the ray the rectangle is too wide. When it's near the $y$ axis the rectangle is too tall. Just one point is "just right". Is that the point you want us to calculate? – Ethan Bolker Mar 10 '17 at 22:13
  • @Joffan yes, the rectangle is always aligned to the axes and the sector is always symmetrically. – Sebastian Templin Mar 10 '17 at 22:29
  • @EthanBolker right, I'm searching for $P_1, $w and $h. It's always possible that it fits perfectly, because in my case, it's always symmetrically. – Sebastian Templin Mar 10 '17 at 22:38
  • 1
    Most programming languages work with radians instead of degrees. If $\alpha$ is in degrees, then you have to convert it to radians first. If you're going to implement Hugh's answer, replace the occurences of 90 with $\pi/2$. – Paul Mar 11 '17 at 00:11
  • @Paul Wow, that's new to me. Thanks a lot! – Sebastian Templin Mar 11 '17 at 00:46

3 Answers3

3

Here's a possible answer, based on the truth of my comment.

No way to draw a picture now.

In your figure, let $z$ be the distance from the center to the top edge of the rectangle. Then $$. z = (w/2)\cot(\alpha/2). $$ Then the distance to the bottom edge of the rectangle is $$ z+ h = z + (w/3) $$

... will finish later ...

Ethan Bolker
  • 95,224
  • 7
  • 108
  • 199
  • 2
    Then according to Pythagoras, we have $(z + h)^2 + (w/2)^2 = R^2$, where $R = r - 2b$. This can be rewritten to $w^2 (13 + 12 \cot(\alpha/2) + 9 \cot(\alpha/2)^2)/36 = R^2$, which yields $w = 6R / \sqrt{13 + 12 \cot(\alpha/2) + 9 \cot(\alpha/2)^2}$. And then $x_1 = r-w/2$ and $y_1 = r-z-h$. – Paul Mar 10 '17 at 23:47
  • @Paul Why not edit my answer to finish the job? That's the kind of community SE tries to be. – Ethan Bolker Mar 11 '17 at 00:06
  • Alright, I will – Paul Mar 11 '17 at 00:18
  • I translated it to code: w = (6 * (r - b * 2)) / sqrt(13 + (12 * cot(alpha / 2)) + (9 * pow(cot(alpha / 2), 2))). But the rectangle is too big: http://imgur.com/a/Pp0pi – Sebastian Templin Mar 11 '17 at 00:59
  • 1
    @SebastianTemplin Clearly we're almost there. Hard to debug remotely. Two ideas. Radians or degrees? The formula wants $\alpha$ in radians. And check all the parentheses for operator precedence in the denominator (don't rely on defaults). – Ethan Bolker Mar 11 '17 at 01:07
  • @EthanBolker I've edited my post. I'm using degrees but have a helper function that calculates it to radians. Parentheses seems to be correct to me. – Sebastian Templin Mar 11 '17 at 01:17
  • I checked @Paul 's work for $w$. It looks right. Print it out or look at it in a debugger to see if it makes sense. Then it looks as if the $y$ coordinate of $P_1$ in your picture is $z+h$ (measured down) while the $x$ coordinate is $w/2$ (measured left), all relative to the center at the origin. I don't think I can do any more tonight ... – Ethan Bolker Mar 11 '17 at 01:45
  • @EthanBolker and Paul, now I got it. My $\alpha$ was wrong. Your solution works perfect. Thanks a lot! – Sebastian Templin Mar 11 '17 at 15:09
  • @EthanBolker I first didn't edit the answer because I wasn't sure it was appropriate. My edit got rejected, so that was not a strange guess: "This edit deviates from the original intent of the post. Even edits that must make drastic changes should strive to preserve the goals of the post's owner." – Paul Mar 11 '17 at 15:59
  • @Paul I think it was appropriate given that I suggested it, but the rejecter didn't notice. It's moot now since the OP has put the material together and solved his problem. – Ethan Bolker Mar 11 '17 at 18:19
0

Using the figure as our guide, and placing the origin at the center.

Your rectangle is symmetric about the line $x = 0$ It is bound on the upper-left corner by the line $y=x$ It is bound on the lower left corner by the circle $x^2 + y^2 = R^2$

Where $R$ is the radius of the circle that bounds the rectangle.

We can call these two coordinates $(x,x), (x,y)$

$x<0, y<0, |x|<|y|$

$H = x-y\\ W = -2x$

$W = 3H\\ -2x = 3x-3y\\ y = \frac 53 x$

$x^2 + (\frac 53 x)^2 = R^2\\ \frac {34}{9} x^2= R^2\\ x = - \frac {3}{\sqrt {34}} R$

the 4 corners are:

$(- \frac {3}{\sqrt {34}} R, - \frac {3}{\sqrt {34}} R) , ( \frac {3}{\sqrt {34}} R, - \frac {3}{\sqrt {34}} R), (\frac {3}{\sqrt {34}} R,- \frac {5}{\sqrt {34}} R), (-\frac {3}{\sqrt {34}} R,- \frac {5}{\sqrt {34}} R)$

the area $= \frac 43 x^2 = \frac {12}{34} r^2$

Doug M
  • 57,877
  • Thanks a lot. I tried to translate it to code, but it doesn't work. I also can't really follow you. Is there a difference between R and r? What is x and y? Where comes the 34 from? I not always have 6 fields. Cheers. – Sebastian Templin Mar 08 '17 at 20:05
  • 1
    $R = r-2b, 34 = 25+9, P_1(x,y)$ – Doug M Mar 08 '17 at 20:20
  • Ok I understand. I tried it, but it doesn't work. In addition I don't think that you can solve it without using α. But thanks for the approach. – Sebastian Templin Mar 08 '17 at 21:01
0

@DougM almost had the correct approach but he treated the diagonal line as if it has the equation $y=x$ which is not the case.

Taking the centre of the circle to be the origin we know that the inner-most circle has the equation $R^2=x^2+y^2$ where $R=r-2b$

The diagonal line which touches the top-right corner of the square makes a nangle $\frac{\alpha}{2}-90$ with the x-axis and it passes through the origin so it has an equation $y=tan(\frac{\alpha}{2}-90)x$

The bottom right corner on the rectangle is clearly $1.5w$ to the right of the centre of the circle so it has a coordinate $(1.5w,y_1)$. Because this point passes through the circle we can substitute it into the equation of the circle so we get $R^2=(1.5w)^2+y_1^2$

The top right corner on the rectangle is just $h$ above the bottom right corner so it has a coordinate $(1.5w,y_1+h)$. Putting this into the equation of the line we get $y_1+h=tan(\frac{\alpha}{2}-90)(1.5w)$

Using these two equations we can eliminate $y_1$

$R^2=(1.5w)^2+((tan(\frac{\alpha}{2}-90)(1.5w)-h)^2$

Use the fact that $w=3h$

$R^2=(4.5h)^2+((tan(\frac{\alpha}{2}-90)(4.5h)-h)^2$

You can rearrange this to get $h$ in terms of $\alpha$ and $R$. The solution is:

$$h=\frac{2R}{\sqrt{81tan^2(\frac{\alpha}{2}-90)-36tan(\frac{\alpha}{2}-90)+85}}$$

This approach gives you $h$ and $w$. Perhaps you can use this to find the point $P_1$. Keep in mind that in my solution I take the centre of the circle as the origin, you will have to make a slight adjustment to get the answer into your coordinate system.

Hugh
  • 2,341
  • Thanks a lot, but unfortunately it doesn't work. what does $tan^2$ mean? Is $2R = 2(r−2b)?$ – Sebastian Templin Mar 10 '17 at 23:36
  • 1
    $tan^2(x) = (tan(x))^2$ and $R$ is the radius of the small circle while $r$ is the radius of the big circle so $R=r-2b$ – Hugh Mar 10 '17 at 23:41
  • 1
    @SebastianTemplin I made an error with the angle in the formulas. It should be $tan(\frac{\alpha}{2}-90)$ not $tan(90-\frac{\alpha}{2})$. I have edited my answer – Hugh Mar 10 '17 at 23:43
  • I translated it to code: $h = (2 * (r - 2b)) / Math.pow((81 * Math.pow(Math.tan((alpha / 2) - 90), 2)) - (36 * Math.tan((α / 2) - 90)) + 85, 0.5)$ but the results doesn't look correctly. Please ignore $P_1$ and focus on the size: http://imgur.com/a/OeHZa – Sebastian Templin Mar 11 '17 at 00:00
  • 1
    @SebastianTemplin I used my formula to manually calculate the two examples in your image. According to my calculation the value of $h$ is larger in the second circle. I'd suggest you do a manual calculation and compare with the value the code gives to convince yourself that the code is right. Maybe the math.tan() function requires an angle in radians. – Hugh Mar 11 '17 at 00:29
  • @SebastianTemplin Maybe it's a coincidence but it looks like the first example in your image was the correct size http://imgur.com/a/pvDMH – Hugh Mar 11 '17 at 00:36
  • Yep, first looks perfect but second not. It should work with all angles. – Sebastian Templin Mar 11 '17 at 00:47
  • @SebastianTemplin My manual calculation tells me that in the second circle $h$ is 40% larger, that seems right to me. When you did the calculation manually did you get a smaller $h$ for the second circle? – Hugh Mar 11 '17 at 00:50
  • yes the second circle has a smaller h – Sebastian Templin Mar 11 '17 at 01:02
  • 1
    @SebastianTemplin For the first example alpha is 45. $h=\frac{2R}{\sqrt{81tan^2(\frac{45}{2}-90)-36tan(\frac{45}{2}-90)+85}}= \frac{2R}{\sqrt{472+87+85}}=0.079R$. For the second example alpha is 90. $h=\frac{2R}{\sqrt{81tan^2(\frac{90}{2}-90)-36tan(\frac{90}{2}-90)+85}}= \frac{2R}{\sqrt{81+36+85}}=0.141R$. So $h$ is bigger in the second example. Instead of me finding out what's wrong with your calculation can you tell me what's wrong with mine? – Hugh Mar 11 '17 at 08:28
  • You're absolutely right. There was an error in my $α$ calculation, that's why my second example was always smaller. Now we're on the right track. My $R = 133.875$, $α_1 = 45$ and $α_2 = 90$. The result is $h_1 = 10.55$ and $h_2 = 18.83$. That's to small: http://imgur.com/a/XBiBk – Sebastian Templin Mar 11 '17 at 12:03
  • 1
    Hugh, don't invest too much time. Ethan Bolker's and Paul's solution looks correctly. I'm now trying to get $P_1$ before I will give the bounty. Thanks again! because of you, I found out that my $α$ can't be correctly. – Sebastian Templin Mar 11 '17 at 12:11