Shared segment of parallel lines
I need your help!!
I am working on a problem where I identify approximately parallel lines. From the two lines that I have deemed parallel, I want to calculate the length of the segment that has a shared domain or range, or both domain and range.
In these examples I am using truly parallel lines for sake of simplicity.
There are four scenarios that we have to solve for: positive slope, negative slope, no slope, and undefined slopes.
**Helper functions: **
Code
# calculate range of x values
{
bbox <- sf::
return()
}
# calculate range of y values
{
bbox <- sf::
return()
}
# calculate overlapping range between two ranges
{
if (r1 < r2 || r2 < r1) {
return(NA)
} else {
return()
}
}
{
}
{
bbox <- sf::
sf::
}
Positive Slope
The first scenario is the shared positive slope.
Question:
How do I find the coordinates of the contained line segment to calculate the length? The solution should be able to handle the scenario where x and y are flipped as well.
# Positive Slope Scenario
x <- wk::

We can see that these two lines are parallel. We find their overlapping range:
overlap <-
overlap
$x_overlap
[1] 0.5 2.0
$y_overlap
[1] 0.75 2.00
What we want to calculate is the length of the red line segment contained by the bounding box.

Negative Slope Scenario
We have a very similar scenario. But this time with a negative slope. The solution should be able to handle if I want to find each line segment if x and y are swapped.
x <- wk::
(overlap <- )
$x_overlap
[1] 1 2
$y_overlap
[1] 0.0 0.5

Undefined Slope Scenario
Here, our overlap is only in one dimension as opposed to two. It may be more simple?
I think the answer here is is y_max - y_min.
x <- wk::
(overlap <- )
$x_overlap
[1] NA
$y_overlap
[1] 1.5 2.0

Segment Length:
Here is how we can calculate the overlap in the y-dimension:
overlap <-
y_over <- overlap$y_overlap
y_over - y_over
[1] 0.5
No slope scenario
Similar to the undefined slope. We have a one dimensional overlap. I
think the answer here is x_max - x_min.
x <- wk::

Segment Length:
Here is how we can calculate the overlap in the x-dimension:
overlap <-
x_over <- overlap$x_overlap
x_over - x_over
[1] 1