Home>Article>Backend Development> PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle

PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle

藏色散人
藏色散人 Original
2021-08-16 09:49:18 3922browse

In the previous article "PHP Algorithm Exercise 9: Convert all even numbers to all odd numbers" I introduced to you how to convert all even numbers to all odd numbers through PHP, so today I will continue to show you Bringing the PHP algorithm exercise series~

This article will introduce to you how to calculate the radius and center coordinates of a circle through PHP~

The specific problem descriptionis "How to write a PHP program to calculate the radius and center coordinates (x, y) of a circle formed by three given points on a plane”?

This problem is equivalent to a mathematical problem. Given three points, determine the radius and center coordinates of a circle.

Look at the picture below:

PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle

I will provide you with several formulas:

The formula for setting up a circle is as follows:PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle

Convert the circle equation into a standard equation:PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle

Substitute the above coefficients to solve the circle center (x, y) and radius R:

PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle

PHP implementation code is as follows:

"); printf("(%.3f %.3f) %.3f\n", $x, $y, $r);

The calculation result is:

圆的中心坐标(x,y)和半径: (1.000 1.000) 1.414

Note:

sqrt() function Returns the square root of a number.

The syntax is "sqrt(x)", which means returning the square root of x.

The parameter x represents a number. If the parameter x is a negative number, the sqrt() function returns -1.#IND. (Attachment: Before PHP 5.3.0, this function treated the array as a string Array, thus returning a string with a length of 5 and generating an E_NOTICE level error.)

Finally, I recommend the latest to everyone The most comprehensive "PHP Video Tutorial" ~ Come and learn!

The above is the detailed content of PHP algorithm exercise ten: Calculate the radius and center coordinates of a circle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn