The area - 計算幾何 - 自適應辛普森積分
阿新 • • 發佈:2020-07-17
特別簡單的題
隨便搞搞就能過吧,根據拋物線頂點和另外一個點,就可以求出拋物線的方程,然後再用一般式求出直線方程
函式就是拋物線減去直線,區間就是\([p_2.x,p_3.x]\),然後自適應辛普森積分積分一下就行了
拋物線方程\(y = Ax^2 + Bx + C\),直線方程\(y = Kx + b\)
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const double eps = 1e-9; struct Point{ double x, y; }p[4]; double A, B, C, K, b; double f(double x){// 函式 return A * x * x + x * (B - K) + C - b; } double simpson(double l, double r){ // simpson公式 double mid = l + (r - l) / 2; return (r - l) * (f(l) + 4 * f(mid) + f(r)) / 6; } double asr(double l, double r, double ans){ // 擬合 double mid = l + (r - l) / 2; double ans1 = simpson(l, mid), ans2 = simpson(mid, r); if(fabs(ans1 + ans2 - ans) <= eps) return ans; return asr(l, mid, ans1) + asr(mid, r, ans2); } void solve(){ for(int i = 1; i <= 3; i++) cin >> p[i].x >> p[i].y; A = (p[2].y - p[1].y) / (p[2].x - p[1].x) / (p[2].x - p[1].x); B = -2 * A * p[1].x; C = p[1].y - A * p[1].x * p[1].x - B * p[1].x; K = (p[3].y - p[2].y) / (p[3].x - p[2].x); b = p[3].y - K * p[3].x; printf("%.2lf\n", asr(p[2].x, p[3].x, simpson(p[2].x, p[3].x))); } int main(){ int t; cin >> t; while(t--) solve(); return 0; }