diff --git a/Polygon.c b/Polygon.c index 4a27b00..1319c21 100644 --- a/Polygon.c +++ b/Polygon.c @@ -37,6 +37,7 @@ 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; } @@ -54,7 +55,7 @@ void __Polygon_draw (const Polygon *this) { printf ("{%f;%f}, ", this->points[i].x, this->points[i].y); } - printf ("{%f;%f}, ", this->points[0].x, this->points[0].y); + printf ("{%f;%f}\n", this->points[0].x, this->points[0].y); } double __Polygon_area (const Polygon *this) diff --git a/Rhomb.c b/Rhomb.c index 01310e2..acaf6d0 100644 --- a/Rhomb.c +++ b/Rhomb.c @@ -31,6 +31,7 @@ static Rhomb* __Rhomb_clone (const Rhomb *this) Rhomb *rhomb = malloc (sizeof (Rhomb)); memset (rhomb, 0, sizeof (Rhomb)); Rhomb_copy (rhomb, this); + rhomb->vtable = this->vtable; return rhomb; } @@ -67,7 +68,7 @@ static int __Rhomb_is_square (const Rhomb *this) tmp2 = this->points[1].y - this->points[3].y; diag2 = tmp1 * tmp1 + tmp2 * tmp2; - return (diag1 == diag2); + return (fabs (diag1 - diag2) < 1e-12); } /* public */ diff --git a/main.c b/main.c index 370413c..6a8c7e7 100644 --- a/main.c +++ b/main.c @@ -4,46 +4,82 @@ int main (void) { - Polygon_destroy (NULL); -/* unsigned long i = 0; + unsigned long i = 0; Figure *fig[3]; + static struct point points1[] = { + { + 3.1, + 2.8 + }, + { + 8.1, + -3.1 + }, + { + -1.88, + 2.11 + }, + { + 3.0, + 4.1 + }, + }; + static struct point points2[] = { + { + 8.2, + 4.2 + }, + { + 1.1, + 2.2 + }, + { + 7.7, + 5.6 + }, + { + -12.35, + -8.3 + }, + { + -11.13, + -1.1 + }, + { + 2.3, + -8.5 + }, + }; - fig[0] = (Figure *) Square_new (1); - fig[1] = (Figure *) ColoredSquare_new (2, 2); + fig[0] = (Figure *) Polygon_new (points2, sizeof (points2) / sizeof (points2[0])); + fig[1] = (Figure *) Rhomb_new (points1); - ((ColoredSquare *) fig[1])->vtable->set_color ((ColoredSquare *) fig[1], 5); - ((Square *) fig[0])->vtable->resize ((Square *) fig[0], 2); - ((Square *) fig[1])->vtable->resize ((Square *) fig[0], 3); - - fig[2] = fig[1]->vtable->clone (fig[1]); - ((ColoredSquare *) fig[2])->vtable->set_color ((ColoredSquare *) fig[2], 3); + printf ("Is rhomb square? %d\n", Rhomb_is_square ((Rhomb *) fig[1])); + printf ("Max diag = %f\n", Polygon_max_diag ((Polygon *) fig[0])); + fig[2] = Figure_clone (fig[1]); + printf ("Area = %f\n", Figure_area (fig[2])); for (i = 0; i < sizeof (fig) / sizeof (Figure *); i++) { puts ("---"); - printf ("type = %s\n", fig[i]->vtable->type (fig[i])); + printf ("type = %s\n", Figure_type (fig[i])); - fig[i]->vtable->draw (fig[i]); + Figure_draw (fig[i]); printf ("area(%lu) = %f\n", (unsigned long) fig[i], - fig[i]->vtable->area (fig[i])); - - printf ("diag_length(%lu) = %f\n", - (unsigned long) fig[i], - ((Square *) fig[i])->vtable->diag_length ((Square *) fig[i])); + Figure_area (fig[i])); } puts ("---"); /*goto end; err: end:*/ -/* for (i = 0; i < sizeof (fig) / sizeof (Figure *); i++) + for (i = 0; i < sizeof (fig) / sizeof (Figure *); i++) { - fig[i]->vtable->destroy (fig[i]); + Figure_destroy (fig[i]); } -*/ return 0; }