Skip to content

Commit

Permalink
use getYValueAt(x) (see #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
veillette committed Jul 8, 2016
1 parent 3a1c2b5 commit 4b33e51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
9 changes: 9 additions & 0 deletions js/curve-fitting/model/Curve.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ define( function( require ) {
}
},

/**
* Returns the y-value of the curve given the horizontal coordinate x
* @param {number{ x
* @returns {number}
*/
getYValueAt: function( x ) {
return this.a * Math.pow( x, 3 ) + this.b * Math.pow( x, 2 ) + this.c * ( x ) + this.d
},

// save values to storage. Necessary when switch to adjustable mode
saveValuesToStorage: function() {
this._storage.a = this.a;
Expand Down
22 changes: 11 additions & 11 deletions js/curve-fitting/view/GraphAreaNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ define( function( require ) {
var TICK_LENGTH = 7;

/**
* @param {Curve} curve model.
* @param {Curve} curve - curve model.
* @param {Property.<number>} orderOfFitProperty - Property with current order of fit.
* @param {Property.<boolean>} areResidualsVisibleProperty - Property to track residuals visibility.
* @param {Bounds2} graphModelBounds - bounds of the graph
Expand All @@ -39,7 +39,6 @@ define( function( require ) {
*/
function GraphAreaNode( curve, orderOfFitProperty, areResidualsVisibleProperty, graphModelBounds, modelViewTransform, options ) {


Node.call( this, options );

// determine the graph are bounds in the view
Expand Down Expand Up @@ -105,28 +104,29 @@ define( function( require ) {
var x;

if ( ( points.length > 1 || curve._fitTypeProperty.value === FitType.ADJUSTABLE ) && !isNaN( a ) && !isNaN( b ) && !isNaN( c ) && !isNaN( d ) ) {


// update curve path
curveShape = new Shape();
//update curve view
curveShape.moveTo( xMin, curve.getYValueAt( xMin ) );
if ( orderOfFit === 1 ) {
curveShape.moveTo( xMin, c * xMin + d );
curveShape.lineTo( xMax, c * xMax + d );
curvePath.setShape( modelViewTransform.modelToViewShape( curveShape ) );
curveShape.lineTo( xMax, curve.getYValueAt( xMax ) );
}
else {
for ( x = xMin; x < xMax; x += PLOT_STEP ) {
curveShape.moveTo( x, a * Math.pow( x, 3 ) + b * Math.pow( x, 2 ) + c * x + d );
curveShape.lineTo( x + PLOT_STEP, a * Math.pow( x + PLOT_STEP, 3 ) + b * Math.pow( x + PLOT_STEP, 2 ) + c * ( x + PLOT_STEP ) + d );
curveShape.lineTo( x + PLOT_STEP, curve.getYValueAt( x + PLOT_STEP ) );
}
curvePath.setShape( modelViewTransform.modelToViewShape( curveShape ) );
}
// update residuals
curvePath.setShape( modelViewTransform.modelToViewShape( curveShape ) );

// update path residuals
if ( areResidualsVisibleProperty.value ) {
residualsShape = new Shape();

points.forEach( function( point ) {
if ( orderOfFit ) {
residualsShape.moveToPoint( point.position );
residualsShape.verticalLineTo( a * Math.pow( point.position.x, 3 ) + b * Math.pow( point.position.x, 2 ) + c * point.position.x + d );
residualsShape.verticalLineTo( curve.getYValueAt( point.position.x ) );
}
} );
residualsPath.setShape( modelViewTransform.modelToViewShape( residualsShape ) );
Expand Down

0 comments on commit 4b33e51

Please sign in to comment.