UVA 10112 - Myacm Triangles

题目大意:给出一系列点的坐标,找出三个点,使得这三个点连起来组成的三角形,不包含其他点。且这是能找到的面积最大的三角形。

解题思路:最多15个点,直接三个循环暴力,主要是判断其他点是否在三角形内或三角形上,判断方法有挺多,什么有向法巴拉巴拉。我写了最好懂的,就是面积法。判断SΔABC是否等于SΔABP+SΔACP+SΔBCP。如果等于说明P在三角形内或三角形上。 

ac代码:

#include <iostream>
#include <algorithm> 
#include <cmath>
using namespace std;
struct node{
	char name;
	double x;
	double y;
}point[20]; 
int n, temp; 
char one, two, three;
double sum;
bool contain(int focus, int a, int b, int c)
{
	double x1, x2, y1, y2;
	double s1, s2, s3, sum;
	x1 = point[c].x - point[a].x;
	x2 = point[b].x - point[a].x; 
	y1 = point[c].y - point[a].y;
	y2 = point[b].y - point[a].y;
	sum = fabs(x1*y2 - x2*y1);
	s1 = fabs(x2*(point[focus].y-point[a].y) 
		- y2*(point[focus].x-point[a].x));
	s2 = fabs(x1*(point[focus].y-point[a].y) 
		- y1*(point[focus].x-point[a].x));
	x1 = point[focus].x - point[c].x;
	x2 = point[focus].x - point[b].x; 
	y1 = point[focus].y - point[c].y;
	y2 = point[focus].y - point[b].y;
	s3 = fabs(x1*y2 - x2*y1);
	if (sum == s1+s2+s3)
		return 1;
return 0;
}
int judge(int a, int b, int c)
{
	int t1, t2, t3, t4;
	for (int i=0; i<n; i++)
		if (i != a && i != b && i != c)
			if (contain(i, a, b, c))
				return 0;			
	t1 = point[c].y - point[a].y;
	t2 = point[b].x - point[a].x;
	t3 = point[b].y - point[a].y;
	t4 = point[c].x - point[a].x;
return fabs(0.5*(t1*t2-t3*t4));
}
bool compare(node a, node b)
{
return a.name < b.name;
}
int main()
{
	while (scanf("%d", &n)!=EOF && n){
		for (int i=0; i<n; i++){
			getchar();
			scanf("%c%lf%lf", &point[i].name, &point[i].x, &point[i].y);			
		}
		sum = 0;
		sort(point, point+n, compare);
		for (int i=0; i<n-2; i++)
			for (int j=i+1; j<n-1; j++)
				for (int k=j+1; k<n; k++){
					temp = judge(i, j, k);
					if (temp > sum){
						one = point[i].name;
						two = point[j].name;
						three = point[k].name;
						sum = temp;						
					}
				}
		printf("%c%c%c\n", one, two, three);
	}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bool Accel::travel(Node* treeNode, const Ray3f& ray_, Intersection& its, bool shadowRay, uint32_t& hitIdx) const { Ray3f ray(ray_); //与该节点包围盒不相交 cout << "与该节点包围盒求交" << endl; if (!treeNode->bbox.rayIntersect(ray)) { cout << "与该节点包围盒不相交" << endl; return false; } cout << "与该节点包围盒相交" << endl; bool is_hit = false; //是叶子节点 if (treeNode->is_leaf) { cout << "叶子节点" << endl; cout << "叶子节点内三角形数目:" << treeNode->triangles.size() << endl; //与叶子节点内三角形求交 for (size_t i = 0; i < treeNode->triangles.size(); i++) { //与叶子节点内第i个三角形求交 int index = i; cout << "与叶子节点内第" << index << "个三角形求交" << endl; float u, v, t; if (m_mesh->rayIntersect(treeNode->triangles[index], ray, u, v, t) && t < ray.maxt) { //与叶子节点内第i个三角形相交,更新交点 cout << "与叶子节点内第" << i << "个三角形相交,更新交点" << endl; if (shadowRay) return true; ray.maxt = t; its.t = t; its.uv = Point2f(u, v); its.mesh = m_mesh; hitIdx = treeNode->triangles[index]; is_hit = true; } } cout << endl; } //不是叶子节点 else { cout << "非叶子节点" << endl; for (size_t i = 0; i < 8; i++) { //与该节点第i个子节点求交 int index = i; cout << "与该节点第" << index << "个子节点求交" << endl; is_hit = travel(treeNode->child[index], ray, its, shadowRay, hitIdx); } } return is_hit; }
最新发布
07-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值