[GRASS-dev] [bug #2793] (grass) v.segment: side offset is ignored

Hamish hamish_nospam at yahoo.com
Thu May 3 06:18:24 EDT 2007


> Markus Neteler via RT wrote:
> > https://intevation.de/rt/webrt?serial_num=2793
> > 
> > Apparently Vect_point_on_line() lacks support for side offset.
> > Indeed, it doesn't even have such a parameter.

Hamish:
> if we know the slope of the line at the point (angle*), it is easy
> enough to calculate a point perpendicular to it (in the x,y plane)
> offset by a certain distance with a little trig.
..
> with luck the line direction is ok- shift off to the left or right?


see attached patch. raw & completely untested. All I know is that it
compiles.

> can anyone compose a nicer example for the v.segment help page?
> (v.to.points -n or -v to get points?)



Hamish
-------------- next part --------------
Index: main.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/vector/v.segment/main.c,v
retrieving revision 1.9
diff -u -r1.9 main.c
--- main.c	26 Mar 2007 02:47:04 -0000	1.9
+++ main.c	3 May 2007 10:14:42 -0000
@@ -16,6 +16,7 @@
  **************************************************************/
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include <time.h>
 #include <grass/gis.h>
 #include <grass/Vect.h>
@@ -23,9 +24,11 @@
 #include <grass/glocale.h>
 
 int find_line ( struct Map_info *Map, int lfield, int cat );
-    
+void offset_pt_90(double *, double *, double, double);
+
 int main(int argc, char **argv)
 {
+    int    i;
     int    ret, points_written, lines_written, points_read, lines_read;
     int    lfield;
     int    line;
@@ -117,6 +120,9 @@
 		    break;
 		}
 
+		if(fabs(side_offset) > 0.0)
+		    offset_pt_90(&x, &y, angle, side_offset);
+
                 Vect_append_point ( SPoints, x, y, z );
 		Vect_cat_set ( SCats, 1, id );
 
@@ -154,7 +160,12 @@
 			                      lcat, offset1, offset2, len, buf);
 		    break;
 		}
-		
+
+		if(fabs(side_offset) > 0.0) {
+		    for(i=0; i<SPoints->n_points; i++)
+			offset_pt_90(&SPoints->x[i], &SPoints->y[i], angle, side_offset);
+		}
+
 		Vect_cat_set ( SCats, 1, id );
 
 		Vect_write_line ( &Out, GV_LINE, SPoints, SCats);
@@ -203,4 +214,14 @@
     }
 
     return 0;
+}
+
+
+/* calculate a point perpendicular to the current line angle, offset by a distance
+ * works in the x,y plane.
+ */
+void offset_pt_90(double *x, double *y, double angle, double distance)
+{
+    *x += distance * cos(M_PI_2 - angle);
+    *y += distance * sin(M_PI_2 - angle);
 }


More information about the grass-dev mailing list