Last active
August 27, 2025 20:30
-
-
Save phuang1024/8644b1a7b8362e0476f859d04131fefd to your computer and use it in GitHub Desktop.
Visualization of taylor series definition of exp(j*theta)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import cv2 | |
| import numpy as np | |
| UNITS = np.array([ | |
| [1, 0], | |
| [0, 1], | |
| [-1, 0], | |
| [0, -1], | |
| ]) | |
| SCALE = 100 | |
| def coord_to_px(img, coord): | |
| h, w = img.shape[:2] | |
| px = np.array([w // 2, h // 2]) + coord * SCALE | |
| return tuple(px.astype(np.int32)) | |
| def plot(img, theta, steps=10): | |
| value = np.array([0, 0], dtype=np.float32) | |
| for i in range(steps): | |
| offset = UNITS[i % 4] * theta ** i / math.factorial(i) | |
| cv2.line( | |
| img, | |
| coord_to_px(img, value), | |
| coord_to_px(img, value + offset), | |
| (0, 0, 0), 1, cv2.LINE_AA | |
| ) | |
| value += offset | |
| cv2.line( | |
| img, | |
| coord_to_px(img, np.array([0, 0])), | |
| coord_to_px(img, value), | |
| (0, 0, 255), 1, cv2.LINE_AA | |
| ) | |
| cv2.line( | |
| img, | |
| coord_to_px(img, np.array([0, 0])), | |
| coord_to_px(img, np.array([0, theta])), | |
| (255, 0, 0), 1, cv2.LINE_AA | |
| ) | |
| theta = 0 | |
| theta_velocity = 1 | |
| theta_max = 7 | |
| img = np.empty((600, 600, 3), dtype=np.uint8) | |
| while True: | |
| img[:] = 255 | |
| plot(img, theta, steps=20) | |
| # Add text | |
| cv2.putText( | |
| img, | |
| f"theta: {theta:.1f}", | |
| (10, 30), | |
| cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA | |
| ) | |
| cv2.imshow("img", img) | |
| if cv2.waitKey(10) == ord("q"): | |
| break | |
| theta += theta_velocity * 0.01 | |
| if theta > theta_max: | |
| theta_velocity = -1 | |
| elif theta < -theta_max: | |
| theta_velocity = 1 | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment