如何在边界之间包装值?

时间:2019-02-26 18:46:02

标签: glsl shader

如何围绕任意边界包装值?我想避免进行检查,因此我想出了以下步骤可用于低边界的方法:

from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

#A more common way of speeding up a machine learning algorithm is by using Principal Component Analysis (PCA).

cluster = KMeans(n_clusters = 3)    
mt["cluster"] = cluster.fit_predict(mt[mt.columns[2:]])

# In[9]:

cols = mt.columns    

# In[10]:

#Principal Component separation to create a 2D picture

pca = PCA(n_components = 2)    
mt['x']=abs(pca.fit_transform(mt[cols])[:,0])    
mt['y']=abs(pca.fit_transform(mt[cols])[:,1])    
mt = mt.reset_index()

# In[11]:    

mt.head()

# In[34]:    

cls_clusters = mt[["Class","cluster", "x","y"]]    

# In[13]:    

cls_clusters.tail()    

# In[14]:

from copy import deepcopy    
from matplotlib import pyplot as plt    
plt.rcParams['figure.figsize'] = (16, 9)
plt.style.use('ggplot')

# In[15]:    

# Getting the values and plotting it    
f1 = mt['x'].values    
f2 = mt['y'].values    
X = np.array(list(zip(f1, f2)))    
plt.scatter(f1, f2, c='black', s=7)    

# In[16]:


# Euclidean Distance Caculator
def dist(a, b, ax=1):
    return np.linalg.norm(a - b, axis=ax)

# In[17]:

# Number of clusters

k = 3

# X coordinates of random centroids

C_x = np.random.randint(0, np.max(X)-20, size=k)

# Y coordinates of random centroids

C_y = np.random.randint(0, np.max(X)-20, size=k)
C = np.array(list(zip(C_x, C_y)), dtype=np.float32)
print(C)

# In[18]:    

# Plotting along with the Centroids

plt.scatter(f1, f2, c='#050505', s=7)    
plt.scatter(C_x, C_y, marker='*', s=200, c='g')

# In[19]:    

# To store the value of centroids when it updates    

C_old = np.zeros(C.shape)    

# Cluster Lables(0, 1, 2)

clusters = np.zeros(len(X))

# Error func. - Distance between new centroids and old centroids

error = dist(C, C_old, None)

# Loop will run till the error becomes zero

while error != 0:

    # Assigning each value to its closest cluster

    for i in range(len(X)):    
        distances = dist(X[i], C)    
        cluster = np.argmin(distances)    
        clusters[i] = cluster

    # Storing the old centroid values

    C_old = deepcopy(C)

    # Finding the new centroids by taking the average value

    for i in range(k):    
        points = [X[j] for j in range(len(X)) if clusters[j] == i]    
        C[i] = np.mean(points, axis=0)    

    error = dist(C, C_old, None)

# In[20]:    

colors = ['r', 'g', 'b', 'y', 'c', 'm']    
fig, ax = plt.subplots()

for i in range(k):    
        points = np.array([X[j] for j in range(len(X)) if clusters[j] == i])    
        ax.scatter(points[:, 0], points[:, 1], s=7, c=colors[i])

ax.scatter(C[:, 0], C[:, 1], marker='*', s=200, c='#050505')

# In[31]:    

cluster_map = pd.DataFrame()    
cluster_map['data_index'] = mt.index.values    
cluster_map['cluster'] = cluster.labels_    

# In[32]:    

print(cluster_map[cluster_map.cluster == 3])

这将输出向量float check = step(-1, val)*2.0-1.0; val *= check; 大于-1时所具有的任何值,然后回绕,当向量val大于-1时跳至1。

我的目标是,当一个值超过某个阈值时,它会“结束”并从屏幕的另一侧开始返回。因此,例如,如果移动点超过1,则它将重新出现在屏幕上的-1位置,并继续从该位置开始移动。

另一方面,如果它低于-1,它将出现在位置1并从那里继续移动。

1 个答案:

答案 0 :(得分:0)

float check = step(-1, val)*2.0-1.0;
     

只要此值大于-1,这将有效输出向量val在其分量中具有的任何值,并在超过该值时回绕并跳至1。

不。在任何情况下,函数step()的结果均为0.0或1.0。因此step(-1, val)*2.0-1.0的结果是-1.0或1.0。


  

我的目标是,当一个值超过某个阈值时,它会“结束”并从屏幕的另一侧开始返回。因此,例如,如果移动点超过1,它将重新出现在屏幕上的-1位置

您可以通过函数mod(x, y) https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mod.xhtml来实现,该函数返回x除以y的其余部分:

y = mod(x+1.0, 2.0)-1.0 

x的值无关,函数的结果始终在[-1,1]范围内。如果达到上限1,则紧接着的结果是-1:

请注意,该函数使用mod() https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/mod.xhtml支持genType。这意味着xy甚至可以是vec2vec3vec4类型。当然,xy的类型必须相同。