向多边形添加剪切

2024-09-30 01:29:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图创造一套不同的poylgons。为了创建这个集合,我对从some rotations开始的规则多边形应用一些变换。为了增加多样性,我想add shear on each shape

我的问题是,我的剪切函数doesn't work用于一些多边形,这些多边形已旋转为与x轴内联(输出的比例相差很远)。 更具体地说,它不适用于具有4条以上边且已旋转以与x轴内联的多边形,它适用于rotated polygons with 4 or less sides

我的问题是,有没有人知道一种更可靠的方法可以向多边形添加剪切? 如果没有,你能告诉我如何使我当前的剪切函数更健壮吗

以下是在任何人需要时重新创建问题的代码:

在剪切图和注释掉的剪切图之间切换,以查看工作和非工作示例。 两个DICT的索引/键都是多边形的边数

import math
from random import randint
import numpy as np
import pandas as pd 
import matplotlib.pylab as plt

#Generate regular polygons
def polygon(sides, radius=0.25, rotation=0, translation=None):
    one_segment = math.pi * 2 / sides
    x_coord = []
    y_coord = []
    if sides == 4:
        rotation = 0.785398
    for i in range(sides): 
        x = math.sin(one_segment * i + rotation) * radius
        y = math.cos(one_segment * i + rotation) * radius
        x_coord.append(x)
        y_coord.append(y)
    x_coord.append(x_coord[0])
    y_coord.append(y_coord[0]) 
    return x_coord,y_coord

#Generates stream facing polygons 
def streamfacing_polygon(sides, radius=0.25, rotation=0, translation=None):
    one_segment = math.pi * 2 / sides
    x_coord = []
    y_coord = []
    if sides == 3 or sides == 9:
        rotation = 0.523599
    if sides == 5 or sides ==7:
        rotation = -1.5708
    if sides == 6 or sides == 10:
        rotation = -1.5708
    if sides == 11:
        rotation = 0.139626 
    for i in range(sides): 
        x = math.sin(one_segment * i + rotation) * radius
        y = math.cos(one_segment * i + rotation) * radius
        x_coord.append(x)
        y_coord.append(y)
    x_coord.append(x_coord[0])
    y_coord.append(y_coord[0]) 
    return x_coord,y_coord

#Generate a set of polygons
def polygon_dict(max_num_sides,mode=None):
    polygon_dict = {}
    for i in range(3,max_num_sides):
        if mode == None:
            x,y =polygon(i)
            polygon_dict[i] = x,y
        elif mode == 'streamlined':
            x,y =streamfacing_polygon(i)
            polygon_dict[i] = x,y     
    return polygon_dict

shape_dict = polygon_dict(11)
sl_shape_dict = polygon_dict(11,'streamlined')


shear = np.identity(2)
def add_shear(shape_coord, low, high, key):
    """
    Parameters
    ----------
    shape_coord : x,y coordinates in a nested list(x,y) 
    low : Lowest shear value
    high : Highest shear value
    key : Number of sides of shape

    Returns
    -------
    sheared_poly : x,y coordinates of sheareed shapes
    shear_val : shear value used to produce this 

    """
    shear[0,1] = np.random.uniform(0.95, 1.05)
    shear[0,1] = np.random.uniform(low, high)
    print("\tusing shape_dict {} , shear {}".format(key, shear[0,1]))
    shear_val = shear[0,1]
    shape = shape_coord
    arf = np.asarray(shape)
    ar = arf.copy()

    tempar = ar.copy()
    arshear = np.dot(shear, ar).T

    if key < 0:
        min_original= min(arshear[:,0])
        min_shear = min(arf[:,0])
        miny_original= min(arshear[0,:])
        miny_shear= min(arf[0,:])

        for i in range(len(arshear)):
            arshear[i,0] = arshear[i,0]*(min_original/min_shear)
            arshear[i,1] = arshear[i,1]*(miny_original/min_shear)

        if 0:
            plt.subplot(1, 2, 1)
            a, b = ar.T
            plt.scatter(a,b)
            plt.axis('equal')
            plt.subplot(1, 2, 2)
            c, d = arshear.T
            plt.scatter(c,d)
            plt.axis('equal')
            #plt.show()
        sheared_poly = arshear.T

    else:
        maxx_original= max(arshear[:,0])
        maxx_shear = max(arf[:,0])
        maxy_original= max(arshear[0,:])
        maxy_shear= max(arf[0,:])

        for i in range(len(arshear)):
            arshear[i,0] = arshear[i,0]*(maxx_original/maxx_shear)
            arshear[i,1] = arshear[i,1]*(maxy_original/maxy_shear)
        if 0:
            plt.subplot(1, 2, 1)
            a, b = ar.T
            plt.scatter(a,b)
            plt.axis('equal')
            plt.subplot(1, 2, 2)
            c, d = arshear.T
            plt.scatter(c,d)
            plt.axis('equal')
            #plt.show()
        sheared_poly = arshear.T

    return sheared_poly, shear_val

#add shear to shape 
#switch between sl_shape_dict and shape_dict comments

#tri = sl_shape_dict[5]                  #doesn't work 
tri = shape_dict[5]                      #works
tri_sheared,shear_val =add_shear(tri, low =-0.4, high=-0.2, key=3 )
neg_tri_sheared,shear_val =add_shear(tri, low =-1.1, high=-0.9, key=3 )

#plotting 
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(tri_sheared[0], tri_sheared[1], marker='.', label='lo')
ax.plot(neg_tri_sheared[0], neg_tri_sheared[1], marker='.', label='hi')
plt.xlabel('x')
plt.ylabel('y')
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.set_title('negative shear values')
plt.legend(loc='upper right')   
plt.show()

Tags: ifpltmintridictshapeoriginalpolygon

热门问题