I want to get the coordinates of the bisection points for a given line segment to a specified resolution. For example, when the line is given by {0,0} and {1,0}, the output coordinates should be {0.5,0} for the first iteration, and then {0.25,0} and {0.75,0} for the second iteration, and so on, until the distances between those points are less than some resolution, say 1/16. Is there a convenient way?
Answer
Here's a cute method based on the way Mathematica reduces fractions. Suppose you want to divide the line into 16ths:
n = 16;
x = Reverse @ GatherBy[Range[n - 1]/n, Denominator]
(* {{1/2}, {1/4, 3/4}, {1/8, 3/8, 5/8, 7/8},
{1/16, 3/16, 5/16, 7/16, 9/16, 11/16, 13/16, 15/16}} *)
The actual coordinates can be obtained with:
line = {{0, 0}, {1, 0}};
Map[{1 - #, #}.line &, x, {-1}]
(* {{{1/2, 0}}, {{1/4, 0}, {3/4, 0}}, {{1/8, 0}, {3/8, 0}, {5/8, 0}, {7/8, 0}}, ... *)
Comments
Post a Comment