> For the complete documentation index, see [llms.txt](https://gitbook.testingwaters.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.testingwaters.in/patterns/hexagonal-pattern-from-image.md).

# Hexagonal Pattern From Image

![x count = 20, y count = 20](/files/-LyUxp08Z4Ay2FeG_d1M)

```d
img = Image.ReadFromFile(fil);

//Image Dimensions
w = Image.Dimensions(img)["width"];
h = Image.Dimensions(img)["height"];

//Surface corresponding to image proportions
pnt1 = Point.ByCoordinates(List.RestOfItems(0..w)<1>,List.RestOfItems(0..h)<2>);
srf = NurbsSurface.ByPoints(pnt1, 3, 3);

//Increase for more detail
xc = 10;
yc = 10;

//Hexagon Points Grid
s = 10;
a = s/(2*Math.Tan(30));
p1 = Point.ByCoordinates((0..#xc..s*3)<1>,(0..#yc..a*2)<2>);
p2 = List.Flatten(p1<1>.Translate([0,s*1.5]<2>,[0,a]<2>,0),1);
p3 = Polygon.RegularPolygon(Circle.ByCenterPointRadius(p2,s),6);

p4 = List.Flatten(p3,-1).Points;
p5 = List.Sublists(List.AddItemToEnd(List.FirstItem(p4<1>)<1>,p4<1>)<1>,0..1,1);
p6 = List.AddItemToFront(List.Flatten(p2,-1)<1>,List.DropItems(p5<1>,-1)<1><2>);

//Triangles
//p7 = Polygon.ByPoints(p6);
c1 = Point.ByCoordinates(Math.Sum(p6.X)/3,Math.Sum(p6.Y)/3);

//Triangle UV
u1 = UV.ByCoordinates((p6.X-List.MinimumItem(List.Flatten(p6,-1).X))/List.MaximumItem(List.Flatten(p6,-1).X),
(p6.Y-List.MinimumItem(List.Flatten(p6,-1).Y))/List.MaximumItem(List.Flatten(p6,-1).Y));
//Triangle Centroid
u2 = UV.ByCoordinates((c1.X-List.MinimumItem(List.Flatten(c1,-1).X))/List.MaximumItem(List.Flatten(c1,-1).X),
(c1.Y-List.MinimumItem(List.Flatten(c1,-1).Y))/List.MaximumItem(List.Flatten(c1,-1).Y));

//Minimum and Maximum parameters
minX = List.MinimumItem(List.Flatten(p6.X,-1));
maxX = List.MaximumItem(List.Flatten(p6.X,-1));
minY = List.MinimumItem(List.Flatten(p6.Y,-1));
maxY = List.MaximumItem(List.Flatten(p6.Y,-1));

//b1 + (s-a1)*(b2-b1)/(a2-a1)
//Triangle UV
uv1 = UV.ByCoordinates((p6.X-minX)/(maxX-minX),(p6.Y-minY)/(maxY-minY));
//Triangle Centroid UV
uv2 = UV.ByCoordinates((c1.X-minX)/(maxX-minX),(c1.Y-minY)/(maxY-minY));

//Image Pixels
pxl1 = List.Flatten(List.Reverse(Image.Pixels(img, w, h)),-1);
prm1 = List.Flatten(UV.ByCoordinates((0..1..#h)<1>, (0..1..#w)<2>),-1);
clr1 = (ColorRange.ByColorsAndParameters(pxl1, prm1)).GetColorAtParameter(uv2);

//Triangles on Surface
cnt1 = srf.PointAtParameter(uv2.U,uv2.V);
thk1 = 0.25;
tri1 = Surface.ByPerimeterPoints(srf.PointAtParameter(uv1.U,uv1.V)).Thicken(thk1);
pln1 = Plane.ByOriginNormal(cnt1,srf.NormalAtPoint(cnt1));

tri2 = tri1.Scale(pln1,(1-Color.Brightness(clr1))+0.00001,(1-Color.Brightness(clr1))+0.00001,1);
```

![x-count = 10, y count =10](/files/-LyUxWSOQk-xH-0V6CqB)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.testingwaters.in/patterns/hexagonal-pattern-from-image.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
