博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线段相交吗
阅读量:4524 次
发布时间:2019-06-08

本文共 2870 字,大约阅读时间需要 9 分钟。

import java.util.ArrayList;import java.util.Scanner;public class Main {	public static void main(String[] args) {		ArrayList
list = new ArrayList
(); ArrayList
x = new ArrayList
(); ArrayList
y = new ArrayList
(); int count = input(list, x, y);// 返回线段条数 if (count == 0) { System.out.print("false"); System.exit(0); } int horizontal = 0;// 水平线段条数 int vertical = 0;// 垂直线段条数 if (count % 2 == 0) { horizontal = count / 2; vertical = count / 2; } else { horizontal = count / 2; vertical = count / 2 + 1; } int lengthLast = list.get(count - 1);// 最后一条线段的长度 boolean b = false; int x1 = x.get(count - 1);// 最后一条线段的起点坐标 int y1 = y.get(count - 1); if (count % 4 == 1) {// 最后一条向上走 // 找到上面最近一条可以接触的水平线段,判断是否可以接触,下面的情况类似。 for (int i = 0; i < horizontal - 1; i++) {// 遍历所有水平线 int a1 = x.get(2 * i + 1); int b1 = y.get(2 * i + 1); int a2 = x.get(2 * i + 2); int b2 = y.get(2 * i + 2); if ((a1 >= x1 && x1 >= a2) || (a2 >= x1 && x1 >= a1)) { if (b1 > y1) {// 上面的线段 if (lengthLast >= b1 - y1) { b = true; break; } } } } } else if (count % 4 == 2) {// 左 for (int i = 0; i < vertical - 1; i++) {// 遍历所有垂直线段 int a1 = x.get(2 * i); int b1 = y.get(2 * i); int a2 = x.get(2 * i + 1); int b2 = y.get(2 * i + 1); if ((b1 >= y1 && y1 >= b2) || (b2 >= y1 && y1 >= b1)) { if (a1 < x1) {// 左边的线段 if (lengthLast >= Math.abs(a1 - x1)) { b = true; break; } } } } } else if (count % 4 == 3) {// 下 for (int i = 0; i < horizontal - 1; i++) {// 遍历所有水平线 int a1 = x.get(2 * i + 1); int b1 = y.get(2 * i + 1); int a2 = x.get(2 * i + 2); int b2 = y.get(2 * i + 2); if ((a1 >= x1 && x1 >= a2) || (a2 >= x1 && x1 >= a1)) { if (b1 < y1) {// 上面的线段 if (lengthLast >= Math.abs(b1 - y1)) { b = true; break; } } } } } else if (count % 4 == 0) {// 右 for (int i = 0; i < vertical - 1; i++) {// 遍历所有垂直线段 int a1 = x.get(2 * i); int b1 = y.get(2 * i); int a2 = x.get(2 * i + 1); int b2 = y.get(2 * i + 1); if ((b1 >= y1 && y1 >= b2) || (b2 >= y1 && y1 >= b1)) { if (a1 > x1) {// 左边的线段 if (lengthLast >= Math.abs(a1 - x1)) { b = true; break; } } } } } System.out.print(b); } public static int input(ArrayList
list, ArrayList
x, ArrayList
y) { Scanner scanner = new Scanner(System.in); x.add(0);// 记录原点 y.add(0); int i = 0; String s = scanner.nextLine(); if (!s.equals("")) { String[] temp = s.split(" "); for (int k = 0; k < temp.length; k++) { i++; int n = Integer.parseInt(temp[k]);// 线段长度 list.add(n); int preX = x.get(i - 1);// 上一个点的坐标 int preY = y.get(i - 1); if (i % 4 == 1) {// 上 x.add(preX); y.add(preY + n); } else if (i % 4 == 2) {// 左 x.add(preX - n); y.add(preY); } else if (i % 4 == 3) {// 下 x.add(preX); y.add(preY - n); } else if (i % 4 == 0) {// 下 x.add(preX + n); y.add(preY); } } return temp.length; } else { return 0; } }}

  

转载于:https://www.cnblogs.com/xiaoxueyong/p/6026663.html

你可能感兴趣的文章