Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将坐标调整到重心_Python_Coordinates - Fatal编程技术网

Python:将坐标调整到重心

Python:将坐标调整到重心,python,coordinates,Python,Coordinates,我有一个python脚本,我在其中导入三角形元素的坐标,以及来自两个独立文本文件的元素定义 坐标文件如下所示: id,x,y, 1, 0, 0 2, 0, 1 3, 0, 2 4, 1, 0 5, 1, 1 6, 1, 2 7, 2, 0 8, 2, 1 9, 2, 2 #!/usr/bin/env python open("D://Documents//SkyDrive//afstudere

我有一个python脚本,我在其中导入三角形元素的坐标,以及来自两个独立文本文件的元素定义

坐标文件如下所示:

id,x,y,
  1,  0,   0
  2,  0,   1
  3,  0,   2
  4,  1,   0
  5,  1,   1
  6,  1,   2
  7,  2,   0
  8,  2,   1
  9,  2,   2
    #!/usr/bin/env python


open("D://Documents//SkyDrive//afstuderen//99 EEM - Abaqus 6.11.2//scripting//_COORDINATEN.txt", "r")
import csv
import itertools

with open("_COORDINATEN.txt") as file:
    data = csv.reader(file)
    next(data)
    coords = []
    coords = ([[float(x) for x in line[1:]] for line in data])


open("D://Documents//SkyDrive//afstuderen//99 EEM - Abaqus 6.11.2//scripting//_ELEMENTEN.txt", "r")
import csv
import itertools

with open("_ELEMENTEN.txt") as file:
    data2 = csv.reader(file)
    next(data2)
    elems = []
    elems = ([[int(x)-1 for x in line[1:]] for line in data2])


#Flip the original elements if required
for i,elem in enumerate(elems):
    ecoords = [coords[e] for e in elem]

    a = [x2-x1 for x1,x2 in zip(ecoords[0],ecoords[1])]
    b = [x2-x1 for x1,x2 in zip(ecoords[1],ecoords[2])]

    n = a[0]*b[1]-a[1]*b[0]

    if n < 0:
        elems[i] = [ elem[0], elem[2], elem[1] ]

#bewerking elementen
newcoords = []
newelems  = []
for elem in elems:
    ecoords = [coords[e] for e in elem]
    newelem = range( len(newcoords), len(newcoords)+len(ecoords) )

    newcoords += ecoords
    newelems.append( newelem )

cohelems = []
for e,elem in enumerate(elems):
  for edge in [[0,1],[1,2],[2,0]]:

    eedge = [elem[i] for i in edge]

    for e2,elem2 in enumerate(elems[e+1:]):

      e2 += e+1

      for edge2 in [[0,1],[1,2],[2,0]]:

        eedge2 = [elem2[i] for i in edge2]

        if all([i in eedge2 for i in eedge]):

          newedge  = [newelems[e][i] for i in edge ]
          newedge += [newelems[e2][i] for i in edge2]

          cohelems.append( newedge[-1::-1] )
元素文件如下所示:

id, n1, n2, n3
 1, 1, 2, 4
 2, 1, 2, 5
 3, 2, 3, 5
 4, 3, 5, 6
 5, 5, 6, 8
 6, 6, 8, 9
 7, 5, 7, 8
 8, 4, 5, 7
在脚本中,当三角形元素的两条边位于同一位置时,我定义了一个新元素(矩形)。我首先为每个三角形元素定义唯一的节点(因此元素不再共享同一个节点),然后通过角点中的四个节点定义一个新元素。 见下图

这很好,但是新定义的元素的厚度为零。我确实想让它们有一个物理厚度。因此,我想调整三角形元素节点的坐标,并将它们稍微移动到元素的重心

如何找到三角形元素的重心,然后在元素重心方向上更改节点的坐标,水平距离为0.001,垂直距离为0.001

我目前拥有的脚本如下所示:

id,x,y,
  1,  0,   0
  2,  0,   1
  3,  0,   2
  4,  1,   0
  5,  1,   1
  6,  1,   2
  7,  2,   0
  8,  2,   1
  9,  2,   2
    #!/usr/bin/env python


open("D://Documents//SkyDrive//afstuderen//99 EEM - Abaqus 6.11.2//scripting//_COORDINATEN.txt", "r")
import csv
import itertools

with open("_COORDINATEN.txt") as file:
    data = csv.reader(file)
    next(data)
    coords = []
    coords = ([[float(x) for x in line[1:]] for line in data])


open("D://Documents//SkyDrive//afstuderen//99 EEM - Abaqus 6.11.2//scripting//_ELEMENTEN.txt", "r")
import csv
import itertools

with open("_ELEMENTEN.txt") as file:
    data2 = csv.reader(file)
    next(data2)
    elems = []
    elems = ([[int(x)-1 for x in line[1:]] for line in data2])


#Flip the original elements if required
for i,elem in enumerate(elems):
    ecoords = [coords[e] for e in elem]

    a = [x2-x1 for x1,x2 in zip(ecoords[0],ecoords[1])]
    b = [x2-x1 for x1,x2 in zip(ecoords[1],ecoords[2])]

    n = a[0]*b[1]-a[1]*b[0]

    if n < 0:
        elems[i] = [ elem[0], elem[2], elem[1] ]

#bewerking elementen
newcoords = []
newelems  = []
for elem in elems:
    ecoords = [coords[e] for e in elem]
    newelem = range( len(newcoords), len(newcoords)+len(ecoords) )

    newcoords += ecoords
    newelems.append( newelem )

cohelems = []
for e,elem in enumerate(elems):
  for edge in [[0,1],[1,2],[2,0]]:

    eedge = [elem[i] for i in edge]

    for e2,elem2 in enumerate(elems[e+1:]):

      e2 += e+1

      for edge2 in [[0,1],[1,2],[2,0]]:

        eedge2 = [elem2[i] for i in edge2]

        if all([i in eedge2 for i in eedge]):

          newedge  = [newelems[e][i] for i in edge ]
          newedge += [newelems[e2][i] for i in edge2]

          cohelems.append( newedge[-1::-1] )
#/usr/bin/env python
打开(“D://Documents//SkyDrive//afstuderen//99 EEM-Abaqus 6.11.2//scripting//\u coordinaren.txt”,“r”)
导入csv
进口itertools
打开(“_coordinaren.txt”)作为文件:
data=csv.reader(文件)
下一步(数据)
coords=[]
coords=([[float(x)表示第x行[1:]表示第x行数据])
打开(“D://Documents//SkyDrive//afstuderen//99 EEM-Abaqus 6.11.2//scripting//\u ELEMENTEN.txt”,“r”)
导入csv
进口itertools
打开(“_ELEMENTEN.txt”)作为文件:
data2=csv.reader(文件)
下一步(数据2)
元素=[]
元素=([[int(x)-1表示第x行[1:]表示第2行数据])
#如果需要,翻转原始图元
对于i,枚举中的元素(元素):
ecoords=[coords[e]表示元素中的e]
a=[x2-x1表示x1,x2表示zip(ecoords[0],ecoords[1])]
b=[x2-x1表示x1,x2表示zip(ecoords[1],ecoords[2])]
n=a[0]*b[1]-a[1]*b[0]
如果n<0:
元素[i]=[elem[0],元素[2],元素[1]]
#贝沃金元素
newcoords=[]
newelems=[]
对于元素中的元素:
ecoords=[coords[e]表示元素中的e]
newelem=范围(len(newcoords),len(newcoords)+len(ecoords))
newcoords+=ecoords
newelem.append(newelem)
coherems=[]
对于e,枚举中的元素(元素):
对于[[0,1]、[1,2]、[2,0]]中的边:
eedge=[elem[i]表示边缘中的i]
对于e2,枚举中的elem2(elems[e+1:]):
e2+=e+1
对于[[0,1]、[1,2]、[2,0]]中的边2:
eedge2=[elem2[i]表示第2边中的i]
如果所有([i在eedge中为i在eedge中]):
newedge=[newelems[e][i]表示边缘中的i]
newedge+=[newelems[e2][i]代表edge2中的i]
append(newedge[-1::-1])

我不会试图使它与您的变量名完全对应。相反,我将给出一个一般的例子,说明如何进行您想要的收缩。你应该能够把它应用到你自己的事情上。我在使用Michael Mauderer链接的页面上的公式之一

问题只是向量代数。如果您一般不打算对点使用向量类,那么至少可以定义一些向量操作:

def add_vectors(*points):
  new_x = 0.0
  new_y = 0.0
  for point in points:
    new_x += point[0]
    new_y += point[1]
  return [new_x, new_y]

def subtract_vectors(a, b):
  new_x = a[0] - b[0]
  new_y = a[1] - b[1]
  return [new_x, new_y]


def mul_by_scalar(vector, scalar):
  new_x = vector[0] * scalar
  new_y = vector[1] * scalar
  return [new_x, new_y]
有了这些,剩下的就更容易了:

triangle = [[0,0], [1,0], [1,1]]

# finding the center of mass:
#   CM = (1/3) * (a + b + c)
# CM:       position vector to the center of mass
# a, b, c:  position vectors to the corners

CM = mul_by_scalar(add_vectors(*triangle), 1.0/3)

# For every point of the triangle, find a vector that points towards its CM.
# Scale the vectors to 10% (in this instance).

point_to_CM_vectors = []
for point in triangle:
  point_to_CM_vectors.append(subtract_vectors(CM, point))

# Make a new triangle, contracted by 10%.

new_triangle = []
for point, motion in zip(triangle, point_to_CM_vectors):
  new_triangle.append(add_vectors(point, mul_by_scalar(motion, 0.10)))

您可以很容易地看到如何内联
添加向量
减去向量
逐标量mul\u
的功能来“手动”执行操作,但您会不断重复自己的操作,代码会让人困惑。

您看过这个吗?谢谢你的回答,我知道如何找到三角形的重心。但是我不知道如何用python编程,尤其是如何改变三角形元素在重心方向上的坐标