Home Reference Source
public class | source

Vector

Extends:

WorldElement → Vector

The Vector class is used for handling vector quantities and for drawing them onto the world.

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

Flag for setting the angle style of the vector.

public

Color of the components.

public

Flag for enabling the drawing of the vector's components. Default value is false.

public

Flag for drawing the components at the starting point of the vector.

public

Label for the vector.

public

Sets the -x magnitude of the vector.

public

Sets the -y magnitude of the vector.

Method Summary

Public Methods
public

add(x: number | object, y: number)

Add a value to the -x and -y components of the vector.

public

Get the angle of the vector.

public

Dot product between two vectors.

public

mag(): number

Get the magnitude of the vector.

public

Get the magnitude squared of the vector.

public

mult(x: number | object, y: number)

Multiply the -x and -y components of the vector by a constant value.

public

Get a readable format of the vector's components.

public

set(x: number | object, y: number)

Set the -x and -y components of the vector.

public

setMag(mag: number, angle: number)

Set the vector's magnitude and angle.

public

Gets the standard angle of the vector.

public

sub(x: number | object, y: number)

Subtract a value to the -x and -y components of the vector.

Private Methods
private

draw()

Main draw function for the vector called by the World automatically.

private

drawLine(x0: number, y0: number, x1: number, y1: number, color: string, dashed: boolean): Array

Draws a vector between two points.

private

Tests if the mouse is over the vector.

Inherited Summary

From class WorldElement
public

Sets the color in HEX format of the element.

public

Sets the cursor style when the mouse is over the element.

public

Flag for enabling or diabling this element.

public

Flag set when the element is being dragged around.

public

Sets the font of the element.

public

Sets the current height of the element.

public

Flag for allowing the element to be dragged around.

public

Sets the move style of the element.

public

Flag set when the mouse is over the element.

public

Sets a callback function for when the element is dragged around.

public

Sets the position of the element.

public

Sets the renderer used for the element.

public

Sets the rotation of the element.

public

A copy of the z-index value is stored here.

public

Sets the drawing scale of the element.

public

Flag for determining if the element will be a top-most element when dragged around.

public

Flag used to determine if an object is a child of WorldElement.

public

Sets the current width of the element.

public

Sets the world where this element is found.

public

Sets the z-index of the element.

public

Adds the provided coordinates to the position.

public

Sets if the element is draggable.

public

Function called when the mouse moves to check if the pointer is over the element.

public

Sets the color in HEX format of the element.

public

Sets the position of the element.

public

Sets the scale of the element.

public

setZ(z: number)

Sets the z-index of the element.

private

resize()

Function called when the canvas gets resized.

private

setWorld(world: World)

Sets the world object where element will be drawn.

private

topmost(state: boolean)

Sets the element as a topmost element.

Public Constructors

public constructor(opts: object) source

Override:

WorldElement#constructor

Params:

NameTypeAttributeDescription
opts object
  • optional

Object that contains valid Vector properties with values. Their values will be assigned at the end of the constructor. If an invalid property is passed then the value will be ignored.

Public Members

public angleStyle: number source

Flag for setting the angle style of the vector. Default value is ANGLE_STYLE.DEG.

public componentColor: string source

Color of the components. Default value is COLORS.COMPONENT.

public components: boolean source

Flag for enabling the drawing of the vector's components. Default value is false.

public componentsAtOrigin: boolean source

Flag for drawing the components at the starting point of the vector. Default value is true.

public label: string source

Label for the vector.

public x: number source

Sets the -x magnitude of the vector. Default value is 0.

public y: number source

Sets the -y magnitude of the vector. Default value is 0.

Public Methods

public add(x: number | object, y: number) source

Add a value to the -x and -y components of the vector.

Params:

NameTypeAttributeDescription
x number | object

Value to add to the -x component of the vector. If x is another vector, the components of this vector are added to the target vector.

y number
  • optional

Value to add to the -y component of the vector.

public angle(): number source

Get the angle of the vector. The type of angle depends on the value of Vector.angleStyle.

Return:

number

The angle of the vector.

public dot(x: number | object, y: number): number source

Dot product between two vectors.

Params:

NameTypeAttributeDescription
x number | object

x component of the vector used for calculating the dot product. If x is another vector, the dot product is calculated between x and the target vector.

y number
  • optional

y component of the vector used for calculating the dot product.

Return:

number

The dot product between the two vector.

public mag(): number source

Get the magnitude of the vector.

Return:

number

The magnitude of the vector.

public magSquared(): number source

Get the magnitude squared of the vector.

Return:

number

The magnitude squared of the vector.

public mult(x: number | object, y: number) source

Multiply the -x and -y components of the vector by a constant value.

Params:

NameTypeAttributeDescription
x number | object

Value that multiplies the -x component of the vector. If x is another vector, the components of this vector are multiplied with the target vector.

y number
  • optional

Value that multiplies the -y component of the vector.

public print(): string source

Get a readable format of the vector's components.

Return:

string

Readable string with the vector's -x and -y components.

public set(x: number | object, y: number) source

Set the -x and -y components of the vector.

Params:

NameTypeAttributeDescription
x number | object

x component of the vector. If x is another vector, the components of this vector are used to set the target vector.

y number
  • optional

y component of the vector.

public setMag(mag: number, angle: number) source

Set the vector's magnitude and angle.

Params:

NameTypeAttributeDescription
mag number

Magnitude of the vector.

angle number

Angle of the vector.

public standardAngle(): number source

Gets the standard angle of the vector. This means that the angle ranges from 0 to 360°. Only works when the angleStyle is in degrees.

Return:

number

The standard angle.

public sub(x: number | object, y: number) source

Subtract a value to the -x and -y components of the vector.

Params:

NameTypeAttributeDescription
x number | object

Value to subtract to the -x component of the vector. If x is another vector, the components of this vector are subtracted to the target vector.

y number
  • optional

Value to subtract to the -y component of the vector.

Private Methods

private draw() source

Main draw function for the vector called by the World automatically.

private drawLine(x0: number, y0: number, x1: number, y1: number, color: string, dashed: boolean): Array source

Draws a vector between two points. The vector is a line with an equilateral triangle at the tip of the vector.

Params:

NameTypeAttributeDescription
x0 number

Start -x coordinate of the vector in pixels.

y0 number

Start -y coordinate of the vector in pixels.

x1 number

End -x coordinate of the vector in pixels.

y1 number

End -y coordinate of the vector in pixels.

color string

Color of the vector in HEX format.

dashed boolean

Flag for drawing the vector with a dashed line.

Return:

Array

Array with elem 0 being the magnitude and elem 1 the angle in radians.

private isMouseOver(): boolean source

Tests if the mouse is over the vector.

Override:

WorldElement#isMouseOver

Return:

boolean

True if the mouse is over the vector, false otherwise.