求大佬帮忙看看,这样做有问题吗??
查看原帖
求大佬帮忙看看,这样做有问题吗??
519384
Link_Cut_Y楼主2022/4/22 22:22

自适应辛普森积分干了100多行,害怕写错了,赶紧来求助一下大佬……

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

#define x first
#define y second

using namespace std;

const double eps = 1e-4;
typedef pair<double, double> PDD;

struct Square
{
	PDD p[4]; 
}s;

struct Circle
{
	PDD p;
	double r;
}c;

double get_line_intersection(double x, PDD A, PDD B)
{
	if (B.x < A.x) swap(A, B);
	double k = (B.y - A.y) / (B.x - A.x);
	double b = A.y - k * A.x;
	// 确定函数 y = kx + b
	
	return k * x + b;
}

namespace circle_solving
{
	bool have_intersection(double x, Circle c) // 判断圆 c 与直线 x 是否有交点 
	{
		double d = fabs(c.p.x - x);
		if (d >= c.r) return false;
		return true;
	}
	
	PDD get_intersection(double x, Circle c)
	{
		double d = fabs(c.p.x - x);
		double k = sqrt(c.r * c.r - d * d);
		return {c.p.y + k, c.p.y - k};
	}
}

namespace square_solving
{
	bool have_intersection(double x, Square s)
	{
		int cnt = 0;
		for (int i = 0; i < 4; i ++ )
		{
			int ne = (i + 1) % 4;
			if (s.p[i].x < x && s.p[ne].x < x) continue;
			if (s.p[i].x > x && s.p[ne].x > x) continue;
			cnt ++ ;
		}
		
		if (!cnt) return false;
		return true;
	}
	
	PDD get_intersection(double x, Square s)
	{
		PDD ans;
		bool have_ans = false;
		for (int i = 0; i < 4; i ++ )
		{
			int ne = (i + 1) % 4;
			if (s.p[i].x < x && s.p[ne].x < x) continue;
			if (s.p[i].x > x && s.p[ne].x > x) continue;
			if (!have_ans) ans.x = get_line_intersection(x, s.p[i], s.p[ne]), have_ans = true;
			else ans.y = get_line_intersection(x, s.p[i], s.p[ne]);
		}
		
		return ans;
	}
}

using namespace circle_solving;
using namespace square_solving;

double q[10];

double f(double x)
{
	int cnt = 0;
	if (have_intersection(x, c))
	{
		PDD qwq = get_intersection(x, c);
		q[ ++ cnt] = qwq.x;
		q[ ++ cnt] = qwq.y;
	}
	
	if (have_intersection(x, s))
	{
		PDD qaq = get_intersection(x, s);
		q[ ++ cnt] = qaq.x;
		q[ ++ cnt] = qaq.y;
	}
	
	if (cnt != 4) return 0;
	
	sort(q + 1, q + 4 + 1);
	return q[3] - q[2];
}

double simpson(double l, double r)
{
	auto mid = (l + r) / 2;
	return (r - l) * (f(l) + f(r) + 4 * f(mid)) / 6;
}

double query(double l, double r, double s)
{
	auto mid = (l + r) / 2;
	auto left = simpson(l, mid), right = simpson(mid, r);
	if (fabs(s - left - right) < eps) return left + right;
	return query(l, mid, left) + query(mid, r, right);
}

int main()
{
	
}
2022/4/22 22:22
加载中...