第十三届 ACM/CCPC 吉林省赛 G. Spotlight

本文是ACM/CCPC吉林省赛Problem G. Spotlight的题解。题目要求根据给定的光线起点s、经过点e和点p的坐标,判断点p是否在光线上。这是一个简单几何问题,采用向量法求解,通过判断ps和es向量是否同向得出结果。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值