/
tinsels.rego
59 lines (48 loc) · 1.69 KB
/
tinsels.rego
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
package tree.tinsels
# check tinsel gradients
deny[message] {
some tinsel
point_a := input.tinsels[tinsel][0]
point_b := input.tinsels[tinsel][1]
point_b[1] <= point_a[1]
message := sprintf("tinsel does not have positive gradient (%v %v)", [point_a, point_b])
}
# check tinsels fall within the bound of the outline
deny[message] {
# check tinsels
some t
some p
# find the start and end point for each
point := input.tinsels[t][p]
# find the set of segments for universal quantification
segment_matches := {segment |
# check outline segments
some i
i < count(input.outline) -1
# find a start and end for each segment
outline_point_a := input.outline[i]
outline_point_b := input.outline[i+1]
segment := [outline_point_a, outline_point_b]
# we make an assumption that the points are unique
outline_point_a != outline_point_b
# find the gradient of the outline segment
gradient := (outline_point_a[1] - outline_point_b[1]) /
(outline_point_a[0] - outline_point_b[0])
# find the y intercept to build the line equation
y_intercept := -1*((-1*outline_point_a[1]) + (gradient * outline_point_a[0]))
# there is an intercept with the segment's line equation
expected_y := gradient * point[0] + y_intercept
expected_y == point[1]
# x is within line segment range
segment_x_range := sort([outline_point_a[0], outline_point_b[0]])
point[0] >= segment_x_range[0]
point[0] <= segment_x_range[1]
# y is within line segment range
segment_y_range := sort([outline_point_a[1], outline_point_b[1]])
point[1] >= segment_y_range[0]
point[1] <= segment_y_range[1]
}
# if there are no matches
count(segment_matches) == 0
message := "tinsel does not start and end on outline"
}