Poj Intersecting Lines (Find the intersection of straight lines)

  • Ideas
    There are two formulas:
    1. Know the coordinates of two points (x1, y1,), (x2, y2). Find the linear equation (general form)
    A x + B y + C = 0 Ax+By+C=0Ax+By+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 * y2A=y 2y1,B=x1x2,C=x2y1x1y 2
    2. Known two straight line equations (general form)
    A 1 x + B 1 y + C 1 = 0 A_1x+B_1y+C_1=0A1x+B1Y+C1=0
    A 2 x + B 2 y + C 2 = 0 A_2x + B_2y + C_2 = 0A2x+B2Y+C2=0
    Find the point of intersection of two straight lines (when there is an intersection point)
    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=A1B2A2B1B1C2B2C1
    y = A 2 ∗ C 1 − A 1 ∗ C 2 A 1 ∗ B 2 − A 2 ∗ B 1 y= \frac{A_2*C_1-A_1*C_2}{A_1*B_2-A_2*B_1} Y=A1B2A2B1A2C1A1C2
  • Code
#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;
}

Guess you like

Origin blog.csdn.net/weixin_43311695/article/details/108798600