计算两点之间的距离作为圆的半径然后使用 python class 计算面积和周长
calculate distance between two points as radius of a circle then calculate area and perimeter using python class
我一直在研究我的代码,编写了公式,但需要帮助重新组织以按预期执行 python 代码。
- 用户输入为一对坐标,
- 代码计算出的距离公式又是圆的半径,
- 然后我将根据输入的点坐标计算圆的 (a) 面积和 (b) 周长。
我在 class 圈中定义了我的公式。需要帮助重写下面的整个代码才能工作。现在只有输入有效。
import math
class Circle():
def __init__(self, r, a, p):
self.radius = r
self.area = a
self.perimeter = p
def point_distance(x1, y1, x2, y2):
r = math.sqrt(((y2-y1)**2) + ((x2-x1)**2))
return r
def area(self):
a = 2*math.pi*self.radius**2
return a
def perimeter(self):
return 2*self.radius*3.14
x1, y1 = input("Enter the coordinates of the center of the circle (x, y): ").split(',')
x2, y2 = input("Enter the coordinates of the point on the circle (x, y): ").split(',')
x1,y1 = int(x1), int(y1)
x2,y2 = int(x2), int(y2)
# distance = math.sqrt((y2-y1)**2) + ((x2-x1)**2)
print(f"The radius of the circle is {point_distance(x1, y1, x2, y2):.2f}")
## the code is not returning anything. Only the input works.
你试过了吗
import math
class Circle():
def point_distance(x1, y1, x2, y2):
r = math.sqrt((y2-y1)**2 + (x2-x1)**2)
return r
def area_calc(r):
a = 2*math.pi*r**2
return a
def perimeter_calc(r):
p = 2*r*3.14
return p
############
def main():
x1, y1 = input("Enter the coordinates of the center of the circle (x, y): ").split(',')
x2, y2 = input("Enter the coordinates of the point on the circle (x, y): ").split(',')
x1,y1 = int(x1), int(y1)
x2,y2 = int(x2), int(y2)
radius = Circle.point_distance(x1, y1, x2, y2)
area = Circle.area_calc(radius)
perimeter = Circle.perimeter_calc(radius)
print("Radius :", radius)
print("Area :", area)
print("Perimeter :", perimeter)
if __name__ == "__main__":
main()
Output:
Enter the coordinates of the center of the circle (x, y): 1,2
Enter the coordinates of the point on the circle (x, y): 3,4
Radius : 2.8284271247461903
Area : 50.265482457436704
Perimeter : 17.762522343406076
我一直在研究我的代码,编写了公式,但需要帮助重新组织以按预期执行 python 代码。
- 用户输入为一对坐标,
- 代码计算出的距离公式又是圆的半径,
- 然后我将根据输入的点坐标计算圆的 (a) 面积和 (b) 周长。
我在 class 圈中定义了我的公式。需要帮助重写下面的整个代码才能工作。现在只有输入有效。
import math
class Circle():
def __init__(self, r, a, p):
self.radius = r
self.area = a
self.perimeter = p
def point_distance(x1, y1, x2, y2):
r = math.sqrt(((y2-y1)**2) + ((x2-x1)**2))
return r
def area(self):
a = 2*math.pi*self.radius**2
return a
def perimeter(self):
return 2*self.radius*3.14
x1, y1 = input("Enter the coordinates of the center of the circle (x, y): ").split(',')
x2, y2 = input("Enter the coordinates of the point on the circle (x, y): ").split(',')
x1,y1 = int(x1), int(y1)
x2,y2 = int(x2), int(y2)
# distance = math.sqrt((y2-y1)**2) + ((x2-x1)**2)
print(f"The radius of the circle is {point_distance(x1, y1, x2, y2):.2f}")
## the code is not returning anything. Only the input works.
你试过了吗
import math
class Circle():
def point_distance(x1, y1, x2, y2):
r = math.sqrt((y2-y1)**2 + (x2-x1)**2)
return r
def area_calc(r):
a = 2*math.pi*r**2
return a
def perimeter_calc(r):
p = 2*r*3.14
return p
############
def main():
x1, y1 = input("Enter the coordinates of the center of the circle (x, y): ").split(',')
x2, y2 = input("Enter the coordinates of the point on the circle (x, y): ").split(',')
x1,y1 = int(x1), int(y1)
x2,y2 = int(x2), int(y2)
radius = Circle.point_distance(x1, y1, x2, y2)
area = Circle.area_calc(radius)
perimeter = Circle.perimeter_calc(radius)
print("Radius :", radius)
print("Area :", area)
print("Perimeter :", perimeter)
if __name__ == "__main__":
main()
Output:
Enter the coordinates of the center of the circle (x, y): 1,2
Enter the coordinates of the point on the circle (x, y): 3,4
Radius : 2.8284271247461903
Area : 50.265482457436704
Perimeter : 17.762522343406076