KieranWynn / pyquaternion

A fully featured, pythonic library for representing and using quaternions
http://kieranwynn.github.io/pyquaternion/
MIT License
339 stars 68 forks source link

Apply a rotation to an entire matrix of points #69

Closed kjans123 closed 3 years ago

kjans123 commented 3 years ago

image

from pyquaternion import Quaternion
import numpy as np
import matplotlib.pyplot as plt

rc = 4
angles = np.linspace(0, 2*np.pi, 200)
xc = rc*np.cos(angles)
yc = rc*np.sin(angles)
zc = [0]*200
circle = np.array([xc, yc, zc]).T

my_quaternion = Quaternion(axis=[1, 0, 0], degrees=90)

ncircle = []
for ix in circle:
    v = ix.copy()
    vp = my_quaternion.rotate(v)
    ncircle.append(vp)
ncircle = np.array(ncircle)

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
origin = np.array([0, 0, 0])
ax.plot(circle[:, 0],
        circle[:, 1],
        circle[:, 2])

ax.plot(ncircle[:, 0],
        ncircle[:, 1],
        ncircle[:, 2])
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

Instead of using the above for-loop, is there anyway to apply the 90-degree rotation as a matrix operation on the first (blue) circle?