Poj Intersecting Lines (Encuentre la intersección de líneas rectas)

  • Idea
    Hay dos fórmulas:
    1. Conozca las coordenadas de dos puntos (x1, y1,), (x2, y2). Encuentre la ecuación lineal (forma general)
    A x + B y + C = 0 Ax + By + C = 0Una x+B y+C=0
    A = y 2 − y 1 , B = x 1 − x 2 , C = x 2 ∗ y 1 − x 1 ∗ y 2 A=y2-y1,B=x1-x2,C=x2*y1-x1*y2 UN=y 2-y 1 ,segundo=x 1-x 2 ,C=x 2año 1-x 1y 2
    2. Dos ecuaciones en línea recta conocidas (forma general)
    A 1 x + B 1 y + C 1 = 0 A_1x + B_1y + C_1 = 0UN1X+segundo1y+C1=0
    A 2 x + B 2 y + C 2 = 0 A_2x + B_2y + C_2 = 0UN2X+segundo2y+C2=0
    Encuentra el punto de intersección de dos líneas rectas (cuando hay un punto de intersección)
    x = B 1 ∗ C 2 - B 2 ∗ C 1 A 1 ∗ B 2 - A 2 ∗ B 1 x = \ frac {B_1 * C_2-B_2 * C_1 } {A_1 * B_2-A_2 * B_1}X=UN1B2- A2B1segundo1C2- B2C1
    y = UNA 2 ∗ C 1 - UNA 1 ∗ C 2 UNA 1 ∗ B 2 - UNA 2 ∗ B 1 y = \ frac {A_2 * C_1-A_1 * C_2} {A_1 * B_2-A_2 * B_1} y=UN1B2- A2B1UN2C1- A1C2
  • Código
#pragma GCC optimize(2)
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cmath> 

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
#define pi acos(-1.0)
#define e exp(1.0)
#define pb push_back
#define mk make_pair
#define fir first
#define sec second
#define scf scanf
#define prf printf
typedef pair<ll,ll> pa;
const ll INF=0x3f3f3f3f3f3f3f3f;
int N,T;
const double eps=1e-8;
double x1,y1,x2,y2,x3,y3,x4,y4;
void do_(){
    
    
	int i,j,x_1,y_1,x_2,y_2;
	x_1=x2-x1;
	y_1=y2-y1;
	x_2=x4-x3;
	y_2=y4-y3;
	if(fabs(x_1*y_2-x_2*y_1)<eps){
    
    
		x_1=x3-x1;
		y_1=y3-y1;
		x_2=x4-x1;
		y_2=y4-y1;
		if(fabs(x_1*y_2-x_2*y_1)<eps)
		prf("LINE\n");
		else
		prf("NONE\n");
	}
	else{
    
    
		double a1=y2-y1,b1=x1-x2,c1=x2*y1-x1*y2;
		double a2=y4-y3,b2=x3-x4,c2=x4*y3-x3*y4;
		double x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
		double y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
		prf("POINT %.2f %.2f\n",fabs(x)<eps?0:x,fabs(y)<eps?0:y);
	}
	return ; 
}
int main()
{
    
    
//  freopen(".../.txt","w",stdout);
//  freopen(".../.txt","r",stdin);
//	ios::sync_with_stdio(false);
//	cin>>N;
	scf("%d",&N);
	prf("INTERSECTING LINES OUTPUT\n");
	while(N--){
    
    
		scf("%lf %lf %lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
		do_();
	}
	prf("END OF OUTPUT\n");
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_43311695/article/details/108798600
Recomendado
Clasificación