X 和 Y 从斜坡截取 - Python
X and Y Intercepts From Slopes - Python
我一直在研究斜率计算器,该计算器还可以找到 x 和 y 截距...我如何在 Python 中执行此操作?谢谢!
这是我当前的代码:
def getSlope(x1, y1, x2, y2):
slope = (y2-y1)/(x2-x1)
return slope
def getYInt(x1, y1, x2, y2):
s = getSlope(x1, y1, x2, y2)
x = 0
y = s*0 + yi
要找到 y 截距 (b),您需要将 x 设置为 x 值之一,将 y 设置为 1,如果 y 值并求解:
y=mx+b
b=y-mx
函数可能如下所示:
m=getSlope(x1,y1,x2,y2)
b=y1-m*x1
return b
该点的坐标将是 (0,b)
,因此您可以 return 如果需要,可以改用它。
坡度:
from __future__ import division
def getSlope((x1, y1), (x2, y2)):
return (y2-y1)/(x2-x1)
对于 y 截距
def getYInt((x1, y1), (x2, y2)):
slope = getSlope((x1, y1), (x2, y2))
y = -x1*slope+y1
return (0, y)
>>> slope((7, 3), (2, 9))
-1.2
>>> getYInt((7, 3), (2, 9))
(0, 11.4)
>>>
一行的公式是
y = m * x + c # m-->slope, c-->intercept
c = y - m * x # same formula rearranged.
在你的 getYInt
函数中你只需要这些行:
def getYInt(x1, y1, x2, y2):
s = getSlope(x1, y1, x2, y2)
return y1 - s * x1
此外,如果您使用的是 Python 2 系列,请注意整数除法。
import sys
def test(did_pass):
""" Print the result of a test. """
linenum = sys._getframe(1).f_lineno # Get the caller's line number.
if did_pass:
msg = "Test at line {0} ok.".format(linenum)
else:
msg = ("Test at line {0} FAILED.".format(linenum))
print(msg)
def slope(x1, y1, x2, y2):
return (y2-y1)/(x2-x1)
test(slope(5, 3, 4, 2) == 1.0) # ok
test(slope(1, 2, 3, 2) == 0.0) # ok
test(slope(1, 2, 3, 3) == 0.5) # ok
test(slope(2, 4, 1, 2) == 2.0) # ok
def intercept(x1, y1, x2, y2): # y1 = mx1 + q -> q = y1 - mx1
m = slope(x1, y1, x2, y2)
q = y1 - m * x1
return q
test(intercept(1, 6, 3, 12) == 3.0) # ok
test(intercept(6, 1, 1, 6) == 7.0) # ok
test(intercept(4, 6, 12, 8) == 5.0) # ok
我一直在研究斜率计算器,该计算器还可以找到 x 和 y 截距...我如何在 Python 中执行此操作?谢谢! 这是我当前的代码:
def getSlope(x1, y1, x2, y2):
slope = (y2-y1)/(x2-x1)
return slope
def getYInt(x1, y1, x2, y2):
s = getSlope(x1, y1, x2, y2)
x = 0
y = s*0 + yi
要找到 y 截距 (b),您需要将 x 设置为 x 值之一,将 y 设置为 1,如果 y 值并求解:
y=mx+b
b=y-mx
函数可能如下所示:
m=getSlope(x1,y1,x2,y2)
b=y1-m*x1
return b
该点的坐标将是 (0,b)
,因此您可以 return 如果需要,可以改用它。
坡度:
from __future__ import division
def getSlope((x1, y1), (x2, y2)):
return (y2-y1)/(x2-x1)
对于 y 截距
def getYInt((x1, y1), (x2, y2)):
slope = getSlope((x1, y1), (x2, y2))
y = -x1*slope+y1
return (0, y)
>>> slope((7, 3), (2, 9))
-1.2
>>> getYInt((7, 3), (2, 9))
(0, 11.4)
>>>
一行的公式是
y = m * x + c # m-->slope, c-->intercept
c = y - m * x # same formula rearranged.
在你的 getYInt
函数中你只需要这些行:
def getYInt(x1, y1, x2, y2):
s = getSlope(x1, y1, x2, y2)
return y1 - s * x1
此外,如果您使用的是 Python 2 系列,请注意整数除法。
import sys
def test(did_pass):
""" Print the result of a test. """
linenum = sys._getframe(1).f_lineno # Get the caller's line number.
if did_pass:
msg = "Test at line {0} ok.".format(linenum)
else:
msg = ("Test at line {0} FAILED.".format(linenum))
print(msg)
def slope(x1, y1, x2, y2):
return (y2-y1)/(x2-x1)
test(slope(5, 3, 4, 2) == 1.0) # ok
test(slope(1, 2, 3, 2) == 0.0) # ok
test(slope(1, 2, 3, 3) == 0.5) # ok
test(slope(2, 4, 1, 2) == 2.0) # ok
def intercept(x1, y1, x2, y2): # y1 = mx1 + q -> q = y1 - mx1
m = slope(x1, y1, x2, y2)
q = y1 - m * x1
return q
test(intercept(1, 6, 3, 12) == 3.0) # ok
test(intercept(6, 1, 1, 6) == 7.0) # ok
test(intercept(4, 6, 12, 8) == 5.0) # ok