-1

How to find the length of a curved line in Python. An example to illustrate the problem is given below. Any inputs?

My data is given below:

        xdata    ydata ### xdata units = voltage, ydata units = amperes
0       2.130  10.432500
1       2.213  10.434000
2       2.279  10.437000
3       2.379  10.440000
4       2.496  10.441000
5       2.579  10.439000
6       2.612  10.441000
7       2.745  10.442000
8       2.812  10.439000
9       2.845  10.445000
10      2.912  10.445000
11      2.928  10.441000
12      2.945  10.444000
13      2.961  10.438000
14      2.995  10.443000
15      3.011  10.446000
16      3.028  10.442000
17      3.045  10.447000
18      3.061  10.440000
19      3.877  10.362000
20      4.509  10.414000
21      5.191  10.414000
22      5.790  10.419000
23      6.405  10.409000
24      6.971  10.415000
25      7.553  10.414000
26      8.136  10.411000
27      8.751  10.414000
28      9.250  10.410000
29      9.883  10.405000

enter image description here

I have no idea where and how to start to find the length?

Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 1
    This is way too broad. What does your input data look like? Where is the code? – Brian Nov 06 '19 at 21:43
  • Welcome to StackOverflow. Please follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is a knowledge base for *specific* programming problems -- not a design, coding, research, or tutorial resource. – Prune Nov 06 '19 at 21:51
  • @BrianJoseph I have updated my question with data. Sorry about that. – Mainland Nov 06 '19 at 21:55
  • @Prune I have updated my question with data. Sorry about that. – Mainland Nov 06 '19 at 21:56
  • @Mainland how about using QGIS – Msquare Nov 06 '19 at 21:59
  • 1
    @Mainland any reason you wouldn't use a sum of lengths computed using the Pythagorean theorem?¯\_(ツ)_/¯ – verandaguy Nov 06 '19 at 22:00
  • @verandaguy how would you do that? – Mainland Nov 06 '19 at 22:19
  • Thanks for the data. We also expect you to make an honest attempt to solve the problem. "How would you do that"? is something you need to look up: computing distances, and summing a list of values. – Prune Nov 06 '19 at 22:33
  • @Mainland take the horizontal run between each pair of points, the vertical rise between those two same points, compute the hypotenuse, and continue to the next pair of points. Sum the lengths of the hypotenuses. That said, seeing some of the other answers here, that's a fairly inelegant solution in terms of how it'd be implemented. – verandaguy Nov 07 '19 at 15:31
  • @Prune I am blank when I got the problem. After posting here I got the idea of calculating the geometric distance between two points. – Mainland Nov 07 '19 at 21:48

1 Answers1

1

Break down the problem into calculating the distance between ever consecutive pair of points. The entire curve length will be the sum of these values.

The python reduce function will essentially do this for you as long as you can tell it how to compute the distance between 2 points and provide the data (assuming it is in a pandas df format).

from functools import reduce

reduce(lambda p1, p2: np.linalg.norm(p1 - p2), df[['xdata', 'ydata']].values)

>>> output
5.136345594110207

P.S.: More info on the euclidian distance meteric I used here.

Brian
  • 1,572
  • 9
  • 18
  • This sounds good. What are the units of this output? – Mainland Nov 06 '19 at 22:02
  • @Mainland lol you never gave me units to begin with. How about `5.13 bananas`? The units should be of the same order of `xdata` and `ydata` (assuming they share units) – Brian Nov 06 '19 at 22:03
  • Sorry about that. I updated my question with units. However, your response made me laugh at the I way provided you data. Thanks for that. – Mainland Nov 06 '19 at 22:07
  • @Mainland I always hated physics so I'm not sure. But if this is a volt/amp curve then the units should be volt-amps? – Brian Nov 07 '19 at 14:29
  • I guess it is not a volt-amp unit. Anyhow thanks for the inputs. – Mainland Nov 07 '19 at 21:46