Open kgilpin opened 1 month ago
Add a function to generate a random map of stars
The user wants to generate a randomized star map based on a predefined set of star names and magnitudes stored in a YAML file (nova/stars.yml
). The function should take two parameters: the number of stars to be created and the radius of the universe. The star data should be randomly selected from the YAML file, and the stars' positions should be generated within the given radius.
To solve this issue, we need to create a function that can read star data from nova/stars.yml
, and then generate random star positions within a given radius. Here is the breakdown of steps:
nova/stars.yml
.num_stars
and radius
.nova/models.py:
generate_star_map(num_stars, radius)
that handles the entire process of reading star data, selecting random stars, and generating their positions.nova/init.py:
nova/models.py
yaml
and random
.generate_star_map
to read and parse nova/stars.yml
.(x, y)
coordinates within the given radius.generate_star_map
: This function will combine both reading data and generating random positions, and return the list of randomly selected stars along with their positions.nova/init.py
generate_star_map
where it's required.nova/models.py:
import yaml
import random
def generate_star_map(num_stars, radius):
# Read star data from YAML file
with open('nova/stars.yml', 'r') as file:
star_data = yaml.safe_load(file)
# Randomly select stars
selected_stars = random.sample(star_data, num_stars)
# Generate random positions within the given radius
for star in selected_stars:
angle = random.uniform(0, 2 * 3.14159)
r = radius * (random.uniform(0, 1) ** 0.5) # Random radius within the circle
star['x'] = r * math.cos(angle)
star['y'] = r * math.sin(angle)
return selected_stars
# Additional imports for mathematical operations
import math
nova/init.py:
from nova.models import generate_star_map
Add a function to generate a random map of stars.
Implement this as a new, standalone function.
Parameters include:
Assign star names and magnitude from a file
nova/stars.yml
, which contains this type of data: