Home > Enterprise >  Moving single point from curve knowing distance
Moving single point from curve knowing distance

Time:01-30

I have a set of x and y points (whatever the function behind) here in black. I would like to move the (x0, y0) point to the (x1,y1) knowing that there is 3 cm (whatever the unit) from (x0, y0) to (x1,y1) at 90° angle.

1

I would like to do it in Python, however obviously this is pretty bad.

fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D
ax = fig.add_subplot(111)

c = [55,  53, 54]
d = [29,  27,  27]

c = [55,  53   3, 54]
dd = [29,  27   3,  27 ]

ax.plot(c,d,'-o', c='g')
ax.plot(c,dd,'-o', c='b')

Final answer translated into Python (Thanks to picobit) :

import math

def rotate_vector(x0, y0, angle):
    magnitude = math.sqrt(x0**2   y0**2)
    xhat = x0/magnitude
    yhat = y0/magnitude
    x_rot = -yhat * math.sin(angle)   xhat * math.cos(angle)
    y_rot = yhat * math.cos(angle)   xhat * math.sin(angle)
    x_rot = x_rot * 3
    y_rot = y_rot * 3
    x_final = x_rot   x0
    y_final = y_rot   y0
    return x_final, y_final

x0 = 3
y0 = 4
angle = math.pi / 2
x_final, y_final = rotate_vector(x0, y0, angle)
print("x_final:", x_final)
print("y_final:", y_final)

x_final: 0.5999999999999996
y_final: 5.8

CodePudding user response:

Prerequisites:

  • enter image description here

  • Related