hdu1086 You can Solve a Geometry Problem too

系统-等保三级-CentOS Linux 7合规基线检查 shell脚本 阅读详情

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;
}


开源网格划分软件-Gmsh二次开发教程 (4)网格生成与控制​ ​本章将深入讲解Gmsh的网格生成技术,包括全局/局部网格控制、边界层生成、自适应细化等,并提供完整的代码示例。 阅读详情

相关推荐

BraTS数据集保姆级使用指南:从下载、预处理到可视化(附Python脚本)

本文提供BraTS数据集的完整使用指南,涵盖从下载、预处理到可视化的全流程,并附有Python脚本示例。详细解析了医学影像处理中的数据结构、标准化流程和可视化技巧,帮助初学者快速掌握BraTS数据集的应用方法,提升医学影像分析效率。

weixin_27310417的博客 461

HDU 1086 You can Solve a Geometry Problem too

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

muxi@Achilles的专栏 1074

基因ID转换避坑指南:如何用org.Hs.eg.db把ENSEMBL ID变成可读的gene symbol

本文详细介绍了如何将RNA-seq等数据中常见的ENSEMBL ID转换为可读的基因符号(Gene Symbol),重点对比了biomaRt与org.Hs.eg.db两大工具的优劣,并深入剖析了多对多映射、版本号失效及非编码基因Symbol缺失三大核心陷阱。文章提供了针对TCGA、GEO等不同数据源的定制化处理方案与完整的稳健转换流程,帮助生物信息学分析者避免常见错误,确保下游分析的准确性。

weixin_28223453的博客 700

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

保姆级教程:在RK3568上搞定PR2100K+GC2385双摄(从DTS配置到HAL层补丁)

本文详细介绍了在RK3568平台上调试PR2100K+GC2385双摄系统的完整流程,从DTS配置到HAL层补丁。涵盖硬件连接、媒体控制器拓扑验证、Camera3配置、HAL层特殊补丁及性能优化等关键步骤,帮助开发者快速解决双摄调试中的常见问题,提升嵌入式视觉系统开发效率。

weixin_28724661的博客 195

Modelsim 安装步骤详解

Modelsim 安装步骤详解目录一、modelsim简述及下载1、简介及特点2、modelsim版本3、modelsim下载二、安装步骤详解三、注册简述四、总结与参考资料1、总结2、参考资料 目录 本篇文章主要对Modelsim SE版本安装步骤的介绍。 一、modelsim简述及下载 1、简介及特点 简介 modelsim是Mentor公司开发的优秀的HDL语言仿真软件。 它能提供友好的仿真环境,采用单内核支持VHDL和Verilog混合仿真的仿真器。它采用直接优化的编译技术、Tcl/Tk技术和单

执迷不悟 15万+

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

空白页 663

电子设计实战:用MOS管手把手搭建D触发器电路(含时序分析避坑指南)

本文详细介绍了如何使用MOS管从零开始搭建D触发器电路,重点解析了建立时间和保持时间等关键时序参数对电路稳定性的影响。通过实测波形分析和避坑指南,帮助电子工程师掌握D触发器的实际搭建技巧与优化方法,提升数字电路设计的可靠性。

x1y2z的博客 1159

HDU1086 You can Solve a Geometry Problem too

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

浅雨歌 640

FPGA SoC 的 RISC-V 固件开发全攻略(三):RISC-V MCU 启动流程深入解析

本文是《FPGA SoC 的 RISC-V 固件开发全攻略》专栏第 3 篇。上一篇:第 2 篇《开发环境搭建与工具链避坑》 | 下一篇:第 4 篇《RISC-V 与 ARM Cortex-M 开发差异对比》

XavierKong的博客 43

k8s 不同node之间pod ip不通

底层k8s使用calico网络,ipip网络模式,kubevirt启动了2台虚拟机;在这两台虚拟机上再搭建k8s,calico网络,ipip网络模式。说明:我是使用helm安装的calico,改这个模式要改crd资源ippools。其他方式安装的calico修改方法可能不一样。在虚拟机上的k8s上启动了2个pod,分布在不同的node上,这两个pod不能ping通对方的pod ip。修改虚拟机之上的k8s集群的calico网络的ipipMode为Never即可,之前使用的是Always。

weixin_40548182的博客 4054

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的博客 191

AI编程工具到底是初学者的绊脚石还是垫脚石?

现在是AI时代,我们要做的,是学会拥抱AI、使用AI,成为AI的驾驭者,如果你因为害怕使用AI学不到东西,那么大可不必,因为AI必然是今后的趋势,相信会用AI在今后的求职面试中也会有很大的占比。另外我之前写了多篇有关AI编程开发工具Trae的使用技巧和教程,需要的话你可以关注公众号后通过菜单栏获取。另外我也把所有Trae相关的文章整合到了腾讯ima知识库,感兴趣的可以关注公众号以后回复。

cpp加油站的专栏 184

HDU-1086 You can Solve a Geometry Problem too

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

包络面的博客 297

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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的博客 604

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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 313

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

岁月流风、、 1302

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

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

Tconan99 677

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的博客 341

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...

一身清贫怎敢入繁华 412

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

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

scf0920 847

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

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

more time 273

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的专栏 977
上一篇: 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、付费专栏及课程。

余额充值