-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex.c
95 lines (82 loc) · 2.5 KB
/
vertex.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
/*
** EPITECH PROJECT, 2018
** cpp_rush1
** File description:
** Exercice 02
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include "new.h"
#include "vertex.h"
typedef struct
{
Class base;
int x, y, z;
} VertexClass;
static void Vertex_ctor(VertexClass *this, va_list *args)
{
if (this == NULL)
raise("This must be initialized");
this->x = va_arg(*args, int);
this->y = va_arg(*args, int);
this->z = va_arg(*args, int);
}
static void Vertex_dtor(VertexClass *this)
{
if (this == NULL)
raise("This must be initialized");
}
static const char *Vertex_str(VertexClass *this)
{
char *buf = NULL;
if (this == NULL)
raise("This must be initialized");
asprintf(&buf, "<%s (%d, %d, %d)>",
((Class *)this)->__name__, this->x, this->y, this->z);
return (buf);
}
static Object *Vertex_add(const Object *this, const Object *other)
{
Class *res;
if (this == NULL || other == NULL)
raise("This must be initialized");
res = new(Vertex, ((VertexClass *)this)->x, ((VertexClass *)this)->y,
((VertexClass *)this)->z);
((VertexClass *)res)-> x += ((VertexClass *)other)->x;
((VertexClass *)res)-> y += ((VertexClass *)other)->y;
((VertexClass *)res)-> z += ((VertexClass *)other)->z;
return ((Object *)res);
}
static Object *Vertex_sub(const Object *this, const Object *other)
{
Class *res;
if (this == NULL || other == NULL)
raise("This must be initialized");
res = new(Vertex, ((VertexClass *)this)->x, ((VertexClass *)this)->y,
((VertexClass *)this)->z);
((VertexClass *)res)-> x -= ((VertexClass *)other)->x;
((VertexClass *)res)-> y -= ((VertexClass *)other)->y;
((VertexClass *)res)-> z -= ((VertexClass *)other)->z;
return ((Object *)res);
}
static const VertexClass _description = {
{ /* Class struct */
.__size__ = sizeof(VertexClass),
.__name__ = "Vertex",
.__ctor__ = (ctor_t)&Vertex_ctor,
.__dtor__ = (dtor_t)&Vertex_dtor,
.__str__ = (to_string_t)&Vertex_str, /* Implement this method for exercice 02 */
.__add__ = (binary_operator_t)&Vertex_add, /* Implement this method for exercice 03 */
.__sub__ = (binary_operator_t)&Vertex_sub, /* Implement this method for exercice 03 */
.__mul__ = NULL,
.__div__ = NULL,
.__eq__ = NULL,
.__gt__ = NULL,
.__lt__ = NULL
},
.x = 0,
.y = 0,
.z = 0
};
const Class *Vertex = (const Class *)&_description;