Commit | Line | Data |
---|---|---|
c2da4d40 JL |
1 | package com.hexidec.util; |
2 | ||
3 | public class Cartesian | |
4 | { | |
5 | private int x; | |
6 | private int y; | |
7 | ||
8 | public Cartesian(int x, int y) | |
9 | { | |
10 | this.x = x; | |
11 | this.y = y; | |
12 | } | |
13 | ||
14 | public Cartesian(Cartesian cartesian) | |
15 | { | |
16 | this.x = cartesian.getX(); | |
17 | this.y = cartesian.getY(); | |
18 | } | |
19 | ||
20 | public Cartesian() | |
21 | { | |
22 | this(0, 0); | |
23 | } | |
24 | ||
25 | public int getX() { return x; } | |
26 | public int getY() { return y; } | |
27 | ||
28 | public void setX(int i) { x = i; } | |
29 | public void setY(int i) { y = i; } | |
30 | ||
31 | public void mirror(Cartesian cpSource) | |
32 | { | |
33 | this.setX(cpSource.getX()); | |
34 | this.setY(cpSource.getY()); | |
35 | } | |
36 | ||
37 | public void add(Cartesian cpAdd) | |
38 | { | |
39 | this.setX(this.getX() + cpAdd.getX()); | |
40 | this.setY(this.getY() + cpAdd.getY()); | |
41 | } | |
42 | ||
43 | public void subtract(Cartesian cpSub) | |
44 | { | |
45 | this.setX(this.getX() - cpSub.getX()); | |
46 | this.setY(this.getY() - cpSub.getY()); | |
47 | } | |
48 | ||
49 | public void scale(int magnitude) | |
50 | { | |
51 | this.setX(this.getX() * magnitude); | |
52 | this.setY(this.getY() * magnitude); | |
53 | } | |
54 | ||
55 | public Cartesian getScaledInstance(int magnitude) | |
56 | { | |
57 | Cartesian cpNew = this.dupe(); | |
58 | cpNew.scale(magnitude); | |
59 | return cpNew; | |
60 | } | |
61 | ||
62 | public Cartesian getScaledInstance(Cartesian cpMagnitude) | |
63 | { | |
64 | Cartesian cpNew = this.dupe(); | |
65 | cpNew.setX(cpNew.getX() * cpMagnitude.getX()); | |
66 | cpNew.setY(cpNew.getY() * cpMagnitude.getY()); | |
67 | return cpNew; | |
68 | } | |
69 | ||
70 | public Cartesian addToLimit(Cartesian cpBase, Cartesian cpMin, Cartesian cpMax) | |
71 | { | |
72 | Cartesian cpNew = this.dupe(); | |
73 | cpNew.add(cpBase); | |
74 | if(cpNew.getX() < cpMin.getX()) { cpNew.setX(cpMin.getX()); } | |
75 | if(cpNew.getY() < cpMin.getY()) { cpNew.setY(cpMin.getY()); } | |
76 | if(cpNew.getX() > cpMax.getX()) { cpNew.setX(cpMax.getX()); } | |
77 | if(cpNew.getY() > cpMax.getY()) { cpNew.setY(cpMax.getY()); } | |
78 | return cpNew; | |
79 | } | |
80 | ||
81 | public Cartesian translate(Cartesian cpOffset, boolean unknown) | |
82 | { | |
83 | Cartesian cpNew = this.dupe(); | |
84 | cpNew.add(cpOffset); | |
85 | return cpNew; | |
86 | } | |
87 | ||
88 | public Cartesian translate(Cartesian cpOffset) | |
89 | { | |
90 | return translate(cpOffset, false); | |
91 | } | |
92 | ||
93 | public double distance(Cartesian cpTarget) | |
94 | { | |
95 | // Formula -> SQRT(SQR(x2 - x1) + SQR(y2 - y1)) | |
96 | int xDiff = cpTarget.getX() - this.getX(); | |
97 | int yDiff = cpTarget.getY() - this.getY(); | |
98 | return Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)); | |
99 | } | |
100 | ||
101 | public Cartesian midpoint(Cartesian cpTarget) | |
102 | { | |
103 | // Formula -> (x1 + x2) / 2, (y1 + y2) / 2 | |
104 | int xMid = (this.getX() + cpTarget.getX()) / 2; | |
105 | int yMid = (this.getY() + cpTarget.getY()) / 2; | |
106 | return new Cartesian(xMid, yMid); | |
107 | } | |
108 | ||
109 | public double slope(Cartesian cpTarget) | |
110 | { | |
111 | // Formula -> (y1 - y2) / (x1 - x2) | |
112 | int xDiff = this.getX() - cpTarget.getX(); | |
113 | int yDiff = this.getY() - cpTarget.getY(); | |
114 | return yDiff / xDiff; | |
115 | } | |
116 | ||
117 | public Cartesian dupe() | |
118 | { | |
119 | return new Cartesian(this); | |
120 | } | |
121 | ||
122 | public boolean equals(Cartesian cpCompare) | |
123 | { | |
124 | return ((this.getX() == cpCompare.getX()) && (this.getY() == cpCompare.getY())); | |
125 | } | |
126 | ||
127 | public String toString() | |
128 | { | |
129 | return "(" + this.getX() + "," + this.getY() + ")"; | |
130 | } | |
131 | ||
132 | public String toKey() | |
133 | { | |
134 | return "X" + this.getX() + "Y" + this.getY(); | |
135 | } | |
136 | ||
137 | } |