hdu 2857(对称点与直线交点问题)

Mirror and Light

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
The light travels in a straight line and always goes in the minimal path between two points, are the basic laws of optics.

Now, our problem is that, if a branch of light goes into a large and infinite mirror, of course,it will reflect, and leave away the mirror in another direction. Giving you the position of mirror and the two points the light goes in before and after the reflection, calculate the reflection point of the light on the mirror.
  
You can assume the mirror is a straight line and the given two points can’t be on the different sizes of the mirror.
 

Input
The first line is the number of test case t(t<=100).
  
The following every four lines are as follow:
  X1 Y1
  X2 Y2
  Xs Ys
  Xe Ye

  (X1,Y1),(X2,Y2) mean the different points on the mirror, and (Xs,Ys) means the point the light travel in before the reflection, and (Xe,Ye) is the point the light go after the reflection.

  The eight real number all are rounded to three digits after the decimal point, and the absolute values are no larger than 10000.0.
 

Output
  Each lines have two real number, rounded to three digits after the decimal point, representing the position of the reflection point.
 

Sample Input
   
   
   
   
1 0.000 0.000 4.000 0.000 1.000 1.000 3.000 1.000
 

Sample Output
   
   
   
   
2.000 0.000
 

公式:

    ·已知直线上的两点(x1, y1)(x2, y2),求直线ax+by+c=0

      a = y2-y1; b = x1-x2;  c = y1*x2-x1*y2;


·已知(x0, y0),求关于直线ax+by+c=0的对称点(x, y)

      k = -2 * (a*x0+b*y0+c)/(a*a+b*b);

      x = x0 + k*a;

      y = y0 + k*b;


·已知直线与直线,求交点(x, y)

      如果b1==0则

      

      否则

      


参考博客:http://blog.csdn.net/u012861385/article/details/24559227


#include<cstdio>
#include<string>
#include<cmath>
#define eps 1e-4
using namespace std;

struct point 
{
    double x,y;
};
point symmetric_point(point p1, point l1, point l2)// 求p1的对称点
{
	point ret;
	if (l1.x > l2.x - eps && l1.x < l2.x + eps)//斜率不存在
	{
		ret.x = (2 * l1.x - p1.x);
		ret.y = p1.y;
	}
	else
	{
		double k = (l1.y - l2.y ) / (l1.x - l2.x);
		if(k + eps > 0 && k - eps < 0)//斜率为零
		{
			ret.x = p1.x;
			ret.y = l1.y - (p1.y - l1.y);
		}
		else
		{
			ret.x = (2*k*k*l1.x + 2*k*p1.y - 2*k*l1.y - k*k*p1.x + p1.x) / (1 + k*k);
			ret.y = p1.y - (ret.x - p1.x ) / k;
		}
	}
	return ret;
}
point intersection(point u1,point u2,point v1,point v2)//求两直线的交点
{
	point ret=u1;
	double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
	/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
	ret.x+=(u2.x-u1.x)*t;
	ret.y+=(u2.y-u1.y)*t;
	return ret;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		point m1,m2,l1,l2;
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&m1.x,&m1.y,&m2.x,&m2.y,&l1.x,&l1.y,&l2.x,&l2.y);
		point tmp=symmetric_point(l1,m1,m2);
		point ans=intersection(tmp,l2,m1,m2);
		printf("%.3lf %.3lf\n",ans.x,ans.y);
	}
	return 0;
}




你可能感兴趣的:(计算几何)