Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Line<CustomDataType>

Class representing a Line

example

Without custom data (JS/TS):

const line = new Line({
x1: 10,
y1: 20,
x2: 30,
y2: 40,
});
example

With custom data (JS/TS):

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

With custom data (TS):

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;
example

With custom class extending Line (implements LineGeometry (x1, y1, x2, y2)):

// extending inherits the qtIndex method
class Laser extends Line {

constructor(props) {
// call super to set x1, y1, x2, y2 (and data, if given)
super(props);
this.color = props.color;
}
}

const laser = new Laser({
color: 'green',
x1: 10,
y1: 20,
x2: 30,
y2: 40,
});
example

With custom class and mapping LineGeometry:

// 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');
example

With custom object that implements LineGeometry:

const player = {
name: 'Jane',
health: 100,
x1: 10,
y1: 20,
x2: 30,
y2: 40,
qtIndex: Line.prototype.qtIndex,
});
example

With custom object and mapping LineGeometry:

// 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).

Hierarchy

  • Line

Implements

Index

Constructors

Properties

Methods

Constructors

constructor

  • new Line<CustomDataType>(props: LineProps<CustomDataType>): Line<CustomDataType>
  • Line Constructor

    Type parameters

    • CustomDataType = void

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

    Parameters

    • props: LineProps<CustomDataType>

      Line properties

    Returns Line<CustomDataType>

Properties

Optional data

data?: CustomDataType

Custom data.

x1

x1: number

X start of the line.

x2

x2: number

X end of the line.

y1

y1: number

Y start of the line.

y2

y2: number

Y end of the line.

Methods

qtIndex

  • Determine which quadrant this line belongs to.

    beta

    Parameters

    Returns number[]

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

Static intersectRect

  • intersectRect(x1: number, y1: number, x2: number, y2: number, minX: number, minY: number, maxX: number, maxY: number): boolean
  • check if a line segment (the first 4 parameters) intersects an axis aligned rectangle (the last 4 parameters)

    beta
    remarks

    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

    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

Generated using TypeDoc