Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></app>
              
            
!

CSS

              
                html, body
  height: 100%
  
svg
  border: 1px solid black
  background: white
  
              
            
!

JS

              
                class Rectangle {
  constructor(x, y, width, height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }

  get right() {
    return this.x + this.width;
  }

  get top() {
    return this.y + this.height;
  }

  get bottom() {
    return this.y;
  }

  get left() {
    return this.x;
  }

  get midPoint() {
    return {
      x: this.x + this.width / 2,
      y: this.y + this.height / 2
    };
  }

  overlaps(rect) {
    return (
      this.left < rect.right &&
      this.right > rect.left &&
      this.top > rect.bottom &&
      this.bottom < rect.top
    );
  }

  containsRectangle(rect) {
    return (
      this.left <= rect.left &&
      this.right >= rect.right &&
      this.top >= rect.top &&
      this.bottom <= rect.bottom
    );
  }

  containsPoint(x, y) {
    return !(
      x < this.left ||
      y > this.top ||
      x > this.right ||
      y < this.bottom
    );
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.count = 0;
  }

  get length() {
    return this.count;
  }

  clear() {
    this.count = 0;
    this.head = null;
    this.tail = null;
  }

  add(data) {
    const node = {
      data: data,
      next: null
    };

    if (this.count === 0) {
      this.tail = node;
    } else {
      this.head.next = node;
    }
    this.head = node;
    this.count++;
  }
  
  // todo: just redirect the list
  addAll(list) {
    let curNode = list.tail;
    while (curnode != null) {
      this.add(curNode.data);
      curNode = curNode.next;
    }
  }
  
  get(target) {
    let curNode = this.tail;
    for (let i = 0; i < target; i++) {
      curNode = curNode.next;
    }
    return curNode.data;
  }

  remove(target) {
    this.count--;
    let prevNode = null;
    let deletedNode = this.tail;
    
    if (target === 0) {
      deletedNode = this.tail;
      this.tail = this.tail.next || null;
    } else {    
      for (let i = 0; i < target; i++) {
        prevNode = deletedNode;
        deletedNode = deletedNode.next;
      } 

      if (target === this.count) {
          this.head = prevNode;
      }
    }
  
    if (this.count === 0) {
      this.tail = null;
      this.head = null;
    }

    
    return deletedNode.data;
  }
}

class QuadTree {
  constructor(level, bounds) {
    this.level = level;
    this.bounds = bounds;
    this.maxLevels = 5;
    this.maxObjects = 5;
    this.objects = new LinkedList();
    this.nodes = new Array(4).fill(null);
  }

  insert(rect) {
    if (this.nodes[0] !== null) {
      let index = this._getQuadrantIndex(rect);
      console.log('placed into: ', index)
      if (index != -1) {
        this.nodes[index].insert(rect);
      }
    }

    this.objects.add(rect);

    if (this.objects.length > this.maxObjects && this.level < this.maxLevels) {
      if (this.nodes[0] === null) {
        this.split();
        console.log('split')
      }
      
      let i = 0;
      while (i < this.objects.length) {
        let index = this._getQuadrantIndex(this.objects.get(i));
        if (index != -1) {
          console.log('placed into: ', index)
          this.nodes[index].insert(this.objects.remove(i));
        } else {
          i++;
        }
      }
    }
  }
  
  retreive(objectList, rect) {
    let index = getIndex(rect);
    if (index != -1 && nodes[0] != null) {
      nodes[index].retrieve(objects, rect);
    }
    
    objectList.addAll(this.objects);
    
    return objectList;
  }

  split() {
    const quadrant = this._getQuadrants();    
    const level = this.level;
    this.nodes[0] = new QuadTree(level + 1, quadrant.bottomRight);
    this.nodes[1] = new QuadTree(level + 1, quadrant.bottomLeft);
    this.nodes[2] = new QuadTree(level + 1, quadrant.topLeft);
    this.nodes[3] = new QuadTree(level + 1, quadrant.topRight);
  }

  clear() {
    this.objects.clear();

    for (let i = 0; i < nodes.length; i++) {
      if (nodes[i] != null) {
        nodes[i].clear();
        nodes[i] = null;
      }
    }
  }

  _getQuadrants() {
    if (this.quadrants) {
      return this.quadrants;
    }

    const { x, y, width, height } = this.bounds;
    const { x: midX, y: midY } = this.bounds.midPoint;

    this.quadrants = {
      bottomRight: new Rectangle(midX, y, width/2, height/2),
      bottomLeft: new Rectangle(x, y,  width/2, height/2),
      topLeft: new Rectangle(x, midY,  width/2, height/2),
      topRight: new Rectangle(midX, midY, width/2, height/2),
    };

    return this.quadrants;
  }

  // Gets the quadrant index of the given rectangle, or in otherwords figures out if one of hte quadrants can completly contain the given rectangles
  _getQuadrantIndex(rect) {
    const quadrant = this._getQuadrants();
    if (quadrant.bottomRight.containsRectangle(rect)) {
      return 0;
    }
    if (quadrant.bottomLeft.containsRectangle(rect)) {
      return 1;
    }
    if (quadrant.topLeft.containsRectangle(rect)) {
      return 2;
    }
    if (quadrant.topRight.containsRectangle(rect)) {
      return 3;
    }
    return -1;
  }
}


const SvgRectangleComponent = props => {
  return <rect {...props}  />;
} 

const QuadTreeComponent = ({ width, height, quadtree }) => {
  let ck = 0;
  let cb = 1000;
  let nodes = [quadtree];
  let components = [];

  while (nodes.length > 0) {
    let curTree = nodes.shift();
    for (let i = 0; i < curTree.objects.length; i++) {
      let curObject = curTree.objects.get(i)
      components.push(
              <SvgRectangleComponent key={cb++} 
          x={curObject.x} 
          y={curObject.y} 
          width={curObject.width} 
          height={curObject.height} 
          style={{ fill: "rgb(0,0,200)", fillOpacity: "0.7" }}
          />)
    }
    if (curTree.nodes[0] === null) {
      
      let curBounds = curTree.bounds;      
      components.push(
        <SvgRectangleComponent key={ck++} 
          x={curBounds.x} 
          y={curBounds.y} 
          width={curBounds.width} 
          height={curBounds.height} 
          style={{ strokeWidth:"3", stroke:"rgb(255,0,0)", fill: "ffffff", fillOpacity: "0" }}
          />
      );
    } else {
      nodes = [...nodes, ...curTree.nodes];
    }
  }

  return (
    <svg width={width} height={height}>
      {components}
    </svg>
  );
};

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

function weightedRand2(spec) {
  var i, sum=0, r=Math.random();
  for (i in spec) {
    sum += spec[i];
    if (r <= sum) return i;
  }
}
  
class App extends React.Component {
  constructor(props) {
    super(props);
    this.bounds = new Rectangle(props.x,props.y,props.width,props.height);
    this.quadtree = new QuadTree(0,this.bounds);
    for (let i = 0; i < 125; i++) {
      // let x = getRandomInt(1, props.width - 10);
      let y = getRandomInt(1, props.height - 10);
      let x = getRandomInt(1,props.width / weightedRand2({3:0.5, 2:0.3, 1:0.2}));
      // let y = props.height / weightedRand2({1:0.7, 2:0.2, 3:0.1});
      this.quadtree.insert(new Rectangle(x, y, getRandomInt(5,20), getRandomInt(5,20)));
    }
  }

  render() {
    return (
      <div>
        <QuadTreeComponent {...this.props} quadtree={this.quadtree} />
      </div>
    );
  }
}

ReactDOM.render(<App x={0} y={0} width={600} height={600} />, document.getElementById("app"));

              
            
!
999px

Console