Home > Net >  How can I flip a curved line between two points in Python, and convert convex curve to concave?
How can I flip a curved line between two points in Python, and convert convex curve to concave?

Time:02-10

I have two points say p1 = (1,2) and p2 = (5,4).

I want to plot straight line and curved line between these two points in Python. To draw the curved line, I have created a function called draw_curve which looks as follows:

import matplotlib.pyplot as plt
import numpy as np

#Points
p1 = [1, 2]
p2 = [5, 4]

#Function to draw curved line
def draw_curve(p1, p2):
    
    a = (p2[1] - p1[1])/ (np.cosh(p2[0]) - np.cosh(p1[0]))
    b = p1[1] - a * np.cosh(p1[0])
    x = np.linspace(p1[0], p2[0], 100)
    y = a * np.cosh(x)   b
    
    return x, y

And I plot the straight line and curved line between the two points using the following code:

#Markers
plt.plot(p1[0], p1[1], marker = "o", markersize = 10, color = "black", label = "P1")
plt.plot(p2[0], p2[1], marker = "o", markersize = 10, color = "red", label = "P1")

#Straight line
plt.plot((p1[0], p2[0]),
         (p1[1], p2[1]),
         linewidth = 5,
         label = "Straight line")

#Curved line
x, y = draw_curve(p1, p2)

plt.plot(x, y, linewidth = 5, label = "Curved line", color = "orange")

plt.legend()

The resulting plot is as shown: enter image description here

The orange-colored curve I get now is convex shaped. However, I'd like to get a concave curve between the two points as well. It should look something as shown by the red-colored curve below. In a way, I'd like to mirror the orange curve using the blue line as the mirror.

What would be the appropriate way to do it? Can the function be adjusted accordingly? Or is there a way to flip the curve using the blue line as mirror? Thank you!

enter image description here

CodePudding user response:

This seems like simple math ;)

Get the difference between the blue an orange curve on y, reverse it, and add to the blue curve

blue_y = np.linspace(p1[1], p2[1], 100)

plt.plot(x, blue_y (blue_y-y)[::-1], linewidth = 5, label = "mirror", color = "red")

mirror curve

graphical hint to why the array need to be reversed

enter image description here

  • Related