Skip to content

Commit

Permalink
ENH: draw random circle, rectangle and save as ppm
Browse files Browse the repository at this point in the history
  • Loading branch information
visrut7 committed Dec 30, 2023
1 parent fe9ec31 commit a112e19
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#### Basic Commands
- Only for Linux now
- v run .
- display -resize 400x400 -filter Lanczos -sharpen 0x1 images/image1.ppm
- display -resize 400x400 -filter Point images/image1.ppm
73 changes: 61 additions & 12 deletions src/main.v
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')!
}
26 changes: 26 additions & 0 deletions src/ppm/ppm.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module ppm

import os

const width = 20

pub struct ImageDimensions {
x int
y int
Expand All @@ -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')!
Expand Down

0 comments on commit a112e19

Please sign in to comment.