2021-10-15 06:36来源:m.sf1369.com作者:宇宇
回炉了一下几何,图形学用到
import math
class Point:
def __init__(self):
self.x=0
self.y=0
def input(self,pname):
self.x=int(input(Enter the x of point {0}: .format(pname)))
self.y=int(input(Enter the y of point {0}: .format(pname)))
a=Point()
b=Point()
c=Point()
a.input(A)
b.input(B)
c.input(C)
if a.x==b.x: #When the line is vetical to y-axis
x=a.x
print(Formula: x={0}.format(a.x))
else:
k=(b.y-a.y)/(b.x-a.x)
y_intercept=a.y-k*a.x
print(Formula: y={0}x+{1}.format(k,y_intercept))
distance=abs((b.y-a.y)*c.x - (b.x-a.x)*c.y + b.x*a.y +b.y*a.x) / math.sqrt((b.y-a.y)**2+(b.x-a.x)**2)
print('The distance from the point C({0},{1}) to a line[A({2},{3})-B({4},{5})] is {6}'
.format(c.x,c.y, a.x, a.y, b.x, b.y, distance))Enter the x of point A: 0
Enter the y of point A: 5
Enter the x of point B: 5
Enter the y of point B: 5
Enter the x of point C: 5
Enter the y of point C: 0
Formula: y=0.0x+5.0
The distance from the point C(5,0) to a line[A(0,5)-B(5,5] is 5.0