|
| 1 | +package files.models.shapes; |
| 2 | + |
| 3 | +public class Point { |
| 4 | + private double x, y, z; |
| 5 | + |
| 6 | + Point(double x) { |
| 7 | + this(x, 0, 0); |
| 8 | + } |
| 9 | + |
| 10 | + Point(double x, double y) { |
| 11 | + this(x, y, 0); |
| 12 | + } |
| 13 | + |
| 14 | + Point(double x, double y, double z) { |
| 15 | + this.x = x; |
| 16 | + this.y = y; |
| 17 | + this.z = z; |
| 18 | + } |
| 19 | + |
| 20 | + int calcRelation(Line2D line) { |
| 21 | + // Will return greater than 0 if point is left of line |
| 22 | + // Will return 0 if point is on the line |
| 23 | + // Will return left than 0 if point is right of line |
| 24 | + // Formula: d=(x−x1)(y2−y1)−(y−y1)(x2−x1) |
| 25 | + // we use d as result, we use x as point.getX(), we use y as point.getY(), |
| 26 | + // we use x1 as start.getX(), we use y1 as start.getY(), we use x2 as end.getX(), and we use y2 as end.getY() |
| 27 | + double result = (x - line.getStart().getX()) * (line.getEnd().getY() - line.getStart().getY()) - (y - line.getStart().getY()) * (line.getEnd().getX() - line.getStart().getX()); |
| 28 | + if (0 < result) { |
| 29 | + return -1; |
| 30 | + } else if (0 > result) { |
| 31 | + return 1; |
| 32 | + } else { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + boolean isLeftOfLine(Line2D line) { |
| 38 | + if (0 < calcRelation(line)) |
| 39 | + return true; // if the calculation is positive, the point is on the left side of the line |
| 40 | + else return false; |
| 41 | + } |
| 42 | + |
| 43 | + boolean isOnLine(Line2D line) { |
| 44 | + if (0 == calcRelation(line)) return true; // if the calculation is zero, the point is on the line |
| 45 | + else return false; |
| 46 | + } |
| 47 | + |
| 48 | + boolean isRightOfLine(Line2D line) { |
| 49 | + // Our Choice: This is the only important check, as we chose to use this continiously for collision detection in objects |
| 50 | + if (0 > calcRelation(line)) |
| 51 | + return true; // if the calculation is negative, the point is on the right side of the line |
| 52 | + else return false; |
| 53 | + } |
| 54 | + |
| 55 | + public double getX() { |
| 56 | + return x; |
| 57 | + } |
| 58 | + |
| 59 | + public double getY() { |
| 60 | + return y; |
| 61 | + } |
| 62 | +} |
0 commit comments