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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/// An enum used for the `Surface.draw_straight_line` method.
pub enum Direction {
Left,
Right,
Up,
Down
}
/// Main graphical struct.
pub struct Surface {
width: usize,
height: usize,
data: Vec<Vec<char>>,
}
impl Surface {
fn distance(x1: isize, y1: isize, x2: isize, y2: isize) -> f64 {
let x = ((x2-x1).pow(2) + (y2-y1).pow(2)) as f64;
x.sqrt()
}
/// Creates a new `Surface` of width `width` and height `height` filled with spaces.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// ```
pub fn new(width: usize, height: usize) -> Surface {
let v = vec![vec![' '; width]; height];
Surface {
width,
height,
data: v
}
}
/// Get the width of this surface.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// println!("{}", surf.width());
/// ```
pub fn width(&self) -> usize {
self.width
}
/// Get the height of this surface.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// println!("{}", surf.height());
/// ```
pub fn height(&self) -> usize {
self.height
}
/// Get a reference to the raw vector data of this surface.
///
/// Example:
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// println!("{:?}", surf.raw_data());
/// ```
pub fn get_raw_data(&self) -> &Vec<Vec<char>> {
&self.data
}
/// Fill this surface with `c`.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// surf.fill('.');
/// ```
pub fn fill(&mut self, c: char) {
self.data.clear();
self.data = vec![vec![c; self.width]; self.height];
}
/// Display this surface to the terminal.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// surf.fill('.');
/// surf.display();
/// ```
pub fn display(&self) {
for row in &self.data {
let row_str = row.iter().cloned().collect::<String>();
println!("{}", row_str);
}
}
/// Draw character `c` at `(x, y)`.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// surf.draw_pixel(2, 2, '#');
/// ```
pub fn draw_pixel(&mut self, x: isize, y: isize, c: char) -> bool {
let mut placed = false;
if x >= 0 && x < self.width as isize && y >= 0 && y < self.height as isize {
self.data[y as usize][x as usize] = c;
placed = true;
}
placed
}
/// Draw a straight line using `c` starting at `(x, y)` with length `length` in direction `dir`.
///
/// Example
/// ```
/// use terminalgl::{Surface, Direction};
/// let mut surf = Surface::new(10, 10);
/// surf.draw_straight_line(1, 1, 5, Direction::Right, '#');
/// ```
pub fn draw_straight_line(&mut self, mut x: isize, mut y: isize, mut length: isize, dir: Direction, c: char) {
let mut addx: isize = 0;
let mut addy: isize = 0;
match dir {
Direction::Left => addx = -1,
Direction::Right => addx = 1,
Direction::Up => addy = -1,
Direction::Down => addy = 1
}
if length < 0 {
length = -length;
addx = -addx;
addy = -addy;
}
for _ in 0..length {
self.draw_pixel(x, y, c);
x += addx;
y += addy;
}
}
/// Draw a rectangle using `c` with the top-left corner at `(x, y)` with width `width` and height `height`.
/// If `fill` is false, only the outline of the rectangle will be drawn. Otherwise the entire rectangle will be filled.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// surf.draw_rectangle(1, 1, 5, 5, '#', true);
/// ```
pub fn draw_rectangle(&mut self, x: isize, y: isize, width: usize, height: usize, c: char, fill: bool) {
if !fill {
self.draw_straight_line(x, y, width as isize, Direction::Right, c);
self.draw_straight_line(x, y+(height as isize)-1, width as isize, Direction::Right, c);
self.draw_straight_line(x, y+1, (height-2) as isize, Direction::Down, c);
self.draw_straight_line(x+(width as isize)-1, y, height as isize-2, Direction::Down, c);
}
else {
for i in 0..width {
self.draw_straight_line(x+i as isize, y, height as isize, Direction::Down, c);
}
}
}
/// Draw a line using `c` with starting point `(x1, y1)` and ending point `(x2, y2)`.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// surf.draw_line(1, 1, 5, 5, '#');
/// ```
pub fn draw_line(&mut self, x1: isize, y1: isize, x2: isize, y2: isize, c: char) {
if x1 == x2 {
self.draw_straight_line(x1, y1, y2-y1, Direction::Down, c);
}
if y1 == y2 {
self.draw_straight_line(x1, y1, x2-x1, Direction::Right, c);
}
let dist = Surface::distance(x1, y1, x2, y2);
let dx = (x2-x1) as f64 / dist;
let dy = (y2-y1) as f64 / dist;
let mut x = x1 as f64;
let mut y = y1 as f64;
for _ in 0..dist.round() as isize {
self.draw_pixel(x.round() as isize, y.round() as isize, c);
x += dx;
y += dy;
}
self.draw_pixel(x2, y2, c);
}
/// Draw an ellipse using `c` with a center at `(h, k)` with a width of `width` (on both sides) and a height of `height` (on both sides).
/// If `fill` is false, only the outline of the ellipse will be drawn. Otherwise, the entire ellipse will be filled.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// surf.draw_ellipse(4, 4, 3, 3, '#', false);
/// ```
pub fn draw_ellipse(&mut self, h: isize, k: isize, a: usize, b: usize, c: char, fill: bool) {
for x in 0..a*2+1 {
let shiftx: isize = x as isize + h - a as isize;
let inside = ((a*a) as isize - (shiftx-h).pow(2)).abs() as f64;
let y: f64 = (b as f64) / (a as f64) * inside.sqrt() + k as f64;
if fill {
let ydist: isize = 2 * (k - y.round() as isize).abs();
self.draw_straight_line(shiftx as isize, y.round() as isize, ydist+1, Direction::Up, c);
continue;
}
let ydist: isize = 2 * (k - y.round() as isize);
self.draw_pixel(shiftx, y.round() as isize, c);
self.draw_pixel(shiftx, (y.round() as isize + ydist) as isize, c);
}
}
/// Draw a polygon using the points specified in `points`.
/// `points` must have a length of at least 2.
/// Sub-vectors of `points` must be `2` or more elements long.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// let pts = vec![vec![1, 1], vec![9, 3], vec![5, 7]];
/// surf.draw_polygon(&pts, '#');
/// surf.display();
/// ```
pub fn draw_polygon(&mut self, points: &Vec<Vec<isize>>, c: char) {
for i in 0..points.len() {
let mut next = i+1;
if next >= points.len() {
next = 0;
}
self.draw_line(points[i][0], points[i][1], points[next][0], points[next][1], c);
}
}
/// Draw a line of text starting at `(x, y)` moving to the right for each character in `text`.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut surf = Surface::new(10, 10);
/// let text = String::from("hi there");
/// surf.draw_text(1, 1, &text);
/// ```
pub fn draw_text(&mut self, x: isize, y: isize, text: &String) {
for (i, c) in text.chars().enumerate() {
self.draw_pixel(x+i as isize, y, c);
}
}
/// Display a surface `other` on top of this surface.
/// Any space characters (`' '`) in `other` will be considered transparent and will not be overwritten in this surface.
///
/// Example
/// ```
/// use terminalgl::Surface;
/// let mut s1 = Surface::new(10, 10);
/// let mut s2 = Surface::new(5, 5);
/// s1.fill('.');
/// s2.fill('#');
/// s1.blit(1, 1, &s2);
/// ```
pub fn blit(&mut self, x: isize, y: isize, other: &Surface) {
for (i, row) in other.data.iter().enumerate() {
for (j, c) in row.iter().enumerate() {
if *c == ' ' {
continue;
}
self.draw_pixel(x+j as isize, y+i as isize, *c);
}
}
}
}