forked from klmntv27/ulearn-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.2.java
More file actions
79 lines (62 loc) · 1.63 KB
/
4.2.java
File metadata and controls
79 lines (62 loc) · 1.63 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
public class Line {
private final Point Start;
private final Point End;
public Line(Point start, Point end) {
Start = start;
End = end;
}
public double getLength() {
return Start.getDistance(End);
}
public boolean hasPoint(Point p) {
return getLength() == p.getDistance(Start) + p.getDistance(End);
}
public Point getStartPoint() {
return Start;
}
public Point getEndPoint() {
return End;
}
public String toString() {
return Start.toString() + ", " + End.toString();
}
}
public class Point {
private final int X;
private final int Y;
public Point(int x, int y) {
X = x;
Y = y;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
public double getDistance(Point p2) {
int xDistance = Math.abs(X - p2.getX());
int yDistance = Math.abs(Y - p2.getY());
return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
}
public String toString() {
return "[" + X + ", " + Y + "]";
}
}
public class PolygonalLine {
private ArrayList<Line> lines = new ArrayList<>();
public double getLength() {
int totalLength = 0;
for(Line line : lines)
totalLength += line.getLength();
return totalLength;
}
public void addPoint(Point p) {
Point lastPoint = lines.get(lines.size() - 1).getEndPoint();
Line newLine = new Line(lastPoint, p);
lines.add(newLine);
}
public void setPoints(ArrayList<Line> lines) {
this.lines = lines;
}
}