Rac. Angle

Angle measured by a turn value in the range [0,1) that represents the amount of turn in a full circle.

Most functions through RAC that can receive an Angle parameter can also receive a number value that will be used as turn to instantiate a new Angle. The main exception to this behaviour are constructors, which always expect to receive Angle objects.

For drawing operations the turn value is interpreted to be pointing to the following directions:

  • 0/4 - points right
  • 1/4 - points downwards
  • 2/4 - points left
  • 3/4 - points upwards

Constructor

new Angle(rac, turn)

Creates a new Angle instance.

The turn value is constrained to the rance [0,1), any value outside is reduced back into range using a modulo operation.

new Rac.Angle(rac, 1/4)  // turn is 1/4
new Rac.Angle(rac, 5/4)  // turn is 1/4
new Rac.Angle(rac, -1/4) // turn is 3/4
new Rac.Angle(rac, 1)    // turn is 0
new Rac.Angle(rac, 4)    // turn is 0
Parameters:
Name Type Description
rac Rac

Instance to use for drawing and creating other objects

turn number

The turn value

Members

rac :Rac

Instance of Rac used for drawing and passed along to any created object.

turn :number

Turn value of the angle, constrained to the range [0,1).

Methods

(static) from(rac, something) → {Rac.Angle}

Returns an Angle derived from something.

  • When something is an instance of Angle, returns that same object.
  • When something is a number, returns a new Angle with something as turn.
  • When something is a Rac.Ray, returns its angle.
  • When something is a Rac.Segment, returns its angle.
  • Otherwise an error is thrown.
Parameters:
Name Type Description
rac Rac

Instance to pass along to newly created objects

something Rac.Angle | Rac.Ray | Rac.Segment | number

An object to derive an Angle from

(static) fromDegrees(rac, degrees) → {Rac.Angle}

Returns an Angle derived from degrees.

Parameters:
Name Type Description
rac Rac

Instance to pass along to newly created objects

degrees number

The measure of the angle, in degrees

(static) fromRadians(rac, radians) → {Rac.Angle}

Returns an Angle derived from radians.

Parameters:
Name Type Description
rac Rac

Instance to pass along to newly created objects

radians number

The measure of the angle, in radians

add(angle) → {Rac.Angle}

Returns a new Angle with the sum of this and the angle derived from angle.

Parameters:
Name Type Description
angle Rac.Angle | number

An Angle to add

cos() → {number}

Returns the cosine of this.

degrees() → {number}

Returns the measure of the angle in degrees.

distance(angle, clockwiseopt) → {Rac.Angle}

Returns a new Angle that represents the distance from this to the angle derived from angle.

rac.Angle(1/4).distance(1/2, true)  // turn is 1/2
rac.Angle(1/4).distance(1/2, false) // turn in 3/4
Parameters:
Name Type Attributes Default Description
angle Rac.Angle | number

An Angle to measure the distance to

clockwise boolean <optional>
true

The orientation of the measurement

equals(angle) → {boolean}

Returns true when the difference with the turn value of the angle derived from angle is under Rac#unitaryEqualityThreshold, otherwise returns false.

For this method otherAngle can only be Angle or number, any other type returns false.

This method will consider turn values in the oposite ends of the range [0,1) as equals. E.g. Angle objects with turn values of 0 and 1 - rac.unitaryEqualityThreshold/2 will be considered equal.

Parameters:
Name Type Description
angle Rac.Angle | number

An Angle to compare

inverse() → {Rac.Angle}

Returns a new Angle pointing in the opposite direction to this.

rac.Angle(1/8).inverse() // turn is 1/8 + 1/2 = 5/8
rac.Angle(7/8).inverse() // turn is 7/8 + 1/2 = 3/8

mult(factor) → {Rac.Angle}

Returns a new Angle with turn`` set to this.turn * factor`.

Parameters:
Name Type Description
factor number

The factor to multiply turn by

multOne(factor) → {number}

Returns a new Angle with turn set to this.turnOne() * factor.

Useful when doing ratio calculations where a zero angle corresponds to a complete-circle since:

rac.Angle(0).mult(0.5)    // turn is 0
// whereas
rac.Angle(0).multOne(0.5) // turn is 0.5
Parameters:
Name Type Description
factor number

The factor to multiply turn by

negative() → {Rac.Angle}

Returns a new Angle with a turn value equivalent to -turn.

rac.Angle(1/4).negative() // -1/4 becomes turn 3/4
rac.Angle(3/8).negative() // -3/8 becomes turn 5/8

perpendicular() → {Rac.Angle}

Returns a new Angle which is perpendicular to this in the clockwise orientation.

rac.Angle(1/8).perpendicular(true)  // turn is 1/8 + 1/4 = 3/8
rac.Angle(1/8).perpendicular(false) // turn is 1/8 - 1/4 = 7/8

radians() → {number}

Returns the measure of the angle in radians.

shift(angle, clockwiseopt) → {Rac.Angle}

Returns a new Angle result of shifting the angle derived from angle to have this as its origin.

This operation is the equivalent to

  • this.add(angle) when clockwise
  • this.subtract(angle) when counter-clockwise
rac.Angle(0.1).shift(0.3, true)  // turn is 0.1 + 0.3 = 0.4
rac.Angle(0.1).shift(0.3, false) // turn is 0.1 - 0.3 = 0.8
Parameters:
Name Type Attributes Default Description
angle Rac.Angle | number

An Angle to be shifted

clockwise boolean <optional>
true

The orientation of the shift

shiftToOrigin(origin, clockwiseopt) → {Rac.Angle}

Returns a new Angle result of shifting this to have the angle derived from origin as its origin.

The result of angle.shiftToOrigin(origin) is equivalent to origin.shift(angle).

This operation is the equivalent to

  • origin.add(this) when clockwise
  • origin.subtract(this) when counter-clockwise
rac.Angle(0.1).shiftToOrigin(0.3, true)  // turn is 0.3 + 0.1 = 0.4
rac.Angle(0.1).shiftToOrigin(0.3, false) // turn is 0.3 - 0.1 = 0.2
Parameters:
Name Type Attributes Default Description
origin Rac.Angle | number

An Angle to use as origin

clockwise boolean <optional>
true

The orientation of the shift

sin() → {number}

Returns the sine of this.

subtract(angle) → {Rac.Angle}

Returns a new Angle with the angle derived from angle subtracted to this.

Parameters:
Name Type Description
angle Rac.Angle | number

An Angle to subtract

tan() → {number}

Returns the tangent of this.

toString(digitsopt) → {string}

Returns a string representation intended for human consumption.

Parameters:
Name Type Attributes Default Description
digits number <optional>
null

The number of digits to print after the decimal point, when ommited all digits are printed

turnOne() → {number}

Returns the turn value in the range (0, 1]. When turn is equal to 0 returns 1 instead.