【问题标题】:Labeling points, that a generated within a unit circle标记点,在单位圆内生成
【发布时间】:2021-08-09 04:40:57
【问题描述】:

假设我们要生成带有单位圆的标签。

做了什么:我在单位圆内生成了第一个点并将其标记为 1。我在单位圆内生成了第二个点并将其标记为 0。

休息 k-2 个点我想用以下方式标记:如果一个点位于圆的一半 1 -> 它被标记为 1。如果在圆的一半 0 -> 那么它是标签 0。所以这意味着,在生成前 2 个点后,我需要通过分隔前两个点的圆心画一条线。之后,我们将新生成的点与这条线进行比较并标记它。如何在 Python 中瘦身?

【问题讨论】:

  • 在无数条分隔两点的线中,您要选择哪一条?如果两点和圆心共线呢?
  • 这完全取决于你在哪里把圆圈分成两半
  • @wjm 1) 哪个都没有关系。它只需要将点分成不同的半圆。 2)可能发生,但可能性很小
  • @Sayse 没关系,只需将前两点分成不同的半圆
  • 您可以保存前两个角度,例如a0a1。给定角度ak,如果(a0 - ak) % (2 * np.pi) < (a1 - ak) % (2 * np.pi),则可以将标签设置为0,否则设置为1

标签: python algorithm geometry


【解决方案1】:

这个怎么样?

这个想法是在构造前两个点x[0,:]x[1,:]之后,分别标记为y[0] = 0y[1] = 1,你可以构造一个向量n垂直于直线的角平分线向量x[0,:]x[1,:] 之间的角度。这就是为什么,这个向量被构造为

n = x[0,:] / norm(x[0,:])  -  x[1,:] / norm(x[1,:])

之后,在角平分线的一侧,n 指向的一侧,然后向量标记为01,如果它在角度的另一侧平分线。点积n.dot(x[i,:]) > 0 表示x[i,:]n 所指的角平分线的一侧。

import numpy as np
import matplotlib.pyplot as plt

k = 12
x = np.empty((k, 2), dtype=float)
y = np.empty(k, dtype=int)
n = np.array([0.,0.])
for i in range(k):
    if i < 2:        
        angle = np.pi * np.random.uniform(0, 2)
        x[i,:] = np.array([np.cos(angle), np.sin(angle)])
        n = n + ((-1)**i) * x[i,:]
        length = np.random.uniform(0, 1)
        x[i,:] = np.sqrt(length)*x[i,:]
        y[i] = i
    else:
        angle = np.pi * np.random.uniform(0, 2)
        length = np.random.uniform(0, 1)
        x[i,:] = np.array([np.cos(angle), np.sin(angle)])
        if n.dot(x[i,:]) > 0: 
            y[i] = 0 
        else: 
            y[i] = 1
        x[i,:] = np.sqrt(length) * x[i,:]



s = np.linspace( 0 , 2 * np.pi , 150 )
xc = np.cos( s )
yc = np.sin( s )

xl = np.linspace( -1, 1, 150)  
yl = (- n[0]/n[1]) * xl


figure, axes = plt.subplots( 1 )

axes.plot( xc, yc )
axes.plot(0, 0)
axes.plot( xl, yl )

axes.plot(x[y==1, 0], x[y==1, 1], 'ro')
axes.plot(x[y==0, 0], x[y==0, 1], 'bo')

axes.set_aspect( 1 )  
plt.show()

【讨论】:

  • 谢谢!但是n变量是什么意思呢?
  • @LiverToll92 我在回答中添加了解释。
【解决方案2】:

保存两个角度之一,命名为saved_angle,对于所有点xy,检查是否x * np.sin(saved_angle) - y * np.cos(saved_angle) &lt; 0。这是一个代码示例:

import matplotlib.pyplot as plt
import numpy as np

plt.xlim([-1, 1])
plt.ylim([-1, 1])

# Get two random points, saving the angle of one.

length = np.random.uniform(0, 1)
angle = np.pi * np.random.uniform(0, 2)
x = np.sqrt(length) * np.cos(angle)
y = np.sqrt(length) * np.sin(angle)
plt.plot([x], [y], 'ko')

length = np.random.uniform(0, 1)
saved_angle = np.pi * np.random.uniform(0, 2)
x = np.sqrt(length) * np.cos(saved_angle)
y = np.sqrt(length) * np.sin(saved_angle)
plt.plot([x], [y], 'ko')

# Plot a bunch of random points, checking the condition.

for _ in range(100):
    length = np.random.uniform(0, 1)
    angle = np.pi * np.random.uniform(0, 2)
    x = np.sqrt(length) * np.cos(angle)
    y = np.sqrt(length) * np.sin(angle)
    plt.plot([x], [y], 'bo' if x * np.sin(saved_angle) - y * np.cos(saved_angle) < 0 else 'ro')

plt.show()

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2019-04-25
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多