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 |
length |
Number
|
The length of the segment |
Members
rac :Rac
Instance of Rac used for drawing and passed along to any created
object.
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 resulting Arc is centered at ray.start, starting at
ray.angle, and with a radius of length.
When endAngle is ommited or null, the segment's angle is used as
default resulting in a complete-circle arc.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
endAngle |
Rac.Angle
|
<optional> <nullable> |
null
|
An |
clockwise |
Boolean
|
<optional> |
true
|
The orientation of the new |
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 resulting Arc is centered at ray.start, starting at
ray.angle, and with a radius of length.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
angleDistance |
Rac.Angle
|
Number
|
The angle distance from the
segment's start to the new |
||
clockwise |
Boolean
|
<optional> |
true
|
The orientation of the new |
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
is 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 |
inverse() → {Rac.Segment}
Returns a new Segment pointing towards
ray.angle.inverse().
The resulting Segment keeps the same start and length as this.
moveEndPoint(newEndPoint) → {Rac.Segment}
Returns a new Segment starting at startPoint() and ending at
newEndPoint.
When startPoint() and newEndPoint are considered
equal, the resulting Segment defaults to
ray.angle.
Parameters:
| Name | Type | Description |
|---|---|---|
newEndPoint |
Rac.Point
|
The end point of the new |
moveStartPoint(newStartPoint) → {Rac.Segment}
Returns a new Segment starting at newStartPoint and ending at
endPoint().
When newStartPoint and endPoint() are considered
equal, the resulting Segment defaults
to ray.angle.
Parameters:
| Name | Type | Description |
|---|---|---|
newStartPoint |
Rac.Point
|
The start point of the new |
nextSegmentLegWithHyp(hypotenuse, clockwiseopt) → {Rac.Segment}
Returns a new Segment that starts from endPoint() and corresponds
to the leg of a right triangle where this is the other cathetus and
the hypotenuse is of length hypotenuse.
The resulting Segment points towards the perpendicular angle of
ray.angle.inverse() in the clockwise
orientation.
When hypotenuse is smaller that the segment's length, returns
null since no right triangle is possible.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
hypotenuse |
Number
|
The length of the hypotenuse side of the
right triangle formed with |
||
clockwise |
Boolean
|
<optional> |
true
|
The orientation of the
perpendicular angle from |
nextSegmentPerpendicular(clockwiseopt, newLengthopt, nullable) → {Rac.Segment}
Returns a new Segment starting from endPoint() towards the
perpendicular angle of
ray.angle.inverse() in the clockwise
orientation.
The resulting Segment uses newLength, when ommited or null
defaults to 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 points towards
ray.angle.perpendicular(false).
- See also:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
clockwise |
Boolean
|
<optional> |
true
|
The orientation of the
perpendicular angle from |
newLength |
Number
|
<optional> <nullable> |
null
|
The length of the new |
nextSegmentToAngle(angle, newLengthopt, nullable) → {Rac.Segment}
Returns a new Segment starting from endPoint() and towards angle.
The resulting Segment uses newLength, or when ommited or null
defaults to length instead.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
angle |
Rac.Angle
|
Number
|
The angle of the new |
||
newLength |
Number
|
<optional> <nullable> |
null
|
The length of the new |
nextSegmentToAngleDistance(angleDistance, clockwiseopt, newLengthopt, nullable) → {Rac.Segment}
Returns a new Segment starting from endPoint() and pointing towards
ray.angle.inverse() shifted by
angleDistance in the clockwise orientation.
The resulting Segment uses newLength, when ommited or
null defaults to 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 is directly over and
pointing in the inverse angle of this. As the angleDistance
increases the two segments separate with the pivot at endPoint().
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 |
newLength |
Number
|
<optional> <nullable> |
null
|
The length of the new |
nextSegmentToPoint(nextEndPoint) → {Rac.Segment}
Returns a new Segment starting from endPoint() and ending at
nextEndPoint.
When endPoint() and nextEndPoint are considered
equal, the resulting Segment defaults
to ray.angle.
- See also:
Parameters:
| Name | Type | Description |
|---|---|---|
nextEndPoint |
Rac.Point
|
The end point of the next |
nextSegmentWithLength(newLength) → {Rac.Segment}
Returns a new Segment starting from endPoint(), with the given
newLength, and keeping the same angle as this.
Parameters:
| Name | Type | Description |
|---|---|---|
newLength |
Number
|
The length of the next |
perpendicular(clockwiseopt) → {Rac.Segment}
Returns a new Segment pointing towards the
perpendicular angle of
ray.angle in the clockwise orientation.
The resulting Segment keeps the same start 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(distance) → {Rac.Point}
Returns a new Point along the segment's ray at the given distance
from ray.start.
When distance is negative, the resulting Point is located in the
opposite direction of the segment's ray.
- See also:
Parameters:
| Name | Type | Description |
|---|---|---|
distance |
Number
|
The distance from |
pointAtLengthRatio(ratio) → {Rac.Point}
Returns a new Point along the segment's ray at a distance of
length * ratio from ray.start.
When ratio is negative, the resulting Point is located in the
opposite direction of the segment's ray.
- See also:
Parameters:
| Name | Type | Description |
|---|---|---|
ratio |
Number
|
The factor to multiply |
reverse() → {Rac.Segment}
Returns a new Segment starting at endPoint() and ending at
startPoint().
The resulting Segment uses the inverse
angle to ray.angle and keeps the same length as this.
segmentBisector(newLengthopt, nullable, clockwiseopt) → {Rac.Segment}
Returns a new Segment from the segment's middle point towards the
perpendicular angle in the clockwise orientation.
The resulting Segment uses newLength, or when ommited or null
defaults to length instead.
- See also:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
newLength |
Number
|
<optional> <nullable> |
null
|
The length of the new |
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.
text(string, formatopt) → {Rac.Text}
Returns a new Text located at start and oriented towards ray.angle
with the given string and format.
When format is provided, the angle for the resulting Text is still
set to ray.angle.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
string |
String
|
The string of the new |
||
format |
Rac.Text.Format
|
<optional> |
rac.Text.Format.topLeft
|
The format of the new |
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.Segment(55, 77, 0.2, 100).toString()
// returns: 'Segment((55,77) a:0.2 l:100)'
translatePerpendicular(distance, clockwiseopt) → {Rac.Segment}
Returns a new Segment with the start point translated by distance
towards the perpendicular of ray.angle in the clockwise orientaton.
The resulting segment keeps the same angle and length as 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 translated by distance
towards the given angle, and keeping the same angle and length as
this.
Parameters:
| Name | Type | Description |
|---|---|---|
angle |
Rac.Angle
|
Number
|
An |
distance |
Number
|
The distance to move the start point by |
translateToLength(distance) → {Rac.Segment}
Returns a new Segment with the start point translated by distance
along the segment's ray, and keeping the same angle and length as
this.
When distance is negative, the resulting Segment is translated in
the opposite direction of the segment's ray.
- See also:
Parameters:
| Name | Type | Description |
|---|---|---|
distance |
Number
|
The distance to move the start point by |
translateToLengthRatio(ratio) → {Rac.Segment}
Returns a new Segment with the start point translated along the
segment's ray by a distance of length * ratio. The resulting segment
keeps the same angle and length as this.
When ratio is negative, the resulting Segment is translated in the
opposite direction of the segment's ray.
- See also:
Parameters:
| Name | Type | Description |
|---|---|---|
ratio |
Number
|
The factor to multiply |
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 |
withAngleAdd(increment) → {Rac.Segment}
Returns a new Segment with increment added to ray.angle.
All other properties are copied from this.
Parameters:
| Name | Type | Description |
|---|---|---|
increment |
Rac.Angle
|
Number
|
The angle to add |
withAngleShift(angle, clockwiseopt) → {Rac.Segment}
Returns a new Segment with angle set to
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 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 |
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 |
withLengthAdd(increment) → {Rac.Segment}
Returns a new Segment with increment added to length.
All other properties are copied from this.
Parameters:
| Name | Type | Description |
|---|---|---|
increment |
Number
|
The length to add |
withLengthRatio(ratio) → {Rac.Segment}
Returns a new Segment with a length of length * ratio.
All other properties are copied from this.
Parameters:
| Name | Type | Description |
|---|---|---|
ratio |
Number
|
The factor to multiply |
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 |
withStartExtension(distance) → {Rac.Segment}
Returns a new Segment with the start point translated against the
segment's ray by the given distance, while keeping the same
endPoint(). The resulting segment keeps the same 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
|