-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: draw random circle, rectangle and save as ppm
- Loading branch information
Showing
3 changed files
with
88 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,72 @@ | ||
module main | ||
|
||
import os | ||
import rand | ||
import ppm | ||
|
||
fn main() { | ||
image_dimensions := ppm.ImageDimensions{ | ||
x: 2 | ||
y: 2 | ||
const width = 20 | ||
|
||
fn draw_random_circle(mut inputs [width][width]f64) { | ||
// Adding a buffer to reduce the chances of the circle touching the grid edges | ||
buffer := 2 | ||
|
||
// Calculate the radius within a range that considers the buffer | ||
radius := rand.int_in_range(2, width / 2 - buffer - 1) or { return } | ||
|
||
// Adjust point displacement to account for the buffer | ||
point_displacement := rand.int_in_range(radius + buffer, width - radius - buffer) or { return } | ||
|
||
for i in 0 .. inputs.len { | ||
for j in 0 .. inputs[0].len { | ||
// Calculate the distance from the center | ||
di := i - point_displacement | ||
dj := j - point_displacement | ||
|
||
// Check if the point is inside the circle and assign 1.0 to inputs[i][j] | ||
if di * di + dj * dj <= radius * radius { | ||
inputs[i][j] = 1.0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn draw_random_rectangle(mut inputs [width][width]f64) { | ||
min_dimension := 3 // Minimum dimension for the rectangle | ||
|
||
// Randomly determine the top-left corner of the rectangle | ||
top_left_x := rand.int_in_range(0, width - min_dimension) or { return } | ||
top_left_y := rand.int_in_range(0, width - min_dimension) or { return } | ||
|
||
image1 := ppm.PPM{ | ||
max_value: 255 | ||
dimensions: image_dimensions | ||
buffer: [0, 255, 0, 255, 0, 0, 0, 0, 255, 0, 255, 0] | ||
// Randomly determine the dimensions of the rectangle | ||
rect_width := rand.int_in_range(min_dimension, width - top_left_x) or { return } | ||
rect_height := rand.int_in_range(min_dimension, width - top_left_y) or { return } | ||
|
||
// Fill the rectangle area in inputs with 1.0 | ||
for i in 0 .. inputs.len { | ||
for j in 0 .. inputs[0].len { | ||
if i >= top_left_x && i < top_left_x + rect_width && j >= top_left_y | ||
&& j < top_left_y + rect_height { | ||
inputs[i][j] = 1.0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
if !os.is_dir('images') { | ||
os.mkdir('images')! | ||
fn print_matrix(matrix [width][width]f64) { | ||
for row in matrix { | ||
println(row) | ||
} | ||
} | ||
|
||
fn main() { | ||
mut inputs := [width][width]f64{} | ||
|
||
draw_random_circle(mut inputs) | ||
|
||
image1 := ppm.create_ppm_from_inputs(inputs) | ||
ppm.save_ppm_to_file(image1, 'images/circle.ppm')! | ||
|
||
draw_random_rectangle(mut inputs) | ||
|
||
ppm.save_ppm_to_file(image1, 'images/image1.ppm')! | ||
image2 := ppm.create_ppm_from_inputs(inputs) | ||
ppm.save_ppm_to_file(image2, 'images/rectangle.ppm')! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters