Skip to content

Commit

Permalink
Track initial position for restoring to bucket, see #93
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Dec 20, 2024
1 parent 41ca95c commit 0ef7b62
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions js/least-squares-regression/model/LeastSquaresRegressionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,19 @@ export default class LeastSquaresRegressionModel {
invertY: true
} );

let savedCustomDataPoints: Vector2[] = [];
let savedCustomDataPoints: Array<{ currentPoint: Vector2; initialPoint: Vector2 }> = [];

// What to do when the selected Data Set changes. no need to unlink, present for the lifetime of the sim
this.selectedDataSetProperty.link( ( selectedDataSet, oldSelectedDataSet ) => {

// saved the position data of CUSTOM if we are going from CUSTOM to another dataSet
if ( oldSelectedDataSet && oldSelectedDataSet === DataSet.CUSTOM ) {
savedCustomDataPoints = this.graph.dataPointsOnGraph.map( dataPoint => dataPoint.positionProperty.value.copy() );
savedCustomDataPoints = this.graph.dataPointsOnGraph.map( dataPoint => {
return {
initialPoint: dataPoint.positionProperty.initialValue.copy(),
currentPoint: dataPoint.positionProperty.value.copy()
};
} );
}

// unlink the listeners to dataPoints
Expand All @@ -115,7 +120,9 @@ export default class LeastSquaresRegressionModel {

// use the savedCustomDataPoints to populate the dataPoints array
savedCustomDataPoints.forEach( dataPoint => {
this.dataPoints.push( new DataPoint( dataPoint.copy() ) );
const newDataPoint = new DataPoint( dataPoint.initialPoint.copy() );
newDataPoint.positionProperty.value = dataPoint.currentPoint.copy();
this.dataPoints.push( newDataPoint );
} );

this.dataPoints.forEach( dataPoint => {
Expand Down

0 comments on commit 0ef7b62

Please sign in to comment.