oop_in_c/Polygon.c

120 lines
2.7 KiB
C
Raw Normal View History

2012-05-02 19:55:53 +04:00
#include <stdio.h>
#include <string.h>
#include <math.h>
2012-05-02 19:02:38 +04:00
#include "Polygon.h"
2012-05-02 19:55:53 +04:00
/* considered to be protected */
void Polygon_constructor (Polygon *this, const struct point *points, size_t npoints)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
Figure_constructor ((Figure *) this);
this->npoints = npoints;
this->points = malloc (npoints * sizeof (struct point));
memcpy (this->points, points, npoints * sizeof (struct point));
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
void Polygon_destructor (Polygon *this)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
free (this->points);
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
void Polygon_copy (Polygon *dest, const Polygon *src)
2012-05-02 19:02:38 +04:00
{
free (dest->points);
2012-05-02 19:55:53 +04:00
dest->npoints = src->npoints;
dest->points = malloc (src->npoints * sizeof (struct point));
memcpy (dest->points, src->points, src->npoints * sizeof (struct point));
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
/* private */
static const char* __Polygon_type ()
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
return "Polygon";
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
static Polygon* __Polygon_clone (const Polygon *this)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
Polygon *poly = malloc (sizeof (Polygon));
memset (poly, 0, sizeof (Polygon));
2012-05-02 19:55:53 +04:00
Polygon_copy (poly, this);
2012-05-03 01:36:00 +04:00
poly->vtable = this->vtable;
2012-05-02 19:55:53 +04:00
return poly;
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
static void __Polygon_destroy (Polygon *this)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
Polygon_destructor (this);
free (this);
2012-05-02 19:02:38 +04:00
}
void __Polygon_draw (const Polygon *this)
2012-05-02 19:02:38 +04:00
{
size_t i = 0;
2012-05-02 19:02:38 +04:00
2012-05-02 19:55:53 +04:00
for (i = 0; i < this->npoints; i++)
{
printf ("{%f;%f}, ", this->points[i].x, this->points[i].y);
}
2012-05-03 01:36:00 +04:00
printf ("{%f;%f}\n", this->points[0].x, this->points[0].y);
2012-05-02 19:02:38 +04:00
}
double __Polygon_area (const Polygon *this)
2012-05-02 19:02:38 +04:00
{
double s = 0.0;
size_t i = 0;
2012-05-02 19:55:53 +04:00
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;
}
2012-05-03 16:04:04 +04:00
return fabs (s) * 0.5;
2012-05-02 19:55:53 +04:00
// Trapezium rule for convex and non-convex polygons
// S=abs(сумма{(x[i+1]-x[i])*(y[i+1]+y[i])/2})
2012-05-02 19:02:38 +04:00
}
double __Polygon_max_diag (const Polygon *this)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
double diag = 0.0,
tmp = 0.0,
tmp1 = 0.0,
tmp2 = 0.0;
size_t i = 0, j = 0;
2012-05-02 19:55:53 +04:00
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;
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
/* public */
Polygon* Polygon_new (const struct point *points, size_t npoints)
2012-05-02 19:02:38 +04:00
{
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));
2012-05-02 19:55:53 +04:00
Polygon_constructor (poly, points, npoints);
poly->vtable = &vtable;
2012-05-02 19:55:53 +04:00
return poly;
2012-05-02 19:02:38 +04:00
}