-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_drawing.rs
93 lines (79 loc) · 3.05 KB
/
cli_drawing.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
use std::io;
// Function to draw a rectangle in the command line
fn draw_rectangle(width: u32, height: u32) {
for _ in 0..height {
println!("{}", "*".repeat(width as usize));
}
}
// Function to draw a triangle in the command line
fn draw_triangle(height: u32) {
for i in 1..=height {
println!("{}", "*".repeat(i as usize));
}
}
// Function to draw a circle-like pattern (approximated)
fn draw_circle(radius: u32) {
let r_squared = (radius * radius) as f64;
for y in -radius as i32..=radius as i32 {
for x in -radius as i32..=radius as i32 {
let distance = (x * x + y * y) as f64;
if distance <= r_squared {
print!("*");
} else {
print!(" ");
}
}
println!();
}
}
fn main() {
println!("Welcome to the Command-Line Drawing Tool!");
loop {
println!("\nChoose a shape to draw:");
println!("1. Rectangle");
println!("2. Triangle");
println!("3. Circle");
println!("4. Exit");
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed to read input");
let choice: u32 = match choice.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input. Please enter a number between 1 and 4.");
continue;
}
};
match choice {
1 => {
let mut width = String::new();
let mut height = String::new();
println!("Enter the width of the rectangle:");
io::stdin().read_line(&mut width).expect("Failed to read input");
let width: u32 = width.trim().parse().expect("Please enter a valid number");
println!("Enter the height of the rectangle:");
io::stdin().read_line(&mut height).expect("Failed to read input");
let height: u32 = height.trim().parse().expect("Please enter a valid number");
draw_rectangle(width, height);
}
2 => {
let mut height = String::new();
println!("Enter the height of the triangle:");
io::stdin().read_line(&mut height).expect("Failed to read input");
let height: u32 = height.trim().parse().expect("Please enter a valid number");
draw_triangle(height);
}
3 => {
let mut radius = String::new();
println!("Enter the radius of the circle:");
io::stdin().read_line(&mut radius).expect("Failed to read input");
let radius: u32 = radius.trim().parse().expect("Please enter a valid number");
draw_circle(radius);
}
4 => {
println!("Goodbye!");
break;
}
_ => println!("Invalid choice. Please enter a number between 1 and 4."),
}
}
}