@timohausmann/quadtree-ts
    Preparing search index...

    Class Line<CustomDataType>

    Class representing a Line

    const line = new Line({
    x1: 10,
    y1: 20,
    x2: 30,
    y2: 40,
    });
    const line = new Line({
    x1: 10,
    y1: 20,
    x2: 30,
    y2: 40,
    data: {
    name: 'Jane',
    health: 100,
    },
    });
    interface ObjectData {
    name: string;
    health: number;
    }
    const entity: ObjectData = {
    name: 'Jane',
    health: 100,
    };

    // Typescript will infer the type of the data property
    const line1 = new Line({
    x1: 10,
    y1: 20,
    x2: 30,
    y2: 40,
    data: entity,
    });

    // You can also pass in a generic type for the data property
    const line2 = new Line<ObjectData>({
    x1: 10,
    y1: 20,
    x2: 30,
    y2: 40,
    });
    line2.data = entity;
    // extending inherits the geometry's properties and the qtIndex method
    class Laser extends Line {

    constructor(color, x1, y1, x2, y2) {
    super({ x1, y1, x2, y2 });
    this.color = color;
    }
    }

    const laser = new Laser('green', 10, 20, 30, 40);
    // no need to extend if you don't implement LineGeometry
    class Laser {

    constructor(color) {
    this.color = color;
    this.start = [10, 20];
    this.end = [30, 40];
    }

    // add a qtIndex method to your class
    qtIndex(node) {
    // map your properties to LineGeometry
    return Line.prototype.qtIndex.call({
    x1: this.start[0],
    y1: this.start[1],
    x2: this.end[0],
    y2: this.end[1],
    }, node);
    }
    }

    const laser = new Laser('green');
    const player = {
    health: 100,
    x1: 10,
    y1: 20,
    x2: 30,
    y2: 40,
    qtIndex: Line.prototype.qtIndex,
    });
    // Note: this is not recommended but possible.
    // Using this technique, each object would have it's own qtIndex method.
    // Rather add qtIndex to your prototype, e.g. by using classes like shown above.
    const player = {
    name: 'Jane',
    health: 100,
    start: [10, 20],
    end: [30, 40],
    qtIndex: function(node) {
    return Line.prototype.qtIndex.call({
    x1: this.start[0],
    y1: this.start[1],
    x2: this.end[0],
    y2: this.end[1],
    }, node);
    },
    });

    Type Parameters

    • CustomDataType = void

      Type of the custom data property (optional, inferred automatically).

    Implements

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    Custom data.

    x1: number

    X start of the line.

    x2: number

    X end of the line.

    y1: number

    Y start of the line.

    y2: number

    Y end of the line.

    Methods

    • Beta

      Determine which quadrant this line belongs to.

      Parameters

      Returns number[]

      Array containing indexes of intersecting subnodes (0-3 = top-right, top-left, bottom-left, bottom-right)

    • Beta

      check if a line segment (the first 4 parameters) intersects an axis aligned rectangle (the last 4 parameters)

      Parameters

      • x1: number

        line start X

      • y1: number

        line start Y

      • x2: number

        line end X

      • y2: number

        line end Y

      • minX: number

        rectangle start X

      • minY: number

        rectangle start Y

      • maxX: number

        rectangle end X

      • maxY: number

        rectangle end Y

      Returns boolean

      true if the line segment intersects the axis aligned rectangle

      There is a bug where detection fails on corner intersections when the line enters/exits the node exactly at corners (45°) https://stackoverflow.com/a/18292964/860205