oop_in_c/Polygon.c

105 lines
2.3 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, int 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 = strdup (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
{
2012-05-02 19:55:53 +04:00
dest->npoints = src->npoints;
dest->points = strdup (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_copy (poly, this);
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);
g_free (this);
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
static void __Polygon_draw (const Polygon *this)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
int 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);
}
printf ("{%f;%f}, ", this->points[0].x, this->points[0].y);
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
static double __Polygon_area (const Polygon *this)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
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})
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
static 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;
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;
2012-05-02 19:02:38 +04:00
}
2012-05-02 19:55:53 +04:00
/* public */
Polygon* Polygon_new (const struct point *points, int npoints)
2012-05-02 19:02:38 +04:00
{
2012-05-02 19:55:53 +04:00
Polygon *poly = g_new0 (Polygon, 1);
Polygon_constructor (poly, points, npoints);
return poly;
2012-05-02 19:02:38 +04:00
}