You can Solve a Geometry Problem too(线段相交问题)


Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point.
 

Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the number of intersections, and one line one case.
 

Sample Input
  
  
2 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.00 3 0.00 0.00 1.00 1.00 0.00 1.00 1.00 0.000 0.00 0.00 1.00 0.00 0
 

Sample Output
  
  
1 3
题意:给出n条线段,求交点数,注意不存在重合的情况
思路:用叉乘来判断
代码如下:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Main implements Runnable {
    private static final boolean DEBUG = false;
    private Scanner cin;
    private PrintWriter cout;
    private int n;
    private static final int N = 101;
    private static final Line[] lines = new Line[N];

    class Point
    {
        double x, y;
    }

    class Line
    {
        Point a, b;
    }

    private void init()
    {
        try {
            if (DEBUG) {
                cin = new Scanner(new BufferedReader(new FileReader("f:\\OJ\\uva_in.txt")));
            } else {
                cin = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
            }

            cout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean input()
    {
        n = cin.nextInt();
        if (n == 0) return false;

        for (int i = 0; i < n; i++) {
            lines[i] = new Line();
            Point a = new Point();
            a.x = cin.nextDouble();
            a.y = cin.nextDouble();
            lines[i].a = a;
            Point b = new Point();
            b.x = cin.nextDouble();
            b.y = cin.nextDouble();
            lines[i].b = b;
        }
        return true;
    }

    private double cross(Point a, Point b)
    {
        double x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y;

        return x1 * y2 - y1 * x2;
    }

    private boolean isInterSection(Line l1, Line l2)
    {
        Point p1 = new Point();
        p1.x = l1.b.x - l1.a.x;
        p1.y = l1.b.y - l1.a.y;
        Point p2 = new Point();
        p2.x = l2.b.x - l2.a.x;
        p2.y = l2.b.y - l2.a.y;

        double c2 = cross(p1, p2);

        p1.x = l2.a.x - l1.a.x;
        p1.y = l2.a.y - l1.a.y;
        double c1 = cross(p1, p2);

        p1.x = l2.a.x - l1.a.x;
        p1.y = l2.a.y - l1.a.y;
        p2.x = l1.b.x - l1.a.x;
        p2.y = l1.b.y - l1.a.y;
        double c3 = cross(p1, p2);

        if (c2 < 0) {
            c2 = -c2;
            c1 = -c1;
            c3 = -c3;
        }

        if (c2 != 0 && c1 >= 0 && c1 <=  c2 && c3 >= 0 && c3 <= c2) return true;
        else return false;
    }

    private void solve()
    {
        int ans = 0;

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (isInterSection(lines[i], lines[j])) ans++;
            }
        }

        cout.println(ans);
        cout.flush();
    }

    @Override
    public void run()
    {
        init();

        while (input()) {
            solve();
        }
    }

    public static void main(String[] args)
    {
	// write your code here
        new Thread(new Main()).start();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kgduu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值