aboutsummaryrefslogtreecommitdiff
path: root/src/pattern/pattern.h
blob: d53576e50f1f7c6fb2ccddbbfe122f4c0593010f (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
#ifndef PLOC_GEN_PATTERN_PATTERN_H
#define PLOC_GEN_PATTERN_PATTERN_H

#include <stdbool.h>

struct coord
{
    int x;
    int y;
};

struct pattern_type
{
    int width;
    int height;
    int min_length;
    int max_length;
};

struct pattern
{
    struct pattern_type type;
    struct coord *path;
    int length;
};

/*
 * Creates a pattern_type.
 * If max has an invalid value the max_length is set to the
 * maximum possible length.
 */
struct pattern_type gen_pattern_type(int side_len, int min, int max);

/*
 * Returns 0 if a equals b.
 */
int coord_cmp(const void *a, const void *b);

/*
 * If the point happend to be on the line that connects start and end,
 * the result is true, otherwise is false.
 */
bool is_in_line(struct coord a, struct coord c, struct coord b);

/*
 * Returns the index of coord in pattern->path.
 * The return value is negative if coord not found in pattern->path.
 */
int find_in_pattern(struct pattern *pattern, struct coord *coord);

/*
 * Gives back the points in arr, that are between from and to.
 */
int inbetween(int width, int height, struct coord *from, struct coord *to, struct coord *arr);

/*
 * Returns true if there's no unvisited point between
 * the last point of path and next.
 */
bool nothing_inbetween(struct pattern *pattern, struct coord *next);

/*
 * Inline functions.
 */
inline bool len_is_max(struct pattern *pattern)
{
    return pattern->length >= pattern->type.max_length;
}

inline bool len_is_min(struct pattern *pattern)
{
    return pattern->length <= pattern->type.min_length;
}

inline int coord_to_index(int width, struct coord point)
{
    return (point.y * width) + point.x;
}

inline struct coord index_to_coord(int width, int index)
{
    struct coord result;
    result.y = index / width;
    result.x = index % width;
    return result;
}

inline int pattern_point_to_index(struct pattern *pattern, int index)
{
    return coord_to_index(pattern->type.width, pattern->path[index]);
}

#endif