609 - Metal Cutting(几何+暴力)

本文介绍了一个关于金属板切割的问题,目标是最小化切割所需的总长度。通过枚举所有可能的切割顺序并计算每种情况下的总切割长度来寻找最优解。

  Metal Cutting 

In order to build a ship to travel to Eindhoven, The Netherlands, various sheet metal parts have to be cut from rectangular pieces of sheet metal. Each part is a convex polygon with at most 8 vertices. Each rectangular piece of sheet metal has width n and height m, so that the four corners of the sheet can be specified by the Cartesian coordinates (0, 0), (0, m), (nm) and (n, 0) in clockwise order. The cutting machine available can make only straight-line cuts completely through the metal. That is, it cannot cut halfway through the sheet, turn, and then cut some more. You are asked to write a program to determine the minimum total length of cuts this machine has to make in order to cut out the polygon. The cuts must be along the edges of the poligon.


For example, if n = m = 100, and the polygon has vertices (80, 80), (70, 30), (20, 20) and (20, 80), the following diagram shows the optimal cut (the thick lines). The numbers show the order in which the cuts are made.

Input 

The first line of the input is an integer N, then a blank line followed by N datasets. There is a blank line between datasets.

The first line of each dataset contains the two integers n and m where $0 < n, m \le 500$. The next line contains p, the number of vertices in the polygon, where $3 \le p \le 8$. Each of the next p lines contains two integers x and y where 0 < x < n and 0 < y < m, specifying the vertices of the polygon. The vertices are listed in clockwise order. You may assume that the polygon does not intersect itself, and that no three consecutive vertices are colinear.

Output 

For each dataset, print the minimum total length of cuts required to cut out the given polygon, accurate to 3 decimal places. Print a blank line between datasets.

Sample Input 

1

100 100
4
80 80
70 30
20 20
20 80

Sample Output 

Minimum total length = 312.575

题意:一个矩形上面有n个点,组成n条直线,现在求一个切割顺序,使得切割长度最小。

思路:最多8条直线,直接暴力切割顺序,然后去求,求的过程中,每切一条直线就添加进去,然后求长度的时候遍历一遍现有直线,然后就是几何问题了。

利用叉积判断点是否是可取点,然后在这些点找最小距离的点,这样可以求出两个交点,即求出长度。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
const double esp = 1e-9;
using namespace std;
struct Point {
	double x, y;
	Point() {}
	Point(double xx, double yy) {
		x = xx; y = yy;
	}
} p[10];

struct Line {
	Point a, b;
	double A, B, C;
	Line() {}
	Line(Point aa, Point bb) {
		a = aa; b = bb;
		A = b.y - a.y;
		B = a.x - b.x;
		C = (A * a.x + B * a.y);
	}
} l[15], save[15];

inline bool px(Line a, Line b) {
	if (fabs((a.b.y - a.a.y) * (b.b.x - b.a.x) - (b.b.y - b.a.y) * (a.b.x - a.a.x)) < esp) return true;
	return false;
}

inline Point jd(Line a, Line b) {
	Point pp((b.B * a.C - a.B * b.C) / (a.A * b.B - b.A * a.B) ,(a.A * b.C - b.A * a.C) / (a.A * b.B - b.A * a.B));
	return pp;
}

int t, P, ln, sn, i, ord[10];
double n, m;

inline double dis(Point a, Point b) {
	return sqrt((b.y - a.y) * (b.y - a.y) + (b.x - a.x) * (b.x - a.x));
}

inline bool outside(Point a, Point b, Point c) {
	return (c.x - a.x) * (b.x - a.x) + (c.y - a.y) * (b.y - a.y) > esp;
}

inline double find(Line li) {
	double mina = 2000000000, minb = 2000000000;
	Point aa, bb;
	for (int i = 0; i < sn; i++) {
		if (px(li, save[i])) continue;
		Point c = jd(li, save[i]);
		double disa = dis(li.a ,c), disb = dis(li.b , c);
		if (outside(li.b, li.a, c) && disa - mina < esp) {
			mina = disa;
			aa = c;
		}
		if (outside(li.a, li.b, c) && disb - minb < esp) {
			minb = disb;
			bb = c;
		}
	}
	return dis(aa, bb);
}

inline double cal() {
	double sum = 0;
	sn = 4;
	for (int i = 0; i < P; i++) {
		sum += find(l[ord[i]]);
		save[sn++] = l[ord[i]];
	}
	return sum;
}

inline double solve() {
	double ans = 2000000000;
	save[sn++] = Line(Point(0, 0), Point(0, m));
	save[sn++] = Line(Point(0, 0), Point(n, 0));
	save[sn++] = Line(Point(n, 0), Point(n, m));
	save[sn++] = Line(Point(0, m), Point(n, m));
	do {
		double t = cal();
		ans = min(ans, t);
	} while (next_permutation(ord, ord + P));
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		ln = 0, sn = 0;
		scanf("%lf%lf", &n, &m);
		scanf("%d", &P);
		for (i = 0; i < P; i++) {
			ord[i] = i;
			scanf("%lf%lf", &p[i].x, &p[i].y);
			if (i != 0)
				l[ln++] = Line(Point(p[i - 1].x, p[i - 1].y), Point(p[i].x, p[i].y));
		}
		l[ln++] = Line(Point(p[P - 1].x, p[P - 1].y), Point(p[0].x, p[0].y));
		printf("Minimum total length = %.3lf\n", solve());
		if (t) printf("\n");
	}
	return 0;
}


是的,你已经走在了正确的设计思路上。第3个模块——“SwapChain/DComp模块”,本质上就是渲染目标资源(RenderTarget)管理的核心部分。关于**视觉树是否应该独立出来为一个模块**,这取决于你的系统对灵活性、可维护性、跨平台兼容性和性能优化的需求。 --- ## ✅ 是否将视觉树独立成模块? ### ✅ **建议分离出独立模块** #### 优点: 1. **解耦性强** - 视觉树负责 UI 元素的组织与布局; - 渲染目标负责图像输出; - 分离后两者可以独立变化、测试和调试。 2. **更易支持多平台** - 在 Win32 使用 DComp,在 UWP/WPF 可使用不同的视觉树实现; - 如果将来需要支持 OpenGL 或 Metal,只需替换视觉树模块。 3. **生命周期清晰** - 窗体关闭时只释放视觉树相关资源; - 渲染目标可以继续用于后台处理或缓存。 4. **支持复杂的 UI 架构** - 多层 Visual Tree 支持透明叠加、动画、阴影等效果; - 模块化架构让这些功能更容易实现。 --- ### ❌ 不建议分离的情况: - **小型项目或嵌入式场景**:如果 UI 极其简单,且不打算扩展,可以简化为统一结构。 - **性能极度敏感的实时应用**:频繁的模块间通信可能带来额外开销。 --- ## 🔄 推荐的模块关系图 ```text +-----------------------------+ | 渲染后端核心框架 | +-----------------------------+ | 1. 系统检测模块 | -> CSystemDetector | 2. 共享设备模块 | -> CDeviceManager | 3. 渲染目标模块 | -> CRenderTarget (含 SwapChain/DComp) | 4. 表面纹理管理模块 | -> CTextureManager | 5. D2D绘图上下文封装模块 | -> CD2DContextWrapper | 6. 渲染流程控制模块 | -> CRenderPipeline | 7. CDC兼容性封装模块 | -> CCDCCompatibilityLayer +-----------------------------++-----------------------------+ | 独立模块 | | 8. 视觉树管理模块 | -> CVisualTreeManager +-----------------------------+ ``` > 视觉树模块(CVisualTreeManager)通过接口与渲染目标模块交互,而不是直接依赖其实现。 --- ## 🛠 示例:模块间交互逻辑 ```cpp // CVisualTreeManager.h class CVisualTreeManager { public: void SetRenderTarget(IRenderTarget* pRenderTarget); void UpdateLayout(); void Render(); private: IRenderTarget* m_pRenderTarget; std::vector<IVisualElement*> m_visualElements; }; // CRenderTarget.h class CRenderTarget : public IRenderTarget { public: void Present(); ID2D1DeviceContext* GetD2DContext(); }; ``` --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值