C - Structures for point, polygonal line and polygon

Asked

Viewed 264 times

1

Good night

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

I implemented the struct for the dots 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 correct and if it is the best way.

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

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

1 answer

2


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

typedef struct {
    Ponto p0, p1;
} Linha;

A polygonal line (polyline or polilinha) consists of several points, each two consecutive forming a segment. As in your problem you have at most 100 elements, we can define a polygonal 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 polyline, in which the last vertex binds to 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), is sufficient to deduce the existence of the following segments:

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

If these same three dots had been passed to a polyline (in the same order), the segments would be:

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

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

typedef Polilinha Poligono;
  • 1

    Thanks Jefferson! I forgot that I need to define the amount of points of the line within the structure itself. In the case of polygons, when you say manipulate, you mean manipulate within the program itself, right?

  • 1

    This, manipulating in the sense of using functions / accessing their values

Browser other questions tagged

You are not signed in. Login or sign up in order to post.