Advertisement
SforzandoCF

beactr

May 2nd, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. import java.awt.Point;
  4.  
  5. public class Main {
  6. public static void main (String[] args) {
  7. ArrayList<Vector> vecs = new ArrayList<Vector>();
  8. for (String s : args)
  9. vecs.add(parseVector(s));
  10. }
  11.  
  12. public static Vector parseVector (String s) {
  13. s = s.strip();
  14. if (!s.startsWith("<") || !s.startsWith(">")) throw new IllegalArgumentException("not a vector");
  15. s = s.substring(1, s.length() - 2);
  16. String[] s2 = s.split(", ");
  17. boolean[] polar = new boolean[4];
  18. double[] val = new double[4];
  19. for (int i = 0; i < 4; i++) {
  20. String s3 = s2[i];
  21. if (s3.startsWith("m") && (i % 2) == 0) {
  22. val[i] = Double.parseDouble(s3.substring(1));
  23. polar[i] = true;
  24. }
  25. if (s3.startsWith("r") && (i % 2) == 1) {
  26. val[i] = Double.parseDouble(s3.substring(1));
  27. polar[i] = true;
  28. }
  29. if (s3.startsWith("d") && (i % 2) == 1) {
  30. val[i] = Math.toRadians(Double.parseDouble(s3.substring(1)));
  31. polar[i] = true;
  32. }
  33. if (s3.startsWith("x") && (i % 2) == 0) {
  34. val[i] = Double.parseDouble(s3.substring(1));
  35. polar[i] = false;
  36. }
  37. if (s3.startsWith("y") && (i % 2) == 1) {
  38. val[i] = Double.parseDouble(s3.substring(1));
  39. polar[i] = false;
  40. }
  41. throw new IllegalArgumentException("not a vector");
  42. }
  43. if (polar[0] && polar[1] && !(polar[2] || polar[3])) {
  44. PolarVector v = new PolarVector(val[0], val[1]);
  45. return new CartesianVector(v.getDestinationX(), v.getDestinationY(), val[2], val[3]);
  46. }
  47. if (!(polar[0] || polar[1]) && polar[2] && polar[3]) {
  48. CartesianVector v = new CartesianVector(val[0], val[1]);
  49. return new PolarVector(v.getSourceAngle(), v.getDistance(), val[2], val[3]);
  50. }
  51. if (polar[0] && polar[1] && polar[2] && polar[3])
  52. return new PolarVector(val[0], val[1], val[2], val[3]);
  53. if (!(polar[0] || polar[1] || polar[2] || polar[3]))
  54. return new CartesianVector(val[0], val[1], val[2], val[3]);
  55. throw new IllegalArgumentException("not a vector");
  56. }
  57.  
  58. public static abstract sealed class Vector permits CartesianVector, PolarVector {
  59. public static Vector add (Vector v1, Vector v2) {
  60. Vector t = v2.translate(v1.getDestinationX(), v1.getDestinationY());
  61. return new CartesianVector(0, 0, t.getDestinationX(), t.getDestinationY());
  62. }
  63.  
  64. public Vector subtract (Vector other) {
  65. return add(this, other.getInverse());
  66. }
  67.  
  68. public static Vector multiply (Vector v1, Vector v2, MultiplicationType type) {
  69. return type.multiply(v1, v2);
  70. }
  71.  
  72. public abstract double getSourceX ();
  73.  
  74. public abstract double getSourceY ();
  75.  
  76. public abstract double getDestinationX ();
  77.  
  78. public abstract double getDestinationY ();
  79.  
  80. public abstract double getSourceAngle ();
  81.  
  82. public abstract double getDistance ();
  83.  
  84. public abstract double getAngle ();
  85.  
  86. public abstract double getMagnitude ();
  87.  
  88. public abstract Vector getSourceVector ();
  89.  
  90. public abstract Vector getMagnitudeVector ();
  91.  
  92. public abstract Vector getInverse ();
  93.  
  94. public abstract Vector translate (double x, double y);
  95.  
  96. public abstract Vector applyDistanceScale (double scalar);
  97.  
  98. public abstract Vector applyMagnitudeScale (double scalar);
  99.  
  100. public abstract Vector scale (double scalar);
  101.  
  102. public abstract Vector rotate (double angle);
  103.  
  104. public abstract CartesianVector toCartesian ();
  105.  
  106. public abstract PolarVector toPolar ();
  107.  
  108. public static interface MultiplicationType {
  109. public static MultiplicationType DOT_PRODUCT = new DotProduct();
  110. public static MultiplicationType CROSS_PRODUCT = new CrossProduct();
  111.  
  112. Vector multiply (Vector v1, Vector v2);
  113.  
  114. public static class DotProduct implements MultiplicationType {
  115. public Vector multiply (Vector v1, Vector v2) {
  116. double magnitude = v1.getMagnitude() * v2.getMagnitude();
  117. return new PolarVector(magnitude, v1.getAngle());
  118. }
  119. }
  120.  
  121. public static class CrossProduct implements MultiplicationType {
  122. public Vector multiply (Vector v1, Vector v2) {
  123. Vector pv1;
  124. Vector pv2;
  125. if (v1.getAngle() < v2.getAngle()) {
  126. pv1 = v1;
  127. pv2 = v2;
  128. } else {
  129. pv1 = v2;
  130. pv2 = v1;
  131. }
  132. return new PolarVector(calcMag(v1.getMagnitudeVector(), v2.getMagnitudeVector()), ((pv2.getAngle() - pv1.getAngle()) / 2));
  133. }
  134.  
  135. private double calcMag (Vector m1, Vector m2) {
  136. return m2.rotate(-m1.getAngle()).getDestinationY() - m1.getMagnitude();
  137. }
  138. }
  139. }
  140. }
  141.  
  142. public static final class CartesianVector extends Vector {
  143. private final double x1;
  144. private final double y1;
  145.  
  146. private final double x2;
  147. private final double y2;
  148.  
  149. public CartesianVector (Point magnitude) {
  150. this(0, 0, magnitude.getX(), magnitude.getY());
  151. }
  152.  
  153. public CartesianVector (double mx, double my) {
  154. this(0, 0, mx, my);
  155. }
  156.  
  157. public CartesianVector (Point source, Point dest) {
  158. this(source.getX(), source.getY(), dest.getX(), dest.getY());
  159. }
  160.  
  161. public CartesianVector (double s1, double s2, double d1, double d2) {
  162. this.x1 = s1;
  163. this.y1 = s2;
  164.  
  165. this.x2 = d1;
  166. this.y2 = d2;
  167. }
  168.  
  169. public double getSourceX () {
  170. return this.x1;
  171. }
  172.  
  173. public double getSourceY () {
  174. return this.y1;
  175. }
  176.  
  177. public double getDestinationX () {
  178. return this.x2;
  179. }
  180.  
  181. public double getDestinationY () {
  182. return this.y2;
  183. }
  184.  
  185. public double getSourceAngle () {
  186. return Math.atan2(this.y1, this.x1);
  187. }
  188.  
  189. public double getDistance () {
  190. return Math.sqrt(this.x1 * this.x1 + this.y1 * this.y1);
  191. }
  192.  
  193. public double getAngle () {
  194. return Math.atan2(this.y2, this.x2);
  195. }
  196.  
  197. public double getMagnitude () {
  198. return Math.sqrt(this.x2 * this.x2 + this.y2 * this.y2);
  199. }
  200.  
  201. public Vector getSourceVector () {
  202. return new CartesianVector(this.x1, this.y1);
  203. }
  204.  
  205. public Vector getMagnitudeVector () {
  206. return new CartesianVector(this.x2, this.y2);
  207. }
  208.  
  209. public Vector getInverse () {
  210. Vector v = this.translate(0, 0);
  211. return new CartesianVector(this.x1, this.y1, -v.getDestinationX(), -v.getDestinationY());
  212. }
  213.  
  214. public Vector translate (double x, double y) {
  215. return new CartesianVector(this.x1 + x, this.y1 + y, this.x2 + x, this.y2 + y);
  216. }
  217.  
  218. public Vector applyDistanceScale (double scalar) {
  219. return new CartesianVector(this.x1 * scalar, this.y1 * scalar, this.x2, this.y2);
  220. }
  221.  
  222. public Vector applyMagnitudeScale (double scalar) {
  223. return new CartesianVector(this.x1, this.y1, this.x2 * scalar, this.y2 * scalar);
  224. }
  225.  
  226. public Vector scale (double scalar) {
  227. return new CartesianVector(this.x1 * scalar, this.y1 * scalar, this.x2 * scalar, this.y2 * scalar);
  228. }
  229.  
  230. public Vector rotate (double angle) {
  231. return this.toPolar().rotate(angle);
  232. }
  233.  
  234. public CartesianVector toCartesian () {
  235. return this;
  236. }
  237.  
  238. public PolarVector toPolar () {
  239. return new PolarVector(this.getDistance(), this.getSourceAngle(), this.getMagnitude(), this.getAngle());
  240. }
  241.  
  242. public String toString () {
  243. StringBuilder b = new StringBuilder();
  244. b.append("<x");
  245. b.append(this.x1);
  246. b.append(", y");
  247. b.append(this.y1);
  248. b.append(", x");
  249. b.append(this.x2);
  250. b.append(", y");
  251. b.append(this.y2);
  252. b.append(">");
  253. return b.toString();
  254. }
  255. }
  256.  
  257. public static final class PolarVector extends Vector {
  258. private final double sr;
  259. private final double st;
  260.  
  261. private final double dr;
  262. private final double dt;
  263.  
  264. public PolarVector (double r, double t) {
  265. this(0, 0, r, t);
  266. }
  267.  
  268. public PolarVector (double sr, double st, double dr, double dt) {
  269. this.sr = sr;
  270. this.st = st;
  271.  
  272. this.dr = dr;
  273. this.dt = dt;
  274. }
  275.  
  276. public double getSourceX () {
  277. return this.sr * Math.cos(this.st);
  278. }
  279.  
  280. public double getSourceY () {
  281. return this.sr * Math.sin(this.st);
  282. }
  283.  
  284. public double getDestinationX () {
  285. return this.getSourceX() + (this.dr * Math.cos(this.dt));
  286. }
  287.  
  288. public double getDestinationY () {
  289. return this.getSourceY() + (this.dr * Math.sin(this.dt));
  290. }
  291.  
  292. public double getSourceAngle () {
  293. return this.st;
  294. }
  295.  
  296. public double getDistance () {
  297. return this.sr;
  298. }
  299.  
  300. public double getAngle () {
  301. return this.dt;
  302. }
  303.  
  304. public double getMagnitude () {
  305. return this.dr;
  306. }
  307.  
  308. public Vector getSourceVector () {
  309. return new PolarVector(this.sr, this.st);
  310. }
  311.  
  312. public Vector getMagnitudeVector () {
  313. return new PolarVector(this.dr, this.dt);
  314. }
  315.  
  316. public Vector getInverse () {
  317. Vector v = this.translate(0, 0);
  318. return new PolarVector(this.sr, this.st, this.dr, this.dt + Math.PI);
  319. }
  320.  
  321. public Vector translate (double x, double y) {
  322. return new PolarVector(Math.sqrt(x * x + y * y), this.st, this.dr, this.dt);
  323. }
  324.  
  325. public Vector applyDistanceScale (double scalar) {
  326. return new PolarVector(this.sr * scalar, this.st, this.dr, this.dt);
  327. }
  328.  
  329. public Vector applyMagnitudeScale (double scalar) {
  330. return new PolarVector(this.sr, this.st, this.dr * scalar, this.dt);
  331. }
  332.  
  333. public Vector scale (double scalar) {
  334. return new PolarVector(this.sr * scalar, this.st, this.dr * scalar, this.dt);
  335. }
  336.  
  337. public Vector rotate (double angle) {
  338. return new PolarVector(this.sr, this.st, this.dr, this.dt + angle);
  339. }
  340.  
  341. public CartesianVector toCartesian () {
  342. return new CartesianVector(this.getSourceX(), this.getSourceY(), this.getDestinationX(), this.getDestinationY());
  343. }
  344.  
  345. public PolarVector toPolar () {
  346. return this;
  347. }
  348.  
  349. public String toString () {
  350. StringBuilder b = new StringBuilder();
  351. b.append("<m");
  352. b.append(this.sr);
  353. b.append(", r");
  354. b.append(this.st);
  355. b.append(", m");
  356. b.append(this.dr);
  357. b.append(", r");
  358. b.append(this.dt);
  359. b.append(">");
  360. return b.toString();
  361. }
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement