Skip to content

Commit

Permalink
Rotate away from other vertices, see #117
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Jun 3, 2016
1 parent 3e0f18f commit 2c5281d
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions js/common/model/Circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,46 @@ define( function( require ) {
.concat( this.resistors.getArray() );
},

// TODO: Rotate away from other vertices, not toward them.
// Rotate away from other vertices, not toward them.
rotateSingleVertex: function( vertex, pivotVertex ) {
var searchAngle = Math.PI / 4;
this.rotateSingleVertexByAngle( vertex, pivotVertex, searchAngle );
var distance1 = this.closestDistanceToOtherVertex( vertex );
this.rotateSingleVertexByAngle( vertex, pivotVertex, -2 * searchAngle );
var distance2 = this.closestDistanceToOtherVertex( vertex );
if ( distance2 > distance1 ) {
// keep it, we're good.
}
else {

// go back to the best spot
this.rotateSingleVertexByAngle( vertex, pivotVertex, 2 * searchAngle );
}
},

rotateSingleVertexByAngle: function( vertex, pivotVertex, deltaAngle ) {
var distanceFromVertex = vertex.position.distance( pivotVertex.position );
var angle = vertex.position.minus( pivotVertex.position ).angle();
var deltaAngle = Math.PI / 4;

var newPosition = pivotVertex.position.plus( Vector2.createPolar( distanceFromVertex, angle + deltaAngle ) );
vertex.unsnappedPosition = newPosition;
vertex.position = newPosition;
},

closestDistanceToOtherVertex: function( vertex ) {
var closestDistance = null;
for ( var i = 0; i < this.vertices.length; i++ ) {
var v = this.vertices.get( i );
if ( v !== vertex ) {
var distance = v.position.distance( vertex.position );
if ( closestDistance === null || distance < closestDistance ) {
closestDistance = distance;
}
}
}
return closestDistance;
},

clear: function() {
this.selectedCircuitElementProperty.reset();

Expand Down

0 comments on commit 2c5281d

Please sign in to comment.