Go home...

This commit is contained in:
Kolan Sh 2012-05-02 19:55:53 +04:00
parent e10bfcc75d
commit 9490c1e935
5 changed files with 83 additions and 29 deletions

View File

@ -3,15 +3,15 @@
/* considered to be protected */ /* considered to be protected */
void Figure_constructor (Figure *this) void Figure_constructor (Figure *this)
{ {
// nothin here // nothing here
} }
void Figure_destructor (Figure *this) void Figure_destructor (Figure *this)
{ {
// nothin here // nothing here
} }
void Figure_copy (struct Figure *dest, const struct Figure *src) void Figure_copy (struct Figure *dest, const struct Figure *src)
{ {
// nothin here // nothing here
} }

View File

@ -1,5 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Polygon.h" #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 */ /* private */
static const char* __Polygon_type () static const char* __Polygon_type ()
{ {
@ -8,47 +31,74 @@ static const char* __Polygon_type ()
static Polygon* __Polygon_clone (const Polygon *this) 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) static void __Polygon_destroy (Polygon *this)
{ {
Polygon_destructor (this);
g_free (this);
} }
static void __Polygon_draw (const Polygon *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) 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) 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 */ /* public */
Polygon* Polygon_new (const struct point *points) Polygon* Polygon_new (const struct point *points, int npoints)
{ {
Polygon *poly = g_new0 (Polygon, 1);
} Polygon_constructor (poly, points, npoints);
return poly;
/* 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)
{
} }

View File

@ -22,6 +22,8 @@ typedef struct Polygon
} *vtable; } *vtable;
/* Polygon fields */ /* Polygon fields */
int npoints;
struct point struct point
{ {
double x, y; double x, y;
@ -30,7 +32,7 @@ typedef struct Polygon
} 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) 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 */ /* 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_destructor (Polygon *this);
void Polygon_copy (Polygon *dest, const Polygon *src); void Polygon_copy (Polygon *dest, const Polygon *src);

View File

@ -37,13 +37,13 @@ static int __Rhomb_is_square (const Rhomb *this)
} }
/* public */ /* public */
Rhomb* Rhomb_new (const struct point *points) Rhomb* Rhomb_new (const struct point *points, int npoints)
{ {
} }
/* considered to be protected */ /* considered to be protected */
void Rhomb_constructor (Rhomb *this, const struct point *points) void Rhomb_constructor (Rhomb *this, const struct point *points, int npoints)
{ {
} }

View File

@ -25,11 +25,13 @@ typedef struct Rhomb
} *vtable; } *vtable;
/* derived from Polygon */ /* derived from Polygon */
int npoints;
struct point *points; struct point *points;
} Rhomb; } 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) 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 */ /* 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_destructor (Rhomb *this);
void Rhomb_copy (Rhomb *dest, const Rhomb *src); void Rhomb_copy (Rhomb *dest, const Rhomb *src);