-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPolygon.c
119 lines (102 loc) · 2.67 KB
/
Polygon.c
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Polygon.h"
/* considered to be protected */
void Polygon_constructor (Polygon *this, const struct point *points, size_t npoints)
{
Figure_constructor ((Figure *) this);
this->npoints = npoints;
this->points = malloc (npoints * sizeof (struct point));
memcpy (this->points, points, npoints * sizeof (struct point));
}
void Polygon_destructor (Polygon *this)
{
free (this->points);
}
void Polygon_copy (Polygon *dest, const Polygon *src)
{
free (dest->points);
dest->npoints = src->npoints;
dest->points = malloc (src->npoints * sizeof (struct point));
memcpy (dest->points, src->points, src->npoints * sizeof (struct point));
}
/* private */
static const char* __Polygon_type ()
{
return "Polygon";
}
static Polygon* __Polygon_clone (const Polygon *this)
{
Polygon *poly = malloc (sizeof (Polygon));
memset (poly, 0, sizeof (Polygon));
Polygon_copy (poly, this);
poly->vtable = this->vtable;
return poly;
}
static void __Polygon_destroy (Polygon *this)
{
Polygon_destructor (this);
free (this);
}
void __Polygon_draw (const Polygon *this)
{
size_t i = 0;
for (i = 0; i < this->npoints; i++)
{
printf ("{%f;%f}, ", this->points[i].x, this->points[i].y);
}
printf ("{%f;%f}\n", this->points[0].x, this->points[0].y);
}
double __Polygon_area (const Polygon *this)
{
double s = 0.0;
size_t i = 0;
for (i = 0; i < this->npoints; i++)
{
s += this->points[i].x * this->points[(i + 1) % this->npoints].y -
this->points[(i + 1) % this->npoints].x * this->points[i].y;
}
return fabs (s) * 0.5;
// Trapezium rule for convex and non-convex polygons
// S=abs(сумма{(x[i+1]-x[i])*(y[i+1]+y[i])/2})
}
double __Polygon_max_diag (const Polygon *this)
{
double diag = 0.0,
tmp = 0.0,
tmp1 = 0.0,
tmp2 = 0.0;
size_t i = 0, j = 0;
for (i = 0; i < this->npoints - 2; i++)
{
for (j = i + 2; j < this->npoints; j++)
{
tmp1 = (this->points[i].x - this->points[j].x);
tmp2 = (this->points[i].y - this->points[j].y);
tmp = sqrt (tmp1 * tmp1 + tmp2 * tmp2);
if (diag < tmp)
{
diag = tmp;
}
}
}
return diag;
}
/* public */
Polygon* Polygon_new (const struct point *points, size_t npoints)
{
static struct Polygon_vtable vtable = {
__Polygon_type,
__Polygon_clone,
__Polygon_destroy,
__Polygon_draw,
__Polygon_area,
__Polygon_max_diag
};
Polygon *poly = malloc (sizeof (Polygon));
memset (poly, 0, sizeof (Polygon));
Polygon_constructor (poly, points, npoints);
poly->vtable = &vtable;
return poly;
}