Setup

1
2
#r "nuget:SkiaSharp" 
using SkiaSharp;

Triangle class

1
2
3
4
5
6
public class Triangle
{
    public SKPoint A {get;set;}
    public SKPoint B {get;set;}
    public SKPoint C {get;set;}
}

Triangle Point Helper

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
List<Triangle> GetTrianglePoints(int count=10 ,float factor =0.25f,int baseLength=600)
{
    float length= baseLength;
    var a = new SKPoint((float)Math.Cos(Math.PI/3)*length,0);
    var b = new SKPoint(0,(float)Math.Sin(Math.PI/3)*length);
    var c = new SKPoint(length,(float)Math.Sin(Math.PI/3)*length);

    if(factor>1 || factor<0) factor=0.5f;
    float m= count*factor;
    float n= count-m;

    var ls = new List<Triangle>();
    ls.Add(new Triangle()
    {
        A=a,B=b,C=c
    });

    for(var i=0; i<count;i++)
    {
        var aa = new SKPoint((m*a.X + n*b.X)/count , (m*a.Y + n*b.Y)/count );
        var bb = new SKPoint((m*b.X + n*c.X)/count , (m*b.Y + n*c.Y)/count );
        var cc = new SKPoint((m*c.X + n*a.X)/count , (m*c.Y + n*a.Y)/count );

        var temp = new Triangle()
        {
            A = aa,
            B = bb,
            C = cc
        };
        ls.Add(temp);
        a=aa;
        b=bb;
        c=cc;
    }
    return ls;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Create an image and fill it blue
int width = 1200;
int height = 700;

int step =50;
SKBitmap bmp = new(width, height);
SKCanvas canvas = new(bmp);

canvas.Clear(SKColor.Parse("#fff"));

Random rand = new();
SKPaint paint = new() 
{ 
    Color = SKColors.White.WithAlpha(100), 
    IsAntialias = true ,
    StrokeWidth = 2,
    ColorF = SKColor.Parse("#003366")
};

foreach(var item in GetTrianglePoints(10,.2f,700))
{
    //paint.ColorF = listOfColor[rand.Next(0,listOfColor.Count)];
    canvas.DrawLine(item.A,item.B,paint);
    canvas.DrawLine(item.B,item.C,paint);
    canvas.DrawLine(item.C,item.A,paint);
} 
SKFileWStream fs = new("triangleillusion.jpg");
bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50);
bmp.Display();

images