ในส่วนนี้ เราจะมาดูวิธีการสร้างคลาสพอยต์โดยใช้คลาสที่ซับซ้อนจาก STL ใน C++ และนำไปใช้กับปัญหาที่เกี่ยวข้องกับเรขาคณิต จำนวนเชิงซ้อนมีอยู่ในคลาสเชิงซ้อนจาก STL (#include
การกำหนดคลาสของคะแนน
ในการทำให้ซับซ้อนเป็นจุด เราจะเปลี่ยนชื่อของคอมเพล็กซ์
# include <complex> typedef complex<double> point; # define x real() # define y imag()
เราต้องจำไว้ว่า x และ y ถูกใช้เป็นมาโคร สิ่งเหล่านี้ไม่สามารถใช้เป็นตัวแปรได้
ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <iostream>
#include <complex>
using namespace std;
typedef complex<double> point;
#define x real()
#define y imag()
int main() {
point my_pt(4.0, 5.0);
cout << "The point is :" << "(" << my_pt.x << ", " << my_pt.y << ")";
} ผลลัพธ์
The point is :(4, 5)
ในการใช้เรขาคณิต เราจะพบว่าระยะห่างของ P จากจุดกำเนิด (0, 0) ซึ่งแสดงเป็น −abs(P) มุมที่สร้างโดย OP จากแกน X โดยที่ O คือจุดกำเนิด:arg(z) การหมุนของ P เกี่ยวกับจุดกำเนิดคือ P * ขั้ว (r, θ)
ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <iostream>
#include <complex>
#define PI 3.1415
using namespace std;
typedef complex<double> point;
#define x real()
#define y imag()
void print_point(point my_pt){
cout << "(" << my_pt.x << ", " << my_pt.y << ")";
}
int main() {
point my_pt(6.0, 7.0);
cout << "The point is:" ;
print_point(my_pt);
cout << endl;
cout << "Distance of the point from origin:" << abs(my_pt) << endl;
cout << "Tangent angle made by OP with X-axis: (" << arg(my_pt) << ") rad = (" << arg(my_pt)*(180/PI) << ")" << endl;
point rot_point = my_pt * polar(1.0, PI/2);
cout << "Point after rotating 90 degrees counter-clockwise, will be: ";
print_point(rot_point);
} ผลลัพธ์
The point is:(6, 7) Distance of the point from origin:9.21954 Tangent angle made by OP with X-axis: (0.86217) rad = (49.4002) Point after rotating 90 degrees counter-clockwise, will be: (-6.99972, 6.00032)