New vector position

80 Views Asked by At

How can I calculate the new position of a 'point', with just a distance value coming from the center of the selection.

Example: I have 8 vertices selected, I have a little script in Maya that will iterate through each point, find the distance from the center of the selection to the current point in the loop. It saves these values and creates an "average distance from the center" value.

import maya.cmds as cmds
import math
sel = cmds.ls(sl=1, fl=1)
averageDistance = 0
cmds.setToolTo('Move')
oldCoordArray = []
cs = cmds.manipMoveContext("Move", q=1, p=1)
for i in range(0, len(sel), 1):
    vts = cmds.pointPosition(sel[i])
    x = round(float(cs[0]),4) - round(float(vts[0]),4)
    y = round(float(cs[1]),4) - round(float(vts[1]),4)
    z = round(float(cs[2]),4) - round(float(vts[2]),4)
    distanceFromCenter = math.sqrt((x * x) + (y * y) + (z * z))
    print "Start Point: X: %s Y: %s Z: %s" %(round(float(cs[0]),4),round(float(cs[1]),4),round(float(cs[2]),4))
    print "End Point: X: %s Y: %s Z: %s" %(round(float(vts[0]),4),round(float(vts[1]),4),round(float(vts[2]),4))
    print "Distance: %s" %distanceFromCenter
    oldCoordArray += [(round(float(vts[0]),4),round(float(vts[1]),4),round(float(vts[2]),4))]
    averageDistance += distanceFromCenter
    if (i == len(sel) -1):
        averageDistance / len(sel)
        for i in range(0, len(sel), 1):
            #New position = oldCoordArray[i] - newCoordArray[i]
            print oldCoordArray[i]

Once this script has processed, I have these variables:

  1. It's Old position in (X,Y,Z)
  2. A new average distance from the center point.
  3. I also have available the old distance from the centerpoint.

I'm assuming to calculate the new position, I'd have to find the new (X,Y,Z) values using the average distance that I've calculated, but I don't know how unfortunately.

EDIT

Link To Image

  • Black = Each vertex position (X,Y,Z)
  • Red = Distance between center and vertex
  • Blue = Average of ALL verts from the Centerpoint (X,Y,Z) & Vertex (X,Y,Z) = integerXYZ (blue is the same distance)
  • Green is the position I'm trying to find in (XYZ)

Am I doing this wrong, for blue, should I be averaging each single axis? example:

  • BlueX= Average of CenterpointX & vertexX
  • BlueY= Average of CenterpointY & vertexY
  • BlueZ= Average of CenterpointZ & vertexZ
  • GreenX, GreenY, GreenZ = BlueX, BlueY, BlueZ

Is that correct?

1

There are 1 best solutions below

8
On

So you want to compute new points that are each respectively on the line connecting the center and the old point. Each of these new points is a distance of "averageDistance" from the center point.

Call the center point $c$ (cs above) and the points $v_i$ (vts above). Let the new point be $n_i$. Then you can compute the new point as follows:

$$ n_i = \frac{(v_i - c)}{\|v_i - c\|}*averageDistance + c$$