-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathrust.rs
170 lines (142 loc) · 4.01 KB
/
rust.rs
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
//! This example draws a crude version of the Rust logo.
//! The version is "crude" because it's all very approximate with plenty of guesswork.
//! If you can come up with a way to do this so that it exactly matches the logo, please
//! do submit it!
//! In any case, this is an excellent example of creatively using fills and some other
//! techniques to create a fairly complex drawing.
extern crate turtle;
use turtle::Turtle;
fn main() {
let mut turtle = Turtle::new();
turtle.set_fill_color("black");
turtle.begin_fill();
turtle.pen_up();
turtle.forward(130.0);
turtle.pen_down();
gear(&mut turtle);
turtle.end_fill();
turtle.pen_up();
turtle.forward(45.0);
turtle.left(90.0);
turtle.pen_down();
inner_gear(&mut turtle);
letter(&mut turtle);
turtle.hide();
}
fn gear(turtle: &mut Turtle) {
// Much of this code was figured out by guessing and checking
let teeth = 32.0;
let tooth_size = 20.0;
let angle = 360.0 / teeth;
turtle.right(180.0 - angle * 6.0 / 2.0);
for _ in 0..teeth as u64 {
turtle.forward(tooth_size);
turtle.left(180.0 - angle * 7.0);
turtle.forward(tooth_size);
turtle.right(180.0 - angle * 6.0);
}
turtle.right(angle * 6.0 / 2.0);
}
fn inner_gear(turtle: &mut Turtle) {
inner_circle(turtle);
inner_teeth(turtle);
}
fn inner_circle(turtle: &mut Turtle) {
turtle.set_fill_color("white");
turtle.begin_fill();
for _ in 0..360 {
turtle.forward(1.5);
turtle.right(1.0);
}
turtle.end_fill();
turtle.set_fill_color("black");
}
fn inner_teeth(turtle: &mut Turtle) {
let tooth_size = 40.0;
turtle.pen_up();
turtle.left(90.0);
turtle.forward(10.0);
turtle.right(90.0);
turtle.pen_down();
for _ in 0..5 {
turtle.pen_up();
turtle.forward(tooth_size / 2.0);
turtle.pen_down();
turtle.begin_fill();
turtle.right(120.0);
turtle.forward(tooth_size);
turtle.right(120.0);
turtle.forward(tooth_size);
turtle.right(120.0);
turtle.end_fill();
turtle.pen_up();
turtle.forward(tooth_size / 2.0);
turtle.right(90.0);
turtle.forward(5.0);
turtle.left(90.0);
turtle.pen_down();
inner_tooth_circle(turtle);
turtle.pen_up();
turtle.left(90.0);
turtle.forward(5.0);
turtle.right(90.0);
for _ in 0..360 / 5 / 2 {
turtle.forward(3.41);
turtle.right(2.0);
}
turtle.pen_down();
}
}
fn inner_tooth_circle(turtle: &mut Turtle) {
turtle.set_fill_color("white");
turtle.begin_fill();
for _ in 0..360 / 8 {
turtle.forward(1.0);
turtle.right(8.0);
}
turtle.end_fill();
turtle.set_fill_color("black");
}
fn letter(turtle: &mut Turtle) {
turtle.pen_up();
turtle.right(180.0);
turtle.pen_down();
for _ in 0..(360 / 5 - 8) / 2 {
turtle.forward(3.41);
turtle.left(2.0);
}
// Trick for making the turtle face to the right
let heading = turtle.heading();
turtle.right(heading);
turtle.set_pen_size(12.0);
turtle.forward(110.0);
for _ in 0..120 {
// By going forward and then backward, we avoid any "gaps" between the very small movements
// To see why this is needed, try replacing this with a single movement of the difference
// between the two movements.
turtle.forward(1.0);
turtle.backward(0.5);
turtle.right(1.5);
}
turtle.forward(52.0);
turtle.right(90.0);
turtle.forward(50.0);
turtle.backward(90.0);
turtle.left(90.0);
turtle.forward(65.0);
turtle.backward(95.0);
turtle.pen_up();
turtle.backward(95.0);
turtle.pen_down();
turtle.forward(40.0);
for _ in 0..80 {
turtle.forward(1.0);
turtle.backward(0.5);
turtle.right(1.0);
}
for _ in 0..40 {
turtle.forward(1.0);
turtle.backward(0.5);
turtle.left(0.5);
}
}