Rac. Angle

Angle measured through 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 is 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 considers 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 are 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 adding angle to this, in the given clockwise orientation.

This operation is equivalent to shifting angle where this is considered the angle of origin.

The return is 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
rac.Angle(0.1).shift(0.5, true).turn
// returns 0.6, since 0.5 + 0.1 = 0.6

rac.Angle(0.1).shift(0.5, false).turn
// returns 0.4, since 0.5 - 0.1 = 0.4

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

Returns a new Angle result of adding this to origin, in the given clockwise orientation.

This operation is equivalent to shifting this where origin is considered the angle of origin.

The return is 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
rac.Angle(0.1).shiftToOrigin(0.5, true).turn
// returns 0.6, since 0.5 + 0.1 = 0.6

rac.Angle(0.1).shiftToOrigin(0.5, false).turn
// returns 0.4, since 0.5 - 0.1 = 0.4

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
rac.Angle(0.2)).toString()
// returns: 'Angle(0.2)'

turnOne() → {Number}

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