【问题标题】:making a function that translates a point around another point制作一个围绕另一个点平移一个点的函数
【发布时间】:2019-07-08 13:51:59
【问题描述】:

给定我的程序理论上应该的点数组,找到彼此最远的两个点。然后计算这两个点与 x 轴的夹角。然后将阵列中的所有点围绕所有点的平均中心旋转该角度。出于某种原因,我围绕中心旋转所有点的翻译功能不起作用,它给了我意想不到的值。我相当确定我用来执行此操作的数学是准确的,因为我使用 wolfram alpha 测试了我使用的公式并在 desmos 上绘制了点。我不确定我的代码有什么问题,因为它一直给我意想不到的输出。任何帮助将不胜感激。 这是转换数组的代码:

def translation(array,centerArray):
    array1=array
    maxDistance=0
    point1=[]
    point2=[]
    global angle
    for i in range(len(array1)):
        for idx in range(len(array1)):
            if(maxDistance<math.sqrt(((array1[i][0]-array1[idx][0])**2)+((array1[i][1]-array1[idx][1])**2)+((array1[i][2]-array1[idx][2])**2))):
                maxDistance=math.sqrt(((array1[i][0]-array1[idx][0])**2)+((array1[i][1]-array1[idx][1])**2)+((array1[i][2]-array1[idx][2])**2))
                point1 = array1[i]
                point2 = array1[idx]
    angle=math.atan2(point1[1]-point2[1],point1[0]-point2[0]) #gets the angle between two furthest points and xaxis


    for i in range(len(array1)): #this is the problem here 
        array1[i][0]=((array[i][0]-centerArray[0])*math.cos(angle)-(array[i][1]-centerArray[1])*math.sin(angle))+centerArray[0] #rotate x cordiate around center of all points 
        array1[i][1]=((array[i][1]-centerArray[1])*math.cos(angle)+(array[i][0]-centerArray[0])*math.sin(angle))+centerArray[1] #rotate y cordiate around center of all points 

    return array1   

这是我用来测试它的代码。 tortose 是我设置的海龟图形名称

tortose.color("violet")
testarray=[[200,400,9],[200,-100,9]] #array of 2 3d points but don't worry about z axis it will not be used for in function translation
print("testsarray",testarray)
for i in range(len(testarray)): #graph points in testarray
    tortose.setposition(testarray[i][0],testarray[i][1]) 
    tortose.dot()
testcenter=findCenter(testarray) # array of 1 point in the center of all the points format center=[x,y,z] but again don't worry about z
print("center",testcenter)
translatedTest=translation(testarray,testcenter) # array of points after they have been translated same format and size of testarray

print("translatedarray",translatedTest) #should give the output [[-50,150,9]] as first point but instead give output of [-50,-99.999999997,9] not sure why
tortose.color("green")
for i in range(len(testarray)): #graphs rotated points 
    tortose.setposition(translatedTest[i][0],translatedTest[i][1]) 
    tortose.dot()

print(angle*180/3.14) #checks to make sure angle is 90 degrees because it should be in this case this is working fine



tortose.color("red")
tortose.setposition(testcenter[0],testcenter[1])
tortose.dot()

find center 代码找到数组中所有点的中心不要担心z轴,因为它不用于平移:

def findCenter(array):
    sumX = 0
    sumY = 0
    sumZ = 0
    for i in range(len(array)):
        sumX += array[i][0]
        sumY += array[i][1] 
        sumZ += array[i][2]
    centerX= sumX/len(array)
    centerY= sumY/len(array)
    centerZ= sumZ/len(array)
    #print(centerX)
    #print(centerY)
    #print(centerZ)
    centerArray=[centerX,centerY,centerZ]
    return centerArray

import math
import turtle
tortose = turtle.Turtle()
tortose.penup()

我的预期输出应该是 (-50,150) 的一个点,但它给了我一个 (-50,-99.99999999999997) 的点

【问题讨论】:

    标签: python-3.x math graphing


    【解决方案1】:

    这是进行就地旋转时的常见错误:

    array1[i][0]= ...
    array1[i][1]= ... array[i][0] ...
    

    首先你更新array1[i][0]。然后更新array1[i][1],但在应该使用旧值时使用新值。相反,暂时存储旧值:

    x = array1[i][0]
    array1[i][0]=((array[i][0]-centerArray[0])*math.cos(angle)-(array[i][1]-centerArray[1])*math.sin(angle))+centerArray[0] #rotate x cordiate around center of all points 
    array1[i][1]=((array[i][1]-centerArray[1])*math.cos(angle)+(x-centerArray[0])*math.sin(angle))+centerArray[1] #rotate y cordiate around center of all points 
    

    【讨论】:

    • 我不明白问题出在哪里,我使用数组存储旧值,使用数组 1 存储新值,这应该不是问题
    • 我试过了,它奏效了,你能更好地解释一下为什么我所做的没有奏效
    • 您正在为array1[i][0] 分配一个新值,并且在下一行中,您使用该值但假定它是旧值。
    猜你喜欢
    • 2012-11-21
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多