首页 > 专题系列 > Java解POJ > POJ 1269 Intersecting Lines [解题报告] Java
2013
11-09

POJ 1269 Intersecting Lines [解题报告] Java

Intersecting Lines

问题描述 :

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.

Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.

输入:

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

输出:

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read “END OF OUTPUT”.

样例输入:

5
0 0 4 4 0 4 4 0
5 0 7 6 1 0 2 3
5 0 7 6 3 -6 4 -3
2 0 2 27 1 5 18 5
0 3 4 0 1 2 2 5

样例输出:

INTERSECTING LINES OUTPUT
POINT 2.00 2.00
NONE
LINE
POINT 2.00 5.00
POINT 1.07 2.20
END OF OUTPUT

解题代码:

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
	public static void main(String[] args){
		Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n=scanner.nextInt();
		double[] x,y;
		double px,py;
		double k1,k2,b1,b2;
		System.out.println("INTERSECTING LINES OUTPUT");
		for (int i=0;i< n ;i++ ){
			x=new double[4];
			y=new double[4];
			for (int j=0;j< 4 ;j++ ){
				x[j]=scanner.nextDouble();
				y[j]=scanner.nextDouble();
			}
			if ((x[1]-x[0])*(y[3]-y[2])==(x[3]-x[2])*(y[1]-y[0])){
				if ((x[3]-x[0])*(y[2]-y[1])==(x[2]-x[1])*(y[3]-y[0])){
					System.out.println("LINE");
				}
				else{
					System.out.println("NONE");
				}
			}
			else{
				if (x[0]==x[1]){
					px=x[0];
					k2=(y[3]-y[2])/(x[3]-x[2]);
					b2=y[3]-k2*x[3];
					py=k2*px+b2;
				}
				else if (x[2]==x[3]){
					px=x[2];
					k1=(y[1]-y[0])/(x[1]-x[0]);
					b1=y[1]-k1*x[1];
					py=k1*px+b1;
				}
				else{
					k1=(y[1]-y[0])/(x[1]-x[0]);
					b1=y[1]-k1*x[1];
					k2=(y[3]-y[2])/(x[3]-x[2]);
					b2=y[3]-k2*x[3];
					px=(b2-b1)/(k1-k2);
					py=k1*px+b1;
				}
				px=(Math.round(px*100.0))/100.0;
				py=(Math.round(py*100.0))/100.0;
				System.out.println("POINT "+getO(px)+" "+getO(py));
			}
		}
		System.out.println("END OF OUTPUT");
	}

	public static String getO(double d){
		String ds=""+d;
		while (true){
			int index=0;
			boolean flag=false;
			for (int j=0;j< ds.length() ;j++ ){
				if (flag){
					index++;
				}
				if (ds.charAt(j)=='.'){
					flag=true;
				}
			}
			if (index==2){
				break;
			}
			else if (index==1){
				ds=ds+"0";
			}
		}
		return ds;
	}
}

  1. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!

  2. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!