Wolfram Research

Function Repository Resource:

BlendLine

Source Notebook

Blend between lines

Contributed by: Sander Huisman

ResourceFunction["BlendLine"][{line1,line2}]

gives the line halfway between line1 and line2.

ResourceFunction["BlendLine"][{line1,line2},α]

gives the line a fraction α between line1 and line2.

ResourceFunction["BlendLine"][{line1,line2,},α]

gives the line a fraction α between lines linei.

ResourceFunction["BlendLine"][{line1,line2,},{α1,α2,}]

gives several lines with fractions αi.

Details and Options

Line objects line1 and line2 should contain a single line.
Line objects line1 and line2 can be in any dimension.

Examples

Basic Examples

Interpolate halfway between two lines:

In[1]:=
ResourceFunction[
 "BlendLine"][{Line[{{x1, y1}, {x2, y2}}], Line[{{x3, y3}, {x4, y4}}]}]
Out[1]=

Interpolate a fraction of the way between two lines:

In[2]:=
a = Line[{{0, 0}, {1, 0}, {3, 0.5}}];
b = Line[{{-1, 2}, {1, 2}, {3, 1}}];
Graphics[{a, b, Red, ResourceFunction["BlendLine"][{a, b}, 0.2]}]
Out[4]=

Scope

Interpolate two lines at multiple fractions:

In[5]:=
a = Line[{{0, 0}, {1, 0}, {3, 0.5}}];
b = Line[{{-1, 2}, {1, 2}, {3, 1}}];
Graphics[{a, b, Red, ResourceFunction["BlendLine"][{a, b}, {0.2, 0.4, 0.6, 0.8}]}]
Out[7]=

Blend between three lines:

In[8]:=
a = Line[{{0, 0}, {1, 0.4}}];
b = Line[{{1, 2}, {3, 1}}];
c = Line[{{4, 4}, {5, 4.5}}];
Graphics[{a, b, c, Red, ResourceFunction["BlendLine"][{a, b, c}, Range[0.025, 0.975, 0.05]]}, PlotRange -> All]
Out[11]=

Interpolate between two lines in 3D:

In[12]:=
a = Line[{{0, 0, 0}, {1, 0, 1}, {3, 0.5, -1}}];
b = Line[{{-1, 2, -2}, {1, 2, -1}, {3, 1, 2}}];
Graphics3D[{a, b, Red, ResourceFunction["BlendLine"][{a, b}, Range[1/10, 9/10, 1/10]]}]
Out[14]=

Interpolate a line in 4D:

In[15]:=
a = Line[{{0, 0, 0, 0}, {1, 2, 2, 0}}];
b = Line[{{-1, 2, 2, 3}, {1, 3, 4, 2}, {1, 0, 3, 1}}];
ResourceFunction["BlendLine"][{a, b}, 0.2]
Out[17]=

Applications

Create a grid:

In[18]:=
Graphics[{ResourceFunction[
   "BlendLine"][{Line[{{0, 0}, {0, 10}}], Line[{{5, 0}, {5, 10}}]}, Subdivide[5]],
  ResourceFunction[
   "BlendLine"][{Line[{{0, 0}, {5, 0}}], Line[{{0, 10}, {5, 10}}]}, Subdivide[8]]}]
Out[18]=

Design a fancy curved rooftop design using straight beams:

In[19]:=
a = Line[{{0, 0, 1}, {0, 1, 0}}];
b = Line[{{1, 0, 0}, {1, 1, 1}}];
c = Line[{{0, 0, 1}, {1, 0, 0}}];
d = Line[{{0, 1, 0}, {1, 1, 1}}];
Graphics3D[{Blue, ResourceFunction["BlendLine"][{a, b}, Subdivide[15]], Red, ResourceFunction["BlendLine"][{c, d}, Subdivide[15]]}]
Out[23]=

Properties and Relations

Lines do not need the same number of data points. The resulting line can have more points than the original ones:

In[24]:=
p1 = {{-1, 1}, {0, 2}, {2, 1}};
p2 = {{-1, 0}, {0, -1}, {1.5, 0.2}, {2, -0.1}};
Graphics[{Line[p1], Line[p2], PointSize[Large], Point[p1], Point[p2], Red, ResourceFunction["BlendLine"][{Line[p1], Line[p2]}], Point @@ ResourceFunction["BlendLine"][{Line[p1], Line[p2]}]}]
Out[26]=

Extrapolate beyond the original lines by using numbers outside the range (0,1):

In[27]:=
a = Line[{{0, 0}, {1, 0}, {3, 0.5}}];
b = Line[{{-1, 2}, {1, 2}, {3, 1}}];
Graphics[{a, b, Red, ResourceFunction[
   "BlendLine"][{a, b}, {-0.4, -0.2, 0.2, 0.4, 0.6, 0.8, 1.2, 1.4}]}]
Out[29]=

Neat Examples

Interpolate between an octagon and a hexagon:

In[30]:=
Graphics@ResourceFunction[
  "BlendLine"][{Line[Append[#, First[#]] &@CirclePoints[8]], Line[Append[#, First[#]] &@CirclePoints[0.2, 6]]}, Subdivide[15]]
Out[30]=

Interpolate between a circle and a square:

In[31]:=
a = Line[Append[#, First[#]] &[CirclePoints[{3, Pi/4}, 100]]];
b = Line[Append[#, First[#]] &[CirclePoints[{1, Pi/4}, 4]]];
Manipulate[
 Graphics[{a, b, ResourceFunction["BlendLine"][{a, b}, i]}], {{i, 0.5}, 0, 1}, SaveDefinitions -> True]
Out[32]=

Resource History

Related Resources

License Information