Length curve different after 3D rotation

Question:

I’m fitting a 2D hyperbolic curve, and print its length as :

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import math

x_max = [ 0 ,0.30473705, 0.61084798, 0.92107771 ,1.23816188, 1.56481854 ,1.90373634, 2.25755771 ,2.62885515, 3.02009822 ,3.43360879 ,3.87150099, 4.33560211, 4.82734953 ,5.34765826 ,
5.8967524, 6.4739534 ,7.07741748, 7.70381481, 8.34794525, 9.0022899, 9.65650683 ,10.29689661, 10.63238871, 10.96583401, 11.29574022 ,11.62058439, 11.93898196, 12.24985216, 12.55250925, 12.84666547 ,
13.13237121 ,13.40992797 ,13.67980107, 13.94254652 ,14.19875635, 14.44902132 ,14.69390756, 14.93394339 ,15.16961312 ,15.40135543 ,15.62956446 ,15.85459244]  

y_max = [0 , 0.58066397, 1.16060483, 1.7383529 ,2.31236765 ,2.88098944 ,3.4423895 ,3.99451788 ,4.53504866 ,5.06132252, 5.57028649, 6.05843167 ,6.52173013 ,6.95557317,
7.35471492 ,7.71322704 ,8.02447342, 8.28111796, 8.47518419 ,8.59819344 ,8.64141831, 8.59630147, 8.45510575, 8.37804206, 8.29255528, 8.19429211, 8.0804061 ,7.94957511 ,
7.8017459, 7.63775674 ,7.45896669 ,7.26696151 ,7.06335404 ,6.84966673 ,6.62727386 ,6.39738196, 6.16103222, 5.91911387 ,5.67238211 ,5.42147667 ,5.16693933 ,4.90922942 ,4.64873726] 


x_max = np.array(x_max)
y_max = np.array(y_max)

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot(x_max, y_max,'-o', c='b') 
plt.show()  

print(np.sum(np.sqrt(np.diff(x_max)**2 + np.diff(y_max)**2))) 
# 21.311541547561703

However after rotate this 2D curve into a 3D space with a given direction, the printed length is different for a same array size/number of points, why ?
I guess this is my rotation matrix that goes a bit stretched, but why

%matplotlib widget

DIR = 297.72642328058686

xs = (x_max * math.cos(np.radians(DIR))) - (x_max * math.sin(np.radians(DIR))) 
ys = (x_max * math.sin(np.radians(DIR))) + (x_max* math.cos(np.radians(DIR))) 
zs =  y_max


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z') 

ax.scatter(xs, ys, zs, c='r')
plt.show()

def curve_length(x, y, z):
    length = 0.0
    for i in range(len(x) - 1):
        dx = x[i+1] - x[i]
        dy = y[i+1] - y[i]
        dz = z[i+1] - z[i]
        length += (dx**2 + dy**2 + dz**2)**0.5
    return length

print(curve_length(xs, ys, zs))
# 26.740320076015575
Asked By: Acqueux

||

Answers:

The rotation matrix you are using is wrong. Change

xs = (x_max * math.cos(np.radians(DIR))) - (x_max * math.sin(np.radians(DIR))) 
ys = (x_max * math.sin(np.radians(DIR))) + (x_max* math.cos(np.radians(DIR))) 

to

xs = (x_max * math.cos(np.radians(DIR))) 
ys = (x_max * math.sin(np.radians(DIR)))

Gives the correct result.

Answered By: adrianop01
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.