इस खंड में, हम देखेंगे कि C++ में STL से कॉम्प्लेक्स क्लास का उपयोग करके पॉइंट क्लास कैसे बनाई जाती है। और उन्हें ज्यामिति से संबंधित कुछ समस्याओं पर लागू करते हैं। कॉम्प्लेक्स नंबर STL के कॉम्प्लेक्स क्लास (#include <complex>) के अंदर मौजूद होता है।
पॉइंट क्लास को परिभाषित करना
कॉम्प्लेक्स को पॉइंट बनाने के लिए, हम कॉम्प्लेक्स<डबल> के नाम को पॉइंट में बदल देंगे, फिर कॉम्प्लेक्स क्लास के रियल() को x में और इमैज() को y में बदल देंगे। इस प्रकार, हम पॉइंट क्लास का अनुकरण कर सकते हैं।
# 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)
ज्यामिति लागू करने के लिए, हम पा सकते हैं कि मूल बिंदु (0, 0) से P की दूरी -abs(P) से दर्शाई जाती है। मूल बिंदु O से X-अक्ष के साथ OP द्वारा बनाया गया कोण: arg(z)। मूल बिंदु के बारे में P का घूर्णन P * polar(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)