Browse Source

Small pointer improvements (removed some double pointers that weren't needed)

master
Taylor Bockman 5 years ago
parent
commit
2551efe8ed
  1. 4
      include/vertex.h
  2. 40
      src/main.c
  3. 13
      src/vertex.c

4
include/vertex.h

@ -32,8 +32,8 @@ struct Vertex *new_vertex(int number, int x, int y);
* @param parent The parent vertex to add the new one to. * @param parent The parent vertex to add the new one to.
* @param child The child vertex. * @param child The child vertex.
*/ */
void add_vertex(struct Vertex **head, struct Vertex **parent, void add_vertex(struct Vertex *head, struct Vertex *parent,
struct Vertex **child); struct Vertex *child);
/** /**
* Frees a single vertex. This does not free it's next and previous pointers. * Frees a single vertex. This does not free it's next and previous pointers.

40
src/main.c

@ -24,27 +24,23 @@ void chapter1() {
struct Vertex *v16 = new_vertex(16, -2, 9); struct Vertex *v16 = new_vertex(16, -2, 9);
struct Vertex *v17 = new_vertex(17, 5, 5); struct Vertex *v17 = new_vertex(17, 5, 5);
// TODO: You may be able to change this back to normal as long as you're add_vertex(v0, v0, v1);
// using -> to set the value on the struct. This is interpreted add_vertex(v0, v1, v2);
// as (*thing).xyz so you are doing too much work on pointers add_vertex(v0, v2, v3);
// in this. add_vertex(v0, v3, v4);
add_vertex(&v0, &v0, &v1); add_vertex(v0, v4, v5);
add_vertex(&v0, &v1, &v2); add_vertex(v0, v5, v6);
add_vertex(&v0, &v2, &v3); add_vertex(v0, v6, v7);
add_vertex(&v0, &v3, &v4); add_vertex(v0, v7, v8);
add_vertex(&v0, &v4, &v5); add_vertex(v0, v8, v9);
add_vertex(&v0, &v5, &v6); add_vertex(v0, v9, v10);
add_vertex(&v0, &v6, &v7); add_vertex(v0, v10, v11);
add_vertex(&v0, &v7, &v8); add_vertex(v0, v11, v12);
add_vertex(&v0, &v8, &v9); add_vertex(v0, v12, v13);
add_vertex(&v0, &v9, &v10); add_vertex(v0, v13, v14);
add_vertex(&v0, &v10, &v11); add_vertex(v0, v14, v15);
add_vertex(&v0, &v11, &v12); add_vertex(v0, v15, v16);
add_vertex(&v0, &v12, &v13); add_vertex(v0, v16, v17);
add_vertex(&v0, &v13, &v14);
add_vertex(&v0, &v14, &v15);
add_vertex(&v0, &v15, &v16);
add_vertex(&v0, &v16, &v17);
init_ear(v0); init_ear(v0);
@ -52,8 +48,6 @@ void chapter1() {
triangulate(v0); triangulate(v0);
//print_polygon(v0);
free_polygon(&v0); free_polygon(&v0);
} }

13
src/vertex.c

@ -18,13 +18,13 @@ struct Vertex *new_vertex(int number, int x, int y)
return newv; return newv;
} }
void add_vertex(struct Vertex **head, struct Vertex **parent, void add_vertex(struct Vertex *head, struct Vertex *parent,
struct Vertex **child) struct Vertex *child)
{ {
(*parent)->next = *child; parent->next = child;
(*child)->prev = *parent; child->prev = parent;
(*child)->next = *head; child->next = head;
(*head)->prev = *child; head->prev = child;
}; };
void free_vertex(struct Vertex *vertex) void free_vertex(struct Vertex *vertex)
@ -57,7 +57,6 @@ void print_vertex(const struct Vertex *vertex)
void print_polygon(const struct Vertex *head) { void print_polygon(const struct Vertex *head) {
const struct Vertex *v = head->next; const struct Vertex *v = head->next;
printf("-------");
while(v != head) { while(v != head) {
print_vertex(v); print_vertex(v);
v = v->next; v = v->next;

Loading…
Cancel
Save