Rac. Segment

Segment of a Ray with a given length.

instance.Segment

Instances of Rac contain a convenience rac.Segment function to create Segment objects from primitive values. This function also contains ready-made convenience objects, like rac.Segment.zero, listed under instance.Segment.

Example
let rac = new Rac()
let ray = rac.Ray(55, 77, 1/4)
// new instance with constructor
let segment = new Rac.Segment(rac, ray, 100)
// or convenience function
let otherSegment = rac.Segment(55, 77, 1/4, 100)
See also:

Constructor

new Segment(rac, ray, length)

Creates a new Segment instance.

Parameters:
Name Type Description
rac Rac

Instance to use for drawing and creating other objects

ray Rac.Ray

A Ray the segment will be based of

length Number

The length of the segment

Members

length :Number

The length of the segment.

rac :Rac

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

ray :Rac.Ray

The Ray the segment is based of.

Methods

angle() → {Rac.Angle}

Returns the angle of the segment's ray.

arc(endAngleopt, nullable, clockwiseopt) → {Rac.Arc}

Returns a new Arc based on this segment, with the given endAngle and clockwise orientation.

The returned Arc will use this segment's start as center, its angle as start, and its length as radius.

When endAngle is ommited or null, the segment's angle is used instead resulting in a complete-circle arc.

Parameters:
Name Type Attributes Default Description
endAngle Rac.Angle <optional>
<nullable>
null

An Angle to use as end for the new Arc, or null to use this.angle()

clockwise Boolean <optional>
true

The orientation of the new Arc

arcWithAngleDistance(angleDistance, clockwiseopt) → {Rac.Arc}

Returns a new Arc based on this segment, with the arc's end at angleDistance from the segment's angle in the clockwise orientation.

The returned Arc will use this segment's start as center, its angle as start, and its length as radius.

Parameters:
Name Type Attributes Default Description
angleDistance Rac.Angle | Number

The angle distance from the segment's start to the new Arc end

clockwise Boolean <optional>
true

The orientation of the new Arc

clampToLength(value, startInsetopt, endInsetopt) → {Number}

Returns the given value clamped to [startInset, length-endInset].

When startInset is greater that length-endInset the range for the clamp becomes imposible to fulfill. In this case the returned value will be the centered between the range limits and still clampled to [0, length].

Parameters:
Name Type Attributes Default Description
value Number

A value to clamp

startInset Number <optional>
0

The inset for the lower limit of the clamping range

endInset endInset <optional>
0

The inset for the higher limit of the clamping range

endPoint() → {Rac.Point}

Returns a new Point where the segment ends.

equals(otherSegment) → {Boolean}

Returns true when ray and length in both segments are equal; otherwise returns false.

When otherSegment is any class other that Rac.Segment, returns false.

Segments' length are compared using Rac#equals.

See also:
Parameters:
Name Type Description
otherSegment Rac.Segment

A Segment to compare

inverse() → {Rac.Segment}

Returns a new Segment poiting towards the inverse angle of this.angle().

The resulting Segment will have the same startPoint() and length as this.

See also:

moveEndPoint(newEndPoint) → {Rac.Segment}

Returns a new Segment starting at this.startPoint() and ending at newEndPoint.

When this.startPoint() and newEndPoint are considered equal, the new Segment will use this.angle().

See also:
Parameters:
Name Type Description
newEndPoint Rac.Point

The end point of the new Segment

moveStartPoint(newStartPoint) → {Rac.Segment}

Returns a new Segment starting at newStartPoint and ending at this.endPoint().

When newStartPoint and this.endPoint() are considered equal, the new Segment will use this.angle().

See also:
Parameters:
Name Type Description
newStartPoint Rac.Point

The start point of the new Segment

nextSegmentLegWithHyp(hypotenuse, clockwiseopt) → {Rac.Segment}

Returns a new Segment starting from endPoint() which corresponds to the leg of a right triangle where this is the other cathetus and the hypotenuse is of length hypotenuse.

The new Segment will point towards the perpendicular angle of this.angle().[inverse() in the clockwise orientation.

When hypotenuse is smaller that the segment's length, returns null since no right triangle is possible.

See also:
Parameters:
Name Type Attributes Default Description
hypotenuse Number

The length of the hypotenuse side of the right triangle formed with this and the new Segment

clockwise Boolean <optional>
true

The orientation of the perpendicular angle from endPoint()

nextSegmentPerpendicular(clockwiseopt, lengthopt, nullable) → {Rac.Segment}

Returns a new Segment starting from endPoint() towards the perpendicular angle of this.angle().inverse() in the clockwise orientation.

The new Segment will have the given length, or when ommited or null will use this.length instead.

Notice that the perpendicular is calculated from the inverse of the segment's angle. E.g. with clockwise as true, the resulting Segment will be pointing towards this.angle().perpendicular(false).

See also:
Parameters:
Name Type Attributes Default Description
clockwise Boolean <optional>
true

The orientation of the perpendicular angle from endPoint()

length Number <optional>
<nullable>
null

The length of the new Segment, or null to use this.length

nextSegmentToAngle(angle, lengthopt, nullable) → {Rac.Segment}

Returns a new Segment starting from endPoint() towards angle with the given length.

The new Segment will have the given length, or when ommited or null will use this.length instead.

Parameters:
Name Type Attributes Default Description
angle Rac.Angle | Number

The angle of the new Segment

length Number <optional>
<nullable>
null

The length of the new Segment, or null to use this.length

nextSegmentToAngleDistance(angleDistance, clockwiseopt, lengthopt, nullable) → {Rac.Segment}

Returns a new Segment starting from endPoint() towards the given angleDistance from this.angle().inverse() in the clockwise orientation.

The new Segment will have the given length, or when ommited or null will use this.length instead.

Notice that the angleDistance is applied to the inverse of the segment's angle. E.g. with an angleDistance of 0 the resulting Segment will be directly over and pointing in the inverse angle of this. As the angleDistance increases the two segments separate with the pivot at endPoint().

See also:
Parameters:
Name Type Attributes Default Description
angleDistance Rac.Angle | Number

An angle distance to apply to the segment's angle inverse

clockwise Boolean <optional>
true

The orientation of the angle shift from endPoint()

length Number <optional>
<nullable>
null

The length of the new Segment, or null to use this.length

nextSegmentToPoint(nextEndPoint) → {Rac.Segment}

Returns a new Segment starting from endPoint() and up to the given nextEndPoint.

When endPoint() and nextEndPoint are considered equal, the new Segment will use this.angle().

See also:
Parameters:
Name Type Description
nextEndPoint Rac.Point

The end point of the next Segment

nextSegmentWithLength(length) → {Rac.Segment}

Returns a new Segment starting from endPoint() with the given length and the same angle as this.

Parameters:
Name Type Description
length Number

The length of the next Segment

perpendicular(clockwiseopt) → {Rac.Segment}

Returns a new Segment pointing towards the perpendicular angle of this.angle() in the clockwise orientation.

The resulting Segment will have the same startPoint() and length as this.

See also:
Parameters:
Name Type Attributes Default Description
clockwise Boolean <optional>
true

The orientation of the perpendicular

pointAtBisector() → {Rac.Point}

Returns a new Point at the middle point the segment.

pointAtLength(length) → {Rac.Point}

Returns a new Point in the segment's ray at the given length from this.startPoint(). When length is negative, the new Point is calculated in the inverse direction of this.angle().

See also:
Parameters:
Name Type Description
length Number

The distance from this.startPoint()

pointAtLengthRatio(ratio) → {Rac.Point}

Returns a new Point in the segment's ray at a distance of this.length * ratio from this.startPoint(). When ratio is negative, the new Point is calculated in the inverse direction of this.angle().

See also:
Parameters:
Name Type Description
ratio Number

The factor to multiply length by

reverse() → {Rac.Segment}

Returns a new Segment with its start point set at this.endPoint(), angle set to this.angle().inverse(), and same length as this.

See also:

segmentBisector(lengthopt, nullable, clockwiseopt) → {Rac.Segment}

Returns a new Segment from the segment's middle point towards the perpendicular angle in the clockwise orientation.

The new Segment will have the given length, or when ommited or null will use this.length instead.

See also:
Parameters:
Name Type Attributes Default Description
length Number <optional>
<nullable>
null

The length of the new Segment, or null to use this.length

clockwise Boolean <optional>
true

The orientation of the perpendicular

segmentToBisector() → {Rac.Segment}

Returns a new Segment from the starting point to the segment's middle point.

See also:

startPoint() → {Rac.Point}

Returns the start of the segment's ray.

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

translatePerpendicular(distance, clockwiseopt) → {Rac.Segment}

Returns a new Segment with the start point moved the given distance towards the perpendicular angle to this.angle() in the clockwise orientaton. All other properties are copied from this.

Parameters:
Name Type Attributes Default Description
distance Number

The distance to move the start point by

clockwise Boolean <optional>
true

The orientation of the perpendicular

translateToAngle(angle, distance) → {Rac.Segment}

Returns a new Segment with the start point moved towards angle by the given distance. All other properties are copied from this.

Parameters:
Name Type Description
angle Rac.Angle | Number

An Angle to move the start point towards

distance Number

The distance to move the start point by

translateToLength(length) → {Rac.Segment}

Returns a new Segment with the start point moved along the segment's ray by the given length. All other properties are copied from this.

When length is negative, start is moved in the inverse direction of angle.

Parameters:
Name Type Description
length Number

The length to move the start point by

vertex()

Calls p5.vertex to represent this Segment.

Added to Rac.Segment.prototype when Rac.P5Drawer is setup as rac.drawer.

withAngle(newAngle) → {Rac.Segment}

Returns a new Segment with angle set to newAngle.

All other properties are copied from this.

Parameters:
Name Type Description
newAngle Rac.Angle | Number

The angle for the new Segment

withAngleAdd(angle) → {Rac.Segment}

Returns a new Segment with angle added to this.angle().

All other properties are copied from this.

Parameters:
Name Type Description
angle Rac.Angle | Number

The angle to add

withAngleShift(angle, clockwiseopt) → {Rac.Segment}

Returns a new Segment with angle set to this.ray.angle.shift(angle, clockwise).

All other properties are copied from this.

Parameters:
Name Type Attributes Default Description
angle Rac.Angle | Number

The angle to be shifted by

clockwise Boolean <optional>
true

The orientation of the shift

withEndExtension(distance) → {Rac.Segment}

Returns a new Segment with distance added to this.length, which results in endPoint() for the resulting Segment moving in the direction of the segment's ray by the given distance.

All other properties are copied from this.

Using a positive distance results in a longer segment, using a negative distance results in a shorter one.

This method performs the same operation as withLengthAdd.

Parameters:
Name Type Description
distance Number

The distance to add to length

withLength(newLength) → {Rac.Segment}

Returns a new Segment with length set to newLength.

All other properties are copied from this.

Parameters:
Name Type Description
newLength Number

The length for the new Segment

withLengthAdd(length) → {Rac.Segment}

Returns a new Segment with length added to this.length.

All other properties are copied from this.

Parameters:
Name Type Description
length Number

The length to add

withLengthRatio(ratio) → {Rac.Segment}

Returns a new Segment with length set to this.length * ratio.

All other properties are copied from this.

Parameters:
Name Type Description
ratio Number

The factor to multiply length by

withRay(newRay) → {Rac.Segment}

Returns a new Segment with ray set to newRay.

All other properties are copied from this.

Parameters:
Name Type Description
newRay Rac.Ray

The ray for the new Segment

withStartExtension(distance) → {Rac.Segment}

Returns a new Segment with the start point moved in the inverse direction of the segment's ray by the given distance. The resulting Segment will have the same endPoint() and angle() as this.

Using a positive distance results in a longer segment, using a negative distance results in a shorter one.

Parameters:
Name Type Description
distance Number

The distance to move the start point by

withStartPoint(newStartPoint) → {Rac.Segment}

Returns a new Segment with start point set to newStart.

All other properties are copied from this.

Parameters:
Name Type Description
newStartPoint Rac.Point

The start point for the new Segment