কম্পিউটার টিউটোরিয়াল

C++-এ কমপ্লেক্স নম্বর দিয়ে জ্যামিতি (Geometry using Complex Numbers)


পয়েন্ট ক্লাস সংজ্ঞা

STL-র <complex> হেডার থেকে complex<double> ব্যবহার করে পয়েন্ট ক্লাস বানানো যায়। রিয়েল পার্টকে 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)

জ্যামিতিক অপারেশন

  • অরিজিন থেকে দূরত্ব: abs(P)
  • X-অক্ষের সাথে কোণ: arg(P) (রেডিয়ানে)
  • অরিজিন wzglęতে ঘুরানো: 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 from origin: " << abs(my_pt) << endl;
    cout << "Angle with X-axis: (" << arg(my_pt) << ") rad = (" << arg(my_pt)*(180/PI) << " deg)" << endl;
    point rot_point = my_pt * polar(1.0, PI/2); // ৯০ ডিগ্রি ঘুরানো
    cout << "After 90 deg rotation: "; print_point(rot_point);
}
The point is:(6, 7)
Distance from origin:9.21954
Angle with X-axis: (0.86217) rad = (49.4002 deg)
After 90 deg rotation: (-6.99972, 6.00032)
  1. সি++-এ ডাডেনি নম্বর

  2. C++ ব্যবহার করে ব্র্যাকেট স্ট্রিংয়ে সমান পয়েন্ট বের করা

  3. অ্যারে ব্যবহার করে সংখ্যাগুলোর গড় (Average) বের করার C++ প্রোগ্রাম

  4. রিকার্সন ব্যবহার করে প্রাকৃতিক সংখ্যাগুলোর যোগফল (Sum of Natural Numbers) বের করার C++ প্রোগ্রাম