Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Desenho vetorial II.
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define M_PI 3.14159265358979323846
- typedef enum Figure_t {tri, ret, reg} Figure;
- typedef struct Point_t {
- float x, y;
- } Point;
- typedef struct Object_t {
- int numPoints;
- Point *points;
- } Object;
- typedef struct ObjectArray_t {
- int num;
- int size;
- Object **objs;
- } ObjectArray;
- ObjectArray* createObjectArray () {
- ObjectArray *obja = calloc (sizeof (Object), 1);
- obja->objs = calloc (sizeof (obja->size), 10);
- obja->size = 10;
- obja->num = 0;
- return obja;
- }
- void reCalloc (ObjectArray *obja) {
- obja->size *= 2;
- obja->objs = realloc (obja->objs, obja->size * sizeof (obja->size));
- }
- Object* createTriangle (Point p1, Point p2, Point p3) {
- Object *ntri;
- ntri = calloc (sizeof (Object), 1);
- ntri->numPoints = 3;
- ntri->points = calloc (sizeof (Point), ntri->numPoints);
- ntri->points[0] = p1;
- ntri->points[1] = p2;
- ntri->points[2] = p3;
- return ntri;
- }
- Object* createRect (Point topleft, float width, float height) {
- Object *nret;
- nret = calloc (sizeof (Object), 1);
- nret->numPoints = 4;
- nret->points = calloc (sizeof (Point), nret->numPoints);
- nret->points[0] = topleft;
- nret->points[1].x = topleft.x + width;
- nret->points[1].y = topleft.y;
- nret->points[2].x = topleft.x + width;
- nret->points[2].y = topleft.y - height;
- nret->points[3].x = topleft.x;
- nret->points[3].y = topleft.y - height;
- return nret;
- }
- Object* createRegularFigure (Point center, int num, float distance) {
- float ang = 0;
- Object *nreg;
- nreg = calloc (sizeof (Object), 1);
- nreg->numPoints = num;
- nreg->points = calloc (sizeof (Point), nreg->numPoints);
- for (int i=0; i<num; i++) {
- nreg->points[i].x = distance * cos (ang) + center.x;
- nreg->points[i].y = distance * sin (ang) + center.y;
- ang += (2*M_PI) / num;
- }
- return nreg;
- }
- Object* selectObj (int obj) {
- int op = obj;
- switch (op) {
- case tri: {
- Point p[3];
- for (int i=0; i<3; i++)
- scanf ("%f %f", &p[i].x, &p[i].y);
- Object *tri = createTriangle (p[0], p[1], p[2]);
- return tri;
- }
- case ret: {
- Point p;
- float w, h;
- scanf("%f %f %f %f", &p.x, &p.y, &w, &h);
- Object *ret = createRect (p, w, h);
- return ret;
- }
- case reg: {
- Point p;
- float d;
- int l;
- scanf("%f %f %f %d", &p.x, &p.y, &d, &l);
- Object *reg = createRegularFigure (p, l, d);
- return reg;
- }
- }
- }
- void addObjects (ObjectArray *obja, int obj) {
- if (obja->num == obja->size)
- reCalloc (obja);
- obja->objs[obja->num] = selectObj (obj);
- obja->num += 1;
- }
- void readObjects (ObjectArray *obja, int n) {
- int obj;
- for (int i=0; i<n; i++) {
- scanf ("%d", &obj);
- addObjects (obja, n);
- }
- }
- void printObjectArray (ObjectArray *obja, int n) {
- for (int i=0; i<n; i++) {
- for (int j=0; j<obja->objs[i]->numPoints; j++)
- printf ("(%.2f,%.2f)", obja->objs[i]->points[j].x, obja->objs[i]->points[j].y);
- printf ("\n");
- }
- }
- void destroyObjectArray (ObjectArray *obja, int n) {
- for (int i=0; i<n; i++)
- free (obja->objs[i]->points);
- free (obja->objs);
- free (obja);
- }
- int main () {
- ObjectArray *objar;
- int num;
- scanf ("%d", &num);
- objar = createObjectArray ();
- readObjects (objar, num);
- printObjectArray (objar, num);
- destroyObjectArray (objar, num);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment