import 'dart:math' as math;

class DVector {
  double x = 0;
  double y = 0;

  DVector(this.x, this.y);

  double get length => math.sqrt(lengthSquared);

  double get lengthSquared => (math.pow(x, 2) + math.pow(y, 2)).toDouble();

  static DVector parse(String source) {
    var splits = source.split(',');
    if (splits.length != 2) {
      throw ArgumentError("Parse $source to DVector failed");
    }
    return DVector(double.parse(splits[0]), double.parse(splits[1]));
  }

  static double angleBetween(DVector vector1, DVector vector2) {
    double y = (vector1.x * vector2.y) - (vector2.x * vector1.y);
    double x = (vector1.x * vector2.x) + (vector1.y * vector2.y);
    return (math.atan2(y, x) * 57.295779513082323);
  }

  @override
  bool operator ==(Object other) {
    return other is DVector && x == other.x && y == other.y;
  }

  DVector operator *(double scalar) {
    return DVector(x * scalar, y * scalar);
  }

  DVector operator /(double scalar) {
    return this * (1.0 / scalar);
  }

  @override
  int get hashCode => x.hashCode ^ y.hashCode;

  @override
  String toString() => '$x,$y';
}
