kgilpin / nova-flask

Flask port of Nova
0 stars 0 forks source link

Star map generation #1

Open kgilpin opened 1 month ago

kgilpin commented 1 month ago

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:

- name: Acamar
  mag: 3.24
- name: Achernar
  mag: 0.46
...
github-actions[bot] commented 1 month ago

Title

Add a function to generate a random map of stars

Problem

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.

Analysis

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:

  1. Read YAML Data: Extract star name and magnitude from nova/stars.yml.
  2. Random Star Selection: Randomly select the required number of stars from the loaded data.
  3. Position Generation: Generate random coordinates for each star within the specified radius:
    • Ensure the coordinates fall within a circular area defined by the radius.
  4. Function Signature: The function will take two parameters: num_stars and radius.

Proposed Changes

  1. nova/models.py:

    • Add a new function generate_star_map(num_stars, radius) that handles the entire process of reading star data, selecting random stars, and generating their positions.
  2. nova/init.py:

    • Import the newly created function to make it accessible where needed.

Detailed Changes:

  1. nova/models.py

    • Add import statements: To handle YAML files and random operations, import yaml and random.
    • Add reading YAML data: Write a helper function or code block within generate_star_map to read and parse nova/stars.yml.
    • Add position generation logic: Implement logic to generate random (x, y) coordinates within the given radius.
    • Define 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.
  2. nova/init.py

    • Import the new function: Add an import statement to use generate_star_map where it's required.

Example:

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