summaryrefslogtreecommitdiff
path: root/liblogdiag/ld-types.c
blob: cde3da60f0476c0211551d06e0081fc4bcaf4c17 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * ld-types.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <math.h>

#include "liblogdiag.h"
#include "config.h"


#define DEFINE_BOXED_TYPE(TypeName, type_name) \
GType \
type_name ## _get_type (void) \
{ \
	static GType our_type = 0; \
	if (our_type == 0) \
		our_type = g_boxed_type_register_static \
			(g_intern_static_string (#TypeName), \
			(GBoxedCopyFunc) type_name ## _copy, \
			(GBoxedFreeFunc) type_name ## _free); \
	return our_type; \
}

DEFINE_BOXED_TYPE (LdPoint, ld_point)
DEFINE_BOXED_TYPE (LdPointArray, ld_point_array)
DEFINE_BOXED_TYPE (LdRectangle, ld_rectangle)

#define DEFINE_BOXED_TRIVIAL_COPY(TypeName, type_name) \
TypeName * \
type_name ## _copy (const TypeName *self) \
{ \
	TypeName *new_copy; \
	g_return_val_if_fail (self != NULL, NULL); \
	new_copy = g_slice_new (TypeName); \
	*new_copy = *self; \
	return new_copy; \
}

#define DEFINE_BOXED_TRIVIAL_FREE(TypeName, type_name) \
void \
type_name ## _free (TypeName *self) \
{ \
	g_return_if_fail (self != NULL); \
	g_slice_free (TypeName, self); \
}

/**
 * ld_point_copy:
 * @self: An #LdPoint structure.
 *
 * Makes a copy of the structure.
 * The result must be freed by ld_point_free().
 *
 * Return value: A copy of @self.
 */
DEFINE_BOXED_TRIVIAL_COPY (LdPoint, ld_point)

/**
 * ld_point_free:
 * @self: An #LdPoint structure.
 *
 * Frees the structure created with ld_point_copy().
 */
DEFINE_BOXED_TRIVIAL_FREE (LdPoint, ld_point)

/**
 * ld_point_distance:
 * @self: An #LdPoint structure.
 * @x: The X coordinate of the second point.
 * @y: The Y coordinate of the second point.
 *
 * Compute the distance between two points.
 */
gdouble
ld_point_distance (LdPoint *self, gdouble x, gdouble y)
{
	gdouble dx, dy;

	g_return_val_if_fail (self != NULL, -1);

	dx = self->x - x;
	dy = self->y - y;
	return sqrt (dx * dx + dy * dy);
}

/**
 * ld_point_array_new:
 * @num_points: The number of points the array can store.
 *
 * Create a new array of points and initialize.
 *
 * Return value: An #LdPointArray structure.
 */
LdPointArray *
ld_point_array_new (gint num_points)
{
	LdPointArray *new_array;

	g_return_val_if_fail (num_points >= 1, NULL);

	new_array = g_slice_new (LdPointArray);
	new_array->num_points = num_points;
	new_array->points = g_malloc0 (num_points * sizeof (LdPoint));
	return new_array;
}

/**
 * ld_point_array_copy:
 * @self: An #LdPointArray structure.
 *
 * Makes a copy of the structure.
 * The result must be freed by ld_point_array_free().
 *
 * Return value: A copy of @self.
 */
LdPointArray *
ld_point_array_copy (const LdPointArray *self)
{
	LdPointArray *new_array;

	g_return_val_if_fail (self != NULL, NULL);

	new_array = g_slice_new (LdPointArray);
	new_array->num_points = self->num_points;
	new_array->points = g_memdup (self->points,
		self->num_points * sizeof (LdPoint));
	return new_array;
}

/**
 * ld_point_array_free:
 * @self: An #LdPointArray structure.
 *
 * Frees the structure created with ld_point_array_copy().
 */
void
ld_point_array_free (LdPointArray *self)
{
	g_return_if_fail (self != NULL);

	g_free (self->points);
	g_slice_free (LdPointArray, self);
}

/**
 * ld_rectangle_copy:
 * @self: An #LdRectangle structure.
 *
 * Makes a copy of the structure.
 * The result must be freed by ld_rectangle_free().
 *
 * Return value: A copy of @self.
 */
DEFINE_BOXED_TRIVIAL_COPY (LdRectangle, ld_rectangle)

/**
 * ld_rectangle_free:
 * @self: An #LdRectangle structure.
 *
 * Frees the structure created with ld_rectangle_copy().
 */
DEFINE_BOXED_TRIVIAL_FREE (LdRectangle, ld_rectangle)

/**
 * ld_rectangle_contains:
 * @self: An #LdRectangle structure.
 * @x: The X coordinate of the point to be checked.
 * @y: The Y coordinate of the point to be checked.
 *
 * Return value: TRUE if the rectangle contains the specified point.
 */
gboolean
ld_rectangle_contains (LdRectangle *self, gdouble x, gdouble y)
{
	g_return_val_if_fail (self != NULL, FALSE);
	return (x >= self->x && x <= self->x + self->width
	     && y >= self->y && y <= self->y + self->height);
}

/**
 * ld_rectangle_intersects:
 * @self: An #LdRectangle structure.
 * @rect: An #LdRectangle to be checked for intersection.
 *
 * Return value: TRUE if the two rectangles intersect.
 */
gboolean
ld_rectangle_intersects (LdRectangle *self, LdRectangle *rect)
{
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (rect != NULL, FALSE);

	return !(self->x > rect->x + rect->width
	      || self->y > rect->y + rect->height
	      || self->x + self->width  < rect->x
	      || self->y + self->height < rect->y);
}

/**
 * ld_rectangle_extend:
 * @self: An #LdRectangle structure.
 * @border: The border by which the rectangle should be extended.
 *
 * Extend a rectangle on all sides.
 */
void
ld_rectangle_extend (LdRectangle *self, gdouble border)
{
	g_return_if_fail (self != NULL);

	self->x -= border;
	self->y -= border;
	self->width  += 2 * border;
	self->height += 2 * border;
}