-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountIntersection.java
More file actions
94 lines (82 loc) · 2.55 KB
/
Copy pathCountIntersection.java
File metadata and controls
94 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package basic;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class CountIntersection {
static class Point {
public final long x;
public final long y;
public Point(long x, long y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
"}";
}
}
public static Point createPoint(int arr1[], int arr2[]) {
long divide = ((long)arr1[0] * (long)arr2[1] - (long)arr1[1] * (long)arr2[0]);
double x = (double) ((long)arr1[1] * (long)arr2[2] - (long)arr1[2] * (long)arr2[1]) / divide;
double y = (double) ((long)arr1[2] * (long)arr2[0] - (long)arr1[0] * (long)arr2[2]) / divide;
if (x % 1 != 0 || y % 1 != 0)
return null;
return new Point((long)x, (long) y);
}
public static String[] solution(int[][] line) {
List<Point> points = new ArrayList<>();
// when
for (int i = 0; i < line.length; i++) {
for (int j = i + 1; j < line.length; j++) {
Point p = createPoint(line[i], line[j]);
if (p != null) {
points.add(p);
}
}
}
// long maxX = points.get(0).x;
long maxX = Long.MIN_VALUE;
long minX = Long.MAX_VALUE;
long maxY = Long.MIN_VALUE;
long minY = Long.MAX_VALUE;
for (Point p : points) {
long px = p.x;
long py = p.y;
if(px > maxX)
maxX = px;
if(px < minX)
minX = px;
if(py > maxY)
maxY = py;
if(py < minY)
minY = py;
}
int width = (int) (maxX - minX + 1);
int height = (int) (maxY - minY + 1);
char[][] printArr = new char[height][width];
for (char[] row : printArr) {
Arrays.fill(row, '.');
}
/*
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printArr[i][j] = '.';
}
}
*/
for (Point point : points) {
int x = (int) (point.x - minX);
int y = (int) (maxY - point.y);
printArr[y][x] = '*';
}
// then
String[] result = new String[height];
for (int i = 0; i < result.length; i++) {
result[i] = new String(printArr[i]);
}
return result;
}
}