-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape.c
133 lines (109 loc) · 3.25 KB
/
shape.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright © 2024 Austin Berrio
*
* @file shape.c
*
* @brief Shapes
*
* Only use pure C.
* Only use libraries when absolutely necessary.
*
* @note Prefixing related objects, functions, etc. assists with autocomplete.
*/
#include "shape.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Line segment operations
line_segment_t* create_line(size_t cols) {
line_segment_t* line = (line_segment_t*) malloc(sizeof(line_segment_t));
if (NULL == line) {
fprintf(stderr, "Failed to allocate memory for line_segment_t.\n");
return NULL;
}
line->start = create_vector(cols);
if (NULL == line->start) {
fprintf(stderr, "Failed to allocate memory for line start.\n");
free(line);
return NULL;
}
line->end = create_vector(cols);
if (NULL == line->end) {
fprintf(stderr, "Failed to allocate memory for line end.\n");
free_vector(line->start);
free(line);
return NULL;
}
return line;
}
void free_line(line_segment_t* line) {
if (NULL == line) {
fprintf(stderr, "Cannot free a NULL line.\n");
return;
}
if (line->start) {
free_vector(line->start);
}
if (line->end) {
free_vector(line->end);
}
free(line);
}
// Polygon operations
polygon_t* create_polygon(size_t max_vertices) {
polygon_t* polygon = (polygon_t*) malloc(sizeof(polygon_t));
if (NULL == polygon) {
fprintf(stderr, "Failed to allocate memory for polygon_t structure.\n");
return NULL;
}
polygon->vertices = create_vector(max_vertices);
if (NULL == polygon->vertices) {
fprintf(stderr, "Failed to allocate memory for polygon vertices.\n");
free(polygon);
return NULL;
}
polygon->vectices_max = max_vertices;
polygon->vertices_count = 0; // Initialize count of vertices
polygon->height = 0.0f; // Default height
polygon->distance = 0.0f; // Default distance
return polygon;
}
void free_polygon(polygon_t* polygon) {
if (NULL == polygon) {
fprintf(stderr, "Cannot free a NULL polygon.\n");
return;
}
if (polygon->vertices) {
free_vector(polygon->vertices);
}
free(polygon);
}
// Screen-space quadrilateral operations
screen_space_t* create_screen_space(size_t max_vertices) {
screen_space_t* screen = (screen_space_t*) malloc(sizeof(screen_space_t));
if (NULL == screen) {
fprintf(stderr, "Failed to allocate memory for screen_space_t structure.\n");
return NULL;
}
screen->vertices = create_vector(max_vertices);
if (NULL == screen->vertices) {
fprintf(stderr, "Failed to allocate memory for screen vertices.\n");
free(screen);
return NULL;
}
screen->vectices_max = max_vertices;
screen->vertices_count = 0; // Initialize count to 0
screen->depth = 0.0f; // Default depth
screen->id = 0; // Default identifier
return screen;
}
void free_screen_space(screen_space_t* screen) {
if (NULL == screen) {
fprintf(stderr, "Cannot free a NULL screen_space_t structure.\n");
return;
}
if (screen->vertices) {
free_vector(screen->vertices);
}
free(screen);
}