D. 判断点线位置 (结构体+引用+函数)

D. 判断点线位置 (结构体+引用+函数)
题目描述

用具有x,y两个整型变量成员的结构类型SPoint来表示坐标点。用SLine结构类型来描述线段,其中包含p1和p2两个SPoint成员。

编写函数direction(const SLine &ab, const SPoint &c),利用向量ab与ac叉乘的值判断点c与直线ab的位置关系。


输入

判断次数

线的两点坐标x1、y1、x2、y2

点坐标x、y

......


输出

位置关系


输入样例1 
3
1 5 2 9
1 3
5 6 7 8
6 7
2 3 1 0
3 3
输出样例1
clockwise
intersect
anti clockwise

提示

 

向量a(x1,y1)与向量b(x2,y2)的叉乘定义为a.x*b.y-a.y*b.x,若结果小于0,表示向量b在向量a的顺时针方向;若结果大于0,表示向量b在向量a的逆时针方向;若等于0,表示向量a与向量b平行。

该题比较简单,主要考察嵌套结构的使用 

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;

struct SPoint
{
	int x;
	int y;
};

struct SLine
{
	SPoint p1;
	SPoint p2;
};

int direction(const SLine& ab, const SPoint& c)
{
	int num= (ab.p2.x - ab.p1.x) * (c.y - ab.p1.y) - (ab.p2.y - ab.p1.y) * (c.x - ab.p1.x);
	if (num < 0)
	{
		return 1;
	}
	else if (num > 0)
	{
		return 3;
	}
	else
	{
		return 2;
	}
}

int main()
{
	int t,ans;
	cin >> t;
	SLine ab;
	SPoint c;
	while (t--)
	{
		cin >> ab.p1.x >> ab.p1.y >> ab.p2.x >> ab.p2.y;
		cin >> c.x >> c.y;
		ans = direction(ab, c);
		if (ans == 1)
		{
			cout << "clockwise" << endl;
			continue;
		}
		else if (ans == 2)
		{
			cout << "intersect" << endl;
			continue;
		}
		else if (ans == 3)
		{
			cout << "anti clockwise" << endl;
			continue;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZZWWWFFF_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值