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

คำนวณพื้นที่รูปไข่ (Ellipse) ใน C++

รูปไข่ (Ellipse) มีพื้นที่คำนวณจากสูตรง่าย: A = π × a × b โดยที่ a = กึ่งแกนใหญ่ (semi-major axis), b = กึ่งแกนเล็ก (semi-minor axis)

ส่วนประกอบของรูปไข่

ชื่อคำอธิบาย
Center (ศูนย์กลาง)จุดกึ่งกลางของรูปไข่ และกึ่งกลางของเส้นเชื่อมจุดโฟกัสสองจุด
Major Axis (แกนใหญ่)เส้นผ่านศูนย์กลางที่ยาวที่สุด ความยาว = 2a
Minor Axis (แกนเล็ก)เส้นผ่านศูนย์กลางที่สั้นที่สุด ความยาว = 2b
Semi-major axis (a)ครึ่งหนึ่งของแกนใหญ่ (ระยะจากศูนย์กลางถึงขอบตามแกนใหญ่)
Semi-minor axis (b)ครึ่งหนึ่งของแกนเล็ก (ระยะจากศูนย์กลางถึงขอบตามแกนเล็ก)
Foci (โฟกัส)จุดสองจุดบนแกนใหญ่ ที่ผลรวมระยะทางจากจุดใดบนรูปไข่ไปทั้งสองโฟกัส คงที่
Latus Rectum (ด้านข้างตั้งฉาก)เส้นผ่านโฟกัสและตั้งฉากกับแกนใหญ่ ความยาว = 2b²/a
Eccentricity (e)ความแบนของรูปไข่ e = c/a โดย c = ระยะจากศูนย์กลางถึงโฟกัส (0 = วงกลม, 1 = parabola)

สูตรสำคัญ

  • พื้นที่: A = πab
  • เส้นรอบรูป (ประมาณ): P ≈ π[3(a+b) - √((3a+b)(a+3b))] (Ramanujan approximation)
  • โฟกัส: c = √(a² - b²)
  • Eccentricity: e = c/a

ตัวอย่างโค้ด

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

struct Ellipse {
    double a;  // semi-major axis
    double b;  // semi-minor axis
    
    Ellipse(double semiMajor, double semiMinor) 
        : a(max(semiMajor, semiMinor)), b(min(semiMajor, semiMinor)) {}
    
    double area() const {
        return M_PI * a * b;
    }
    
    double perimeter() const {
        // Ramanujan approximation
        double h = (a - b) * (a - b) / ((a + b) * (a + b));
        return M_PI * (a + b) * (1 + 3*h / (10 + sqrt(4 - 3*h)));
    }
    
    double eccentricity() const {
        return sqrt(1 - (b*b)/(a*a));
    }
    
    double focalDistance() const {
        return sqrt(a*a - b*b);
    }
    
    void printInfo() const {
        cout << fixed << setprecision(4);
        cout << "=== Ellipse Properties ===" << endl;
        cout << "Semi-major axis (a): " << a << endl;
        cout << "Semi-minor axis (b): " << b << endl;
        cout << "Area: " << area() << endl;
        cout << "Perimeter (approx): " << perimeter() << endl;
        cout << "Eccentricity (e): " << eccentricity() << endl;
        cout << "Focal distance (c): " << focalDistance() << endl;
        cout << "Foci: (±" << focalDistance() << ", 0)" << endl;
    }
};

int main() {
    // ตัวอย่าง: a=5, b=4
    Ellipse ellipse(5, 4);
    ellipse.printInfo();
    
    cout << "\n=== ทดสอบค่าต่างๆ ===" << endl;
    vector<pair<double,double>> testCases = {
        {5, 4}, {10, 10}, {7, 3}, {1, 0.5}
    };
    
    for (auto [a, b] : testCases) {
        Ellipse e(a, b);
        cout << "a=" << a << ", b=" << b 
             << " -> Area=" << e.area() 
             << ", Perimeter=" << e.perimeter()
             << ", e=" << e.eccentricity() << endl;
    }
    
    // รับ input จากผู้ใช้
    double a, b;
    cout << "\nป้อน semi-major axis (a): ";
    cin >> a;
    cout << "ป้อน semi-minor axis (b): ";
    cin >> b;
    
    Ellipse userEllipse(a, b);
    userEllipse.printInfo();
    
    return 0;
}

ผลลัพธ์ตัวอย่าง

=== Ellipse Properties ===
Semi-major axis (a): 5.0000
Semi-minor axis (b): 4.0000
Area: 62.8319
Perimeter (approx): 28.3617
Eccentricity (e): 0.6000
Focal distance (c): 3.0000
Foci: (±3.0000, 0)

=== ทดสอบค่าต่างๆ ===
a=5, b=4 -> Area=62.8319, Perimeter=28.3617, e=0.6000
a=10, b=10 -> Area=314.1593, Perimeter=62.8319, e=0.0000
a=7, b=3 -> Area=65.9734, Perimeter=33.5867, e=0.8944
a=1, b=0.5 -> Area=1.5708, Perimeter=4.8442, e=0.8660

หมายเหตุ

  • ใช้ M_PI จาก <cmath> (ต้อง define _USE_MATH_DEFINES ใน Windows) หรือใช้ acos(-1)
  • เส้นรอบรูปของรูปไข่ไม่มี closed-form solution ใช้ approximation (Ramanujan ดีที่สุด)
  • ถ้า a = b → วงกลม (e = 0, Area = πr², Perimeter = 2πr)