简体   繁体   English

如何翻转 Python 中两点之间的曲线,并将凸曲线转换为凹曲线?

[英]How can I flip a curved line between two points in Python, and convert convex curve to concave?

I have two points say p1 = (1,2) and p2 = (5,4) .我有两点说p1 = (1,2)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:我想要 plot Python 中这两点之间的直线和曲线。为了绘制曲线,我创建了一个名为draw_curve的 function,如下所示:

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:而我plot两点之间的直线和曲线使用如下代码:

#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:得到的plot如图: 在此处输入图像描述

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? function可以相应调整吗? Or is there a way to flip the curve using the blue line as mirror?或者有没有办法使用蓝线作为镜像来翻转曲线? Thank you!谢谢!

在此处输入图像描述

This seems like simple math;)这似乎是简单的数学;)

Get the difference between the blue an orange curve on y, reverse it, and add to the blue curve获取 y 上蓝色和橙色曲线之间的差异,反转它,然后添加到蓝色曲线

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

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

镜像曲线

graphical hint to why the array need to be reversed为什么数组需要反转的图形提示

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM