import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import norm
from matplotlib.collections import LineCollection
n = 10
radius = 20
A = np.random.uniform(-10, 10, (n, 2))
B = A / norm(A, axis=1, keepdims=True) * radius # normalize and put on a circle with given radius
lines = np.dstack([B[:, 1], -B[:, 0], -B[:, 1], B[:, 0]]).reshape(-1, 2, 2)
fig, axes = plt.subplots(ncols=3, figsize=(14, 4))
for ax in axes:
ax.set_aspect('equal')
for ax in axes[:2]:
lc = LineCollection(lines, colors='blue', linewidths=2)
ax.add_collection(lc)
if ax == axes[0]:
ax.scatter(A[:, 1], -A[:, 0], color='crimson')
ax.scatter(-A[:, 1], A[:, 0], color='crimson')
elif ax == axes[1]:
ax.set_xlim(-radius / 2, radius / 2)
ax.set_ylim(-radius / 2, radius / 2)
for k in range(2):
axes[2].scatter(lines[:, k, 0], lines[:, k, 1], color='crimson')
axes[0].set_title('lines in circle and dots')
axes[1].set_title('lines till border')
axes[2].set_title('dots on circle')
plt.show()
给定一个
n*2
矩阵A
,对于每一行i
,一行由A[i,0]*x + A[i,1]*y == 0
定义。这意味着0,0
始终位于直线上,以及点x=A[i,1],y=-A[i,0]
。与任何值相乘(例如通过标准化)将再次在直线上给出点以下代码显示了可视化这些行的3种方法:
x=A[i,1],y=-A[i,0]
和x=-A[i,1],y=A[i,0]
一起被圆切割李>相关问题 更多 >
编程相关推荐