diff --git a/Figure.c b/Figure.c index 425b42c..6d5f37e 100644 --- a/Figure.c +++ b/Figure.c @@ -3,15 +3,15 @@ /* considered to be protected */ void Figure_constructor (Figure *this) { - // nothin here + // nothing here } void Figure_destructor (Figure *this) { - // nothin here + // nothing here } void Figure_copy (struct Figure *dest, const struct Figure *src) { - // nothin here + // nothing here } diff --git a/Polygon.c b/Polygon.c index ca00ac1..de04a6b 100644 --- a/Polygon.c +++ b/Polygon.c @@ -1,5 +1,28 @@ +#include +#include +#include + #include "Polygon.h" +/* considered to be protected */ +void Polygon_constructor (Polygon *this, const struct point *points, int npoints) +{ + Figure_constructor ((Figure *) this); + this->npoints = npoints; + this->points = strdup (points, npoints * sizeof (struct point)); +} + +void Polygon_destructor (Polygon *this) +{ + free (this->points); +} + +void Polygon_copy (Polygon *dest, const Polygon *src) +{ + dest->npoints = src->npoints; + dest->points = strdup (src->points, src->npoints * sizeof (struct point)); +} + /* private */ static const char* __Polygon_type () { @@ -8,47 +31,74 @@ static const char* __Polygon_type () static Polygon* __Polygon_clone (const Polygon *this) { - + Polygon *poly = malloc (sizeof (Polygon)); + memset (poly, 0, sizeof ( + Polygon_copy (poly, this); + return poly; } static void __Polygon_destroy (Polygon *this) { - + Polygon_destructor (this); + g_free (this); } static void __Polygon_draw (const Polygon *this) { + int i = 0; + for (i = 0; i < this->npoints; i++) + { + printf ("{%f;%f}, ", this->points[i].x, this->points[i].y); + } + printf ("{%f;%f}, ", this->points[0].x, this->points[0].y); } static double __Polygon_area (const Polygon *this) { + double s = 0.0, x, y; + int 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 s * 0.5; + // Trapezium rule for convex and non-convex polygons + // S=abs(сумма{(x[i+1]-x[i])*(y[i+1]+y[i])/2}) } static double __Polygon_max_diag (const Polygon *this) { + double diag = 0.0, + tmp = 0.0, + tmp1 = 0.0, + tmp2 = 0.0; + int 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) +Polygon* Polygon_new (const struct point *points, int npoints) { - -} - -/* considered to be protected */ -void Polygon_constructor (Polygon *this, const struct point *points) -{ - -} - -void Polygon_destructor (Polygon *this) -{ - -} - -void Polygon_copy (Polygon *dest, const Polygon *src) -{ - + Polygon *poly = g_new0 (Polygon, 1); + Polygon_constructor (poly, points, npoints); + return poly; } diff --git a/Polygon.h b/Polygon.h index 1176904..6f44adb 100644 --- a/Polygon.h +++ b/Polygon.h @@ -22,6 +22,8 @@ typedef struct Polygon } *vtable; /* Polygon fields */ + int npoints; + struct point { double x, y; @@ -30,7 +32,7 @@ typedef struct Polygon } Polygon; -Polygon* Polygon_new (const struct point *points); +Polygon* Polygon_new (const struct point *points, int npoints); static inline const char* Polygon_type (const Polygon *this) { @@ -63,7 +65,7 @@ static inline double Polygon_max_diag (const Polygon *this) } /* considered to be protected */ -void Polygon_constructor (Polygon *this, const struct point *points); +void Polygon_constructor (Polygon *this, const struct point *points, int npoints); void Polygon_destructor (Polygon *this); void Polygon_copy (Polygon *dest, const Polygon *src); diff --git a/Rhomb.c b/Rhomb.c index 3a21248..9510730 100644 --- a/Rhomb.c +++ b/Rhomb.c @@ -37,13 +37,13 @@ static int __Rhomb_is_square (const Rhomb *this) } /* public */ -Rhomb* Rhomb_new (const struct point *points) +Rhomb* Rhomb_new (const struct point *points, int npoints) { } /* considered to be protected */ -void Rhomb_constructor (Rhomb *this, const struct point *points) +void Rhomb_constructor (Rhomb *this, const struct point *points, int npoints) { } diff --git a/Rhomb.h b/Rhomb.h index 5ba5c0b..96907bb 100644 --- a/Rhomb.h +++ b/Rhomb.h @@ -25,11 +25,13 @@ typedef struct Rhomb } *vtable; /* derived from Polygon */ + int npoints; + struct point *points; } Rhomb; -Rhomb* Rhomb_new (const struct point *points); +Rhomb* Rhomb_new (const struct point *points, int npoints); static inline const char* Rhomb_type (const Rhomb *this) { @@ -67,7 +69,7 @@ static inline int Rhomb_is_square (const Rhomb *this) } /* considered to be protected */ -void Rhomb_constructor (Rhomb *this, const struct point *points); +void Rhomb_constructor (Rhomb *this, const struct point *points, int npoints); void Rhomb_destructor (Rhomb *this); void Rhomb_copy (Rhomb *dest, const Rhomb *src);