hdu1086 You can Solve a Geometry Problem too

利用树莓派制作智能音箱 阅读详情

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9306    Accepted Submission(s): 4563


Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point. 
 

Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. 
A test case starting with 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the number of intersections, and one line one case.
 

Sample Input
2 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.00 3 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.000 0.00 0.00 1.00 0.00 0
 

Sample Output
1 3


        题意是计算线段交点数量,不用舍去重复交点。。。水题一A,刚开始是想着联立方程组解直线方程,然后看解出来的(x, y)是不是在两条直线的x和y的区间上,后来感觉这么写太长,上网看了看用叉乘积和向量做挺简洁。。。整理一下


直接解方程判点在不在线段区间上


#include <cstdio>
#include <iostream>
#include <cmath>

using namespace std;

const double LIMIT = 1e-8;

typedef struct {
	double x, y;
}point;

typedef struct {
	point p1;
	point p2;
}line;

line L[15];

void Swap(double &a, double &b) {  //给a,b交换排序,好判断点是否在区间内
	if (a > b) {
		double tem = a;
		a = b;
		b = tem;
	}
}

bool IsItersect(line l1, line l2) {
	//两直线平行且垂直于相同坐标轴
	if (fabs(l1.p1.x - l1.p2.x) < LIMIT && fabs(l2.p1.x - l2.p2.x) < LIMIT || \
		fabs(l1.p1.y - l1.p2.y) < LIMIT && fabs(l2.p1.y - l2.p2.y) < LIMIT) {
		return false;
	}
	//垂直于不同的坐标轴,应该再判断下交点有没有在线段上,这都过了,数据是有多水
	if (fabs(l1.p1.x - l1.p2.x) < LIMIT && fabs(l2.p1.y - l2.p2.y) < LIMIT || \
		fabs(l1.p1.y - l1.p2.y) < LIMIT && fabs(l2.p1.x - l2.p2.x) < LIMIT) {
		return true;
	}
	double k1 = (l1.p1.y - l1.p2.y) / (l1.p1.x - l1.p2.x);  //斜率存在,计算斜率
	double k2 = (l2.p1.y - l2.p2.y) / (l2.p1.x - l2.p2.x);
	if (fabs(k1 - k2) < LIMIT)  //斜率相等,平行
		return false;
	double b1 = l1.p1.y - k1 * l1.p1.x;
	double b2 = l2.p1.y - k2 * l2.p1.x;
	double x0 = (b2 - b1) / (k1 - k2);    //解出x0, y0为交点
	double y0 = k1 * x0 + b1;
	Swap(l1.p1.x, l1.p2.x);
	Swap(l2.p1.x, l2.p2.x);
	Swap(l1.p1.y, l1.p2.y);
	Swap(l2.p1.y, l2.p2.y);
	//看交点是否在线段上,好长( -_-) ! 
	if (x0 > l1.p1.x && x0 < l1.p2.x && x0 > l2.p1.x && x0 < l2.p2.x \
		&& y0 > l1.p1.y && y0 < l1.p2.y && y0 > l2.p1.y && y0 < l2.p2.y \
		|| fabs(x0 - l1.p1.x) < LIMIT && fabs(y0 - l1.p1.y) < LIMIT || \
		fabs(x0 - l1.p2.x) < LIMIT && fabs(y0 - l1.p2.y) < LIMIT \
		|| fabs(x0 - l2.p1.x) < LIMIT && fabs(y0 - l2.p1.y) < LIMIT || \
		fabs(x0 - l2.p2.x) < LIMIT && fabs(y0 - l2.p2.y) < LIMIT)
		return true;
	else
		return false;
}

int main()
{
	int N;
	while (~scanf("%d", &N) && N) {
		for (int i = 0; i < N; i++) {
			scanf("%lf%lf%lf%lf", &L[i].p1.x, &L[i].p1.y, &L[i].p2.x, &L[i].p2.y);
		}
		int cou = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (i != j) {
					if (IsItersect(L[i], L[j]))
						cou++;
				}
			}
		}
		printf("%d\n", cou / 2);
	}
	return 0;
}



用向量和叉乘积


引用下别人的博客里的东西

来源:http://www.cnblogs.com/zhangchaoyang/articles/2668562.html

首先引出计算几何学中一个最基本的问题:如何判断向量在的顺时针方向还是逆时针方向?

把p0定为原点,p1的坐标是(x1,y1),p2的坐标是(x2,y2)。向量的叉积(cross product)实际上就是矩阵的行列式:

当叉积为正时,说明在的顺时针方向上;叉积为0说明两向量共线(同向或反向)。

当同时满足:

(1)和在的两侧(即一个顺时针方向上,一个在逆时针方向上)

(2)和在的两侧

时可肯定和相交。

            图1

图1是线段相交的一般情形。

图2只满足第(1)条,不满足第(2)条所以不能证明和相交。

            图2

图3和图4是一种特殊情况,它不满足第(2)条,因为和重合,即和的叉积为0。

可见当叉积为0时要分情况讨论,当p3在线段p1p2上时两线段相交;当p3在线段p1p2的延长线上时两线段不相交。


最后一句话其实也不用分情况讨论,只要改改判断条件就行了,改成d1*d2 <= 0 && d3 * d4 <= 0


#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

const double LIMIT = 1e-8;

typedef struct {
	double x, y;
}point, dir;

typedef struct {
	point p1;
	point p2;
}line;

line L[15];

double cross(point pp1, point pp2, point pp3) {
	dir t1, t2;
	t1.x = pp1.x - pp2.x;
	t1.y = pp1.y - pp2.y;
	t2.x = pp3.x - pp2.x;
	t2.y = pp3.y - pp2.y;
	return t1.x * t2.y - t1.y * t2.x;
}

bool IsItersect(line l1, line l2) {
	double d1, d2, d3, d4;
	d1 = cross(l2.p1, l1.p1, l1.p2);
	d2 = cross(l2.p2, l1.p1, l1.p2);
	d3 = cross(l1.p2, l2.p1, l2.p2);
	d4 = cross(l1.p1, l2.p1, l2.p2);
	if ((d1 * d2 < 0 || fabs(d1 * d2) < LIMIT) && (d3 * d4 < 0 || fabs(d3 * d4) < LIMIT)) {  //将d1 * d2 == 0和d3 * d4 == 0的情况包含进来
		return true;
	}
	else {
		return false;
	}
}

int main()
{
	int N;
	while (~scanf("%d", &N) && N) {
		for (int i = 0; i < N; i++)
			scanf("%lf%lf%lf%lf", &L[i].p1.x, &L[i].p1.y, &L[i].p2.x, &L[i].p2.y);
		int cou = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (i != j && IsItersect(L[i], L[j]))
					cou++;
			}
		}
		printf("%d\n", cou / 2);
	}
	return 0;
}


程序员的自我修养 一个好的程序员的自我修养需要从不同角度出发,工作能力是一个方面,对自身的定位和了解也是一个方面,程序员的工作能力修养包括需求理解、功能设计、沟通确认、代码规范、成果验证、代码重构优化等,通过自我剖析来对自身进行定位并了解自身的优缺点,如去除劣根性、端正意识形态与态度、提升自己的核心竞争力等。 自我剖析 一个成功的人,必须要先了解自己,只有了解自己是什么样的人,了解自身有哪些优缺点,了解自己更合适做什么工作,知道自己做什么才能发挥出自身的优势,这样才能使自己在工作中发光发热,在职业生涯的道路上走的更远。 阅读详情

相关推荐

DSP28335做FFT傅里叶变换

1. 看了一下例程,居然没有FFT的例程,难道这个DSP28335不能做FFT吗?对了C2000系列是有C2000 ware这个库的。方便很多,不过目前不确定在C5000上运行的FFT能直接迁移到DSP28335上行不?测试了一下,是可以的,因为都是用的math.h,下面的一些正弦函数基本一样的 2. 硬件简介,其中DSP28335和TLV320AIC23B之间使用I2C接口进行寄存器配置,...

weixin_30390075的博客 3339

HDU 1086 You can Solve a Geometry Problem too

题目大意:给出n条线段,判断有几个交点,因为任意两条的都是只有一个,就是说排除了共线的情况。 思路:首先进行快速排斥实验,就是其中有一条线段的x的最小值要小于另一条线段的最大值,而另一条线段的最小值要小于此线段的最大值,y值同理可得,说的形象点就是两个线段组成的矩形要相交。 然后进行跨立实验,就是叉乘是否异号。对两条线段都要进行叉乘     下面来自:常州市第一中学  林厚从的资料

muxi@Achilles的专栏 1076

Isaac Sim 车辆动力学:从参数调优到户外仿真的挑战与实战

本文深入探讨了Isaac Sim在车辆动力学仿真中的应用,从参数调优到户外场景模拟的实战经验。详细解析了悬架系统、轮胎物理模型等核心参数的设置技巧,并分享了应对户外仿真挑战的实用方案。通过Vehicle Wizard高级使用和传感器配置,帮助开发者高效完成车辆仿真与算法开发。

weixin_29192211的博客 411

You can Solve a Geometry Problem too HDU - 1086

Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much ...

qq_43813697的博客 290

QGIS中高效处理CAD数据的两种实用技巧

本文详细解析了在QGIS中高效处理CAD数据的两种核心方法。针对DWG/DXF文件在QGIS中直接打开失败的问题,介绍了使用QGIS内置数据导入管理器进行标准化转换的流程,并重点推荐了功能更强大的Another DXF Importer插件,以更好地保留文字、块属性等复杂信息。文章对比了两种方法的优劣与适用场景,并提供了坐标系设置、中文乱码等常见问题的避坑指南,帮助用户根据数据需求选择最佳导入策略。

weixin_28363123的博客 130

IAR下载Linux环境适配:入门级安装指导

详细介绍如何在Linux系统中完成iar下载与环境配置,帮助开发者快速上手IAR集成开发环境,解决常见安装问题,提升开发效率。

weixin_35754676的博客 775

hdu 1086 You can Solve a Geometry Problem too

You can Solve a Geometry Problem too Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the exp

空白页 666

2025年下半年网络规划设计师综合知识真题及答案解析

1983年,ARPANet完成技术转型,对此后的 Internet发展全关重要,这一转型是指( ) A.电路交换转向分组交换 B.商业化转型 C.民用化转型 D.从 NCP 协议转向 TCP/IP 答案:D 解析:ARPANet 最初使用 NCP (Network Control Protocol)协议进行主机问通信。1983年1月1日,ARPANet 正式切换到TCP/IP 协议族,为后续Internet的发展奠定了统一协议基础。

鹿鸣天涯 984

HDU1086 You can Solve a Geometry Problem too

微微发亮的传送 计算几何的基础题,给定一些线段,判断有多少个交点,同时遇到很多条直线相交的时候可以重复计算,非常良心了。 我搬出了我的线段类和点类,写的那叫一个复杂,不过好在还是A了,如果不许重复计算的话我们就得求交点了…… #include #include #include #include #include using namespace std; const doubl

浅雨歌 643

[Dify] 如何构建一个带评分功能的 Dify AI 教练系统

本文介绍如何利用Dify开源平台构建具备自动评分功能的AI教练系统。系统通过Prompt设计AI教练角色,使用OpenAPI定义评分工具接口,结合工作流编排实现提问、回答收集、评分和反馈的全流程。文章详细讲解了从AI教练身份设定、多维度评分标准设计到前端可视化展示的实现方法,并提供了可扩展建议,适用于语言训练、面试模拟等多种场景。该系统展示了Dify平台在构建智能化评估应用方面的优势,实现零代码开发专业AI教练解决方案。

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 340

【proteus】模拟串口和物理串口通讯

proteus提供了两种串口,一种是模拟的串口,还有一张是用软件仿真到硬件的物理串口。他们的区别就是:模拟串口只能在proteus内部进行通讯,但是物理可以通过软件映射到硬件上和单机或者串口软件通讯。 物理串口在元器件搜索COMPIM即可找到。 模拟串口在这里: PROTEUS仿真图: 物理串口COMPIM的设置: 模拟串口的设置: VSPD是一款虚拟串口软件,VSPD下载链接: 链接:https://pan.baidu.com/s/1AZygJCeWZfQTnxdCb_3TPg 提取码:12pm

阿蛋的博客 8946

You can Solve a Geometry Problem too HDU 1086

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5956Accepted Submission(s): 2865 Problem Description ...

weixin_30413739的博客 192

orin+96712接GMSL2相机调试经验

orin gmsl camera调试

乐于分享 4478

HDU-1086 You can Solve a Geometry Problem too

HDU-1086 You can Solve a Geometry Problem too AC代码及题解

包络面的博客 298

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

hdu1086 You can Solve a Geometry Problem too(判断线段相交)

hdu1086 You can Solve a Geometry Problem too(判断线段相交) 题目 描述 Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the exp...

Twinkle_sone的博客 612

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

You can Solve a Geometry Problem too HDU - 1086 (向量)

HDU - 1086 Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are a...

FIVE 314

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

hdu 1086 You can Solve a Geometry Problem too

 You can Solve a Geometry Problem tooTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 714    Accepted Submission(s): 342Problem Description

abao_的专栏 1143

HDU-1086 You can Solve a Geometry Problem too 解题报告

Description Many geometry(几何)problems were designed in the ACM/ICPC. Andnow, I also prepare a geometry problem for this final exam. According to theexperience of many ACMers, geometry problems are al

岁月流风、、 1303

HDU 1086 You can Solve a Geometry Problem too && 简单几何

题意:给你一百条线段,问这些线段交点的个数,重复交点需要重复计算。 解法:先存下所有线段,然后双重for循环,两两判断是否相交,先求两条直线是否相交,再判断交点是否在线段上,统计个数。 代码: #include #include #include #include #include #include #include #include #include #define inf 0x3f

Tconan99 682

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/32768 K (Java/Others)Total Submission(s): 6932Accepted Submission(s): 3350 Problem Descrip...

anque1234的博客 164

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

You can Solve a Geometry Problem tooTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12006    Accepted Submission(s): 5946Problem DescriptionMany...

BBHHTT的博客 344

hdu1086 You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry probl...

一身清贫怎敢入繁华 413

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码。。是不是有点浪费。。。但是我觉得似乎哪里也优化不了了。。。。 判断线段相交就是利用的叉积。假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交。此时需要用同样的方法去判断L2是否在L1的两个端

scf0920 852

HDU 1086 You can Solve a Geometry Problem too [计算几何]

Description给N条线段,求交点个数,如果重合,算两个Algorithm简直模板 首先我参考 《挑战程序设计竞赛》发现了一个方法,不过书上说还有一个别的方法,ccw函数 字有点歪,不管了然后我百度良久,都没有看到什么是CCW 好在还有谷歌 于是就查到了 6.1 Geometric Primitives 虽然是JAVA写的,好在还是比较好读,改一下就变成C++的。 然后这个

more time 274

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/32768 K (Java/Others) Total Submission(s): 6997    Accepted Submission(s): 3385 Problem Desc

TOKHE的专栏 979
上一篇: JAVA连接sqlserver2012数据库
下一篇: hdu1115 Lifting the Stone 多边形重心
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址