-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.rb
178 lines (150 loc) · 3.56 KB
/
graphics.rb
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
module Graphics
class Canvas
attr_reader :width, :height
attr_accessor :canvas
def initialize(width, height)
@width = width
@height = height
@canvas = Array.new(height) { Array.new(width) }
end
def set_pixel(x, y)
@canvas[y][x] = true
end
def pixel_at?(x, y)
@canvas[y][x]
end
def draw(figure)
figure.path.each { |coordinate| set_pixel coordinate[0], coordinate[1] }
end
def render_as(renderer)
renderer.render(self)
end
end
module Renderers
module Ascii
def self.render(canvas)
canvas.canvas.map { |row| Renderers.process_row(row, '@', '-') }.join("\n")
end
end
module Html
HEADER = <<-EOS
<!DOCTYPE html>
<html>
<head>
<title>Rendered Canvas</title>
<style type="text/css">
.canvas {
font-size: 1px;
line-height: 1px;
}
.canvas * {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 5px;
}
.canvas i {
background-color: #eee;
}
.canvas b {
background-color: #333;
}
</style>
</head>
<body>
<div class="canvas">
EOS
FOOTER = <<-EOS
</div>
</body>
</html>
EOS
def self.render(canvas)
HEADER +
canvas.canvas.map { |row| Renderers.process_row(row, '<b></b>', '<i></i>') }
.join("<br>\n") +
FOOTER
end
end
private
def self.fill_pixel(pixel, color, transparent)
pixel ? color : transparent
end
def self.process_row(row, color, transparent)
row.map { |pixel| Renderers.fill_pixel(pixel, color, transparent) }.join
end
end
class Point
include Graphics
attr_reader :x, :y, :path
def initialize(x, y)
@x = x
@y = y
@path = [[@x, @y]]
end
end
class Line
include Graphics
attr_reader :from, :to, :path
def initialize(from, to)
@from, @to = from, to
@delta_x, @delta_y = (@to.x - @from.x).abs, -(@to.y - @from.y).abs
@step_x, @step_y = (@from.x < @to.x ? 1 : -1), (@from.y < @to.y ? 1 : -1)
@err, @x, @y = @delta_x + @delta_y, @from.x, @from.y
@path = [[@x, @y]]
set_path
end
private
def set_path
begin
@error_2 = 2 * @err
correct_x
correct_y
@path << [@x, @y]
end until (@x == @to.x && @y == @to.y)
end
def correct_x
if @error_2 >= @delta_y
@err += @delta_y
@x += @step_x
end
end
def correct_y
if @error_2 <= @delta_x
@err += @delta_x
@y += @step_y
end
end
end
class Rectangle
include Graphics
attr_reader :left, :right, :top_left, :top_right,
:bottom_left, :bottom_right, :path
def initialize(first, second)
@min_x, @max_x = [first.x, second.x].min, [first.x, second.x].max
@min_y, @max_y = [first.y, second.y].min, [first.y, second.y].max
set_corners
set_path
end
private
def set_corners
@top_left = @left = Point.new @min_x, @min_y
@top_right = Point.new @max_x, @min_y
@bottom_left = Point.new @min_x, @max_y
@bottom_right = @right = Point.new @max_x, @max_y
end
def set_path
@path = Line.new(@top_left, @top_right).path +
Line.new(@top_right, @bottom_right).path +
Line.new(@bottom_right, @bottom_left).path +
Line.new(@top_left, @bottom_left).path
end
end
def eql?(other)
path == other.path
end
alias :== :eql?
def hash
@path.hash
end
end