简体   繁体   English

计算直线的角度(度)

[英]Calculate angle (degree) of a straight line

I'm trying to determine angle in degrees of a straight line of two points, I came across many solutions online but none of them worked for me, consider this piece of code我试图确定两点直线的角度,我在网上遇到了很多解决方案,但没有一个对我有用,考虑这段代码

import matplotlib.pyplot as plt
data = np.array([7405.,7447.4,7433.99,7410.,7443.15,7429.4,7590.03,7550.,7566.32,7619.62,7549.71,7551.8,7530,7522.99,7499.75,7453.99,7542.16,7564.,7552.77,7552])
y = [7606.672474,7570.240928]
plt.plot(data)
plt.plot([6,17], y)
plt.show()

在此处输入图片说明

The target line is y it should be around -5 degrees just by looking at it.目标线是y ,仅通过观察它就应该在 -5 度左右。 It seems like most online solutions suggest that we can find the angle by doing似乎大多数在线解决方案都表明我们可以通过以下方式找到角度

degree = np.math.atan2(y[-1] - y[0], x[-1] - x[0])
degree = np.degrees(degree)

I omitted the other values of y to just the first and last point for simplicity so the x[-1] - x[0] part here would be 11=17-6 which is the length of y line across the x-axis, this is what most online solutions suggest, however all the approaches failed to get the right angle for this, I should note that during my tests some approaches seemed to give the right angle for a given data unit for example while totally failing on a different data unit like为简单起见,我将 y 的其他值省略到第一个和最后一个点,因此这里的x[-1] - x[0]部分将是 11=17-6,即 y 线穿过 x 轴的长度,这是大多数在线解决方案所建议的,但是所有方法都未能为此找到正确的角度,我应该注意到,在我的测试期间,某些方法似乎为给定的数据单元提供了正确的角度,例如,在不同的数据上完全失败单位喜欢

data = [52.3384984,53.04757978,52.04276249,51.77348257,49.93056673,52.24062341,55.74022485,60.77761392,60.89290148,60.1995072,60.40524964,59.00590344,59.67589831,56.49266698,49.02464746,51.53876823,57.77368203,59.48092106,56.63155446,56.0648491 ]
y = [51.337288,50.331895]
plt.plot(data)
plt.plot([3,15], y)
plt.show()

I also tried to min-max normalize the data but no success, so considering we have the first and last point of a line and its length how can we or is it possible to determine its angle in degrees?我还尝试对数据进行最小-最大归一化,但没有成功,所以考虑到我们有一条线的第一个和最后一个点及其长度,我们如何或是否有可能以度为单位确定它的角度?

There are two angles you need to understand.有两个角度你需要理解。 The first one is calculated based data, the second one is calculated based on figure.第一个是根据数据计算的,第二个是根据数字计算的。

First one第一

The code you wrote is calculating first one:您编写的代码正在计算第一个:

degree = np.math.atan2(y[-1] - y[0], x[-1] - x[0])
degree = np.degrees(degree)

It's delta_y = y[-1] - y[0] = -36.43 , delta_x = x[-1] - x[0] = 11它是delta_y = y[-1] - y[0] = -36.43 , delta_x = x[-1] - x[0] = 11

degree = -73.20 which totally make sense if you draw a triangle in your mind. degree = -73.20如果您在脑海中画一个三角形,这完全有意义。

Second one第二个

However, you may question me that you are watching a line around -5 degree.但是,您可能会问我您正在观看 -5 度左右的线。 That's second one which involve calculating display ratio, (notice y axis and x axis has different unit length in inches).这是涉及计算显示比例的第二个,(注意 y 轴和 x 轴具有不同的单位长度(以英寸为单位))。 Here I found a separated question to help you calculate that.在这里,我找到了一个单独的问题来帮助您计算。

from operator import sub
def get_aspect(ax):
    # Total figure size
    figW, figH = ax.get_figure().get_size_inches()
    # Axis size on figure
    _, _, w, h = ax.get_position().bounds
    # Ratio of display units
    disp_ratio = (figH * h) / (figW * w)
    # Ratio of data units
    # Negative over negative because of the order of subtraction
    data_ratio = sub(*ax.get_ylim()) / sub(*ax.get_xlim())

    return disp_ratio / data_ratio

So you need multiple that ratio to get manhattan distance of line end points.因此,您需要乘以该比率才能获得线端点的曼哈顿距离。

ax = plt.gca()
ratio = get_aspect(ax)
degree = np.math.atan2((y[-1] - y[0])*ratio, x[-1] - x[0])
degree = np.degrees(degree)

The result is -4.760350735146195 which is around -5.结果是 -4.760350735146195,大约是 -5。

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

相关问题 如何计算具有 4 个点 (x1,y1,.,x4,y4) 的矩形在顺时针方向上的旋转角度,使其笔直或 0 度 - How to calculate rotation angle of a rectangle with 4 points (x1,y1,.,x4,y4) in clock-wise direction, to make it straight or 0 degree 如何找到一个点的坐标,使其与给定的线成 135 度角? - How to find the coordinates of a point such that it makes a 135 degree angle with a given line? 困惑为什么这不会产生斜率 1 和角度 45 的直线 - Confused why this doesn't produce a straight line of slope 1 and angle 45 如何计算直线与水平轴之间的角度? - How to calculate the angle between a line and the horizontal axis? 以角度旋转正方形 - Rotate a square by an angle in degree 计算轮廓上特定零件/边/线的斜率,长度和角度? - Calculate slope, length and angle of a specific part / side / line on a contour? 在 Python 中计算直线(斜率为 x)和水平之间的角度(度数) - Calculate angle (degrees) in Python between line (with slope x) and horizontal 根据随机位置点计算两条相连线段的角度 - Calculate the angle of two connected line segments based on a randomly position point 通过角度和长度计算线的端点坐标 - Calculate coordinates of end point of a line by having the angle and the length 计算平均学位分布 - Calculate average degree distribution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM