Rac. Angle

Angle measured with 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 of 0 points right, with the direction rotating clockwise:

rac.Angle(0/4) // points right
rac.Angle(1/4) // points downwards
rac.Angle(2/4) // points left
rac.Angle(3/4) // points upwards

instance.Angle

Instances of Rac contain a convenience rac.Angle function to create Angle objects with fewer parameters. This function also contains ready-made convenience objects, like rac.Angle.quarter, listed under instance.Angle.

Example
let rac = new Rac()
// new instance with constructor
let angle = new Rac.Angle(rac, 3/8)
// or convenience function
let otherAngle = rac.Angle(3/8)
See also:

Constructor

new Angle(rac, turn)

Creates a new Angle instance.

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

(new Rac.Angle(rac, 1/4)) .turn // returns 1/4
(new Rac.Angle(rac, 5/4)) .turn // returns 1/4
(new Rac.Angle(rac, -1/4)).turn // returns 3/4
(new Rac.Angle(rac, 1))   .turn // returns 0
(new Rac.Angle(rac, 4))   .turn // returns 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.

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

Example
// returns 1/2, since 1/2 - 1/4 = 1/4
rac.Angle(1/4).distance(1/2, true).turn
// returns 3/4, since 1 - (1/2 - 1/4) = 3/4
rac.Angle(1/4).distance(1/2, false).turn

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.

The otherAngle parameter 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.

See also:
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.

Example
// returns 3/8, since 1/8 + 1/2 = 5/8
rac.Angle(1/8).inverse().turn
// returns 3/8, since 7/8 + 1/2 = 3/8
rac.Angle(7/8).inverse().turn

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.

Parameters:
Name Type Description
factor Number

The factor to multiply turn by

Example
rac.Angle(0).mult(0.5).turn    // returns 0
// whereas
rac.Angle(0).multOne(0.5).turn // returns 0.5

negative() → {Rac.Angle}

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

Example
// returns 3/4, since 1 - 1/4 = 3/4
rac.Angle(1/4).negative().turn
// returns 5/8, since 1 - 3/8 = 5/8
rac.Angle(3/8).negative().turn

perpendicular() → {Rac.Angle}

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

Example
// returns 3/8, since 1/8 + 1/4 = 3/8
rac.Angle(1/8).perpendicular(true).turn
// returns 7/8, since 1/8 - 1/4 = 7/8
rac.Angle(1/8).perpendicular(false).turn

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
Parameters:
Name Type Attributes Default Description
angle Rac.Angle | Number

An Angle to be shifted

clockwise Boolean <optional>
true

The orientation of the shift

Example
// returns 0.4, since 0.1 + 0.3 = 0.4
rac.Angle(0.1).shift(0.3, true).turn
// returns 0.8, since 0.1 - 0.3 = 0.8
rac.Angle(0.1).shift(0.3, false).turn

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

Example
// returns 0.4, since 0.3 + 0.1 = 0.4
rac.Angle(0.1).shiftToOrigin(0.3, true).turn
// returns 0.2, since 0.3 - 0.1 = 0.2
rac.Angle(0.1).shiftToOrigin(0.3, false).turn

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

Example
// returns 'Angle(0.2)'
rac.Angle(0.2)).toString()

turnOne() → {Number}

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