Hexagonal Pattern From Image
Scaled triangles corresponding to pixel brightness within each hexagon that makes up a hexagonal grid stretching across the extents of an image
x count = 20, y count = 20
1
img = Image.ReadFromFile(fil);
2
3
//Image Dimensions
4
w = Image.Dimensions(img)["width"];
5
h = Image.Dimensions(img)["height"];
6
7
//Surface corresponding to image proportions
8
pnt1 = Point.ByCoordinates(List.RestOfItems(0..w)<1>,List.RestOfItems(0..h)<2>);
9
srf = NurbsSurface.ByPoints(pnt1, 3, 3);
10
11
//Increase for more detail
12
xc = 10;
13
yc = 10;
14
15
//Hexagon Points Grid
16
s = 10;
17
a = s/(2*Math.Tan(30));
18
p1 = Point.ByCoordinates((0..#xc..s*3)<1>,(0..#yc..a*2)<2>);
19
p2 = List.Flatten(p1<1>.Translate([0,s*1.5]<2>,[0,a]<2>,0),1);
20
p3 = Polygon.RegularPolygon(Circle.ByCenterPointRadius(p2,s),6);
21
22
p4 = List.Flatten(p3,-1).Points;
23
p5 = List.Sublists(List.AddItemToEnd(List.FirstItem(p4<1>)<1>,p4<1>)<1>,0..1,1);
24
p6 = List.AddItemToFront(List.Flatten(p2,-1)<1>,List.DropItems(p5<1>,-1)<1><2>);
25
26
//Triangles
27
//p7 = Polygon.ByPoints(p6);
28
c1 = Point.ByCoordinates(Math.Sum(p6.X)/3,Math.Sum(p6.Y)/3);
29
30
//Triangle UV
31
u1 = UV.ByCoordinates((p6.X-List.MinimumItem(List.Flatten(p6,-1).X))/List.MaximumItem(List.Flatten(p6,-1).X),
32
(p6.Y-List.MinimumItem(List.Flatten(p6,-1).Y))/List.MaximumItem(List.Flatten(p6,-1).Y));
33
//Triangle Centroid
34
u2 = UV.ByCoordinates((c1.X-List.MinimumItem(List.Flatten(c1,-1).X))/List.MaximumItem(List.Flatten(c1,-1).X),
35
(c1.Y-List.MinimumItem(List.Flatten(c1,-1).Y))/List.MaximumItem(List.Flatten(c1,-1).Y));
36
37
//Minimum and Maximum parameters
38
minX = List.MinimumItem(List.Flatten(p6.X,-1));
39
maxX = List.MaximumItem(List.Flatten(p6.X,-1));
40
minY = List.MinimumItem(List.Flatten(p6.Y,-1));
41
maxY = List.MaximumItem(List.Flatten(p6.Y,-1));
42
43
//b1 + (s-a1)*(b2-b1)/(a2-a1)
44
//Triangle UV
45
uv1 = UV.ByCoordinates((p6.X-minX)/(maxX-minX),(p6.Y-minY)/(maxY-minY));
46
//Triangle Centroid UV
47
uv2 = UV.ByCoordinates((c1.X-minX)/(maxX-minX),(c1.Y-minY)/(maxY-minY));
48
49
//Image Pixels
50
pxl1 = List.Flatten(List.Reverse(Image.Pixels(img, w, h)),-1);
51
prm1 = List.Flatten(UV.ByCoordinates((0..1..#h)<1>, (0..1..#w)<2>),-1);
52
clr1 = (ColorRange.ByColorsAndParameters(pxl1, prm1)).GetColorAtParameter(uv2);
53
54
//Triangles on Surface
55
cnt1 = srf.PointAtParameter(uv2.U,uv2.V);
56
thk1 = 0.25;
57
tri1 = Surface.ByPerimeterPoints(srf.PointAtParameter(uv1.U,uv1.V)).Thicken(thk1);
58
pln1 = Plane.ByOriginNormal(cnt1,srf.NormalAtPoint(cnt1));
59
60
tri2 = tri1.Scale(pln1,(1-Color.Brightness(clr1))+0.00001,(1-Color.Brightness(clr1))+0.00001,1);
Copied!
x-count = 10, y count =10
Last modified 1yr ago
Copy link