Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คำนวณ arctan2 แบบองค์ประกอบเลือกควอดรันต์ถูกต้องใน Python

ฟังก์ชัน numpy.arctan2(y, x) คำนวณมุมแบบ signed angle ในเรเดียนระหว่างรังสี (1,0) และจุด (x,y) ควอดรันต์ถูกเลือกอัตโนมัติจากเครื่องหมาย x และ y คืนค่ามุมในช่วง [-π, π]

ขั้นตอนการใช้งาน

import numpy as np

สร้างพิกัด 4 จุดในควอดรันต์ต่างๆ

x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])
print("Array1 (x coordinates)...\n", x)
print("\nArray2 (y coordinates)...\n", y)

คำนวณ arctan2 และแปลงเป็นองศา

print("\nResult...", np.arctan2(y, x) * 180 / np.pi)

โค้ดตัวอย่างสมบูรณ์

import numpy as np

x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])
print("Array1 (x coordinates)...\n", x)
print("\nArray2 (y coordinates)...\n", y)
print("\nResult...", np.arctan2(y, x) * 180 / np.pi)

ผลลัพธ์ที่คาดหวัง

Array1 (x coordinates)...
[-1  1  1 -1]
Array2 (y coordinates)...
[-1 -1  1  1]
Result...
[-135. -45.  45. 135.]