(计算几何step8.1.2.2)POJ 1269 Intersecting Lines(使用叉积来计算两条直线的交点)

本文提供了一段用于解决POJ 1269问题的C++代码,该问题涉及判断两条线段是否相交及计算交点坐标。文章详细展示了如何使用向量运算来解决此类几何问题,并提供了完整的源代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * POJ_1269.cpp
 *
 *  Created on: 2013年11月16日
 *      Author: Administrator
 */

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

using namespace std;

const double epsi = 1e-10;
const double pi = acos(-1.0);
const int maxn = 100005;//这个不要开得太小...

struct Point{
	double x,y;
	Point(double _x = 0,double _y = 0):x(_x),y(_y){

	}

	Point operator-(const Point& op2)const{//**要注意这种写法
		return Point(x - op2.x,y - op2.y);
	}

	double operator^(const Point& op2)const{
		return x*op2.y - y*op2.x;
	}
};

int sign(const double& x){//判断x是正数还是负数
	if(x > epsi){
		return 1;//传入的数是一个整数...
	}else if(x < -epsi){
		return -1;//传入的数是一个负数...
	}

	return 0;
}

double sqr(double x){//计算一个数的平方数
	return x*x;
}

double mul(const Point& p0,const Point& p1,const Point& p2){//计算p0p1与p1p2的叉积叉积
	return (p1-p0)^(p2-p0);
}

double dis2(const Point& p0,const Point& p1){//返回两个点的距离的平方
	return sqr(p0.x - p1.x) + sqr(p0.y - p1.y);
}

double dis(const Point& p0,const Point& p1){//返回两个点的距离
	return sqrt(dis2(p0,p1));
}

int cross(const Point& p1,const Point& p2,const Point& p3,const Point& p4,Point& p){//判断p1p2是否跨立p3p4
	double a1 = mul(p1,p2,p3);
	double a2 = mul(p1,p2,p4);

	if(sign(a1) == 0 && sign(a2) == 0){//如果这两根木棒重合
		return 2;
	}

	if(sign(a1 - a2) == 0){//平行..这是要注意的...
		return 0;
	}

	/**
	 * 求交点的核心代码
	 */
	p.x = (a2*p3.x - a1*p4.x)/(a2-a1);
	p.y = (a2*p3.y - a1*p4.y)/(a2-a1);
	return 1;//p1p2和p3p4通过跨立实验
}

Point p1,p2,p3,p4,p;


int main(){
	int t;
	scanf("%d",&t);

	printf("INTERSECTING LINES OUTPUT\n");
	while(t--){
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y,&p3.x,&p3.y,&p4.x,&p4.y);

		int m = cross(p1,p2,p3,p4,p);
		if(m == 0){
			printf("NONE\n");
		}else if(m == 2){
			printf("LINE\n");
		}else{
			printf("POINT %.2lf %.2lf\n",p.x,p.y);
		}
	}
	printf("END OF OUTPUT\n");

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

帅气的东哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值