diff --git a/src/Line.java b/src/Line.java index d573874..94f9ad2 100644 --- a/src/Line.java +++ b/src/Line.java @@ -34,9 +34,9 @@ public class Line{ // get the mean of all the points Vector mean = new Vector(); for(Vector point : points){ - mean.add(point); + mean = mean.add(point); } - mean.div(points.size()); + mean = mean.div(points.size()); // this section calculates the direction vector of the line of best fit Vector direction = new Vector(); @@ -59,8 +59,8 @@ public class Line{ else{ this.direction = new Vector(1, direction.y/direction.x); } - this.position = mean.sub(direction.div(direction.mag()).mul(this.length / 2)); - this.direction = direction.normalize(); + this.direction = this.direction.normalize().mul(length); + this.position = mean.sub(this.direction.div(2)); } public Vector getSlopeIntForm(){ diff --git a/tests/VectorTest.java b/tests/VectorTest.java index 15243f6..e677370 100644 --- a/tests/VectorTest.java +++ b/tests/VectorTest.java @@ -94,4 +94,20 @@ class VectorTest{ assertFloatEquals(v1.angleDiff(v2.mul(100)), (float) toRadians(90)); } + @Test + public void testNormalize(){ + Vector v1 = new Vector(0, 10); + assertFloatEquals(0, v1.normalize().x); + assertFloatEquals(1, v1.normalize().y); + + Vector v2 = new Vector(3, 0); + assertFloatEquals(1, v2.normalize().x); + assertFloatEquals(0, v2.normalize().y); + + Vector v3 = new Vector(-1, 0); + assertFloatEquals(-1, v3.normalize().x); + assertFloatEquals(0, v3.normalize().y); + + } + } \ No newline at end of file