uva 10112 - Myacm Triangles

Problem B: Myacm Triangles

Source file:triangle.{c, cpp, java, pas}
Input file:triangle.in
Output file:triangle.out

There has been considerable archeological work on the ancient Myacm culture. Many artifacts have been found in what have been called power fields: a fairly small area, less than 100 meters square where there are from four to fifteen tall monuments with crystals on top. Such an area is mapped out above. Most of the artifacts discovered have come from inside a triangular area between just three of the monuments, now called the power triangle. After considerable analysis archeologists agree how this triangle is selected from all the triangles with three monuments as vertices: it is the triangle with the largest possible area that does not contain any other monuments inside the triangle or on an edge of the triangle. Each field contains only one such triangle.

Archeological teams are continuing to find more power fields. They would like to automate the task of locating the power triangles in power fields. Write a program that takes the positions of the monuments in any number of power fields as input and determines the power triangle for each power field.

A useful formula: the area of a triangle with vertices (x1, y1), (x2, y2), and (x3, y3) is the absolute value of

0.5 × [( y 3 - y 1)( x 2 - x 1) - ( y 2 - y 1)( x 3 - x 1)].

For each power field there are several lines of data. The first line is the number of monuments: at least 4, and at most 15. For each monument there is a data line that starts with a one character label for the monument and is followed by the coordinates of the monument, which are nonnegative integers less than 100. The first label is A, and the next is B, and so on.

There is at least one such power field described. The end of input is indicated by a 0 for the number of monuments. The first sample data below corresponds to the diagram in the problem.

For each power field there is one line of output. It contains the three labels of the vertices of the power triangle, listed in increasing alphabetical order, with no spaces.

Example input:

6
A 1 0
B 4 0
C 0 3
D 1 3
E 4 4
F 0 6
4
A 0 0
B 1 0
C 99 0
D 99 99
0

Example output:

BEF
BCD

求三角形内没有其他点时面积的最大值,穷举判断;

判断一个点是否在三角形内部有很多方法

题目里告诉我们面积所以首相想到面积法,设三角形abc,还有一点o如果Sabc=Sab+Saco+Sbco;则o在三角形内部或边上否则在三角形外部

#include<stdio.h>
#include<math.h>
double area(int x1,int y1,int x2,int y2,int x3,int y3)
{return fabs(0.5*((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1)));}
void main()
{int  x[16],y[16],n,i,j,k,A,B,C,p,flag;
 double s,sarea;
 char point[16];
 while (scanf("%d",&n),n)
 {
 for (i=1;i<=n;i++)
 {getchar();
  scanf("%c %d %d",&point[i],&x[i],&y[i]);
 }
 sarea=0;
 for (i=  1;i<=n-2;i++)
 for (j=i+1;j<=n-1;j++)
 for (k=j+1;k<=n  ;k++)
 {s=area(x[i],y[i],x[j],y[j],x[k],y[k]);
  flag=1;
  for (p=1;p<=n;p++)
  if (!((p==i)||(p==j)||(p==k)))
  {
  if (s==area(x[i],y[i],x[j],y[j],x[p],y[p])+area(x[i],y[i],x[p],y[p],x[k],y[k])+area(x[p],y[p],x[j],y[j],x[k],y[k]))
  {flag=0;break;}
  }
  if ((flag)&&(s>sarea)) {sarea=s; A=i; B=j; C=k;}
 }
 printf("%c%c%c\n",point[A],point[B],point[C]);
 }
}

  • 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、付费专栏及课程。

余额充值