# tkien2/missile-sim architecture

> Architecture diagram of the GitHub repository tkien2/missile-sim, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tkien2/missile-sim
- Repository: https://github.com/tkien2/missile-sim

## Overview

##### Project Type and Purpose
- This is a browser-based simulation app that models a missile’s (xanh) pursuit of a target aircraft (đỏ) using the Pure Pursuit algorithm.
- The README clarifies that it is not a full-fledged game or complex simulation but a visual demonstration of a specific homing algorithm, namely the HomingAlgorithm function that calculates the angle based on distances computed from x and y offsets.

##### Analyzing the File Structure
- Top-Level Files:
- README.md: Contains instructions, algorithm description, and basic simulation reset guidance.
- main.html: Acts as the entry point and view container for the simulation (likely includes the canvas or other display elements).
- module.js: Likely encapsulates the core calculation or simulation logic including the Pure Pursuit algorithm (HomingAlgorithm function).
- script.js: Likely contains additional logic (such as event handling, simulation loop, or integrating the algorithm from module.js with UI interactions).
- styles.css: Provides styling for the simulation interface.
- The structure indicates a straightforward front-end project with separation of concerns: HTML for layout, CSS for styling, and JavaScript for algorithm logic and simulation flow.

##### Key Components
a. Main Components:
- User Interface (main.html): Represents the visual canvas for the simulation. This might include elements like a canvas or divs where the missile (xanh) and aircraft (đỏ) are rendered.
- Simulation Logic (module.js): Contains the core algorithm (HomingAlgorithm) where the key computation for angle “a” happens. This module handles the math and physics of the simulation.
- Controller / Driver Code (script.js): Manages the simulation cycle such as initializing the simulation, updating positions, handling refresh/restart, and binding user interactions (e.g., if any keypresses are detected).
- Styling (styles.css): Handles visual presentation making the simulation visually clear.
b. Relationships and Data Flow:
- main.html loads and links with styles.css and both JavaScript files.
- The simulation starts in main.html, which invokes script.js to initialize the simulation environment.
- script.js calls functions from module.js (containing HomingAlgorithm) to compute movement based on current positions.
- Visual updates (rendering positions of the missile and airplane) occur in the UI driven by the simulation loop.
c. Architectural Patterns and Design Principles:
- Separation of concerns: HTML handles markup, CSS the styling, and JS (both module.js and script.js) the logic.
- Modularization: Breaking simulation logic into separate modules (as suggested by module.js) supports easier tweaking of the core algorithm versus the simulation/control flow.
d. Technologies and Frameworks:
- Standard web technologies: HTML for structure, CSS for styling, and JavaScript for dynamic interaction. No external frameworks indicated, so it is a vanilla JavaScript project.

- Simulation Control Module (script.js): Indicate its role of initiating the simulation, managing state, and triggering algorithm execution.
- Data Flow and Interactions:
- Annotations:
- Labels and possibly a few notes on how the Pure Pursuit algorithm calculates the target angle using distance formulas and the cosine rule.
- That refreshing the page (F5) resets the simulation—a crucial behavioral insight per the README instructions.

- Modularity: splitting simulation logic from presentation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "UI/Presentation Layer"
        A["main.html"]:::ui
        B["styles.css"]:::ui
    end

    C["Simulation Control Module (script.js)"]:::control
    D["Core Algorithm Module (module.js)\n(HomingAlgorithm)"]:::core
    E["Documentation (README.md)"]:::doc

    %% Data Flow
    A -->|"loads"| C
    A -->|"renders with"| B
    C -->|"calls"| D
    D -->|"returns trajectory"| C
    C -->|"updates visuals"| A
    E -->|"provides reset instructions"| A

    %% Click Events
    click A "https://github.com/tkien2/missile-sim/blob/master/main.html"
    click B "https://github.com/tkien2/missile-sim/blob/master/styles.css"
    click C "https://github.com/tkien2/missile-sim/blob/master/script.js"
    click D "https://github.com/tkien2/missile-sim/blob/master/module.js"
    click E "https://github.com/tkien2/missile-sim/blob/master/README.md"

    %% Styles
    classDef ui fill:#A9D0F5,stroke:#333,stroke-width:2px;
    classDef control fill:#A2D5AB,stroke:#333,stroke-width:2px;
    classDef core fill:#F5A9A9,stroke:#333,stroke-width:2px;
    classDef doc fill:#F7EAC8,stroke:#333,stroke-width:2px;
```
