Functions/GenXdev.Helpers.Physics/Get-TimeOfFlightByInitialVelocityAndAngle.cs

// ################################################################################
// Part of PowerShell module : GenXdev.Helpers.Physics
// Original cmdlet filename : Get-TimeOfFlightByInitialVelocityAndAngle.cs
// Original author : René Vaessen / GenXdev
// Version : 3.28.2026
// ################################################################################
// Copyright (c) 2026 René Vaessen / GenXdev
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ################################################################################



using System.Management.Automation;

namespace GenXdev.Helpers.Physics
{
    [System.ComponentModel.Description(@"
.SYNOPSIS
Calculates the time of flight for a projectile.
.DESCRIPTION
Uses T = (2 v sinθ) / g for ideal motion.
.EXAMPLE
```powershell
Get-TimeOfFlightByInitialVelocityAndAngle -InitialVelocityInMetersPerSecond 20 -AngleInDegrees 45 -As ""minutes""
```

Calculates time of flight for a projectile launched at 20 m/s at 45 degrees, output in minutes.
.EXAMPLE
```powershell
Get-TimeOfFlightByInitialVelocityAndAngle 30 30
```

Calculates time of flight for a projectile launched at 30 m/s at 30 degrees.
")]
    [Cmdlet(VerbsCommon.Get, "TimeOfFlightByInitialVelocityAndAngle")]
    [OutputType(typeof(double))]
    public class GetTimeOfFlightByInitialVelocityAndAngleCommand : PSGenXdevCmdlet
    {
        /// <summary>
        /// Initial velocity in m/s
        /// </summary>
        [Parameter(
            Mandatory = true,
            Position = 0,
            HelpMessage = "Initial velocity in m/s")]
        public double InitialVelocityInMetersPerSecond { get; set; }

        /// <summary>
        /// Launch angle in degrees
        /// </summary>
        [Parameter(
            Mandatory = true,
            Position = 1,
            HelpMessage = "Launch angle in degrees")]
        public double AngleInDegrees { get; set; }

        /// <summary>
        /// Gravity in m/s² (default: 9.81)
        /// </summary>
        [Parameter(
            Mandatory = false,
            Position = 2,
            HelpMessage = "Gravity in m/s² (default: 9.81)")]
        public double GravityInMetersPerSecondSquared { get; set; } = 9.81;

        /// <summary>
        /// Output unit for time
        /// </summary>
        [Parameter(
            Mandatory = false,
            Position = 3,
            HelpMessage = "Output unit for time")]
        [ValidateSet("seconds", "minutes", "hours", "milliseconds", "days")]
        public string As { get; set; } = "seconds";

        /// <summary>
        /// Begin processing - initialization logic
        /// </summary>
        protected override void BeginProcessing()
        {
        }

        /// <summary>
        /// Process record - main cmdlet logic
        /// </summary>
        protected override void ProcessRecord()
        {
            // Convert angle from degrees to radians
            double thetaRad = AngleInDegrees * Math.PI / 180.0;

            // Calculate time of flight using T = (2 v sinθ) / g
            double time = (2.0 * InitialVelocityInMetersPerSecond * Math.Sin(thetaRad)) / GravityInMetersPerSecondSquared;

            // Convert the result to the desired unit using Convert-PhysicsUnit
            var result = InvokeCommand.InvokeScript($"GenXdev\\Convert-PhysicsUnit -Value {time} -FromUnit 'seconds' -ToUnit '{As}'");

            // Output the result
            WriteObject(result);
        }

        /// <summary>
        /// End processing - cleanup logic
        /// </summary>
        protected override void EndProcessing()
        {
        }
    }
}
// ###############################################################################