37 lines
719 B
Java
37 lines
719 B
Java
package de.ph87.kindermalen.drawing;
|
|
|
|
import java.awt.*;
|
|
|
|
public class Vector {
|
|
|
|
public final double x;
|
|
|
|
public final double y;
|
|
|
|
public final double length;
|
|
|
|
public Vector(final Point point) {
|
|
this(point.x, point.y);
|
|
}
|
|
|
|
public Vector(final Vector start, final Point end) {
|
|
this(end.x - start.x, end.y - start.y);
|
|
}
|
|
|
|
public Vector(final double x, final double y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.length = Math.sqrt(x * x + y * y);
|
|
}
|
|
|
|
public Vector withLength(final double newLength) {
|
|
final double factor = newLength / length;
|
|
return new Vector(x / factor, y / factor);
|
|
}
|
|
|
|
public Vector plus(final Vector other) {
|
|
return new Vector(x + other.x, y + other.y);
|
|
}
|
|
|
|
}
|