y u no do it?

Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

Starts at just \$1 per CPM or \$0.10 per CPC.

# Drawing an ellipse outside rectangle, what is the math for this?

Discussion in 'PHP' started by JEET, Jul 3, 2020.

1. #1
Drawing an ellipse outside rectangle, what is the math for this?

I drew a rectangle, and now I want to draw an ellipse outside it so that rectangle fits barely inside the ellipse.

I don't want a perfect circle around the rectangle, that I can do with diagonal length.

I want an ellipse, just barely going around the corners of the rectangle.

Here's my code, and edges of the rectangle are stretching outside the ellipse.
How can I fix this?

If the ellipse is slightly larger than the rectangle, even that is ok,
but must work for rectangle of any proportions.

<?php

\$width=500; \$height=300;
\$im= imagecreatetruecolor( \$width, \$height );
\$b= imagecolorallocate(\$im, 0, 0, 0);
\$w= imagecolorallocate(\$im, 255, 255, 255);
imagefill(\$im, 0,0, \$b);

\$x=(\$width/2)-75;
\$y=(\$height/2)-25;
\$x1=(\$width/2)+75;
\$y1=(\$height/2)+25;

imagerectangle(\$im, \$x,\$y, \$x1,\$y1, \$w);

//try some ratios

\$x= (\$x1-\$x);
\$y=(\$y1-\$y);
\$ratio= ceil(\$x/\$y);
//echo "\$x = \$y = \$ratio";exit;

\$x=\$x+ ceil(\$x/\$ratio);
\$y=\$y+ ceil(\$y/\$ratio);

//draw the ellipse around the rectangle
imageellipse(\$im, (\$width/2), (\$height/2),
\$x,\$y,
\$w);

imagepng(\$im);
?>

Solved! View solution.
, Jul 3, 2020 IP
2. ### hdewantaraWell-Known Member

Messages:
519
47
23
Trophy Points:
130
#2
Hi.
I don't understand how you get lines, after
Perhaps you should derive first the basic equation of an ellipse:
https://www.mathsisfun.com/geometry/ellipse.html#equation

x**2 / a**2 + y**2 / b**2 = 1
(where a = half of ellipse width and b = half of ellipse height)

If we know ratio of a / b then logically the value of a can be solved.
FYI you can use any corners of rectangle for values of x and y.

So i guess your code becomes like the following:
``````<?php

\$width=500; \$height=300;
\$im= imagecreatetruecolor( \$width, \$height );
\$b= imagecolorallocate(\$im, 0, 0, 0);
\$w= imagecolorallocate(\$im, 255, 255, 255);
imagefill(\$im, 0,0, \$b);

//rectangle
\$x=(\$width/2)-75;
\$y=(\$height/2)-25;
\$x1=(\$width/2)+75;
\$y1=(\$height/2)+25;

imagerectangle(\$im, \$x,\$y, \$x1,\$y1, \$w);

//ellipse center
\$ellcx = (\$x + \$x1) / 2;
\$ellcy = (\$y + \$y1) / 2;

//ellipse height / width
\$ellh2wratio = 0.3;

//ellipse width
\$ellw = 2 * sqrt((\$x - \$ellcx)**2 + (\$y - \$ellcy)**2 / \$ellh2wratio**2);

//ellipse height
\$ellh = \$ellh2wratio * \$ellw;

//draw the ellipse around the rectangle
imageellipse(\$im, \$ellcx, \$ellcy, \$ellw, \$ellh, \$w);

imagepng(\$im);
?> ``````
PHP:

Last edited: Jul 4, 2020
Jul 4, 2020 IP
JEET likes this.
3. ### JEETNotable Member

Messages:
3,758
492
19
Trophy Points:
235
#3
How did you get this ellipse ratio? Is this constant, or is it the ratio width and height of rectangle?
\$ellh2wratio = 0.3;

Also, this line doesn't seems right.
//ellipse width
\$ellw = 2 * sqrt((\$x - \$ellcx)*2 + (\$y - \$ellcy)*2 / \$ellh2wratio*2);

I also get parse error because of double * signs.

, Jul 4, 2020 IP
4. #4
It's the ratio of height to width of your ellipse and the value is up to you. For example:
\$ellh2wratio = 0.3 creates flat ellipse.
\$ellh2wratio = 1 makes perfect circle

** is the same as pow()

``\$ellw = 2 * sqrt((\$x - \$ellcx)**2 + (\$y - \$ellcy)**2 / \$ellh2wratio**2);``
PHP:
of if you rather use pow() for php < 5.6:
``\$ellw = 2 * sqrt(pow(\$x - \$ellcx,2) + pow(\$y - \$ellcy,2) / pow(\$ellh2wratio,2));``
PHP:
I derived it by hand from: x^2/a^2 + y^2/b^2 = 1
(see article in https://www.mathsisfun.com/geometry/ellipse.html#equation)

Jul 4, 2020 IP
JEET likes this.
5. ### JEETNotable Member

Messages:
3,758
492
19
Trophy Points:
235
#5
@hdewantara
This is brilliant! Many thanks for this!

, Jul 4, 2020 IP

Messages:
519