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; namespace Demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); CreateTrackBars(); InitializeImage(); } private void Render(Bitmap bmp) { 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); geom = projector4d3d.Project(geom); geom = projector3d2d.Project(geom); foreach (var v in geom.Verticies) DrawPoint(v); foreach (var line in geom.Edges) DrawLine(line); } private void DrawPoint(Vector v) { 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); } private void DrawLine(LineSegment line) { var pen = new Pen(Brushes.Black, 2); g.DrawLine(pen, Denormalize(line.Start), Denormalize(line.End)); } 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]); } private void timer1_Tick(object sender, EventArgs e) { Render(bmp); pictureBox1.Refresh(); foreach (var bar in angleSelectBars) if (bar.Checked) bar.Angle += 0.03; } Bitmap bmp; Graphics g; private void pictureBox1_SizeChanged(object sender, EventArgs e) { InitializeImage(); } [MemberNotNull(nameof(bmp), nameof(g))] private void InitializeImage() { 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++) { var bar = new AngleBar(); bar.Dock = DockStyle.Top; panel1.Controls.Add(bar); angleSelectBars[i] = bar; bar.BringToFront(); } } /* // [-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(); 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; }*/ } }