# 挑战程序竞赛系列（83）：3.6计算几何基础

## 挑战程序竞赛系列（83）：3.6计算几何基础

• 边平行，需要判断任何一条线段的两个顶点是否在另一条线段上。
• 非平行边，求出两条线段的交点，判断交点是否分别在这两条线段内。

```import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

String INPUT = "./data/judge/201709/P1127.txt";

public static void main(String[] args) throws IOException {
new Main().run();
}

class P {

static final double EPS = 1e-10;

double x;
double y;

P(double x, double y){
this.x = x;
this.y = y;
}

}

P sub(P a) {
}

P mul(P a) {
return new P(x * a.x, y * a.y);
}

double dot(P a) {
return add(x * a.x,  y * a.y);
}

double det(P a) {
return add(x * a.y, -y * a.x);
}

@Override
public String toString() {
return "(" + x + "," + y + ")";
}

public double add(double a, double b) {
if (Math.abs(a + b) < EPS * (Math.abs(a) + Math.abs(b))) return 0;
return a + b;
}
}

boolean onSeg(P p1, P p2, P q) {
return p1.sub(q).det(p2.sub(q)) == 0 && p1.sub(q).dot(p2.sub(q)) <= 0;
}

P intersection(P p1, P p2, P q1, P q2) {
double fz = q2.sub(q1).det(q1.sub(p1));
double fm = q2.sub(q1).det(p2.sub(p1));
P q = p2.sub(p1);
q = new P(fz / fm * q.x, fz / fm * q.y);
}

while (true) {
int N = ni();
if (N == 0) break;

P[] p = new P[N];
P[] q = new P[N];

for (int i = 0; i < N; ++i) {
p[i] = new P(nd(), nd());
q[i] = new P(nd(), nd());
}

boolean[][] map = new boolean[N][N];
for (int i = 0; i < N; ++i) {
map[i][i] = true;
for (int j = i + 1; j < N; ++j) {
if (p[i].sub(q[i]).det(p[j].sub(q[j])) == 0) {
map[i][j] = map[j][i] = onSeg(p[i], q[i], p[j])
|| onSeg(p[i], q[i], q[j])
|| onSeg(p[j], q[j], p[i])
|| onSeg(p[j], q[j], q[i]);
}
else {
P r = intersection(p[i], q[i], p[j], q[j]);
map[i][j] = map[j][i] = onSeg(p[i], q[i], r) && onSeg(p[j], q[j], r);
}
}
}

for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
map[i][j] |= map[i][k] && map[k][j];
}
}
}

while (true) {
int a = ni();
int b = ni();
if (a + b == 0) break;
a --;
b --;
out.println(map[a][b] ? "CONNECTED" : "NOT CONNECTED");
}
}
}

FastScanner in;
PrintWriter out;

void run() throws IOException {
boolean oj;
try {
oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
} catch (Exception e) {
oj = System.getProperty("ONLINE_JUDGE") != null;
}

InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
in = new FastScanner(is);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
out.flush();
if (!oj){
System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
}
}

public boolean more(){
return in.hasNext();
}

public int ni(){
return in.nextInt();
}

public long nl(){
return in.nextLong();
}

public double nd(){
return in.nextDouble();
}

public String ns(){
return in.nextString();
}

public char nc(){
return in.nextChar();
}

class FastScanner {
StringTokenizer st;
boolean hasNext;

public FastScanner(InputStream is) throws IOException {
hasNext = true;
}

public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
} catch (Exception e) {
hasNext = false;
return "##";
}
}
return st.nextToken();
}

String next = null;
public boolean hasNext(){
next = nextToken();
return hasNext;
}

public int nextInt() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Integer.parseInt(more);
}

public long nextLong() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Long.parseLong(more);
}

public double nextDouble() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Double.parseDouble(more);
}

public String nextString(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more;
}

public char nextChar(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more.charAt(0);
}
}
}```

98 篇文章33 人订阅

0 条评论

## 相关文章

34911

### 数据分析之numpy

ndarray概述 创建n维数组 接收的是列表类型，所有元素类型必须相同 shape表示各维度大小的元组 dtype表示数组数据类型对象

1071

872

992

591

1826

36010

732

892

1234