Добавьте файлы проекта.

This commit is contained in:
Pavel-Savely Savianok 2023-01-02 19:08:46 +03:00
parent 08c0b31845
commit b16a3b1adc
14 changed files with 539 additions and 0 deletions

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CairoSharp" Version="3.24.24.38" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CairoObjective.DrawObjects
{
public class Line
{
public double X1, X2, Y1, Y2;
public Line(double x1, double y1, double x2, double y2)
{
X1 = x1;
X2 = x2;
Y1 = y1;
Y2 = y2;
}
}
}

72
CairoObjective/Line.cs Normal file
View File

@ -0,0 +1,72 @@
using Cairo;
using CairoObjective.DrawObjects;
namespace CairoObjective
{
#pragma warning disable CS8602 // Разыменование вероятной пустой ссылки.
public class Line
{
public static double DefaultSize = 1;
public static void Make(int pointX1, int pointY1, int pointX2, int pointY2, double size, Cairo.Color color)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(color);
context.LineWidth = size;
context.MoveTo(pointX1, pointY1);
context.LineTo(pointX2, pointY2);
context.Stroke();
}
public static void Make(int pointX1, int pointY1, int pointX2, int pointY2, Cairo.Color color)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(color);
context.LineWidth = DefaultSize;
context.MoveTo(pointX1, pointY1);
context.LineTo(pointX2, pointY2);
context.Stroke();
}
public static void Make(int pointX1, int pointY1, int pointX2, int pointY2, double size)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(Set.Color);
context.LineWidth = size;
context.MoveTo(pointX1, pointY1);
context.LineTo(pointX2, pointY2);
context.Stroke();
}
public static void Make(int pointX1, int pointY1, int pointX2, int pointY2)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(Set.Color);
context.LineWidth = DefaultSize;
context.MoveTo(pointX1, pointY1);
context.LineTo(pointX2, pointY2);
context.Stroke();
}
public static void Make(DrawObjects.Line line)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(Set.Color);
context.LineWidth = DefaultSize;
context.MoveTo(line.X1, line.Y1);
context.LineTo(line.X2, line.Y2);
context.Stroke();
}
public static void Make(DrawObjects.Line line, Color color)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(color);
context.LineWidth = DefaultSize;
context.MoveTo(line.X1, line.Y1);
context.LineTo(line.X2, line.Y2);
context.Stroke();
}
}
}

30
CairoObjective/Set.cs Normal file
View File

@ -0,0 +1,30 @@
namespace CairoObjective
{
public static class Set
{
public static void CheckContext()
{
if (Context == null)
{
throw new NullReferenceException("Context is Null. Set context on Set class.");
}
}
public static Cairo.Context? Context;
public static Cairo.Color Color = new Cairo.Color(1,1,1);//White
#pragma warning disable CS8602 // Разыменование вероятной пустой ссылки.
public static void Background(Cairo.Color color)
{
CheckContext();
var context = Set.Context;
context.SetSourceColor(color);
context.Paint();
}
public static void Background()
{
CheckContext();
var context = Set.Context;
context.SetSourceColor(Color);
context.Paint();
}
}
}

49
CairoObjective/Text.cs Normal file
View File

@ -0,0 +1,49 @@
namespace CairoObjective
{
#pragma warning disable CS8602 // Разыменование вероятной пустой ссылки.
public class Text
{
public static double DefaultFontSize = 20;//Default Font Size
public static void Make(string text, int pointX, int pointY, double size, Cairo.Color color)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(color);
context.SetFontSize(size);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
context.ShowText(text);
}
public static void Make(string text, int pointX, int pointY, Cairo.Color color)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(color);
context.SetFontSize(DefaultFontSize);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
context.ShowText(text);
}
public static void Make(string text, int pointX, int pointY, double size)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(Set.Color);
context.SetFontSize(size);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
context.ShowText(text);
}
public static void Make(string text, int pointX, int pointY)
{
Set.CheckContext();
var context = Set.Context;
context.SetSourceColor(Set.Color);
context.SetFontSize(DefaultFontSize);
Cairo.TextExtents extents = context.TextExtents(text);
context.MoveTo(pointX - extents.Width / 2, pointY + extents.Height / 2);
context.ShowText(text);
}
}
}

31
FractalTree.sln Normal file
View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33103.184
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CairoObjective", "CairoObjective\CairoObjective.csproj", "{943860CD-7811-4089-ACC8-FD57D67A4FE9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FractalTreeGtk", "FractalTreeGtk\FractalTreeGtk.csproj", "{510C072F-F749-4343-A4C2-523358FA3F7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{943860CD-7811-4089-ACC8-FD57D67A4FE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{943860CD-7811-4089-ACC8-FD57D67A4FE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{943860CD-7811-4089-ACC8-FD57D67A4FE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{943860CD-7811-4089-ACC8-FD57D67A4FE9}.Release|Any CPU.Build.0 = Release|Any CPU
{510C072F-F749-4343-A4C2-523358FA3F7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{510C072F-F749-4343-A4C2-523358FA3F7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{510C072F-F749-4343-A4C2-523358FA3F7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{510C072F-F749-4343-A4C2-523358FA3F7E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6D5961A6-3340-47D1-8DA9-9A6219BD8501}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,85 @@
using Gtk;
using CairoObjective;
using System.Linq;
using System.Diagnostics;
namespace FractalTree
{
class CairoWindow : Window
{
DrawingArea drawing = new DrawingArea();
DrawObjects.Grid grid;
int Scaler = 50;
int offsetX = 0;
int offsetY = 0;
public CairoWindow(string title) : base(title)
{
Fullscreen();
DeleteEvent += delegate { Application.Quit(); };
drawing.Drawn += OnDrawn;
KeyPressEvent += CairoWindow_KeyPressEvent;
Add(drawing);
ShowAll();
Line.DefaultSize = 2;
}
private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args)
{
if(args.Event.Key == Gdk.Key.equal)
{
Trace.WriteLine("+");
Scaler++;
QueueDraw();
}
if (args.Event.Key == Gdk.Key.minus && Scaler > 1)
{
Trace.WriteLine("-");
Scaler--;
QueueDraw();
}
if (args.Event.Key == Gdk.Key.a)
{
Trace.WriteLine("←");
offsetX-=10;
QueueDraw();
}
if (args.Event.Key == Gdk.Key.d)
{
Trace.WriteLine("→");
offsetX+=10;
QueueDraw();
}
if (args.Event.Key == Gdk.Key.w)
{
Trace.WriteLine("↑");
offsetY += 10;
QueueDraw();
}
if (args.Event.Key == Gdk.Key.s)
{
Trace.WriteLine("↓");
offsetY -= 10;
QueueDraw();
}
}
private void OnDrawn(object sender, DrawnArgs args)
{
MainDrawer();
}
private void MainDrawer()
{
grid = new(AllocatedWidth, AllocatedHeight, Scaler, offsetX, offsetY);
Set.Context = Gdk.CairoHelper.Create(drawing.GdkWindow);
Set.Background(new Cairo.Color(0, 0, 0));
foreach(CairoObjective.DrawObjects.Line gridline in grid.LinesHorizontal)
{
Line.Make(gridline, new Cairo.Color(1,1,1));
}
foreach (CairoObjective.DrawObjects.Line gridline in grid.LinesVertical)
{
Line.Make(gridline, new Cairo.Color(1, 1, 1));
}
}
}
}

View File

@ -0,0 +1,49 @@
using CairoObjective.DrawObjects;
using System;
using System.Diagnostics;
namespace FractalTree.DrawObjects
{
internal class Grid
{
public Line[] LinesHorizontal;
public Line[] LinesVertical;
int Scaler;
int Width;
int Height;
int offsetX;
int offsetY;
/// <summary>
/// Make Grid Array of lines
/// </summary>
/// <param name="Width">Width of Window</param>
/// <param name="Height">Height of Window</param>
/// <param name="Scaler">Pixels Per Lines</param>
public Grid(int Width, int Height, int Scaler, int offsetX, int offsetY)
{
this.Width = Width;
this.Height = Height;
this.Scaler = Scaler;
this.offsetX = offsetX;
this.offsetY = offsetY;
LinesHorizontal = new Line[this.Width / Scaler + 1];
LinesVertical = new Line[this.Height / Scaler + 1];
MakeHorizontal();
MakeVertical();
}
private void MakeHorizontal()
{
for(int i = 0; i < Width / Scaler + 1; i++)
{
LinesHorizontal[i] = new Line(i * Scaler + offsetX, 0, i * Scaler, Height);
}
}
private void MakeVertical()
{
for (int i = 0; i < Height / Scaler + 1; i++)
{
LinesVertical[i] = new Line(0, i * Scaler, Width, i * Scaler);
}
}
}
}

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CairoSharp" Version="3.24.24.38" />
<PackageReference Include="GtkSharp" Version="3.24.24.38" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CairoObjective\CairoObjective.csproj" />
</ItemGroup>
</Project>

15
FractalTree/Program.cs Normal file
View File

@ -0,0 +1,15 @@
using Gtk;
namespace FractalTree
{
internal class Program
{
static void Main()
{
Application.Init();
//Create the Window
new CairoWindow("Window");
Application.Run();
}
}
}

View File

@ -0,0 +1,31 @@
using Gtk;
using CairoObjective;
using GLib;
using FractalTreeGtk.Draw;
namespace FractalTreeGtk
{
internal class CairoWindow : Window
{
DrawingArea drawing = new DrawingArea();
Fractal fractal = new Fractal();
public CairoWindow(string title) : base(title) {
Fullscreen();
drawing.Drawn += Drawing_Drawn;
Add(drawing);
ShowAll();
}
private void Drawing_Drawn(object o, DrawnArgs args)
{
args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight);
Set.Context = args.Cr;
Set.Background(new Cairo.Color(0, 0, 0));
foreach(CairoObjective.DrawObjects.Line[] lines in fractal.Fractallines)
{
foreach(CairoObjective.DrawObjects.Line line in lines)
Line.Make(line);
}
}
}
}

View File

@ -0,0 +1,87 @@
using CairoObjective.DrawObjects;
using System;
namespace FractalTreeGtk.Draw
{
internal class Fractal
{
double Length = 50;
double deg = 0.2;
int levels = 14;
int level = 0;
double deltaLength = 2;
double deltaDeg = 0;
public Line[][] Fractallines;
public Fractal()
{
Fractallines = new Line[levels + 1][];
CreateBranch();
}
private void CreateBranch()
{
Fractallines[level] = new Line[Convert.ToInt32(Math.Pow(2, level))];
for (int i = 0; i < Fractallines[level].Length; i++)
{
Fractallines[level][i] = new Line(0, 0, 0, -Length);
}
level++;
NextLevel();
}
private void NextLevel()
{
Length -= deltaLength;
//deg += Math.Sin(deg) + deltadeg;
Fractallines[level] = new Line[Convert.ToInt32(Math.Pow(2, level))];
int x = 0;
for (int i = 0; i < Fractallines[level - 1].Length; i++)
{
Fractallines[level][x] = new Line(
Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2,
-(Math.Sin(deg) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(deg) * Length) + Fractallines[level - 1][i].Y2
);
Fractallines[level][++x] = new Line(
Fractallines[level - 1][i].X2, Fractallines[level - 1][i].Y2,
-(Math.Sin(-deg) * Length) + Fractallines[level - 1][i].X2, -(Math.Cos(-deg) * Length) + Fractallines[level - 1][i].Y2
);
x++;
}
deltaDeg += deg + 0.1;
level++;
if(level <= levels)
{
NextLevel();
}
}
//private int SumOfBranches()
//{
// double sum = 0;
// for (int i = 0; i <= levels; i++)
// {
// sum += Math.Pow(2, i);
// }
// return (int)sum;
//}
//private void Branch()
//{
// for(int i = 0; i < Convert.ToInt32(Math.Pow(2, level)); i+=2)
// {
// int z = i;
// if(i == 0)
// {
// z = 1;
// }
// Lines[iter + i] = new Line(
// Lines[iter - z].X2, Lines[iter - z].Y2,
// -(Math.Sin(deg) * Length) + Lines[iter - z].X2, -(Math.Cos(deg) * Length) + Lines[iter - z].Y2
// );
// Lines[iter + i + 1] = new Line(
// Lines[iter - z].X2, Lines[iter - z].Y2,
// -(Math.Sin(-deg) * Length) + Lines[iter - z].X2, -(Math.Cos(-deg) * Length) + Lines[iter - z].Y2
// );
// deg += 0.05;
// }
// iter += Convert.ToInt32(Math.Pow(2, level));
// level++;
//}
}
}

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CairoSharp" Version="3.24.24.38" />
<PackageReference Include="GtkSharp" Version="3.24.24.38" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CairoObjective\CairoObjective.csproj" />
</ItemGroup>
</Project>

18
FractalTreeGtk/Program.cs Normal file
View File

@ -0,0 +1,18 @@
using Gtk;
namespace FractalTreeGtk
{
internal class Program
{
static void Main(string[] args)
{
Application.Init();
//Create the Window
Window myWin = new CairoWindow("My first GTK# Application! ");
Application.Run();
}
}
}