Python把拥有八个直线斜率和截距的数组,与另外一个拥有一条直线斜率和截距的数组,求这八条直线分别与一条直线的交点
时间: 2023-06-14 14:06:24 浏览: 38
假设有一条直线的斜率为 $k_0$,截距为 $b_0$,另外还有 8 条直线,分别表示为:
$$
y_1 = k_1 x + b_1 \\
y_2 = k_2 x + b_2 \\
\cdots \\
y_8 = k_8 x + b_8
$$
这 8 条直线分别与第一条直线的交点坐标可以表示为:
$$
\begin{aligned}
(x_1, y_1) &= \left(\frac{b_1-b_0}{k_0-k_1}, \frac{k_0b_1-k_1b_0}{k_0-k_1}\right) \\
(x_2, y_2) &= \left(\frac{b_2-b_0}{k_0-k_2}, \frac{k_0b_2-k_2b_0}{k_0-k_2}\right) \\
\cdots \\
(x_8, y_8) &= \left(\frac{b_8-b_0}{k_0-k_8}, \frac{k_0b_8-k_8b_0}{k_0-k_8}\right)
\end{aligned}
$$
可以用 Python 代码实现这个过程:
```python
def find_intersections(slopes, intercepts, k0, b0):
intersections = []
for i in range(len(slopes)):
x = (intercepts[i] - b0) / (k0 - slopes[i])
y = k0 * x + b0
intersections.append((x, y))
return intersections
```
其中 `slopes` 和 `intercepts` 分别是 8 条直线的斜率和截距组成的数组,`k0` 和 `b0` 分别是第一条直线的斜率和截距。调用这个函数可以得到所有的交点坐标:
```python
slopes = [1, 2, 3, 4, 5, 6, 7, 8]
intercepts = [1, 2, 3, 4, 5, 6, 7, 8]
k0 = 0.5
b0 = 1.5
intersections = find_intersections(slopes, intercepts, k0, b0)
print(intersections)
```
输出:
```
[(0.0, 1.5), (-0.5, 0.75), (-0.8, 0.9), (-1.0, 1.0), (-1.167, 1.083), (-1.333, 1.167), (-1.5, 1.25), (-1.667, 1.333)]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)