【问题标题】:stretching curve to reach point python拉伸曲线到达点python
【发布时间】:2020-04-22 07:39:44
【问题描述】:

我有一条曲线,我想固定起点并将终点拖动到蓝点,同时保持曲线的一般形状(拉伸和收缩)。有什么想法我该怎么做?

import matplotlib.pyplot as pl
import numpy as np

x = np.linspace(1, 10)

def f(x):
    return np.sin(x) + np.random.normal(scale=0.1, size=len(x))

pl.plot(x, f(x))

pl.plot(8,.2,'bo')

Curve with Point

回答


#shift the end point to the dot
shift_x = 10-x[-1]
shift_y = .2-y[-1]


#shift the data
x=x+shift_x
y=y+shift_y

pl.plot(x,y)
pl.plot(10,.2,'bo')

shift end point to desired location


#shift the data back to the original points based on how close it is
#to the end point
gradient_x = shift_x/len(x)
gradient_y = shift_y/len(y)

for i in range(len(x)):
    y[i] = y[i]-(len(x)-i)*gradient_y
    x[i] = x[i]-(len(x)-i)*gradient_x

pl.plot(x,y)
pl.plot(10,.2,'bo')

All 3 Tranformations

【问题讨论】:

  • 到目前为止您尝试了哪些方法,您遇到了哪些问题?也许测量x,y 从当前y 到所需x,y(点)的距离并相应地延长x?您能否介绍一下您项目的背景?
  • 好吧,我需要像弹簧一样挤压曲线,我觉得应该有一个简单的算法来做到这一点,但我只是卡住了。

标签: python line stretch


【解决方案1】:

您可以使用简单的比例来拉伸:

x_scale = (blue_x - x0) / (x1 - x0)
y_scale = (blue_y - y0) / (y1 - y0)

然后转换曲线中的所有点:

new_x = (old_x - x0) * x_scale + x0
new_y = (old_y - y0) * y_scale + y0

假设 (x0, y0) 是曲线的起点。

【讨论】:

  • @BenShapiro 如果您觉得有用,请随时接受/点赞 =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2021-06-02
  • 2011-08-08
  • 1970-01-01
  • 2023-01-31
  • 2015-04-27
相关资源
最近更新 更多