回炉了一下几何,图形学用到
import mathclass 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
你好:
上面的程序,请看如下代码:
# -*- coding: cp936 -*-end=input("是否结束(y/n):")
while end=="n":
print "Number of coordinates:2"
xx=input("x's:")
yy=input("y's:")
困弯掘 a=float(list(xx)[0])
b=float(list(xx)[1])
c=float(list(yy)[0])
d=float(list(yy)[1])
print "第一个点是:("+str(a)+","+str(c)+")"
print 汪核"第一个点是:("+str(b)+","+str(d)+")"
闹返 x0=c-a
y0=float(d-b)
print "直线方程为:",
if x0==0:
print "x=",a
else:
print "y=%r(x-%r)+%r"%(y0/x0,a,c)
若存在且斜率为K,则切线方程为:y - y1 = k(x-x1)
则圆心到切线的距离 d = r (半径),据此可求出斜率 k
将切线方程化为:y - y1 -kx + kx1 = 0,根据点到直线的距离的公贺昌式(此点为圆心即(0,0)),有:
d = | 0-y1 -0 +kx1| / ( k*k+1)^0.5 = r
而x1,y1,r是已知的,可求出k,至此切线方程呼之欲出
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)