ACM/CCPC 历届真题 题解目录
Problem G. Spotlight
Time Limit: 1000ms Memory Limit: 512MB
Description
A light comes from point s, passed from point e, and finally go to infinite far.
There is a light given by its s and e.
You’re given a point, and your task is to judge if the point is on this light.
Input
First line contains an integer T (1 ≤ T ≤ 10v) represents the number of test cases.
For each test case:
The first line contains four integers sx, sy, ex, ey of the light.
The second line contains two integers px, py of the point.
(1 ≤ sx, sy, ex, ey, px, py ≤ 1000 and (sx, sy) ! = (ex, ey)).
Output
For each test case, if the point is on the light, output “YES” in a single line; otherwise, output “NO” in a single line.
Sample
Input
4
2 2 3 3
2 2
2 2 3 3
1 1
7 7 11 10
855 643
7 7 7 8
7 9
Output
YES
NO
YES
YES
题目大意:
从
s
s
s点发出的光,经过
e
e
e点。现给出
s
s
s点和
e
e
e点的坐标,再给一个
p
p
p点,让你判断p点是否在光线上。
分析:
很简单的几何问题。
由题可知,光线是一个射线。判断
p
p
p点是否在光线上时,注意判断方向。
思路:
采用向量法求解:先求出
p
s
⃗
\vec{ps}
ps和
e
s
⃗
\vec{es}
es的坐标,由于两个向量都包含光源
s
s
s点,故直接判断它们是否同向即可。
代码如下:
#include <iostream>
using namespace std;
//点的结构体
struct point
{
int x, y; //点的横纵坐标
};
int t;
point s, e, p; //判断点p是否在射线s->e上
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d%d", &s.x, &s.y, &e.x, &e.y);
scanf("%d%d", &p.x, &p.y);
//光线是一条射线,由于射线只朝着一个方向传播,我用向量法判断方向是否一致
//1. 求向量PS和ES的坐标
int psx = p.x - s.x, psy = p.y - s.y; //向量PS = (psx, psy)
int esx = e.x - s.x, esy = e.y - s.y; //向量ES = (esx, esy)
//2. 如果PS和ES不平行或平行但反向,则必不在光线上,反之则在光线上。
//x1*y2 - x2*y1 != 0: 代表两个向量不平行, x1 * x2 < 0: 代表两个向量反向;
if(psx * esy - esx * psy || psx * esx < 0) printf("NO\n");
else printf("YES\n");
}
return 0;
}