YMath/Demo/Form1.cs

175 lines
4.5 KiB
C#
Raw Normal View History

2024-09-29 09:04:42 +00:00
using YMath;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YMath.Geometry;
using System.Diagnostics.CodeAnalysis;
2024-09-29 09:04:42 +00:00
namespace Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateTrackBars();
InitializeImage();
2024-09-29 09:04:42 +00:00
}
2024-09-29 09:04:42 +00:00
double angle = 0;
2024-09-29 09:04:42 +00:00
private void Render(Bitmap bmp)
2024-09-29 09:04:42 +00:00
{
g.Clear(Color.White);
Projector projector3d2d = new Projector(
new Camera(1),
new CoordSystem(new Vector(0, 0, 2), YMath.Matrix.Ident(3)));
Projector projector4d3d = new Projector(
new Camera(1),
new CoordSystem(new Vector(0, 0, 0, 3), YMath.Matrix.Ident(4)));
Geom geom = Geometries.NewCube(4);
double[] angles = angleSelectBars.Select(b => b.Angle).ToArray();
projector4d3d.CoordSystem.RotationMatrix
= YMath.Matrix.Rotation(4, angles);
angle += 0.01;
geom = projector4d3d.Project(geom);
geom = projector3d2d.Project(geom);
foreach (var v in geom.Verticies)
DrawPoint(v);
foreach (var line in geom.Edges)
DrawLine(line);
2024-09-29 09:04:42 +00:00
}
private void DrawPoint(Vector v)
2024-09-29 09:04:42 +00:00
{
var pos = Denormalize(v);
var diameter = 2;
var rect = new RectangleF(
pos.X - diameter, pos.Y - diameter, diameter * 2, diameter * 2);
g.FillEllipse(Brushes.Black, rect);
2024-09-29 09:04:42 +00:00
}
private void DrawLine(LineSegment line)
2024-09-29 09:04:42 +00:00
{
var pen = new Pen(Brushes.Black, 2);
g.DrawLine(pen, Denormalize(line.Start), Denormalize(line.End));
}
2024-09-29 09:04:42 +00:00
private PointF Denormalize(Vector p)
{
p = (p + new Vector(1, 1)) / 2;
p[0] *= bmp.Width;
p[1] = 1 - p[1];
p[1] *= bmp.Height;
return new PointF((float)p[0], (float)p[1]);
2024-09-29 09:04:42 +00:00
}
private void timer1_Tick(object sender, EventArgs e)
2024-09-29 09:04:42 +00:00
{
Render(bmp);
pictureBox1.Refresh();
foreach (var bar in angleSelectBars)
if (bar.Checked)
bar.Angle += 0.03;
}
2024-09-29 09:04:42 +00:00
Bitmap bmp;
Graphics g;
2024-09-29 09:04:42 +00:00
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
InitializeImage();
2024-09-29 09:04:42 +00:00
}
[MemberNotNull(nameof(bmp), nameof(g))]
private void InitializeImage()
2024-09-29 09:04:42 +00:00
{
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Width);
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.HighQuality;
pictureBox1.Image = bmp;
}
AngleBar[] angleSelectBars;
private void CreateTrackBars()
{
angleSelectBars = new AngleBar[6];
for (int i = 0; i < angleSelectBars.Length; i++)
2024-09-29 09:04:42 +00:00
{
var bar = new AngleBar();
bar.Dock = DockStyle.Top;
panel1.Controls.Add(bar);
angleSelectBars[i] = bar;
bar.BringToFront();
2024-09-29 09:04:42 +00:00
}
}
/*
// [-1; 1] -> [0; width]
private static Vector[] RelativeToAbsolute(Vector[] points, int width, int height)
{
return points.Select(p => new Vector(
(p[0] + 1) / 2 * width,
(p[1] + 1) / 2 * height))
.ToArray();
}
private static void DrawLines(Graphics g, Pen pen, Line[] lines)
{
foreach (var line in lines)
g.DrawLine(pen,
new PointF((float)line.Start[0], (float)line.End[1]),
new PointF((float)line.Start[0], (float)line.End[1]));
}
public static Line[] Cube(int nDimensions)
{
if (nDimensions == 0)
return new Vector[] { new Vector(0) };
var result = new List<Vector>();
var offset = new Vector(nDimensions);
offset[nDimensions - 1] = 1;
var face /*боже*//* = Cube(nDimensions - 1);
result.AddRange(face.Select(p => p + offset));
result.AddRange(face.Select(p => p - offset));
}
public static Vector[] CubePoints(double size, int nDimensions)
{
int n = 1 << nDimensions;
if (nDimensions >= 32)
throw new ArgumentException("Fuck yourself");
var result = new Vector[n];
for (int i = 0; i < n; i++)
{
var vec = new Vector(nDimensions);
for (int j = 0; j < nDimensions; j++)
vec[j] = ((i >> j) & 1) - 0.5;
result[i] = vec;
}
return result;
}*/
2024-09-29 09:04:42 +00:00
}
}