Given an array of points, I want to draw blue and red lines alternatingly.
Let $n\in\{0,1\}$, and arr
be an array of points
$$\textrm{arr} = \{ \{x_1, y_1\}, \{x_2,y_2\}, \ldots, \{x_n, y_n\}\}.$$
If $n=0$, I draw a blue line from the origin to $\{x_1, y_1\}$, then a red line from $\{x_1, y_1\}$ to $\{x_2, y_2\}$, then a blue line from $\{x_2, y_2\}$ to $\{x_3, y_3\}$, until the last line from $\{x_{n-1}, y_{n-1}\}$ to $\{x_{n}, y_{n}\}$.
If $n=1$, the blue and red lines are reversed. I draw a red line from the origin to $\{x_1, y_1\}$, then a blue line from $\{x_1, y_1\}$ to $\{x_2, y_2\}$, then a red line from $\{x_2, y_2\}$ to $\{x_3, y_3\}$, until the last line from $\{x_{n-1}, y_{n-1}\}$ to $\{x_{n}, y_{n}\}$.
For example, if
n=0
arr = {{1,0}, {2,0}, {3,0}}
then the result is
How can I make a function F[n,arr]
returns a Lines
object that draws the above?
Note: I tried a clumsy approach
colors[n_] := If[Mod[n, 2] == 1, Red, Blue]
F[n_, arr_] := Block[ {res = {}, newarr = Join[{{0, 0}}, arr]},
For[i = 1; i <= Length[arr], i++,
Append[res, {colors[n + i], Line[newarr[[i]], newarr[[i + 1]]]}]];
res]
With the above n
and arr
, I get an empty list (that is not what I want).