You are on page 1of 7

Міністерство освіти і науки України

Харківський національний університет радіоелектроніки

Кафедра програмної інженерії

Звіт
З лабораторної роботи
З дисципліни “ Алгоритми і структури даних”
На тему: “ Динамическое программирование и жадные алгоритмы.”

Виконав:
Ст. гр. ПЗПІ-20-7
Гамалій Костянтин Володимирович

Харків 2021
Завдання (на оцінку «добре»):
Развитие умений и навыков по составлению алгоритмов, используя методы
динамического программирования и жадных алгоритмов.
Для цієї лабораторної роботи мені знадобилось зробити форму з 4-ма
класами.
КЛАС Point
Відповідає за показ місця курсора
class Point
{
public double X;
public double Y;

public Point(double x, double y)


{
X = x;
Y = y;
}

protected Point()
{
throw new System.NotImplementedException();
}

public static bool operator == (Point a, Point b)


{
if(Math.Abs(a.X - b.X) < (double) 1e-6 && Math.Abs(a.Y - b.Y) <
(double) 1e-6)
{
return true;
}
return false;
}

public static bool operator !=(Point a, Point b)


{
return !(a == b);
}
}

КЛАС Polygon
Робить розмітку на полігоні, та відмічає скільки було встановлено
точок.
class Polygon
{
private readonly List<Point> _points;
private readonly int[,] _triangleTable;
private readonly double[,] _table;
private double _triangulationCost;
private readonly List<Triangle> _triangles = new List<Triangle>();

public Polygon(List<Point> points)


{
_points = points;
_triangleTable = new int[points.Count - 1, points.Count - 1];
_table = new double[points.Count - 1, points.Count - 1];
for (int i = 0; i < points.Count - 1; i++)
{
for(int j = 0; j < points.Count - 1; j++)
{
_table[i, j] = -1;
}
}
}

public int[,] TriangleTable => _triangleTable;

public double TriangulationCost => _triangulationCost;

public Triangle[] GetTriangles()


{
if (_points.Count < 3)
{
throw new Exception("n < 3");
}

_triangulationCost = Convert.ToDouble(Triangulation(1,
_points.Count - 1).ToString("N4"));

if(_triangulationCost == Double.MaxValue)
{
throw new Exception("Введён самопересекающийся
многоугольник");
}

CreateTriangles(1, _points.Count - 1);

return _triangles.ToArray();
}

private double Cost(int i, int j, int k)


{
return GetLength(i, j) + GetLength(i, k) + GetLength(j, k);
}

private double GetLength(int i, int k)


{
Point p1 = _points[i];
Point p2 = _points[k];
double res = Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y -
p1.Y, 2));
return Convert.ToDouble(res.ToString("N4"));
}

private double Triangulation(int i, int j)


{
if (_table[i - 1, j - 1] == -1)
{
if (i == j)
{
_table[i - 1, j - 1] = 0;
}
else
{
_table[i - 1, j - 1] = Double.MaxValue;

for (int k = i; k < j; k++)


{
double temp = Triangulation(i, k) + Triangulation(k +
1, j) + Cost(i - 1, k, j);
if (temp < _table[i - 1, j - 1] && CheckTriangle(i -
1, k, j))
{
_table[i - 1, j - 1] = temp;
_triangleTable[i - 1, j - 1] = k;
}
}
}

return _table[i - 1, j - 1];


}

private bool CheckTriangle(int a, int b, int c)


{
/*MessageBox.Show(Intersect(new Point((_points[0].X +
_points[1].X) / 2, (_points[2].Y + _points[1].Y) / 2),
new Point(700, 700), _points[1], _points[0]).ToString());*/
//MessageBox.Show(Check(_points[0], _points[4], _points[4],
_points[0]).ToString());

int countAB = 0, countAC = 0, countBC = 0;


bool abFlag = false, acFlag = false, bcFlag = false;

Point ab = new Point((_points[a].X + _points[b].X) / 2,


(_points[a].Y + _points[b].Y) / 2);
Point abEnd = new Point(700, (_points[a].Y + _points[b].Y) / 2);

Point ac = new Point((_points[a].X + _points[c].X) / 2,


(_points[a].Y + _points[c].Y) / 2);
Point acEnd = new Point(700, (_points[a].Y + _points[c].Y) / 2);

Point bc = new Point((_points[c].X + _points[b].X) / 2,


(_points[c].Y + _points[b].Y) / 2);
Point bcEnd = new Point(700, (_points[c].Y + _points[b].Y) / 2);
for (int j = 1; j <= _points.Count; j++)
{
int i = (j == _points.Count) ? 0 : j;
if ((Intersect(_points[a], _points[b], _points[i], _points[j
- 1])
&& CheckAny(_points[a], _points[b], _points[i],
_points[j - 1]))
|| (Intersect(_points[a], _points[c], _points[i], _points[j
- 1])
&& CheckAny(_points[a], _points[c], _points[i],
_points[j - 1]))
|| (Intersect(_points[b], _points[c], _points[i], _points[j
- 1])
&& CheckAny(_points[b], _points[c], _points[i],
_points[j - 1])))
{
return false;
}
if (Intersect(ab, abEnd, _points[i], _points[j - 1]))
{
if (Check(_points[a], _points[b], _points[i], _points[j -
1]))
{
abFlag = true;
}
countAB++;
}

if (Intersect(ac, acEnd, _points[i], _points[j - 1]))


{
if (Check(_points[a], _points[c], _points[i], _points[j -
1]))
{
acFlag = true;
}
countAC++;
}

if (Intersect(bc, bcEnd, _points[i], _points[j - 1]) )


{
if (Check(_points[b], _points[c], _points[i], _points[j -
1]))
{
bcFlag = true;
}
countBC++;
}
}

if ((countAB % 2 == 0 && !abFlag) || (countAC % 2 == 0 && !


acFlag) || (countBC % 2 == 0 && !bcFlag))
{
return false;
}
return true;
}

private bool CheckAny(Point a, Point b, Point c, Point d)


{
return (a != c && a != d && b != c && b != d);
}

private bool Check(Point a, Point b, Point c, Point d)


{
return ((a == c && b == d) || (a == d && b == c));
}

private double Area(Point a, Point b, Point c)


{
return (b.X - a.X) * (c.Y - a.Y) -
(b.Y - a.Y) * (c.X - a.X);
}

private bool Intersect1(double a, double b, double c, double d)


{
if (a > b)
{
double temp = b;
b = a;
a = temp;
}
if (c > d)
{
double temp = d;
d = c;
c = temp;
}
return Math.Max(a, c) <= Math.Min(b, d);
}

private bool Intersect(Point a, Point b, Point c, Point d)


{
return Intersect1(a.X, b.X, c.X, d.X)
&& Intersect1(a.Y, b.Y, c.Y, d.Y)
&& Area(a, b, c) * Area(a, b, d) <= 0
&& Area(c, d, a) * Area(c, d, b) <= 0;
}
private void CreateTriangles(int i, int j)
{
if (i >= j)
{
return;
}

int k = _triangleTable[i - 1, j - 1];


_triangles.Add(new Triangle(_points[i - 1], _points[k],
_points[j]));
CreateTriangles(i, k);
if(k != 0)
{
CreateTriangles(k + 1, j);
}
}
}

КЛАС Triangle
Заповнює намальовану область трикутниками.
class Triangle
{
public readonly Point P1, P2, P3;

public Triangle(Point p1, Point p2, Point p3)


{
P1 = p1;
P2 = p2;
P3 = p3;
}

public override string ToString()


{
return String.Format($"P1: {P1.X}; {P1.Y} | P2: {P2.X}; {P2.Y} |
P3: {P3.X}; {P3.Y}");
}
}

You might also like