title: Probelm with struct and typedef
body: <p>I have to develop these 4 functions:</p>
  <ol>
  <li><code>double distance (const point_t * a, const point_t * b)</code></li>
  <li><code>double perimeter (const polygon_t * poly)</code></li>
  <li><code>polygon_t create_triangle (point_t p0, double base, double height)</code></li>
  <li><code>polygon_t create_square (point_t p0, double side)</code></li>
  </ol>
  <p>The rest of the body of the program was already defined, but I can't figure out how to develop the last two functions, I would need someone to point me in the correct way.</p>
  <p>My idea was to initialize the vector to then be able to make the square and the triangle from this but I have a difficulty in developing the function.
  Can you help me?</p>
  <p>P.S. I leave you below the delivery of the exercise and what I wrote myself:</p>
  <p>&quot;The program represents polygons of any number of sides using dynamic arrays of points. Triangles and squares, as notable polygons, can be conveniently constructed using the create_triangle() and create_square() shortcuts.
  To construct other polygons it is necessary to manually populate the struct polygon_t through the appropriate assignments.
  The main () constructs a triangle and a square and calculates the perimeter by calling the perimeter () function. The types polygon_t and point_t and the print_polygon () function are already implemented, they are located at the top of the source and are not editable.</p>
  <p>Implement the following functions so that main() compiles and the program is complete:
  the function <code>polygon_t create_triangle (point_t p0, double base, double height)</code> so that it returns by value a structure of type <code>polygon_t</code> suitably populated with the points that form an isosceles triangle having point of origin (bottom left point) in p0, base base length and height length height;</p>
  <p>the function <code>polygon_t create_square (point_t p0, double side)</code> so that it returns by value a structure of the <code>polygon_t</code> type suitably populated with the points that form a square having a point of origin (bottom left point) in p0, and side length side ;</p>
  <p>the function <code>double perimeter (const polygon_t * poly)</code> so that it calculates the perimeter of the polygon passed as an argument: use the <code>distance()</code> function between all the pairs of contiguous points and calculate the sum of the sides;
  do not make assumptions about the number of sides: the data structure can represent any polygons;</p>
  <p>the function <code>double distance (const point_t * a, const point_t * b)</code> which calculates the Euclidean distance between the two data points.</p>
  <p>Example: the following program constructs a square with side 2 and a triangle with base 1 and height 0.86602; then calculate the perimeter of both.&quot;</p>
  <pre><code>#include &lt;assert.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;limits.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stddef.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;malloc.h&gt;
  
  char* readline();
  char* ltrim(char*);
  char* rtrim(char*);
  
  double parse_double(char*);
  
  
  typedef struct {
    double x, y;
  } point_t;
  
  typedef struct {
    size_t len;
    point_t* points;
  } polygon_t;
  
  void print_polygon(const polygon_t* poly) {
    size_t i;
    printf(&quot;polygon_t(%ld)[&quot;, poly-&gt;len);
    for (i = 0; i &lt; poly-&gt;len; ++i) {
        printf(&quot;(%g, %g) &quot;, poly-&gt;points[i].x, poly-&gt;points[i].y);
    }
    printf(&quot;\b]&quot;);
  }
  /* da implementare */
  double distance(const point_t* a, const point_t* b) {
    float dist;
  
    point_t* x = (point_t*) a;
    point_t* y = (point_t*) b;
  
    dist= sqrt((a-&gt;x - b-&gt;x) + (a-&gt;y - b-&gt;y));
  
    return dist;
   }
  
   /* da implementare: deve usare distance() */
   double perimeter(const polygon_t* poly) {
  
       float per_tr= distance(poly-&gt;points, poly-&gt;points) + distance(poly-&gt;points, poly-&gt;points) + 
       distance(poly-&gt;points, poly-&gt;points);
       float per_sq= distance(poly-&gt;points, poly-&gt;points) * 4;
  
    return per_sq &amp;&amp; per_tr;
  
   }
  
   /* da implementare */
   polygon_t create_triangle(point_t p0, double base, double height) {
     size_t i;
     point_t p2;
     polygon_t p1;
  
  
     for (i = 0; i &lt; p1.len; ++i)
        p1.points[i].x= 0;
     //base= distance(p1.points, p1.points);
     //height= sqrt(pow(distance(p1.points, p1.points),2) - (pow(base,2)/4.0)) ;
        p1.len[p1.points].x= 0;
  
  
        return p1;
    }
  
    /* da implementare */
    polygon_t create_square(point_t p0, double side) {
      polygon_t p2;
      size_t i;
      p0.x = 0; 
      p0.y = 0;
  
      for (i = 0; i &lt; p2.len; ++i)
         p2.points[i].x= 0;
      }
  
  
      p2.len[p2.points]= p2.points[side * 4,0];
      // side= distance(p2.points, p2.points);
  
      return p2; 
  
    }
  
    int main()
    {
      FILE* fptr = fopen(getenv(&quot;OUTPUT_PATH&quot;), &quot;w&quot;);
  
     double x = parse_double(ltrim(rtrim(readline())));
     double y = parse_double(ltrim(rtrim(readline())));
     double l = parse_double(ltrim(rtrim(readline())));
     double b = parse_double(ltrim(rtrim(readline())));
     double h = parse_double(ltrim(rtrim(readline())));
  
     point_t origin;
     polygon_t sq;
     polygon_t tr;
     origin.x = x;  /* input riga #1 */
     origin.y = y;  /* input riga #2 */
     sq = create_square(origin, l); /* input riga #3 */
     tr = create_triangle(origin, b, h); /* input riga #4, #5 */
  
     double p1 = perimeter(&amp;sq);
     double p2 = perimeter(&amp;tr);
     fprintf(fptr, &quot;%.2f\n&quot;, p1); /* output riga #1 */
     fprintf(fptr, &quot;%.2f\n&quot;, p2); /* output riga #2 */
  
     fclose(fptr);
  
    return 0;
    }
  
    char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
  
    char* data = malloc(alloc_length);
  
    while (true) {
      char* cursor = data + data_length;
      char* line = fgets(cursor, alloc_length - data_length, stdin);
  
      if (!line) {
          break;
      }
  
      data_length += strlen(cursor);
  
      if (data_length &lt; alloc_length - 1 || data[data_length - 1] == '\n') {
          break;
      }
  
      alloc_length &lt;&lt;= 1;
  
      data = realloc(data, alloc_length);
  
      if (!data) {
          data = '\0';
  
          break;
      }
    }
  
     if (data[data_length - 1] == '\n') {
         data[data_length - 1] = '\0';
  
       data = realloc(data, data_length);
  
      if (!data) {
          data = '\0';
      }
  } else {
      data = realloc(data, data_length + 1);
  
      if (!data) {
          data = '\0';
      } else {
          data[data_length] = '\0';
      }
    }
  
    return data;
    }
  
    char* ltrim(char* str) {
      if (!str) {
        return '\0';
    }
  
    if (!*str) {
      return str;
    }
  
    while (*str != '\0' &amp;&amp; isspace(*str)) {
      str++;
    }
  
    return str;
   }
  
   char* rtrim(char* str) {
     if (!str) {
      return '\0';
     }
  
     if (!*str) {
      return str;
     }
  
     char* end = str + strlen(str) - 1;
  
     while (end &gt;= str &amp;&amp; isspace(*end)) {
      end--;
     }
  
     *(end + 1) = '\0';
  
      return str;
     }
  
     double parse_double(char* str) {
      char* endptr;
      double value = strtod(str, &amp;endptr);
  
      if (endptr == str || *endptr != '\0') {
       exit(EXIT_FAILURE);
      }
  
     return value;
  }
  </code></pre>