Fixed the line of best fit function

This commit is contained in:
Quinn
2023-04-06 17:09:47 -05:00
parent f12e336229
commit 1f46ab4eab
2 changed files with 20 additions and 4 deletions

View File

@@ -34,9 +34,9 @@ public class Line{
// get the mean of all the points // get the mean of all the points
Vector mean = new Vector(); Vector mean = new Vector();
for(Vector point : points){ 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 // this section calculates the direction vector of the line of best fit
Vector direction = new Vector(); Vector direction = new Vector();
@@ -59,8 +59,8 @@ public class Line{
else{ else{
this.direction = new Vector(1, direction.y/direction.x); this.direction = new Vector(1, direction.y/direction.x);
} }
this.position = mean.sub(direction.div(direction.mag()).mul(this.length / 2)); this.direction = this.direction.normalize().mul(length);
this.direction = direction.normalize(); this.position = mean.sub(this.direction.div(2));
} }
public Vector getSlopeIntForm(){ public Vector getSlopeIntForm(){

View File

@@ -94,4 +94,20 @@ class VectorTest{
assertFloatEquals(v1.angleDiff(v2.mul(100)), (float) toRadians(90)); 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);
}
} }