C - Structures for point, polygonal line and polygon

1

Good evening

The exercise asks me to implement three structs: Point (X and Y coordinates), polygonal lines (sequence of 2 to 100 points that are the vertices along the line) and polygon (sequence of 3 to 100 points that are the vertices along the polygon where the latter coincides with the first).

I have implemented the struct for the points as follows:

typedef struct //Estrutura definida para os pontos.
{
    double x;
    double y;
} Ponto;

For the polygonal lines I defined as follows, but I do not know if it is the correct one and if it is the best form.

 typedef struct //Estrutura definida para as linhas poligonais.
 {
     Ponto Vertice;
 } Linha;

Now for the polygons I did not have an idea how to do it. But I know the difference between a polygon line and a polygon is that for the polygon, the last and first coordinates are the same (how do I specify this already inside the struct?). I would like an idea of how to proceed.

    
asked by anonymous 29.05.2017 / 04:13

1 answer

2

Although you do not need this for the answer to your question, a line is defined by two points: start point and end point. So it should be:

typedef struct {
    Ponto p0, p1;
} Linha;

A polygon line ( polyline or polilinha ) consists of several points, each consecutive two forming a segment. Since in your problem you have at most 100 elements, we can define a polygon line as:

typedef struct {
    int n_pontos; /* quantidade de pontos usados nessa polyline */
    Ponto p[100]; /* vetor com os pontos usados */
} Polilinha;

A polygon is a special case of a line, in which the last vertex joins the first vertex. For example, to represent a triangle, I need to define three points; let's say we have the triangle (0,0) , (3,0) , (0,4) , this is enough to deduce the existence of the following segments:

 (0,0) -> (3,0)
 (3,0) -> (0,4)
 (0,4) -> (0,0)

If these same three points were passed to a polyline (in that order), the segments would be:

 (0,0) -> (3,0)
 (3,0) -> (0,4)

So internally, you can represent with the same structure, but you must manipulate it differently; hence:

typedef Polilinha Poligono;
    
29.05.2017 / 05:20