Got rid of the distance variable in Line. now just using direction's length.
This commit is contained in:
@@ -7,18 +7,10 @@ import static processing.core.PApplet.*;
|
|||||||
public class Line{
|
public class Line{
|
||||||
private Vector direction = new Vector(0,0);
|
private Vector direction = new Vector(0,0);
|
||||||
private Vector position = new Vector(0,0);
|
private Vector position = new Vector(0,0);
|
||||||
private float length = 0;
|
|
||||||
|
|
||||||
Line(Vector startPosition, Vector endPosition){
|
Line(Vector startPosition, Vector endPosition){
|
||||||
direction = endPosition.sub(startPosition);
|
direction = endPosition.sub(startPosition);
|
||||||
position = startPosition;
|
position = startPosition;
|
||||||
length = direction.mag();
|
|
||||||
direction = direction.normalize();
|
|
||||||
}
|
|
||||||
Line(Vector direction, Vector position, float lineLength){
|
|
||||||
this.direction = direction.normalize();
|
|
||||||
this.position = position;
|
|
||||||
this.length = lineLength;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,7 +32,7 @@ public class Line{
|
|||||||
|
|
||||||
// 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();
|
||||||
|
float length = 1;
|
||||||
// get the rise and run of the line of best fit
|
// get the rise and run of the line of best fit
|
||||||
for(Vector point : points){
|
for(Vector point : points){
|
||||||
direction.y += (point.x - mean.x)*(point.y - mean.y); // rise
|
direction.y += (point.x - mean.x)*(point.y - mean.y); // rise
|
||||||
@@ -48,8 +40,8 @@ public class Line{
|
|||||||
|
|
||||||
// find the point that's furthest from the mean and use it to set the line length.
|
// find the point that's furthest from the mean and use it to set the line length.
|
||||||
float dist = abs(point.sub(mean).mag());
|
float dist = abs(point.sub(mean).mag());
|
||||||
if(dist > this.length){
|
if(dist > length){
|
||||||
this.length = 2*dist;
|
length = 2*dist;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -78,11 +70,11 @@ public class Line{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public float getLength(){
|
public float getLength(){
|
||||||
return length;
|
return direction.mag();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(PApplet screen){
|
public void draw(PApplet screen){
|
||||||
Vector endPoint = this.position.add(this.direction.mul(this.length));
|
Vector endPoint = this.position.add(this.direction);
|
||||||
screen.line(position.x, position.y, endPoint.x, endPoint.y);
|
screen.line(position.x, position.y, endPoint.x, endPoint.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user