-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating a new boundary force #115
Open
john-guerra
wants to merge
9
commits into
d3:main
Choose a base branch
from
john-guerra:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1fc3ae
Adding a boundary force
john-guerra 2f4ecb4
Fixing the readme
john-guerra e0790bd
Adding images
john-guerra 709f7d6
pointing images to my github
john-guerra f248604
Removing visualization of arrows
john-guerra 81fce76
Updating bl.ocks
john-guerra 53e81d4
Specifying that the paramenters must all be positive
john-guerra 5a0cfb4
Taking out the restriction of possitive values
john-guerra 7d1041e
Pulling latest version
john-guerra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import constant from "./constant"; | ||
|
||
export default function(x0, y0, x1, y1) { | ||
var strength = constant(0.1), | ||
hardBoundary = true, | ||
border = constant( Math.min((x1 - x0)/2, (y1 - y0)/2) ), | ||
nodes, | ||
strengthsX, | ||
strengthsY, | ||
x0z, x1z, | ||
y0z, y1z, | ||
borderz, | ||
halfX, halfY; | ||
|
||
|
||
if (typeof x0 !== "function") x0 = constant(x0 == null ? -100 : +x0); | ||
if (typeof x1 !== "function") x1 = constant(x1 == null ? 100 : +x1); | ||
if (typeof y0 !== "function") y0 = constant(y0 == null ? -100 : +y0); | ||
if (typeof y1 !== "function") y1 = constant(y1 == null ? 100 : +y1); | ||
|
||
function getVx(halfX, x, strengthX, border, alpha) { | ||
return (halfX - x) * Math.min(2, Math.abs( halfX - x) / halfX) * strengthX * alpha; | ||
} | ||
|
||
function force(alpha) { | ||
for (var i = 0, n = nodes.length, node; i < n; ++i) { | ||
node = nodes[i]; | ||
|
||
if ((node.x < (x0z[i] + borderz[i]) || node.x > (x1z[i] - borderz[i])) || | ||
(node.y < (y0z[i] + borderz[i]) || node.y > (y1z[i] - borderz[i])) ) { | ||
node.vx += getVx(halfX[i], node.x, strengthsX[i], borderz[i], alpha); | ||
node.vy += getVx(halfY[i], node.y, strengthsY[i], borderz[i], alpha); | ||
} else { | ||
node.vx = 0; | ||
node.vy = 0; | ||
} | ||
|
||
if (hardBoundary) { | ||
if (node.x >= x1z[i]) node.vx += x1z[i] - node.x; | ||
if (node.x <= x0z[i]) node.vx += x0z[i] - node.x; | ||
if (node.y >= y1z[i]) node.vy += y1z[i] - node.y; | ||
if (node.y <= y0z[i]) node.vy += y0z[i] - node.y; | ||
} | ||
} | ||
} | ||
|
||
function initialize() { | ||
if (!nodes) return; | ||
var i, n = nodes.length; | ||
strengthsX = new Array(n); | ||
strengthsY = new Array(n); | ||
x0z = new Array(n); | ||
y0z = new Array(n); | ||
x1z = new Array(n); | ||
y1z = new Array(n); | ||
halfY = new Array(n); | ||
halfX = new Array(n); | ||
borderz = new Array(n); | ||
|
||
for (i = 0; i < n; ++i) { | ||
strengthsX[i] = (isNaN(x0z[i] = +x0(nodes[i], i, nodes)) || | ||
isNaN(x1z[i] = +x1(nodes[i], i, nodes))) ? 0 : +strength(nodes[i], i, nodes); | ||
strengthsY[i] = (isNaN(y0z[i] = +y0(nodes[i], i, nodes)) || | ||
isNaN(y1z[i] = +y1(nodes[i], i, nodes))) ? 0 : +strength(nodes[i], i, nodes); | ||
halfX[i] = x0z[i] + (x1z[i] - x0z[i])/2, | ||
halfY[i] = y0z[i] + (y1z[i] - y0z[i])/2; | ||
borderz[i] = +border(nodes[i], i, nodes) | ||
} | ||
} | ||
|
||
force.initialize = function(_) { | ||
nodes = _; | ||
initialize(); | ||
}; | ||
|
||
force.x0 = function(_) { | ||
return arguments.length ? (x0 = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x0; | ||
}; | ||
|
||
force.x1 = function(_) { | ||
return arguments.length ? (x1 = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x1; | ||
}; | ||
|
||
force.y0 = function(_) { | ||
return arguments.length ? (y0 = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y0; | ||
}; | ||
|
||
force.y1 = function(_) { | ||
return arguments.length ? (y1 = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y1; | ||
}; | ||
|
||
force.strength = function(_) { | ||
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength; | ||
}; | ||
|
||
force.border = function(_) { | ||
return arguments.length ? (border = typeof _ === "function" ? _ : constant(+_), initialize(), force) : border; | ||
}; | ||
|
||
force.hardBoundary = function(_) { | ||
return arguments.length ? (hardBoundary = _, force) : hardBoundary; | ||
}; | ||
|
||
return force; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this and found that setting vx/vy to zero broke my simulation (i'm applying forceBoundry last).
most likely this
else
should be removedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this may be what @hornj encountered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kumavis, I'll check it out