# EasyGeometry::D2::LinearEntity

## #direction

{% tabs %}
{% tab title="Description" %}
*The `direction vector` of the `LinearEntity`.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:

Returns:
    Easy::Geometry::Vector
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

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

l1.direction  # Vector(1, 1)
s2.direction  # Vector(-1, 1)
```

{% endtab %}
{% endtabs %}

## #angle\_between(other)

{% tabs %}
{% tab title="Description" %}
*Return the non-reflex `angle` formed by rays emanating from the origin with directions the same as the direction vectors of the `linear entities`.*

*From the dot product of vectors v1 and v2 it is known that:*

`dot(v1, v2) = |v1|*|v2|*cos(A)`&#x20;

*where A is the angle formed between the two vectors. We can get the directional vectors of the two lines and readily find the angle between the two using the above formula.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    other - EasyGeometry::D2::LinearEntity
    
Returns:
    Numeric (angle in radians)
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

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

l1.angle_between(s2)  # 90 * Math::PI / 180
```

{% endtab %}
{% endtabs %}

## #parallel\_to?(other)

{% tabs %}
{% tab title="Description" %}
*Are two `LinearEntity` parallel?*

*return `true` if self and other LinearEntity are parallel, `false`* otherwise.
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    other - EasyGeometry::D2::LinearEntity
    
Returns:
    boolean (true or false)
    
Errors:
    raise TypeError if other is not LinearEntity
```

{% endtab %}

{% tab title="Example" %}

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

l1.parallel_to?(l1)  # true
l1.parallel_to?(s2)  # false
```

{% endtab %}
{% endtabs %}

## #perpendicular\_to?(other)

{% tabs %}
{% tab title="Description" %}
*Are two `LinearEntity` perpendicular?*

*return `true` if self and other LinearEntity are perpendicular, `false`* otherwise.
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    other - EasyGeometry::D2::LinearEntity
    
Returns:
    boolean (true or false)
    
Errors:
    raise TypeError if other is not LinearEntity
```

{% endtab %}

{% tab title="Example" %}

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

l1.perpendicular_to?(l1)  # false
l1.perpendicular_to?(s2)  # true
```

{% endtab %}
{% endtabs %}

## #similar\_to?(other)

{% tabs %}
{% tab title="Description" %}
*Are two `linear entities` similar?*

*Return `true` if self and other are contained in the same line.*
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    other - EasyGeometry::D2::LinearEntity
    
Returns:
    boolean (true or false)
    
Errors:
    raise TypeError if other is not LinearEntity
```

{% endtab %}

{% tab title="Example" %}

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

l1.similar_to?(s2)  # false
l1.similar_to?(EasyGeometry::D2::Segment.new([2, 2], [4, 4]))  # true
```

{% endtab %}
{% endtabs %}

## #intersection(entity)

{% tabs %}
{% tab title="Description" %}
*The intersection with another `geometrical entity`*.
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    entity - GeometryEntity(Point, Line etc.)
    
Returns:
    Array of geometrical entities
    
Errors:
    raise TypeError if intersection method is not defined for entity 
```

{% endtab %}

{% tab title="Example" %}

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

l1.intersection(l1)  # [l1]
l1.intersection(s2)  # [Point(0, 0)]
l1.intersection(EasyGeometry::D2::Segment.new([1, 0], [2, 1])) # []
```

{% endtab %}
{% endtabs %}

## #parallel\_line(point)

{% tabs %}
{% tab title="Description" %}
*Create a new `Line` parallel to this `linear entity` which passes through the `point` p*.
{% endtab %}

{% tab title="Signature" %}

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

{% endtab %}

{% tab title="Example" %}

```ruby
l1 = EasyGeometry::D2::Line.new([0, 0], [1, 0])

l1.parallel_line([1, 1])  # Line(Point(1, 1), Point(2, 1))
```

{% endtab %}
{% endtabs %}

## #perpendicular\_line(point)

{% tabs %}
{% tab title="Description" %}
*Create a new `Line` perpendicular to this `linear entity` which passes through the `point`.*
{% endtab %}

{% tab title="Signature" %}

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

{% endtab %}

{% tab title="Example" %}

```ruby
l1 = EasyGeometry::D2::Line.new([0, 0], [1, 0])

l1.perpendicular_line([1, 1])  # Line(Point(1, 1), Point(1, 2))
```

{% endtab %}
{% endtabs %}

## #perpendicular\_segment(point)

{% tabs %}
{% tab title="Description" %}
*Create a `perpendicular line segment` from `point` to this `line`. The enpoints of the segment are `point` and the closest point in the line containing self. (If self is not a line, the point might not be in self.)*
{% endtab %}

{% tab title="Signature" %}

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

{% endtab %}

{% tab title="Example" %}

```ruby
l1 = EasyGeometry::D2::Line.new([0, 0], [1, 0])

l1.perpendicular_segment([0, 0])  # Point(0, 0)
l1.perpendicular_segment([1, 1])  # Segment(Point(1, 1), Point(1, 0))
```

{% endtab %}
{% endtabs %}

## #slope

{% tabs %}
{% tab title="Description" %}
*The slope of this `linear entity`, or `infinity` if vertical.*&#x20;
{% endtab %}

{% tab title="Signature" %}

```ruby
Parameters:
    
Returns:
    Numeric or BigDecimal('Infinity')
    
Errors:
```

{% endtab %}

{% tab title="Example" %}

```ruby
l1 = EasyGeometry::D2::Line.new([0, 0], [1, 1])

l1.slope  # 1
```

{% endtab %}
{% endtabs %}

## #span\_test(point)

{% tabs %}
{% tab title="Description" %}
*Test whether the `point` lies in the positive span of `self`. A point x is 'in front' of a point y if x.dot(y) >= 0.*

*Returns:*

* -1 if `other` is behind `self.p1`
* 0 if `other` is `self.p1`
* 1 if `other` is in front of `self.p1`.
  {% 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
l1 = EasyGeometry::D2::Line.new([0, 0], [1, 1])

l1.span_test([0, 0])  # 0
l1.span_test([1, 1])  # 1
```

{% endtab %}
{% endtabs %}
