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
|
require 'ruby2d'
RSpec.describe Ruby2D::Renderable do
# Create and use a fresh class to ensure nothing is overridden
class SomeShape
include Renderable
def initialize(x: 0, y: 0, width: 100, height: 100)
@x, @y, @width, @height = x, y, width, height
end
end
it "allows colors to be set on objects" do
shape = SomeShape.new
shape.color = 'red'
expect(shape.color.r).to eq(1)
shape.color = [0.9, 0.8, 0.7, 0.6]
expect(shape.color.r).to eq(0.9)
expect(shape.color.g).to eq(0.8)
expect(shape.color.b).to eq(0.7)
expect(shape.color.a).to eq(0.6)
shape.color.r = 0.1
shape.color.g = 0.2
shape.color.b = 0.3
shape.color.a = 0.4
expect(shape.color.r).to eq(0.1)
expect(shape.color.g).to eq(0.2)
expect(shape.color.b).to eq(0.3)
expect(shape.color.a).to eq(0.4)
shape.r = 0.5
shape.g = 0.6
shape.b = 0.7
shape.a = 0.8
expect(shape.r).to eq(0.5)
expect(shape.g).to eq(0.6)
expect(shape.b).to eq(0.7)
expect(shape.a).to eq(0.8)
end
it "allows British English spelling of color (colour)" do
shape = SomeShape.new
shape.colour = 'blue'
expect(shape.color.r).to eq(0)
shape.colour = [0.1, 0.2, 0.3, 0.4]
expect(shape.color.r).to eq(0.1)
expect(shape.color.g).to eq(0.2)
expect(shape.color.b).to eq(0.3)
expect(shape.color.a).to eq(0.4)
shape.colour.r = 0.9
shape.colour.g = 0.8
shape.colour.b = 0.7
shape.colour.a = 0.6
expect(shape.colour.r).to eq(0.9)
expect(shape.colour.g).to eq(0.8)
expect(shape.colour.b).to eq(0.7)
expect(shape.colour.a).to eq(0.6)
expect(shape.color.r).to eq(0.9)
expect(shape.color.g).to eq(0.8)
expect(shape.color.b).to eq(0.7)
expect(shape.color.a).to eq(0.6)
expect(shape.r).to eq(0.9)
expect(shape.g).to eq(0.8)
expect(shape.b).to eq(0.7)
expect(shape.a).to eq(0.6)
end
describe "#contains?" do
shape = SomeShape.new(x: 1, y: 1, width: 2, height: 2)
# Grid looks like this, 2x2 square at point (1, 1):
#
# 0 1 2 3 4
# 0 +--+--+--+--+
# | | | | |
# 1 +--+--+--+--+
# | |XX|XX| |
# 2 +--+--+--+--+
# | |XX|XX| |
# 3 +--+--+--+--+
# | | | | |
# 4 +--+--+--+--+
it "returns true if point is inside the shape" do
expect(shape.contains?(1, 1)).to be true
expect(shape.contains?(2, 1)).to be true
expect(shape.contains?(3, 1)).to be true
expect(shape.contains?(1, 2)).to be true
expect(shape.contains?(2, 2)).to be true
expect(shape.contains?(3, 2)).to be true
expect(shape.contains?(1, 3)).to be true
expect(shape.contains?(2, 3)).to be true
expect(shape.contains?(3, 3)).to be true
end
it "returns false if point is outside the shape" do
# Clockwise around the shape
expect(shape.contains?(0, 0)).to be false
expect(shape.contains?(1, 0)).to be false
expect(shape.contains?(2, 0)).to be false
expect(shape.contains?(3, 0)).to be false
expect(shape.contains?(4, 0)).to be false
expect(shape.contains?(4, 1)).to be false
expect(shape.contains?(4, 2)).to be false
expect(shape.contains?(4, 3)).to be false
expect(shape.contains?(4, 4)).to be false
expect(shape.contains?(3, 4)).to be false
expect(shape.contains?(2, 4)).to be false
expect(shape.contains?(1, 4)).to be false
expect(shape.contains?(0, 4)).to be false
expect(shape.contains?(0, 3)).to be false
expect(shape.contains?(0, 2)).to be false
expect(shape.contains?(0, 1)).to be false
end
end
end
|