forked from stas1f1/Affine3D
/
Objects.cs
186 lines (156 loc) · 6.49 KB
/
Objects.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Affine3D
{
public class PointD
{
public double X { get; set; }
public double Y { get; set; }
public PointD() { X = 0; Y = 0; }
public PointD(double x, double y) { X = x; Y = y; }
public PointD(int x, int y) { X = x; Y = y; }
public PointD(Point p) { X = p.X; Y = p.Y; }
public Point ToPoint() { return new Point((int)X, (int)Y); }
static public bool operator ==(PointD p1, PointD p2)
{
return p1.X == p2.X && p1.Y == p2.Y;
}
static public bool operator !=(PointD p1, PointD p2)
{
return p1.X != p2.X || p1.Y != p2.Y;
}
static public PointD operator +(PointD p1, PointD p2)
{
return new PointD(p1.X + p2.X, p1.Y + p2.Y);
}
static public PointD operator -(PointD p1, PointD p2)
{
return new PointD(p1.X - p2.X, p1.Y - p2.Y);
}
}
/// <summary>
/// Класс трехмерной точки с координатами типа double
/// </summary>
public class Point3D
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public Point3D() { X = 0; Y = 0; Z = 0; }
public Point3D(double x, double y, double z) { X = x; Y = y; Z = z; }
public Point3D(int x, int y, int z) { X = x; Y = y; Z = z; }
static public bool operator ==(Point3D p1, Point3D p2)
{
return p1.X == p2.X && p1.Y == p2.Y && p1.Z == p2.Z;
}
static public bool operator !=(Point3D p1, Point3D p2)
{
return p1.X != p2.X || p1.Y != p2.Y || p1.Z != p2.Z;
}
static public Point3D operator +(Point3D p1, Point3D p2)
{
return new Point3D(p1.X + p2.X, p1.Y + p2.Y, p1.Z + p2.Z);
}
static public Point3D operator -(Point3D p1, Point3D p2)
{
return new Point3D(p1.X - p2.X, p1.Y - p2.Y, p1.Z - p2.Z);
}
public override string ToString()
{
return "{"+ X + ", " + Y + ", " + Z + "}";
}
}
// абстрактный класс фигуры, которая может быть нарисована
public abstract class Figure { }
// класс двумерного ребра (линии)
public class Line : Figure
{
public PointD From { get; set; }
public PointD To { get; set; }
public Line() { From = new PointD(); To = new PointD(); }
public Line(PointD p1, PointD p2) { From = p1; To = p2; }
public Line(Point p1, Point p2) { From = new PointD(p1); To = new PointD(p2); }
public Line(double x1, double y1, double x2, double y2) { From = new PointD(x1, y1); To = new PointD(x2, y2); }
public Line(int x1, int y1, int x2, int y2) { From = new PointD(x1, y1); To = new PointD(x2, y2); }
}
// класс трехмерного ребра (линии)
public class Line3D
{
public Point3D From { get; set; }
public Point3D To { get; set; }
public Line3D() { From = new Point3D(); To = new Point3D(); }
public Line3D(Point3D p1, Point3D p2) { From = p1; To = p2; }
public Line3D(double x1, double y1, double z1, double x2, double y2, double z2)
{
From = new Point3D(x1, y1, z1); To = new Point3D(x2, y2, z2);
}
public Line3D(int x1, int y1, int z1, int x2, int y2, int z2)
{
From = new Point3D(x1, y1, z1); To = new Point3D(x2, y2, z2);
}
}
// класс многоугольника (грани)
public class Polygon : Figure
{
public List<PointD> Points { get; set; } = new List<PointD>();
public Polygon() { }
public Polygon(List<PointD> points) { Points = points; }
public Polygon(List<Point> points) { Points = points.Select(point => new PointD(point)).ToList(); }
public void AddPoint(PointD point) { Points.Add(point); }
public void AddPoint(Point point) { Points.Add(new PointD(point)); }
public void AddPoint(int x, int y) { Points.Add(new PointD(x, y)); }
public void AddPoint(double x, double y) { Points.Add(new PointD(x, y)); }
}
// класс многогранника
public class Polyhedron
{
// список вершин
public List<Point3D> Vertices { get; set; } = new List<Point3D>();
// список ребер
public List<Line3D> Edges { get; } = new List<Line3D>();
// матрица смежности
public Dictionary<Point3D, List<Point3D>> AdjacencyMatrix { get; } = new Dictionary<Point3D, List<Point3D>>();
public Point3D Center
{
get
{
double x = Vertices.Select(point => point.X).Average();
double y = Vertices.Select(point => point.Y).Average();
double z = Vertices.Select(point => point.Z).Average();
return new Point3D(x, y, z);
}
}
public Polyhedron() { }
public Polyhedron(List<Point3D> points)
{
Vertices = points;
foreach (var point in points) AdjacencyMatrix.Add(point, new List<Point3D>());
}
public void AddVertex(Point3D point)
{
Vertices.Add(point);
AdjacencyMatrix.Add(point, new List<Point3D>());
}
public void AddVertex(int x, int y, int z) { AddVertex(new Point3D(x, y, z)); }
public void AddVertex(double x, double y, double z) { AddVertex(new Point3D(x, y, z)); }
public void AddEdge(Point3D p1, Point3D p2)
{
Edges.Add(new Line3D(p1, p2));
Point3D point1 = Vertices.Find(p => p == p1);
Point3D point2 = Vertices.Find(p => p == p2);
if (!AdjacencyMatrix.ContainsKey(point1)) AdjacencyMatrix.Add(point1, new List<Point3D> { p2 });
else AdjacencyMatrix[point1].Add(p2);
if (!AdjacencyMatrix.ContainsKey(point2)) AdjacencyMatrix.Add(point2, new List<Point3D> { p1 });
else AdjacencyMatrix[point2].Add(p1);
}
// добавить семейство ребер из точки from в каждую точку списка to
public void AddEdges(Point3D from, List<Point3D> to)
{
foreach (var p2 in to) AddEdge(from, p2);
}
}
}