Home > Back-end >  Remove the intersection between two curves
Remove the intersection between two curves

Time:06-11

I'm having a curve (parabol) from 0 to 1 on both axes as follows:

enter image description here

I generate another curve by moving the original curve along the x-axis and combine both to get the following graph:

enter image description here

How can I remove the intersected section to have only the double bottoms pattern like this:

enter image description here

The code I use for the graph:

import numpy as np
import matplotlib.pyplot as plt

def get_parabol(start=-1, end=1, steps=100, normalized=True):
    x = np.linspace(start, end, steps)
    y = x**2
    if normalized:
        x = np.array(x)
        x = (x - x.min())/(x.max() - x.min())
        y = np.array(y)
        y = (y - y.min())/(y.max() - y.min())
    return x, y
def curve_after(x, y, x_ratio=1/3, y_ratio=1/2, normalized=False):
    x = x*x_ratio   x.max() - x[0]*x_ratio
    y = y*y_ratio   y.max() - y.max()*y_ratio
    if normalized:
        x = np.array(x)
        x = (x - x.min())/(x.max() - x.min())
        y = np.array(y)
        y = (y - y.min())/(y.max() - y.min())
    return x, y
def concat_arrays(*arr, axis=0, normalized=True):
    arr = np.concatenate([*arr], axis=axis).tolist()
    if normalized:
        arr = np.array(arr)
        arr = (arr - arr.min())/(arr.max() - arr.min())
    return arr

x, y = get_parabol()
new_x, new_y = curve_after(x, y, x_ratio=1, y_ratio=1, normalized=False)
new_x = np.add(x, 0.5)
# new_y = np.add(y, 0.2)
xx = concat_arrays(x, new_x, normalized=True)
yy = concat_arrays(y, new_y, normalized=True)

# plt.plot(x, y, '-')
plt.plot(xx, yy, '--')

I'm doing a research on pattern analysis that requires me to generate patterns with mathematical functions.

Could you show me a way to achieve this? Thank you!

CodePudding user response:

First off, I would have two different parabola functions such that:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)
y1 = np.add(x, 0.3)**2 # Parabola centered at -0.3
y2 = np.add(x, -0.3)**2 # Parabola centered at 0.3

You can choose your own offsets for y1 and y2 depending on your needs.

And then it's simply take the min of the two arrays

y_final = np.minimum(y1, y2)
plt.plot(x, y_final, '--') 

CodePudding user response:

This involves curve fitting. You need to find the intersection part before you drop the values. Since the values of x and y have been normalized, we would have to determine exactly where the two datasets meet. We can see that they meet when x[i] >x[i 1]. Using your cobined xx and yy from the data provided, We therefore can do the following:

data_intersect = int(np.where(np.r_[0,np.diff(xx)] < 0)[0])

x1 = xx[:data_intersect]
x2 = xx[data_intersect:]
y1 = yy[:data_intersect]
y2 = yy[data_intersect:]

difference = np.polyfit(x1, y1, 2) - np.polyfit(x2,y2,2)

meet = np.roots(difference) # all points where the two curves meet

meet = meet[(meet < max(x1)) & (meet >min(x1))] # only point curve meet 

xxx = np.r_[x1[x1<meet], x2[x2>meet]]
yyy = np.r_[y1[x1<meet], y2[x2>meet]]
plt.plot(xxx, yyy, '--')

enter image description here

  • Related