# EasyGeometry::D2::Segment

## #contains?(entity)

{% tabs %}
{% tab title="Description" %}
*Is other GeometryEntity contained in this* Segmen&#x74;*?*

Returns:

* `true` if `entity` is in this Segment.
* `false` otherwise.
  {% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    entity - GeometryEntity(Point, Line etc.) or Array of Numeric(coordinates)
    
Returns:
    boolean (true or false)
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

```ruby
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])

s1.contains?([0, 0])  # true
s1.contains?([1, 0])  # false
```

{% endtab %}
{% endtabs %}

## #==(*other*)

{% tabs %}
{% tab title="Description" %}
*Returns `True` if `self` and `other`are the same mathematical entities.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    other - GeometryEntity(Point, Line etc.) 
    
Returns:
    boolean (true or false)
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

```ruby
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])
s2 = EasyGeometry::D2::Segment.new([0, 0], [1, 0])

s1 == s2  # false
s1 == s1  # true
```

{% endtab %}
{% endtabs %}

## #distance(point)

{% tabs %}
{% tab title="Description" %}
*Finds the shortest distance between a `line segment` and a `point`.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    point - EasyGeometry::D2::Point or Array of Numeric(coordinates)
    
Returns:
    Numeric
    
Errors:
    raise TypeError if point is not Point or valid Array
```

{% endtab %}

{% tab title="Example" %}

```ruby
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])

s1.distance([0, 0])  # 0
s1.distance([-1, 1]) # Math.sqrt(2)
```

{% endtab %}
{% endtabs %}

## #length

{% tabs %}
{% tab title="Description" %}
*The length of the `line segment`.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    
Returns:
    Numeric
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

```ruby
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])

s1.length # Math.sqrt(2)
```

{% endtab %}
{% endtabs %}

## #midpoint

{% tabs %}
{% tab title="Description" %}
*The midpoint of the `line segment`.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    
Returns:
    EasyGeometry::D2::Point
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

```ruby
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])

s1.midpoint # Point(0.5, 0.5)
```

{% endtab %}
{% endtabs %}

## #perpendicular\_bisector(point=nil)

{% tabs %}
{% tab title="Description" %}
*The perpendicular bisector of this `segment`.*&#x20;

*If no `point` is specified or the `point` specified is not on the bisector then the bisector is returned as a `Line`.*

*Otherwise a `Segment` is returned that joins the `point` specified and the intersection of the bisector and the `segment`.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    point - EasyGeometry::D2::Point 
            or Array of Numeric(coordinates) 
            or nothing
    
Returns:
    EasyGeometry::D2::Line or EasyGeometry::D2::Segment
    
Errors:
    raise TypeError if point is not nil AND is not Point or valid Array
```

{% endtab %}

{% tab title="Example" %}

```ruby
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])

s1.perpendicular_bisector # Line([0.5, 0.5], [1.5, -0.5])
```

{% endtab %}
{% endtabs %}
