from matplotlib import pyplot as plt
import numpy as np
# Python program for slope of line
def slope(x1, y1, x2, y2):
return (float)(y2-y1)/(x2-x1)
midPoint = 400
tenPercentOfMidpoint = .10 * midPoint
x = np.linspace(midPoint - tenPercentOfMidpoint, midPoint + tenPercentOfMidpoint, 50)
m = np.empty(x.shape)
c = np.empty(x.shape)
m[(x>=midPoint)] = 1.0
m[(x<midPoint)] = 1.0
c[x<midPoint] = 0
c[x>=midPoint] = 0
y=m*x+c
lenOfx = len(x)
lenOfy = len(y)
s = slope(0, y[0], lenOfx, y[lenOfy - 1])
angle = np.rad2deg(np.arctan2(x[-1] - x[0], lenOfy - 0))
如果我现在打印s
print(s)
结果是1.6
如果我打印angle
print(angle)
结果是57.99
我想在midPoint - tenPercent and midPoint + tenPercent
和slope
45之间画一条线
我相信这只是一个插错东西的问题。您在
slope
中测试的点是(0, y[0])
==(0, 360.0)
和(lenOfx, y[lenOfy - 1])
=(50, 440.0)
这些点不在y=m*x+c
定义的线上。我相信您希望拉动线段的两个端点:计算角度的类似问题;您希望使用
y
中的更改和x
中的更改:坐标数(
len
)不应与坡度计算相关注意
x
和y
是相同的(x == y
返回True
的向量)对于坡度为1的直线,y的变化应与x的变化相同,即根据坡度的定义
最接近坡度1线的方法是更改x
或者通过将百分之十的中点更改为一个数字
相关问题 更多 >
编程相关推荐