JSFiddle - React, Tailwind, and code Playground

by 0x0539

HTML

Click and drag on the canvas to create a collision box.
<p id="overlap-call-count"></p>

JavaScript

var ShapeSet = function(){
    this.shapes = {};
    this.length = 0;
};

ShapeSet.prototype.add = function(shape){
    if(!(shape.id in this.shapes)){
        this.shapes[shape.id] = shape;
        this.length++;
        return true;
    }
    return false;
};

ShapeSet.prototype.remove = function(shape){
    if(shape.id in this.shapes){
        delete this.shapes[shape.id];
        this.length--;
        return true;
    }
    return false;
};

ShapeSet.prototype.has = function(shape){
    return shape.id in this.shapes;    
}

var Partition = function(dimension, shapeAry, shapeSet){
    this.dimension = dimension;
    
    this.shapeSet = shapeSet;
    this.shapeAry = shapeAry;
    this.lShapeSet = new ShapeSet();
    this.rShapeSet = new ShapeSet();
    
    // Explicitly declaring fields without initial values.
    this.split = null;
    this.score = null;
};

Partition.prototype.getLShapeAry = function(splitDimension){
    return splitDimension 
        ? new Ary(this.shapeAry.array, this.shapeAry.aryL, this.split)
        : this.shapeAry;
};

Partition.prototype.getRShapeAry = function(splitDimension){
    return splitDimension
        ? new Ary(this.shapeAry.array, this.split, this.shapeAry.aryR)
        : this.shapeAry;
};

Partition.prototype.update = function(){
    var lastShape = null,
        prior = null,
        score = null, 
        split = null,
        // Rightmost point of all shapes seen so far.
        F = null,
        // Number of shapes to the left if we were to split before the current shape.
        L = 0, 
        // Number of shapes to the right if we were to split before the current shape.
        R = this.shapeSet.length;
    
    // Get bounds of iteration, some subarray of this.shapeAry.
    var i1 = this.shapeAry.getAryL(),
        i2 = this.shapeAry.getAryR();
    
    for(var i = i1; i < i2; i++){
        var shape = this.shapeAry.array[i];
        
        // Check that the current shape is actually part of this partition.
  ...