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); header("Content-Type: image/png"); imagepng($im); ?>
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); header("Content-Type: image/png"); imagepng($im); ?> PHP:
Thanks for replying. 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.
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)