Re: programming competition

*To*: mathgroup at smc.vnet.net*Subject*: [mg4896] Re: programming competition*From*: Xah Lee <xah at best.com>*Date*: Fri, 4 Oct 1996 00:17:35 -0400*Organization*: Best Internet Communications*Sender*: owner-wri-mathgroup at wolfram.com

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_ ]:= Module[{positions}, positions = Flatten@ Position[ N@ Sqrt[#.#]& /@ Rest[ li-RotateRight[li] ], x_/; (x > maxLength), {1} ]; Partition[ Flatten@ (Fold[ Module[{p1 = li[[#2]], p2 = li[[#2+1]]}, Insert[ #1, Drop[ Rest@ Table[ (p2-p1) i + p1, {i, 0, 1, 1./Ceiling@ N[Sqrt[Plus@@((p2-p1)^2)]/maxLength] } ],-1 ], #2+1 (* <--- correction here *) ] ]&, li, Reverse@ positions]), 2] ] Thanks to all who looked into the problem. I'll write more later. Xah xah at best.com; 74631.731 at compuserve.com http://www.best.com/~xah/SpecialPlaneCurves_dir/specialPlaneCurves.html Mountain View, CA, USA ==== [MESSAGE SEPARATOR] ====