Make spliced nodes straight. Fixes #241

This commit is contained in:
Tom MacWright
2012-12-09 00:00:23 -05:00
parent a6327b566b
commit 19082bcf2a
2 changed files with 15 additions and 6 deletions

View File

@@ -38,13 +38,13 @@ iD.modes.AddRoad = function() {
} else if (datum.type === 'way') {
// begin a new way starting from an existing way
node = iD.Node({loc: map.mouseCoordinates()}),
index = iD.util.geo.chooseIndex(datum, d3.mouse(map.surface.node()), map);
choice = iD.util.geo.chooseIndex(datum, d3.mouse(map.surface.node()), map);
node = iD.Node({ loc: choice.loc }),
history.perform(
iD.actions.AddWay(way),
iD.actions.AddNode(node),
iD.actions.AddWayNode(datum.id, node.id, index),
iD.actions.AddWayNode(datum.id, node.id, choice.index),
iD.actions.AddWayNode(way.id, node.id),
'started a road');

View File

@@ -97,14 +97,23 @@ iD.util.geo.dist = function(a, b) {
};
iD.util.geo.chooseIndex = function(way, point, map) {
var dist = iD.util.geo.dist;
var projNodes = way.nodes.map(function(n) {
var dist = iD.util.geo.dist,
projNodes = way.nodes.map(function(n) {
return map.projection(n.loc);
});
for (var i = 0, changes = []; i < projNodes.length - 1; i++) {
changes[i] =
(dist(projNodes[i], point) + dist(point, projNodes[i + 1])) /
dist(projNodes[i], projNodes[i + 1]);
}
return _.indexOf(changes, _.min(changes)) + 1;
var idx = _.indexOf(changes, _.min(changes)),
ratio = dist(projNodes[idx], point) / dist(projNodes[idx], projNodes[idx + 1]),
loc = iD.util.geo.interp(way.nodes[idx].loc, way.nodes[idx + 1].loc, ratio);
return {
index: idx + 1,
loc: loc
};
};