From 7c9022252f72d58e32cd25f66e7f53aa970b2cb8 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Wed, 30 Jun 2021 18:23:49 +0200 Subject: [PATCH] Vector class --- src/main/java/de/oshgnacknak/gruphi/Node.java | 8 +-- .../java/de/oshgnacknak/gruphi/Vector.java | 64 +++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/oshgnacknak/gruphi/Vector.java diff --git a/src/main/java/de/oshgnacknak/gruphi/Node.java b/src/main/java/de/oshgnacknak/gruphi/Node.java index 9bec62d..66b47b5 100644 --- a/src/main/java/de/oshgnacknak/gruphi/Node.java +++ b/src/main/java/de/oshgnacknak/gruphi/Node.java @@ -5,23 +5,23 @@ import java.awt.geom.Point2D; class Node { - public static final double RADIUS = 10; + public static final double RADIUS = 12; public static final Color COLOR = Color.WHITE; - Point2D.Double pos; + Vector pos; double radius; Color color; Node(double x, double y) { - this.pos = new Point2D.Double(x, y); + this.pos = new Vector(x, y); this.radius = RADIUS; this.color = COLOR; } boolean inside(double x, double y) { - return pos.distance(x, y) <= radius; + return pos.dist(x, y) <= radius; } } diff --git a/src/main/java/de/oshgnacknak/gruphi/Vector.java b/src/main/java/de/oshgnacknak/gruphi/Vector.java new file mode 100644 index 0000000..560fd50 --- /dev/null +++ b/src/main/java/de/oshgnacknak/gruphi/Vector.java @@ -0,0 +1,64 @@ +package de.oshgnacknak.gruphi; + +public class Vector { + + double x, y; + + public Vector(double x, double y) { + this.x = x; + this.y = y; + } + + Vector copy() { + return new Vector(x, y); + } + + Vector add(Vector v) { + x += v.x; + y += v.y; + return this; + } + + public Vector sub(Vector v) { + x -= v.x; + y -= v.y; + return this; + } + + Vector mul(double d) { + x *= d; + y *= d; + return this; + } + + double dot(Vector v) { + return x*v.x + y*v.y; + } + + double mag() { + return Math.sqrt(dot(this)); + } + + Vector norm() { + return mul(1 / mag()); + } + + Vector setMag(double mag) { + return mul(mag/this.mag()); + } + + double dist(Vector v) { + return dist(v.x, v.y); + } + + double dist(double x, double y) { + var dx = this.x - x; + var dy = this.y - y; + return Math.sqrt(dx*dx + dy*dy); + } + + + double angle() { + return Math.atan2(y, x); + } +}