I have been playing around my code, written the formulas, but need help in reorganizing to execute python codes as expected.
- User enters as a pair of coordinates,
- the code calculates the distance formula which in turn is the radius of the circle,
- then I will calculate the (a) area and (b) perimeter of the circle based on the entered coordinates of the points.
I defined my formulas in a class circle. Need help in rewriting the whole code below to work. Right now only the inputs work.
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.
CodePudding user response:
have you tried
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
