3a5444d33c163459f721731a0dfd6b491fd27f66
1 package com
.hexidec
.util
;
8 public Cartesian(int x
, int y
)
14 public Cartesian(Cartesian cartesian
)
16 this.x
= cartesian
.getX();
17 this.y
= cartesian
.getY();
25 public int getX() { return x
; }
26 public int getY() { return y
; }
28 public void setX(int i
) { x
= i
; }
29 public void setY(int i
) { y
= i
; }
31 public void mirror(Cartesian cpSource
)
33 this.setX(cpSource
.getX());
34 this.setY(cpSource
.getY());
37 public void add(Cartesian cpAdd
)
39 this.setX(this.getX() + cpAdd
.getX());
40 this.setY(this.getY() + cpAdd
.getY());
43 public void subtract(Cartesian cpSub
)
45 this.setX(this.getX() - cpSub
.getX());
46 this.setY(this.getY() - cpSub
.getY());
49 public void scale(int magnitude
)
51 this.setX(this.getX() * magnitude
);
52 this.setY(this.getY() * magnitude
);
55 public Cartesian
getScaledInstance(int magnitude
)
57 Cartesian cpNew
= this.dupe();
58 cpNew
.scale(magnitude
);
62 public Cartesian
getScaledInstance(Cartesian cpMagnitude
)
64 Cartesian cpNew
= this.dupe();
65 cpNew
.setX(cpNew
.getX() * cpMagnitude
.getX());
66 cpNew
.setY(cpNew
.getY() * cpMagnitude
.getY());
70 public Cartesian
addToLimit(Cartesian cpBase
, Cartesian cpMin
, Cartesian cpMax
)
72 Cartesian cpNew
= this.dupe();
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()); }
81 public Cartesian
translate(Cartesian cpOffset
, boolean unknown
)
83 Cartesian cpNew
= this.dupe();
88 public Cartesian
translate(Cartesian cpOffset
)
90 return translate(cpOffset
, false
);
93 public double distance(Cartesian cpTarget
)
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
));
101 public Cartesian
midpoint(Cartesian cpTarget
)
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
);
109 public double slope(Cartesian cpTarget
)
111 // Formula -> (y1 - y2) / (x1 - x2)
112 int xDiff
= this.getX() - cpTarget
.getX();
113 int yDiff
= this.getY() - cpTarget
.getY();
114 return yDiff
/ xDiff
;
117 public Cartesian
dupe()
119 return new Cartesian(this);
122 public boolean equals(Cartesian cpCompare
)
124 return ((this.getX() == cpCompare
.getX()) && (this.getY() == cpCompare
.getY()));
127 public String
toString()
129 return "(" + this.getX() + "," + this.getY() + ")";
132 public String
toKey()
134 return "X" + this.getX() + "Y" + this.getY();