题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2831
题意:给a, b, c, d, e, f 6个点
abgh是平行四边形。def是三角形。面积相等。
求点 g, h的坐标
思路:
1. DE*DF/2 = AH*AB; (向量DE叉乘向量DF,除以2, 等于 向量AH叉乘 AB)
2. AH = k AC; (向量AH 等于 k倍的向量AC)
将2式代入1式。就可以求得。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; struct point
{
double x, y;
}a, b, c, d, e, f, g, h;
double cross(point a, point b, point c)
{
return (fabs((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)));
}
int main()
{
double k;
while(cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y>>e.x>>e.y>>f.x>>f.y)
{
if(a.x==&&a.y==&&b.x==&&b.y==&&c.x==&&c.y==&&d.x==&&d.y==&&e.x==&&e.y==&&f.x==&&f.y==)
break;
k = cross(d, e, f)/;
k = k/cross(a, b, c); h.x = a.x+k*(c.x-a.x);
h.y = a.y+k*(c.y-a.y);
g.x = b.x+(h.x-a.x);
g.y = b.y+(h.y-a.y);
printf("%.3lf %.3lf %.3lf %.3lf\n", g.x, g.y, h.x, h.y);
}
return ;
}
再贴一个比赛时候的代码。
思想是求的 直线ac的方程,然后h满足方程, 把h.y用h.x 代替。带入条件。
这样做有三个缺点: 1、 把h.x带入方程, 由于先要换成向量的坐标表示, 还有相乘 的部分和替换的部分, 推导的过程很复杂。
2、算平行四边形的时候, 不知道叉积出来到 是正还是负, 带入方程,会错。
3、方程斜率不存在的时候,要另算, 麻烦。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std; struct node
{
double x, y;
} a, b, c, d, e, f, g, h;
double area(node d, node e, node f)
{
return (fabs((e.x-d.x)*(f.y-d.y)-(e.y-d.y)*(f.x-d.x))/);
}
int main()
{
double s_def;
while(cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y>>e.x>>e.y>>f.x>>f.y)
{
if(a.x==&&a.y==&&b.x==&&b.y==&&c.x==&&c.y==&&d.x==&&d.y==&&e.x==&&e.y==&&f.x==&&f.y==)
break;
s_def = area(d, e, f); double k , B;
if(a.x-c.x!=)
{
k = (double)((a.y-c.y)/(a.x-c.x));
B = (double)(a.y-k*a.x);
cout<<k<<" "<<B<<endl;
h.x = (double)((s_def+a.x*b.y+B*b.x-B*a.x-a.y*b.x)/(b.y-a.y-k*b.x+k*a.x)); cout<<h.x<<endl;
h.y = k*h.x+B;
}
else
{
h.x = a.x;
double ab;
ab = sqrt((b.x-a.x)*(b.x-a.x)-(b.y-a.y)*(b.y-a.y));
h.y = a.y + s_def/ab;
}
g.x = b.x + h.x - a.x;
g.y = b.y + h.y - a.y;
printf("%.3lf %.3lf %.3lf %.3lf\n", g.x, g.y, h.x, h.y);
}
return ;
}