hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)
Mirror and Light
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 650 Accepted Submission(s): 316
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.
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.
0.000 0.000
4.000 0.000
1.000 1.000
3.000 1.000
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
struct Point {double x,y;}; //点
struct Ldir{double dx,dy;}; //方向向量
struct Lline{Point p; Ldir dir;}; //直线
// 计算直线的一般式 Ax+By+C=0
void format(Lline ln,double& A,double& B,double& C)
{
A=ln.dir.dy;
B=-ln.dir.dx;
C=ln.p.y*ln.dir.dx-ln.p.x*ln.dir.dy;
}
// 求点p1关于直线ln的对称点p2
Point mirror(Point P,Lline ln)
{
Point Q;
double A,B,C;
format(ln,A,B,C);
Q.x=((B*B-A*A)*P.x-*A*B*P.y-*A*C)/(A*A+B*B);
Q.y=((A*A-B*B)*P.y-*A*B*P.x-*B*C)/(A*A+B*B);
return Q;
}
//求线段交点
struct TLine
{
//直线标准式中的系数
double a, b, c;
};
TLine lineFromSegment(Point p1, Point p2)
{
TLine tmp;
tmp.a = p2.y - p1.y;
tmp.b = p1.x - p2.x;
tmp.c = p2.x * p1.y - p1.x * p2.y;
return tmp;
}
/*求直线的交点,注意平形的情况无解,避免RE*/
const double eps = 1e-; //注意一定要加,否则错误
Point LineInter(TLine l1, TLine l2)
{
//求两直线得交点坐标
Point tmp;
double a1 = l1.a;
double b1 = l1.b;
double c1 = l1.c;
double a2 = l2.a;
double b2 = l2.b;
double c2 = l2.c;
//注意这里b1 = 0
if(fabs(b1) < eps){
tmp.x = -c1 / a1;
tmp.y = (-c2 - a2 * tmp.x) / b2;
}
else{
tmp.x = (c1 * b2 - b1 * c2) / (b1 * a2 - b2 * a1);
tmp.y = (-c1 - a1 * tmp.x) / b1;
}
return tmp;
}
int main()
{
int T;
cin>>T;
cout<<setiosflags(ios::fixed)<<setprecision();
while(T--){
Point p1,p2,ps,pe;
Lline l;
cin>>p1.x>>p1.y;
cin>>p2.x>>p2.y;
cin>>ps.x>>ps.y;
cin>>pe.x>>pe.y;
l.p = p1;
l.dir.dx = p2.x - p1.x;
l.dir.dy = p2.y - p1.y;
Point duichen = mirror(ps,l); //求对称点
//cout<<duichen.x<<' '<<duichen.y<<endl;
TLine l1,l2;
l1 = lineFromSegment(p1,p2);
l2 = lineFromSegment(duichen,pe);
Point inter = LineInter(l1,l2); //求交点
cout<<inter.x<<' '<<inter.y<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)的更多相关文章
- HDU 2857 Mirror and Light
/* hdu 2857 Mirror and Light 计算几何 镜面反射 */ #include<stdio.h> #include<string.h> #include& ...
- 「HDU - 2857」Mirror and Light(点关于直线的对称点)
题目链接 Mirror and Light 题意 一条直线代表镜子,一个入射光线上的点,一个反射光线上的点,求反射点.(都在一个二维平面内) 题解 找出入射光线关于镜子直线的对称点,然后和反射光线连边 ...
- Gym-101915B Ali and Wi-Fi 计算几何 求两圆交点
题面 题意:给你n个圆,每个圆有一个权值,你可以选择一个点,可以获得覆盖这个点的圆中,权值最大的m个的权值,问最多权值是多少 题解:好像是叙利亚的题....我们画画图就知道,我们要找的就是圆与圆交的那 ...
- hdu 2857 点在直线上的投影+直线的交点
Mirror and Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- sgu 129 Inheritance 凸包,线段交点,计算几何 难度:2
129. Inheritance time limit per test: 0.25 sec. memory limit per test: 4096 KB The old King decided ...
- hdu 1147:Pick-up sticks(基本题,判断两线段相交)
Pick-up sticks Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- Spring MVC 中Ajax返回字符串
今天想用Ajax返回一个html的字符串数据. JavaScript代码: function saveMarkSolve() { //editor1.sync(); //var s = editor1 ...
- js获取事件源及触发该事件的对象
怎样获取事件源及触发该事件的对象,方法有非常多,js中能够通过event来实现.以下有个不错的演示样例,感兴趣的朋友能够參考下: function myfunction(event) { event ...
- Qt Multimedia Backends(多媒体后端)翻译
目录 MediaService plugins 媒体服务插件 不同后端支持的媒体播放器功能: 后端支持的摄像头(相机)功能 后端支持的音频解码功能 Audio plugins 音频插件 原文地址: Q ...
- 用Jedis操作redis示例一,Key,value HashMap
简单的key,value形式 public class RedisDemo { public static void main(String[] args) { Jedis jedis = new J ...
- python pivot() 函数
以下为python pandas 库的dataframe pivot()函数的官方文档: Reshape data (produce a “pivot” table) based on column ...
- 使用 log4j 2记录日志
log4j2使用方法还是很简单的 1 需要使用的jar包有两个, 1)log4j-api-2.8.2.jar 2)log4j-core-2.8.2.jar 2 产生Logger 对象非常的简单,使用 ...
- 大数据学习之Scala中main函数的分析以及基本规则(2)
一.main函数的分析 首先来看我们在上一节最后看到的这个程序,我们先来简单的分析一下.有助于后面的学习 object HelloScala { def main(args: Array[String ...
- [svc]expect的爱恨情仇
背景 openvpn生成证书想把它做成一键化,这样添加新用户时候就方便 遇到的问题 我的代码 gg_vpn_keys.exp #!/usr/bin/expect set user [lindex $a ...
- Makefile 使用小结
Makefile的基本格式 #目标:依赖(条件) # 命令 #all: add.c sub.c dive.c mul.c main.c # gcc add.c sub.c div.c mul.c ma ...
- CSS3 图片旋转
.nav_all { position:relative; z-index:; width:172px; display:inline; ; } .nav_all b { display:block; ...