这道题就是求点的时候麻烦了点,思路还是很简单的
刚开始把向量反向写成了swap(x,y),其实应该是x=-x,y=-y
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps=1e-8;
struct Point {
double x,y;
Point(){}
Point(double xx,double yy){x=xx;y=yy;}
Point operator+(Point b){return Point(x+b.x,y+b.y);}
double operator^(const Point b)const{
return x*b.y-y*b.x;
}
Point operator-(Point b){return Point(x-b.x,y-b.y);}
};
struct Line {
Point s,e;
Line(){};
Line(Point ss,Point ee){s=ss;e=ee;}
Point operator &(Line b){
Point res=s;
double t=((s-b.e)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x+=(e.x-s.x)*t;
res.y+=(e.y-s.y)*t;
return res;
}
};
double xmul(Point p0,Point p1,Point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dist(Point a,Point b){
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
}
Point getPoint(Point a,Point b,Point c){
Point q;
q=Point(-(b.y-a.y),b.x-a.x);
if(xmul(a,b,c)>eps) {
q.x=-q.x;
q.y=-q.y;
}
double d=dist(a,b);
q.x*=d/sqrt(q.x*q.x+q.y*q.y);
q.y*=d/sqrt(q.x*q.x+q.y*q.y);
q.x=a.x+q.x;
q.y=a.y+q.y;
return q;
}
Point p[3];
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y);
Point c,d,r,q;
c=getPoint(p[0],p[1],p[2]);
d=getPoint(p[0],p[2],p[1]);
r=Point((c.x+d.x)/2,(c.y+d.y)/2);
c=getPoint(p[1],p[2],p[0]);
d=getPoint(p[1],p[0],p[2]);
q=Point((c.x+d.x)/2,(c.y+d.y)/2);
Point o;
o=Line(p[0],r)&Line(p[1],q);
printf("%.4f %.4f\n",o.x,o.y);
}
return 0;
}