Skip to content

Commit e8c18f0

Browse files
committed
Add subtraction mode
1 parent 02db56f commit e8c18f0

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/lib/polygon.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ polygon.multitester = function multitester(list) {
186186

187187
for(var i = 0; i < list.length; i++) {
188188
var tester = polygon.tester(list[i]);
189+
tester.subtract = list[i].subtract;
189190
testers.push(tester);
190191
xmin = Math.min(xmin, tester.xmin);
191192
xmax = Math.max(xmax, tester.xmax);
@@ -194,10 +195,15 @@ polygon.multitester = function multitester(list) {
194195
}
195196

196197
function contains(pt, arg) {
198+
var yes = false;
197199
for(var i = 0; i < testers.length; i++) {
198-
if(testers[i].contains(pt, arg)) return true;
200+
if(testers[i].contains(pt, arg)) {
201+
// if contained by subtract polygon - exclude the point
202+
yes = testers[i].subtract === false;
203+
}
199204
}
200-
return false;
205+
206+
return yes;
201207
}
202208

203209
return {

src/plots/cartesian/dragbox.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
173173
dragOptions.yaxes = ya;
174174

175175
// take over selection polygons from prev mode, if any
176-
if(e.shiftKey && plotinfo.selection.polygons && !dragOptions.polygons) {
176+
if((e.shiftKey || e.altKey) && plotinfo.selection.polygons && !dragOptions.polygons) {
177177
dragOptions.polygons = plotinfo.selection.polygons;
178178
dragOptions.mergedPolygons = plotinfo.selection.mergedPolygons;
179179
}
180180
// create new polygons, if shift mode
181-
else if(!e.shiftKey || (e.shiftKey && !plotinfo.selection.polygons)) {
181+
else if((!e.shiftKey && !e.altKey) || ((e.shiftKey || e.altKey) && !plotinfo.selection.polygons)) {
182182
plotinfo.selection = {};
183183
plotinfo.selection.polygons = dragOptions.polygons = [];
184-
dragOptions.mergedPolygons = plotinfo.selection.mergedPolygons = [];
184+
plotinfo.selection.mergedPolygons = dragOptions.mergedPolygons = [];
185185
}
186186

187187
prepSelect(e, startX, startY, dragOptions, dragModeNow);

src/plots/cartesian/select.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
4141
xAxisIds = dragOptions.xaxes.map(getAxId),
4242
yAxisIds = dragOptions.yaxes.map(getAxId),
4343
allAxes = dragOptions.xaxes.concat(dragOptions.yaxes),
44-
filterPoly, testPoly, mergedPolygons, currentPolygon;
44+
filterPoly, testPoly, mergedPolygons, currentPolygon,
45+
subtract = e.altKey;
4546

4647
if(mode === 'lasso') {
4748
filterPoly = filteredPolygon([[x0, y0]], constants.BENDPX);
@@ -193,7 +194,8 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
193194

194195
// create outline & tester
195196
if(dragOptions.polygons && dragOptions.polygons.length) {
196-
mergedPolygons = joinPolygons(dragOptions.mergedPolygons, currentPolygon);
197+
mergedPolygons = mergePolygons(dragOptions.mergedPolygons, currentPolygon, subtract);
198+
currentPolygon.subtract = subtract;
197199
testPoly = multipolygonTester(dragOptions.polygons.concat([currentPolygon]));
198200
}
199201
else {
@@ -262,6 +264,7 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
262264

263265
if(currentPolygon && dragOptions.polygons) {
264266
// save last polygons
267+
currentPolygon.subtract = subtract;
265268
dragOptions.polygons.push(currentPolygon);
266269

267270
// we have to keep reference to arrays container
@@ -272,8 +275,22 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
272275
};
273276
};
274277

275-
function joinPolygons(list, poly) {
276-
var res = polybool.union({
278+
function mergePolygons(list, poly, subtract) {
279+
var res;
280+
281+
if(subtract) {
282+
res = polybool.difference({
283+
regions: list,
284+
inverted: false
285+
}, {
286+
regions: [poly],
287+
inverted: false
288+
});
289+
290+
return res.regions;
291+
}
292+
293+
res = polybool.union({
277294
regions: list,
278295
inverted: false
279296
}, {

0 commit comments

Comments
 (0)