Interpolating on a line between to points


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interpolating on a line between to points
# 1  
Old 09-08-2009
Interpolating on a line between to points

I have two points which represent the start and end points of a line. I need to interpolate a number of points on the line between the two points.

Point 1:
x = 455989.0
y = 8673453.4

Point 2:
x = 283957.6
y = 8691250.1

The two points I have are coordinates given in UTM format. x represents easting and y northing (which means they are given in meters).

I need 6916 points on the line between the start and end points (6918 points including the start and end points). The points should be equally spaced along the line. The result should be a file with 6918 rows with one number on each row, starting with point 1, continuing with the first interpolated point on the line, then the second etc, and ending with point 2.

It would be nice to have a general code which could also be used for other points for other lines with different number of interpolations.

Is there anyone who can help me with this problem?

# 2  
Old 09-08-2009
something like the following would work:

Code:
#  cat generate-points
#!/bin/bash -p
nawk -v MinX=$1 -v MinY=$2 -v MaxX=$3 -v MaxY=$4 -v Points=$5 '
{
  StepX=(MaxX-MinX)/(Points-1);
  StepY=(MaxY-MinY)/(Points-1);
  for(y=0;y<Points;y++) {
    X=MinX+StepX*y;
    Y=MinY+StepY*y;
    printf("%0.2f %0.2f\n",X,Y)
  }
  exit;
}
' <$0

which produces e.g. :

#  ./generate-points 455989.0 8673453.4 283957.6 8691250.1 7

455989.00 8673453.40
427317.10 8676419.52
398645.20 8679385.63
369973.30 8682351.75
341301.40 8685317.87
312629.50 8688283.98
283957.60 8691250.10

Then you can redirect output as required.

HTH
# 3  
Old 09-08-2009
Code:
$
$
$ cat lerp.pl
#!/usr/bin/perl -w
# ==============================================================================
# For the end points: P1 = (x0,y0) and P2 = (x1,y1), the linear interpolant
# equation is: y = y0 + (x - x0)*((y1 - y0)/(x1 - x0)).
# In this program, (x0,y0) = ($xstart,$ystart) and (x1,y1) = ($xend,$yend).
# ==============================================================================
@x = ();                  # array to store x-coordinates
@y = ();                  # array to store y-coordinates
$total = 6918;            # total no. of equidistant points, including endpoints
$xstart = 455989.0;       # x0
$xend = 283957.6;         # x1
$ystart = 8673453.4;      # y0
$yend = 8691250.1;        # y1
$iter = $xstart;          # the iterator variable
$incr = (($xstart - $xend)/($total - 1));  # increment per iteration
$i = 0;                   # array index
$yvalue = 0;              # calculated value of y-coordinate
# populate arrays
while ($iter >= $xend) {
  push @x, $iter;
  # calculate y-coordinate
  # y = y0 + (x - x0)*((y1 - y0)/(x1 - x0))
  $yvalue = $ystart + ($iter - $xstart)*(($yend - $ystart)/($xend - $xstart));
  push @y, $yvalue;
  $iter -= $incr;
}
# display
printf("%4s  %9s  %10s  %9s  %9s\n","No.","x-coord","y-coord",
                                    "delta-x","delta-y");
printf("%4s  %9s  %10s  %9s  %9s\n","-"x4,"-"x9,"-"x10,"-"x9,"-"x9);
foreach $elem (@x) {
  printf("%4d  %9.2f  %10.2f  %9.2f  %9.2f\n",$i+1,$elem,$y[$i],
                                              $i == 0 ? 0 : $x[$i]-$x[$i-1],
                                              $i == 0 ? 0 : $y[$i]-$y[$i-1]);
  $i++;
}
$
$
$ perl lerp.pl
 No.    x-coord     y-coord    delta-x    delta-y
----  ---------  ----------  ---------  ---------
   1  455989.00  8673453.40       0.00       0.00
   2  455964.13  8673455.97     -24.87       2.57
   3  455939.26  8673458.55     -24.87       2.57
   4  455914.39  8673461.12     -24.87       2.57
   5  455889.52  8673463.69     -24.87       2.57
   6  455864.65  8673466.26     -24.87       2.57
   7  455839.78  8673468.84     -24.87       2.57
   8  455814.90  8673471.41     -24.87       2.57
   9  455790.03  8673473.98     -24.87       2.57
  10  455765.16  8673476.56     -24.87       2.57
...
...
 998  431192.80  8676018.57     -24.87       2.57
 999  431167.93  8676021.15     -24.87       2.57
1000  431143.06  8676023.72     -24.87       2.57
1001  431118.19  8676026.29     -24.87       2.57
1002  431093.32  8676028.87     -24.87       2.57
...
...
1998  406321.99  8678591.47     -24.87       2.57
1999  406297.12  8678594.04     -24.87       2.57
2000  406272.25  8678596.61     -24.87       2.57
2001  406247.38  8678599.19     -24.87       2.57
2002  406222.51  8678601.76     -24.87       2.57
...
...
2998  381451.18  8681164.36     -24.87       2.57
2999  381426.31  8681166.93     -24.87       2.57
3000  381401.44  8681169.51     -24.87       2.57
3001  381376.57  8681172.08     -24.87       2.57
3002  381351.70  8681174.65     -24.87       2.57
...
...
3998  356580.37  8683737.25     -24.87       2.57
3999  356555.50  8683739.83     -24.87       2.57
4000  356530.63  8683742.40     -24.87       2.57
4001  356505.76  8683744.97     -24.87       2.57
4002  356480.89  8683747.54     -24.87       2.57
...
...
4998  331709.56  8686310.15     -24.87       2.57
4999  331684.69  8686312.72     -24.87       2.57
5000  331659.82  8686315.29     -24.87       2.57
5001  331634.94  8686317.86     -24.87       2.57
5002  331610.07  8686320.44     -24.87       2.57
...
...
5998  306838.75  8688883.04     -24.87       2.57
5999  306813.88  8688885.61     -24.87       2.57
6000  306789.00  8688888.18     -24.87       2.57
6001  306764.13  8688890.76     -24.87       2.57
6002  306739.26  8688893.33     -24.87       2.57
...
...
6915  284032.21  8691242.38     -24.87       2.57
6916  284007.34  8691244.95     -24.87       2.57
6917  283982.47  8691247.53     -24.87       2.57
6918  283957.60  8691250.10     -24.87       2.57
$
$

tyler_durden
# 4  
Old 09-15-2009
Perfect! This works great!

Thanks a lot!

Rune
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with decimal points

Hi All, I would like to set decimal point to 16 in the following bash script but it has syntax error at }: awk '{printf"%.16e", (a<500,a++,$1/(a*1.1212121212121229e-02))}' input.dat >output.datHow may I set it in the correct way please? Thank you very much! (6 Replies)
Discussion started by: sxiong
6 Replies

2. Shell Programming and Scripting

Put three points at the end of a line

Hello I have a file like this Anyway, if you are sincere in finding the druid Alcuin then you're going to need ships. die with the faith that you have stood shield to shield with your brothers. To honour, to glory, to a valiant death and then on to the hall of heroes. Skal! ... (6 Replies)
Discussion started by: thailand
6 Replies

3. UNIX for Dummies Questions & Answers

Interpolating

I am attaching 3 files: 6000.txt, 5500.txt, 5750.txt I am trying to interpolate (I hope I am using the correct word) files 6000.txt and 5500.txt with awk to get 5750.txt... Any ideas on how I can do that? :confused: (5 Replies)
Discussion started by: cosmologist
5 Replies

4. Red Hat

Mount Points? How?

Hi folks, I have been asked to performed the following: Add the following new moint points systemA:/avp and SystemB:/usr/sap/trans to be the new linux server ZZZ How can I add those mount points and how those mount points can become another linuz server?:wall::wall::wall: (2 Replies)
Discussion started by: 300zxmuro
2 Replies

5. Shell Programming and Scripting

Aggregated points

Combine points of specific key (a1) based on user defined size (lets say 200 in this example). so a1 191 and 191+200 and sum of all the values (4th column) and vice versa... Thanx a bunch! a1 191 201 1 a1 201 211 2 a1 211 221 1 a1 ....... .... a2......... ........ (7 Replies)
Discussion started by: quincyjones
7 Replies

6. Post Here to Contact Site Administrators and Moderators

Points?

Has any thought been given to assigning points to threads much in the way the HP ITRC forums do? This might not be possible, just a thought. (1 Reply)
Discussion started by: candlejack
1 Replies

7. UNIX for Advanced & Expert Users

mount points

hi, I believe a mount point does not have to be a physical disk, but rather a logical one? Is this correct? if so, how can I find out if my mount points are on different physical disks? thanks (9 Replies)
Discussion started by: JamesByars
9 Replies

8. Shell Programming and Scripting

How can I get entries between two points

Hi, I am trying to write a script to get entries between two points lets say start and end points from a log file, the log file time format is as follows Start - 07/Aug/2008:18:26:43 End - 07/Aug/2008:19:36:43 I tried using the following awk command but it couldnt pick up the entries... (3 Replies)
Discussion started by: openspark
3 Replies

9. UNIX and Linux Applications

Gnuplot question: how to plot 3D points as colored points in map view?

I have a simple gnuplot question. I have a set of points (list of x,y,z values; irregularly spaced, i.e. no grid) that I want to plot. I want the plot to look like this: - points in map view (no 3D view) - color of each point should depend on its z-value. - I want to define my own color scale -... (0 Replies)
Discussion started by: karman
0 Replies

10. UNIX for Advanced & Expert Users

mount points

sometimes in Solaris 8 when I go to mount filesystems using either the mount command or by editing the /etc/vfstab, i get a nice little error message saying the the number of allowable mount points has been exceeded. I have read man pages until I am blue in the face and no where can I find what the... (3 Replies)
Discussion started by: manderson19
3 Replies
Login or Register to Ask a Question