All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kennycason.kumo.placement.RTreeWordPlacer Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
package com.kennycason.kumo.placement;

import com.github.davidmoten.rtree.Entry;
import com.github.davidmoten.rtree.RTree;
import com.github.davidmoten.rtree.geometry.Geometries;
import com.github.davidmoten.rtree.geometry.Rectangle;
import com.kennycason.kumo.Word;
import rx.Observable;

/**
 * Created by kenny on 2/21/16.
 */
public class RTreeWordPlacer implements RectangleWordPlacer {

    private RTree placedWordRTree;

    @Override
    public void reset() {
        placedWordRTree = RTree.maxChildren(4).create();
    }

    @Override
    public boolean place(final Word word) {
        final Rectangle wordRectangle = Geometries.rectangle(
                word.getPosition().getX(),
                word.getPosition().getY(),
                word.getPosition().getX() + word.getDimension().getWidth(),
                word.getPosition().getY() + word.getDimension().getHeight());

        final Observable> results = placedWordRTree.search(
                wordRectangle);

        final int matches = results.count().toBlocking().single();
        if (matches > 0) {
            return false;
        }
        placedWordRTree = placedWordRTree.add(word.getWord(), wordRectangle);
        return true;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy