diff --git a/README.md b/README.md index 6cbc96e..0b1caa9 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ #### Basic Commands - Only for Linux now - v run . -- display -resize 400x400 -filter Lanczos -sharpen 0x1 images/image1.ppm \ No newline at end of file +- display -resize 400x400 -filter Point images/image1.ppm \ No newline at end of file diff --git a/src/main.v b/src/main.v index 888891b..794488b 100644 --- a/src/main.v +++ b/src/main.v @@ -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')! } diff --git a/src/ppm/ppm.v b/src/ppm/ppm.v index 8a6ef9a..27005c3 100644 --- a/src/ppm/ppm.v +++ b/src/ppm/ppm.v @@ -2,6 +2,8 @@ module ppm import os +const width = 20 + pub struct ImageDimensions { x int y int @@ -14,6 +16,30 @@ pub struct PPM { buffer []int @[required] } +pub fn create_ppm_from_inputs(inputs [width][width]f64) PPM { + dimensions := ImageDimensions{ + x: inputs.len + y: inputs[0].len + } + + mut buffer := []int{cap: dimensions.x * dimensions.y * 3} + for row in inputs { + for value in row { + // Convert the f64 value to an integer for grayscale (0 or 255) + color_value := if value == 1.0 { 255 } else { 0 } + // Append RGB components (all the same for grayscale) + buffer << color_value + buffer << color_value + buffer << color_value + } + } + + return PPM{ + dimensions: dimensions + buffer: buffer + } +} + pub fn save_ppm_to_file(ppm PPM, file_name string) ! { mut file := os.open_file(file_name, 'w')! file.write_string('${ppm.magic_number}\n')!