nyoj-1132-promise me a medal(求线段交点)
/*
Name:nyoj-1132-promise me a medal
Copyright:
Author:
Date: 2018/4/26 20:26:22
Description:
向量之间的运算,不熟悉就绕蒙逼了
用 ACM国际大学生程序设计竞赛 算法与实现的模板
有个坑: 如果第二条线段的起点是第一条线段的终点,那么不需要计算 1
1 2 1 3 1 3 1 4
输出
yes 1.0 3.0 */
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double pi = acos(-1.0);
inline double sqr(double x) {
return x * x;
}
const double eps = 1e-;
int cmp(double x) {
if (fabs(x) < eps) return ;
if (x > )return ;
return -;
}
//point
struct point {
double x, y;
point (){}
point (double a, double b):x(a), y(b) { }
void input() {
scanf("%lf %lf", &x, &y);
}
friend point operator - (const point &a, const point &b) {
return point(a.x-b.x, a.y-b.y);
}
friend point operator * (const double &a, const point &b) {
return point (a * b.x, a*b.y);
}
friend point operator / (const point &a, const double &b) {
return point (a.x / b, a.y /b);
}
friend bool operator == (const point &a, const point &b) {
return (cmp(a.x - b.x) == && cmp(a.y - b.y) == );
}
};
double det(const point &a, const point &b) {
return a.x * b.y - a.y * b.x;
}
//line
struct line {
point a, b;
line() { }
line(point x, point y):a(x), b(y){ }
};
line point_make_line(const point a, const point b) {
return line(a, b);
}
bool parallel(line a, line b) {
return !cmp(det(a.a - a.b, b.a - b.b));
}
bool line_make_point(line a, line b, point &res) {
if(parallel(a,b)) return false;
double s1 = det(a.a-b.a, b.b - b.a);
double s2 = det(a.b-b.a, b.b - b.a);
res = (s1 * a.b - s2 * a.a)/(s1-s2);
return true;
}
int main()
{
int t;
cin>>t;
while (t--) {
point a, b ,c ,d;
cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
line ab(a, b), cd(c, d);
if (b == c) {//如果第二条线段的起点是第一条线段的终点,那么不需要计算
cout<<"yes ";
printf("%.1f %.1f\n",b.x, b.y) ;
continue;
}
if (parallel(ab, cd)) cout<<"no\n";
else {
cout<<"yes ";
point ans;
line_make_point(ab, cd, ans);
printf("%.1f %.1f\n",ans.x, ans.y) ;
}
}
return ;
}
nyoj-1132-promise me a medal(求线段交点)的更多相关文章
- 谈谈"求线段交点"的几种算法(js实现,完整版)
"求线段交点"是一种非常基础的几何计算, 在很多游戏中都会被使用到. 下面我就现学现卖的把最近才学会的一些"求线段交点"的算法总结一下, 希望对大家有所帮助. ...
- poj1408(求线段交点)
求出所有线段的交点,然后利用叉乘求四边形面积即可. // // main.cpp // poj1408 // // Created by 陈加寿 on 15/12/31. // Copyright ( ...
- hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)
Mirror and Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)
Area Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【计算几何初步-线段相交】【HDU1089】线段交点
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- js判断向量叉点 并求出交点坐标
代码如下可以直接运行,判断向量相交并求出交点坐标 <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)
Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...
- POJ_1269_Intersecting Lines_求直线交点
POJ_1269_Intersecting Lines_求直线交点 Description We all know that a pair of distinct points on a plane ...
- sgu 129 Inheritance 凸包,线段交点,计算几何 难度:2
129. Inheritance time limit per test: 0.25 sec. memory limit per test: 4096 KB The old King decided ...
随机推荐
- Python替换文件内容
#!/usr/bin/env python import fileinput for line in fileinput.input('fansik',inplace=1): line = line. ...
- A+B和C (15)
时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定区间[-2的31次方, 2的31次方]内的3个整数A.B和C ...
- PyQt4 颜色选择,字体选择代码
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...
- 系统性能模块psutil
psutil是一个跨平台库,能够轻松实现获取系统运行的进程和系统利用率(包括cpu.内存.磁盘.网络等)信息.它主要用于系统监控,分析和限制系统资源及进程的管理.它实现了同等命令行工具提供的功能,如p ...
- set去重,session,cookie c#与python 对比
端口,发送请求进行监听,然后处理 session 是存储在服务器端的数据,靠sessionId来验证获取信息,没有大小和类型限制, cookie 是存储在客户端的数据,可以长期使用,有面临被获取的 ...
- flex 实现图片播放 方案一 图片全部预加载放内存
这种方案,对于web的应用有局限性,在图片量比较多,比较大的时候,就会爆浏览器异常.一般建议轻量级的采用这种方案. <?xml version="1.0" encoding= ...
- VMWare虚拟机安装步骤
VMWare虚拟机安装 如果以前安装过VMware,则先检查是否卸载干净(注册表等) 一.首先下载VMware Workstation Pro,在这里提供一个下载链接:https://www.nocm ...
- jni 编译错误error: unknown type name '__va_list'
platforms\android-9\arch-arm\usr\include\stdio.h:257:37: error: unknown type name '__va_list' 解 ...
- Docker容器技术-创建一个简单的Web应用
一.创建一个简单的Web应用 1.identicon 基于某个值而自动产生的图像,这个值是IP地址或用户名的散列值. 用途: 通过计算用户名或IP地址的散列值,在网站上提供用于识别用户的图像,以及自动 ...
- eclipse修改项目默认编码为UTF-8
1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧 Text file encodin ...