Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: programming competition

  • To: mathgroup at
  • Subject: [mg4896] Re: programming competition
  • From: Xah Lee <xah at>
  • Date: Fri, 4 Oct 1996 00:17:35 -0400
  • Organization: Best Internet Communications
  • Sender: owner-wri-mathgroup at

Xah Lee wrote:
> This is a programming problem I recently encountered....
> Problem:
> Suppose you have a ordered list {p1, p2, ... , pn}  where points has the form
> {x,y}. You want to modify the list so any neighboring points will have a distance
> less or equal to a given value maxLength. You do this by adding points in between
> points. For example, suppose p3 and p4 has length greater than maxLength. Your new
> list should then be
> {p1, p2, p3, newP1, newP2,..., newPm, p4, p5, ... , pn} where
> newP1, ...newPm lies on the line p3 p4.

I noted that my solution contains a bug after posting. As Robert Hall
pointed out, the order isn't correct. The problem is my Insert position
is wrong. For example, instead inserting the new list between p3 and p4,
my code insert them between p2, p3. The code is fixed by changing #2 to
(#2+1) in the Insert part.
The correct code is:

linearInterpolate::usage = "
linearInterpolate[{p1,p2,...}, maxLength ] returns
{P1,P2,...} such that the length between neighboring points
P[i], P[i+1] is less or equal to maxLength.
Newly created points lies on a line between old points";

linearInterpolate[ li_List, maxLength_ ]:=
        positions = Flatten@ Position[
                N@ Sqrt[#.#]& /@ Rest[ li-RotateRight[li] ],
                x_/; (x > maxLength), {1} 
        Partition[ Flatten@ (Fold[
        Module[{p1 = li[[#2]], p2 = li[[#2+1]]},
                                Rest@ Table[ (p2-p1) i + p1,
                                        {i, 0, 1, 1./Ceiling@
N[Sqrt[Plus@@((p2-p1)^2)]/maxLength] } 
                        #2+1 (* <--- correction here *)
        Reverse@ positions]), 2]

Thanks to all who looked into the problem. I'll write more later.

 xah at; 74631.731 at
 Mountain View, CA, USA


  • Prev by Date: Plotting 3D Conics
  • Next by Date: Re: Re: programming competition
  • Previous by thread: Re: programming competition
  • Next by thread: Re: Re: programming competition