Skip to content

Commit 8109589

Browse files
2 parents ea388b9 + 4c1720d commit 8109589

File tree

14 files changed

+498
-2
lines changed

14 files changed

+498
-2
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ hideMeta: true
2525
title: "How To"
2626
author: "PrashantUnity"
2727
weight: 1
28-
date: 2024-06-10T11:57:15+05:30
28+
date: 2024-08-03
29+
lastmod: 2024-08-03
2930
dateString: June 2024
3031
description: "Guide of How To Create Blog Post, Categories And Etc"
3132
#canonicalURL: "https://canonical.url/to/page"
@@ -106,7 +107,8 @@ template
106107
title: "Name Of Page"
107108
author: "Codefrydev"
108109
weight: 100
109-
date: 2024-06-10T11:57:15+05:30
110+
date: 2024-08-03
111+
lastmod: 2024-08-03
110112
dateString: June 2024
111113
description: "Description Of Page"
112114
keywords: [ "NET", "codefrydev", "C sharp", "CFD", "Skia Sharp","Generate Video","Basic","FFMPEG"]
@@ -120,6 +122,8 @@ keywords: [ "NET", "codefrydev", "C sharp", "CFD", "Skia Sharp","Generate Video"
120122
title: "Replace Me With Actual Title"
121123
author: ["Replace Author Name","Another Author Name"]
122124
weight: 100
125+
date: 2024-08-03
126+
lastmod: 2024-08-03
123127
dateString: June 2024
124128
description: "Guide of How TO Create Blog Post, Categories AND Etc"
125129
#canonicalURL: "https://canonical.url/to/page"
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: "Circle and Points And Lines"
3+
author: "PrashantUnity"
4+
weight: 104
5+
date: 2024-08-03
6+
lastmod: 2024-08-03
7+
dateString: August 2024
8+
description: "Dynamically Generating Bunch of Circle and line and Points on Canvass"
9+
#canonicalURL: "https://canonical.url/to/page"
10+
cover:
11+
image: "cover.jpg" # image path/url
12+
alt: "Generate Thumbnail" # alt text
13+
#caption: "Optical Character Recognition" display caption under cover
14+
15+
tags: [ "CSharp", "SkiaSharp","Polygon","Trigonometry"]
16+
keywords: [ "NET", "codefrydev", "C sharp", "CFD", "SkiaSharp","Trigonometry","Polygon","Math"]
17+
---
18+
19+
## Importing Skiasharp Library from nuget package
20+
21+
```csharp
22+
#r "nuget:SkiaSharp"
23+
```
24+
25+
### Importing namespace to use Skia library
26+
27+
```csharp
28+
using SkiaSharp;
29+
```
30+
31+
### Points On circle
32+
33+
- This function will calculate points on circle and return as list of Points
34+
- I Depends on number of points we wants to find out on circle
35+
- Radius of Circle r
36+
- Center of circle where is it located (x, y)
37+
38+
```csharp
39+
SKPoint[] CirclePoints(int n,float radius=3,float x=0, float y=0 )
40+
{
41+
float degree = (float)(2*Math.PI/n);
42+
return Enumerable
43+
.Range(1,n)
44+
.Select(i=>degree*i)
45+
.Select(i=>(new SKPoint(x+ radius *(float)Math.Cos(i), y+ radius *(float)Math.Sin(i))))
46+
.ToArray();
47+
}
48+
```
49+
50+
### Line points
51+
52+
- This Function will first Get all points on circle
53+
- Then pair the all point with each other as Line Segment Require two Cordinate points one starting point and other end
54+
55+
```csharp
56+
List<(SKPoint,SKPoint)> GetAllLinePoints(int n,int radius=3,float x=0, float y=0 )
57+
{
58+
var arr =CirclePoints(n:n,x:x,y:y, radius:radius);
59+
var ls = new List<(SKPoint, SKPoint)>();
60+
61+
for(var i =0; i<arr.Length; i++)
62+
{
63+
for(var j=i+1; j<arr.Length;j++)
64+
{
65+
ls.Add((arr[i],arr[j]));
66+
}
67+
}
68+
return ls;
69+
}
70+
```
71+
72+
### All Circle
73+
74+
- This Function will give us all circular points
75+
76+
```csharp
77+
List<(float X,float Y)> GetCenter(int m, int n,int radius=60)
78+
{
79+
var ls =new List<(float X,float Y)>();
80+
int X = radius*3;
81+
int Y = radius*3;
82+
for(var i=1; i<m;i++)
83+
{
84+
for(var j=1; j<n;j++)
85+
{
86+
ls.Add((Y*i,X*j));
87+
}
88+
}
89+
return ls;
90+
}
91+
```
92+
93+
### List of random Precalculated hex value
94+
95+
```csharp
96+
var listOfColor = new List<SKColor>
97+
{
98+
SKColor.Parse("#EEF5FF"),
99+
SKColor.Parse("#B4D4FF"),
100+
SKColor.Parse("#86B6F6"),
101+
SKColor.Parse("#176B87"),
102+
SKColor.Parse("#00A9FF"),
103+
SKColor.Parse("#89CFF3"),
104+
SKColor.Parse("#A0E9FF"),
105+
SKColor.Parse("#CDF5FD"),
106+
SKColor.Parse("#FF90BC"),
107+
SKColor.Parse("#FFC0D9"),
108+
SKColor.Parse("#F9F9E0"),
109+
SKColor.Parse("#8ACDD7"),
110+
SKColor.Parse("#F2AFEF"),
111+
SKColor.Parse("#C499F3"),
112+
SKColor.Parse("#33186B"),
113+
114+
};
115+
```
116+
117+
## Final Code to ustiliase all above function
118+
```csharp
119+
// Image size (pixels)
120+
int WIDTH = 1920;
121+
int radius =60;
122+
123+
Random random = new Random();
124+
SKBitmap bmp = new(1280, WIDTH);
125+
SKCanvas canvas = new(bmp);
126+
canvas.Clear(SKColor.Parse("#fff"));
127+
128+
SKPaint paint = new()
129+
{
130+
Color = SKColors.White.WithAlpha(100),
131+
IsAntialias = true ,
132+
StrokeWidth = 1,
133+
ColorF = SKColor.Parse("#000000"),
134+
Style = SKPaintStyle.Stroke
135+
};
136+
137+
var ls = GetCenter(10,7,radius);
138+
for(var n=3;n<=26;n++)
139+
{
140+
float X = ls[n-3].Y;
141+
float Y = ls[n-3].X;
142+
paint.ColorF =listOfColor[random.Next(0,listOfColor.Count)];
143+
foreach(var i in GetAllLinePoints(n:n,x:X,y:Y, radius:radius))
144+
{
145+
canvas.DrawLine(i.Item1,i.Item2,paint);
146+
canvas.DrawText($"{n} Points",X-20,Y+80,paint);
147+
}
148+
canvas.DrawCircle(X,Y,60,paint);
149+
}
150+
bmp.Display()
151+
```
152+
- Image Generated using above Code
153+
154+
![Result](./result.jpg)
9.19 KB
Loading
Loading

content/blog/SkiaSharp/hexagonal.md

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
---
2+
title: "Generating a Hexagonal Grid with SkiaSharp in C#"
3+
author: "PrashantUnity"
4+
weight: 102
5+
date: 2024-08-03
6+
lastmod: 2024-08-03
7+
dateString: August 2024
8+
description: "This guide explains how to generate a hexagonal grid using the SkiaSharp library in C#. It covers the basic concepts of hexagon geometry, provides functions to calculate hexagon points from a single coordinate, and demonstrates how to draw a complete hexagonal grid on a canvas. The tutorial includes code snippets and explanations to ensure the implementation is easily understandable and modifiable by reader."
9+
#canonicalURL: "https://canonical.url/to/page"
10+
cover:
11+
image: "cover.jpg" # image path/url
12+
alt: "Generate Thumbnail" # alt text
13+
#caption: "Optical Character Recognition" display caption under cover
14+
15+
tags: [ "CSharp", "SkiaSharp","Hexagon","Hexagonal Grid"]
16+
keywords: [ "NET", "codefrydev", "C sharp", "CFD", "SkiaSharp","Hexagon","Hexagonal Grid","Skia"]
17+
---
18+
19+
20+
## Concept
21+
22+
- A hexagon consists of six vertices.
23+
- Given one vertex coordinate, we can find all other vertices as the angle between two edges is 120 degrees.
24+
- Consider a starting point **(x, y)** and the width of an edge **b**.
25+
- The right neighbor points will be **(x + b, y)**.
26+
- The right neighbor of the right neighbor will be **(x + b + b * Cos(60), y + b * Sin(60))** and so on.
27+
28+
### Example
29+
30+
![Hexagon Points](./hexagon.png)
31+
32+
## Code Implementation
33+
34+
### SkiaSharp Setup
35+
36+
- Refer to the [SkiaSharp setup guide]({{< relref "blog/skiasharp/basic.md" >}})
37+
38+
### Importing Skiasharp Library
39+
40+
```csharp
41+
#r "nuget:SkiaSharp"
42+
```
43+
44+
### Importing name space
45+
46+
```csharp
47+
using SkiaSharp;
48+
using System.Collections.Generic;
49+
50+
```
51+
52+
### Function to Get Hexagon Points from a Single Coordinate
53+
54+
```csharp
55+
SKPoint[] GetPoints(float x, float y,float width)
56+
{
57+
var ls = new List<SKPoint>();
58+
59+
ls.Add(new SKPoint(x ,y));
60+
ls.Add(new SKPoint(x + width ,y));
61+
ls.Add(new SKPoint(x + width +width/2,y + width*MathF.Sqrt(3)/2));
62+
ls.Add(new SKPoint(x + width ,y + width*MathF.Sqrt(3)));
63+
ls.Add(new SKPoint(x ,y + width*MathF.Sqrt(3)));
64+
ls.Add(new SKPoint(x - width/2 ,y + width*MathF.Sqrt(3)/2));
65+
ls.Add(new SKPoint(x,y));
66+
return ls.ToArray();
67+
}
68+
```
69+
70+
## Function to Get Next Starting Point
71+
72+
```csharp
73+
(float,float) GetNextPoint(float x, float y, float width)
74+
{
75+
return (x + width +width/2,y + width*MathF.Sqrt(3)/2);
76+
}
77+
```
78+
79+
- This function calculates the next starting coordinate from **(x, y)**.
80+
81+
![Hexagone points](./nextpoint.png)
82+
83+
- So we will have two starting point
84+
- both will come alternatively
85+
- Below Code Generate the possible points
86+
87+
```csharp
88+
int width = 1200;
89+
int height = 750;
90+
int step =60;
91+
SKBitmap bmp = new(width, height);
92+
SKCanvas canvas = new(bmp);
93+
94+
canvas.Clear(SKColor.Parse("#fff"));
95+
96+
Random rand = new(0);
97+
SKPaint paint = new()
98+
{
99+
Color = SKColors.White.WithAlpha(100),
100+
IsAntialias = true ,
101+
StrokeWidth = 4,
102+
ColorF = SKColor.Parse("#003366"),
103+
Style = SKPaintStyle.Stroke
104+
};
105+
var ls = GetPoints(width/5,height/3,200);
106+
var skpath = new SKPath();
107+
skpath.AddPoly(ls);
108+
canvas.DrawPath(skpath,paint);
109+
110+
paint.TextSize = 48f;
111+
canvas.DrawText($"(x,y)",ls[0],paint);
112+
canvas.DrawText($"(x + (w)*3/2, y + (w)*sqrt(3)/2)",ls[2],paint);
113+
114+
canvas.DrawText($"(x + (w)*3/2, y + 0)",(ls[0].X +200*(3.0f/2)),ls[0].Y,paint);
115+
116+
bmp.Display();
117+
118+
```
119+
120+
![Hexagone points](./points.jpg)
121+
122+
## final Code
123+
124+
- I am Drawing hexagonal Shape in vertically fashion
125+
- Line number 22 that is while loop is for width of image Horizontaly
126+
- Line number 37 for vertically for height
127+
128+
- Line Number 25 to 36 will deside next verticall starting cordinate points.
129+
130+
```csharp
131+
int width = 1200;
132+
int height = 750;
133+
int step = 60;
134+
SKBitmap bmp = new(width, height);
135+
SKCanvas canvas = new(bmp);
136+
137+
canvas.Clear(SKColor.Parse("#fff"));
138+
139+
SKPaint paint = new()
140+
{
141+
IsAntialias = true,
142+
StrokeWidth = 4,
143+
Color = SKColor.Parse("#003366"),
144+
Style = SKPaintStyle.Stroke
145+
};
146+
147+
float incrementY = step * MathF.Sqrt(3);
148+
bool displaceY = false;
149+
float i = 0;
150+
151+
while (i < width)
152+
{
153+
float y = 0;
154+
if (!displaceY)
155+
{
156+
var (nextX, nextY) = GetNextPoint(i, 0, step);
157+
y = nextY;
158+
i = nextX;
159+
}
160+
else
161+
{
162+
y = 0;
163+
i += step * 1.5f;
164+
}
165+
166+
for (float j = y; j < height; j += incrementY)
167+
{
168+
var points = GetPoints(i, j, step);
169+
var path = new SKPath();
170+
path.AddPoly(points);
171+
canvas.DrawPath(path, paint);
172+
}
173+
174+
displaceY = !displaceY;
175+
}
176+
177+
bmp.Display();
178+
```
179+
180+
### Explanation
181+
182+
1. **Canvas Setup**: Initializes a canvas with a specified width and height, and clears it with a white background.
183+
2. **Paint Setup**: Configures the paint with desired properties such as color, stroke width, and style.
184+
3. **Hexagon Generation**:
185+
- Calculates the vertical increment (`incrementY`) based on the step size.
186+
- Uses a `while` loop to iterate horizontally across the canvas.
187+
- Uses a nested `for` loop to iterate vertically and draw hexagons.
188+
- Alternates the starting Y-coordinate to create a staggered hexagon grid.
189+
190+
### Result
191+
192+
The above code generates a hexagonal grid pattern as shown in the image below:
193+
194+
![Hexagone points](./grid.jpg)
8.22 KB
Loading
70.6 KB
Loading
92 KB
Loading
36.1 KB
Loading
24.2 KB
Loading

0 commit comments

Comments
 (0)