Repository navigation
Expand file tree
/
Copy pathRectangleObject.java
More file actions
63 lines (54 loc) · 2.03 KB
/
Copy pathRectangleObject.java
File metadata and controls
63 lines (54 loc) · 2.03 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
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.Color;
public class RectangleObject extends CollisionObject {
private double width;
private double height;
private Color color;
public RectangleObject(double x, double y, double width, double height, Color color) {
super(x, y);
this.width = width;
this.height = height;
this.color = color;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
@Override
public boolean collidesWith(CollisionObject other) {
if (other instanceof RectangleObject) {
RectangleObject r = (RectangleObject) other;
return (this.getX() < r.getX() + r.width && this.getX() + width > r.getX() && this.getY() < r.getY() + r.height && this.getY() + height > r.getY());
} else {
return false;
}
}
@Override
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.fillRect((int) this.getX(), (int) this.getY(), (int) width, (int) height);
}
@Override
public double ComputeDistance(double CameraX, double CameraY) {
double distance = Double.POSITIVE_INFINITY;
Point2D[] vertices = getVertices();
for (int i = 0; i < vertices.length; i++) {
Line2D segment = new Line2D.Double(vertices[i], vertices[(i + 1) % vertices.length]);
double segmentDistance = segment.ptSegDist(CameraX, CameraY);
distance = Math.min(distance, segmentDistance);
}
return distance;
}
private Point2D[] getVertices() {
Point2D[] vertices = new Point2D[4];
vertices[0] = new Point2D.Double(this.getX(), this.getY());
vertices[1] = new Point2D.Double(this.getX() + width, this.getY());
vertices[2] = new Point2D.Double(this.getX() + width, this.getY() + height);
vertices[3] = new Point2D.Double(this.getX(), this.getY() + height);
return vertices;
}
}