Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.awt.Point;
- public class Main {
- public static void main (String[] args) {
- ArrayList<Vector> vecs = new ArrayList<Vector>();
- for (String s : args)
- vecs.add(parseVector(s));
- }
- public static Vector parseVector (String s) {
- s = s.strip();
- if (!s.startsWith("<") || !s.startsWith(">")) throw new IllegalArgumentException("not a vector");
- s = s.substring(1, s.length() - 2);
- String[] s2 = s.split(", ");
- boolean[] polar = new boolean[4];
- double[] val = new double[4];
- for (int i = 0; i < 4; i++) {
- String s3 = s2[i];
- if (s3.startsWith("m") && (i % 2) == 0) {
- val[i] = Double.parseDouble(s3.substring(1));
- polar[i] = true;
- }
- if (s3.startsWith("r") && (i % 2) == 1) {
- val[i] = Double.parseDouble(s3.substring(1));
- polar[i] = true;
- }
- if (s3.startsWith("d") && (i % 2) == 1) {
- val[i] = Math.toRadians(Double.parseDouble(s3.substring(1)));
- polar[i] = true;
- }
- if (s3.startsWith("x") && (i % 2) == 0) {
- val[i] = Double.parseDouble(s3.substring(1));
- polar[i] = false;
- }
- if (s3.startsWith("y") && (i % 2) == 1) {
- val[i] = Double.parseDouble(s3.substring(1));
- polar[i] = false;
- }
- throw new IllegalArgumentException("not a vector");
- }
- if (polar[0] && polar[1] && !(polar[2] || polar[3])) {
- PolarVector v = new PolarVector(val[0], val[1]);
- return new CartesianVector(v.getDestinationX(), v.getDestinationY(), val[2], val[3]);
- }
- if (!(polar[0] || polar[1]) && polar[2] && polar[3]) {
- CartesianVector v = new CartesianVector(val[0], val[1]);
- return new PolarVector(v.getSourceAngle(), v.getDistance(), val[2], val[3]);
- }
- if (polar[0] && polar[1] && polar[2] && polar[3])
- return new PolarVector(val[0], val[1], val[2], val[3]);
- if (!(polar[0] || polar[1] || polar[2] || polar[3]))
- return new CartesianVector(val[0], val[1], val[2], val[3]);
- throw new IllegalArgumentException("not a vector");
- }
- public static abstract sealed class Vector permits CartesianVector, PolarVector {
- public static Vector add (Vector v1, Vector v2) {
- Vector t = v2.translate(v1.getDestinationX(), v1.getDestinationY());
- return new CartesianVector(0, 0, t.getDestinationX(), t.getDestinationY());
- }
- public Vector subtract (Vector other) {
- return add(this, other.getInverse());
- }
- public static Vector multiply (Vector v1, Vector v2, MultiplicationType type) {
- return type.multiply(v1, v2);
- }
- public abstract double getSourceX ();
- public abstract double getSourceY ();
- public abstract double getDestinationX ();
- public abstract double getDestinationY ();
- public abstract double getSourceAngle ();
- public abstract double getDistance ();
- public abstract double getAngle ();
- public abstract double getMagnitude ();
- public abstract Vector getSourceVector ();
- public abstract Vector getMagnitudeVector ();
- public abstract Vector getInverse ();
- public abstract Vector translate (double x, double y);
- public abstract Vector applyDistanceScale (double scalar);
- public abstract Vector applyMagnitudeScale (double scalar);
- public abstract Vector scale (double scalar);
- public abstract Vector rotate (double angle);
- public abstract CartesianVector toCartesian ();
- public abstract PolarVector toPolar ();
- public static interface MultiplicationType {
- public static MultiplicationType DOT_PRODUCT = new DotProduct();
- public static MultiplicationType CROSS_PRODUCT = new CrossProduct();
- Vector multiply (Vector v1, Vector v2);
- public static class DotProduct implements MultiplicationType {
- public Vector multiply (Vector v1, Vector v2) {
- double magnitude = v1.getMagnitude() * v2.getMagnitude();
- return new PolarVector(magnitude, v1.getAngle());
- }
- }
- public static class CrossProduct implements MultiplicationType {
- public Vector multiply (Vector v1, Vector v2) {
- Vector pv1;
- Vector pv2;
- if (v1.getAngle() < v2.getAngle()) {
- pv1 = v1;
- pv2 = v2;
- } else {
- pv1 = v2;
- pv2 = v1;
- }
- return new PolarVector(calcMag(v1.getMagnitudeVector(), v2.getMagnitudeVector()), ((pv2.getAngle() - pv1.getAngle()) / 2));
- }
- private double calcMag (Vector m1, Vector m2) {
- return m2.rotate(-m1.getAngle()).getDestinationY() - m1.getMagnitude();
- }
- }
- }
- }
- public static final class CartesianVector extends Vector {
- private final double x1;
- private final double y1;
- private final double x2;
- private final double y2;
- public CartesianVector (Point magnitude) {
- this(0, 0, magnitude.getX(), magnitude.getY());
- }
- public CartesianVector (double mx, double my) {
- this(0, 0, mx, my);
- }
- public CartesianVector (Point source, Point dest) {
- this(source.getX(), source.getY(), dest.getX(), dest.getY());
- }
- public CartesianVector (double s1, double s2, double d1, double d2) {
- this.x1 = s1;
- this.y1 = s2;
- this.x2 = d1;
- this.y2 = d2;
- }
- public double getSourceX () {
- return this.x1;
- }
- public double getSourceY () {
- return this.y1;
- }
- public double getDestinationX () {
- return this.x2;
- }
- public double getDestinationY () {
- return this.y2;
- }
- public double getSourceAngle () {
- return Math.atan2(this.y1, this.x1);
- }
- public double getDistance () {
- return Math.sqrt(this.x1 * this.x1 + this.y1 * this.y1);
- }
- public double getAngle () {
- return Math.atan2(this.y2, this.x2);
- }
- public double getMagnitude () {
- return Math.sqrt(this.x2 * this.x2 + this.y2 * this.y2);
- }
- public Vector getSourceVector () {
- return new CartesianVector(this.x1, this.y1);
- }
- public Vector getMagnitudeVector () {
- return new CartesianVector(this.x2, this.y2);
- }
- public Vector getInverse () {
- Vector v = this.translate(0, 0);
- return new CartesianVector(this.x1, this.y1, -v.getDestinationX(), -v.getDestinationY());
- }
- public Vector translate (double x, double y) {
- return new CartesianVector(this.x1 + x, this.y1 + y, this.x2 + x, this.y2 + y);
- }
- public Vector applyDistanceScale (double scalar) {
- return new CartesianVector(this.x1 * scalar, this.y1 * scalar, this.x2, this.y2);
- }
- public Vector applyMagnitudeScale (double scalar) {
- return new CartesianVector(this.x1, this.y1, this.x2 * scalar, this.y2 * scalar);
- }
- public Vector scale (double scalar) {
- return new CartesianVector(this.x1 * scalar, this.y1 * scalar, this.x2 * scalar, this.y2 * scalar);
- }
- public Vector rotate (double angle) {
- return this.toPolar().rotate(angle);
- }
- public CartesianVector toCartesian () {
- return this;
- }
- public PolarVector toPolar () {
- return new PolarVector(this.getDistance(), this.getSourceAngle(), this.getMagnitude(), this.getAngle());
- }
- public String toString () {
- StringBuilder b = new StringBuilder();
- b.append("<x");
- b.append(this.x1);
- b.append(", y");
- b.append(this.y1);
- b.append(", x");
- b.append(this.x2);
- b.append(", y");
- b.append(this.y2);
- b.append(">");
- return b.toString();
- }
- }
- public static final class PolarVector extends Vector {
- private final double sr;
- private final double st;
- private final double dr;
- private final double dt;
- public PolarVector (double r, double t) {
- this(0, 0, r, t);
- }
- public PolarVector (double sr, double st, double dr, double dt) {
- this.sr = sr;
- this.st = st;
- this.dr = dr;
- this.dt = dt;
- }
- public double getSourceX () {
- return this.sr * Math.cos(this.st);
- }
- public double getSourceY () {
- return this.sr * Math.sin(this.st);
- }
- public double getDestinationX () {
- return this.getSourceX() + (this.dr * Math.cos(this.dt));
- }
- public double getDestinationY () {
- return this.getSourceY() + (this.dr * Math.sin(this.dt));
- }
- public double getSourceAngle () {
- return this.st;
- }
- public double getDistance () {
- return this.sr;
- }
- public double getAngle () {
- return this.dt;
- }
- public double getMagnitude () {
- return this.dr;
- }
- public Vector getSourceVector () {
- return new PolarVector(this.sr, this.st);
- }
- public Vector getMagnitudeVector () {
- return new PolarVector(this.dr, this.dt);
- }
- public Vector getInverse () {
- Vector v = this.translate(0, 0);
- return new PolarVector(this.sr, this.st, this.dr, this.dt + Math.PI);
- }
- public Vector translate (double x, double y) {
- return new PolarVector(Math.sqrt(x * x + y * y), this.st, this.dr, this.dt);
- }
- public Vector applyDistanceScale (double scalar) {
- return new PolarVector(this.sr * scalar, this.st, this.dr, this.dt);
- }
- public Vector applyMagnitudeScale (double scalar) {
- return new PolarVector(this.sr, this.st, this.dr * scalar, this.dt);
- }
- public Vector scale (double scalar) {
- return new PolarVector(this.sr * scalar, this.st, this.dr * scalar, this.dt);
- }
- public Vector rotate (double angle) {
- return new PolarVector(this.sr, this.st, this.dr, this.dt + angle);
- }
- public CartesianVector toCartesian () {
- return new CartesianVector(this.getSourceX(), this.getSourceY(), this.getDestinationX(), this.getDestinationY());
- }
- public PolarVector toPolar () {
- return this;
- }
- public String toString () {
- StringBuilder b = new StringBuilder();
- b.append("<m");
- b.append(this.sr);
- b.append(", r");
- b.append(this.st);
- b.append(", m");
- b.append(this.dr);
- b.append(", r");
- b.append(this.dt);
- b.append(">");
- return b.toString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement