[Numpy-discussion] Creating parallel curves

Jonathan Hilmer jkhilmer at gmail.com
Sun Feb 12 14:53:31 EST 2012


Andrea,

Here is how to do it with splines.  I would be more standard to return
an array of normals, rather than two arrays of x and y components, but
it actually requires less housekeeping this way.  As an aside, I would
prefer to work with rotations via matrices, but it looks like there's
no support for that built in to Numpy or Scipy?

	def normal_vectors(x, y, scalar=1.0):
		tck = scipy.interpolate.splrep(x, y)
		
		y_deriv = scipy.interpolate.splev(x, tck, der=1)
		
		normals_rad = np.arctan(y_deriv)+np.pi/2.
		
		return np.cos(normals_rad)*scalar, np.sin(normals_rad)*scalar
	#


Jonathan



On Sun, Feb 12, 2012 at 4:47 AM, Andrea Gavana <andrea.gavana at gmail.com> wrote:
> HI Chris and All,
>
> On 10 February 2012 17:53, Chris Barker wrote:
>> Andrea,
>>
>>> Basically I have a set of x, y data (around 1,000 elements each) and I
>>> want to create 2 parallel "curves" (offset curves) to the original
>>> one; "parallel" means curves which are displaced from the base curve
>>> by a constant offset, either positive or negative, in the direction of
>>> the curve's normal. Something like this:
>>>
>>> http://pyx.sourceforge.net/examples/drawing2/parallel.html
>>
>> THis is called "buffering" in GIS parlance -- there are functions
>> available to do it in GIS an computational geometry libraries: you
>> might look in the shapely package:
>>
>> https://github.com/sgillies/shapely
>
> Thanks, I hoped this one would prove itself useful, but unfortunately
> to me it looks like one of the most impenetrable Python code I have
> ever seen. Or maybe my Python is just too weak. The end result is the
> same.
>
> I have surfed quite a lot and found many reference to Bezier curves,
> with a lot of mathematical acrobatics but little of practical value
> (i.e., code :-) ). In the end I stumbled upon a nice function in the
> matplotlib library (obviously) which can give me the normal line to
> every point in the curve, and I almost got there.
>
> I still have 2 problems (attached sample), picture at
> http://img689.imageshack.us/img689/7246/exampleplot.png
>
> 1) If you run the attached sample, you'll see a plot of a cubic
> polynomial with two "almost" parallel lines to it on the first
> subplot. I say "almost" because at the inflection points of the cubic
> something funny happens (see suplot number 2);
>
> 2) You can see in subplot 3 that the 3 lines are nicely shown as
> "almost" parallel (except the problem at point 1), as the X and Y axes
> have the same scale. Unfortunately I can't keep the same scales in my
> real plot and I can't use axis=square in matplotlib either. How do I
> "normalize" the get_normal_points method to get the same visual
> appearance of parallelism on X and Y having different X and Y scales
> (i.e., the same X and Y scales as in subplot 1)?
>
>>> But by plotting these thing out with matplotlib it seems to me they
>>> don't really look very parallel nor very constant-distance.
>>
>> as we say on the wxPython list -- post a fully functional example, so
>> we can check it out.
>
> Attached as promised.
>
> Thank you in advance for any suggestion.
>
> Andrea.
>
> "Imagination Is The Only Weapon In The War Against Reality."
> http://xoomer.alice.it/infinity77/
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list