Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Telescience2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- calibrations = new List<CalibrationData>();
- offsets = new List<Offsets>();
- resetOffsets();
- updateCalibrationDisplay();
- }
- private List<CalibrationData> calibrations;
- private List<Offsets> offsets;
- //Constants
- private const int POW_MIN_OFF = -4;
- private const int POW_MAX_OFF = 0;
- private const int HA_MIN_OFF = -10;
- private const int HA_MAX_OFF = 10;
- private const int VA_MIN_OFF = 0;
- private const int VA_MAX_OFF = 0;
- private const double G = 10.0;
- private class CalibrationData
- {
- public int power;
- public double horAngle;
- public double verAngle;
- public int destX;
- public int destY;
- public CalibrationData(int power, double horAngle, double verAngle, int destX, int destY)
- {
- this.power = power;
- this.horAngle = horAngle;
- this.verAngle = verAngle;
- this.destX = destX;
- this.destY = destY;
- }
- }
- private class Offsets
- {
- public int HAOff;
- public int VAOff;
- public int PowOff;
- public Offsets(int HAOff, int VAOff, int PowOff)
- {
- this.HAOff = HAOff;
- this.VAOff = VAOff;
- this.PowOff = PowOff;
- }
- }
- private double mod(double a, double b)
- {
- return a - b * Math.Floor(a / b);
- }
- private double toScientificDegree(double angle)
- {
- return mod(360.0 - angle + 90.0, 360.0);
- }
- private double fromScientificDegree(double angle)
- {
- return mod(360.0 - angle + 90.0, 360.0);
- }
- private double toRadian(double angle)
- {
- return angle * (Math.PI / 180.0);
- }
- private double fromRadian(double angle)
- {
- return angle * (180.0 / Math.PI);
- }
- private void resetOffsets()
- {
- offsets.Clear();
- for (int ha = HA_MIN_OFF; ha <= HA_MAX_OFF; ++ha)
- {
- for (int va = VA_MIN_OFF; va <= VA_MAX_OFF; ++va)
- {
- for (int pow = POW_MIN_OFF; pow <= POW_MAX_OFF; ++pow)
- {
- offsets.Add(new Offsets(ha, va, pow));
- }
- }
- }
- }
- private void updateCalibrationDisplay()
- {
- possibleOffsetsDisplay.Text = offsets.Count.ToString();
- if (offsets.Count > 1)
- cVerdictDisplay.Text = "Not Accurate Yet";
- if (offsets.Count == 1)
- {
- cVerdictDisplay.Text = "Accurate!";
- HAOffsetIn.Text = offsets[0].HAOff.ToString();
- POWOffsetIn.Text = offsets[0].PowOff.ToString();
- }
- if (offsets.Count < 1)
- cVerdictDisplay.Text = "Impossible, Reset";
- }
- //Checks the destination for the given settings
- //CD has the entered values
- //Returns True if it matches, False if not
- private bool checkDestination(CalibrationData cd, int sourceX, int sourceY, int HAOff, int VAOff, int PowOff)
- {
- //Get the actual HAngle
- double actHDegree = mod(cd.horAngle + (double)HAOff, 360.0);
- //Convert it to scientific radian
- double actHAngle = toRadian(toScientificDegree(actHDegree));
- //Get the actual VAngle
- double actVDegree = cd.verAngle + (double)VAOff;
- if (actVDegree < 1.0) actVDegree = 1.0;
- if (actVDegree > 90.0) actVDegree = 90.0;
- double actVAngle = toRadian(actVDegree);
- //Get the actual velocity
- double actVelocity = (double)cd.power + (double)PowOff;
- //Compute the range
- double range = actVelocity * actVelocity * Math.Sin(2.0 * actVAngle) / G;
- //Compute DX and DY
- double deltaX = range * Math.Cos(actHAngle);
- double deltaY = range * Math.Sin(actHAngle);
- //Compute destX and destY
- int destX = (int)Math.Round(sourceX + deltaX, 0);
- int destY = (int)Math.Round(sourceY + deltaY, 0);
- //Final check
- return destX == cd.destX && destY == cd.destY;
- }
- private void cAddButton_Click(object sender, EventArgs e)
- {
- //Get data
- int power;
- double horAngle;
- double verAngle;
- int destX;
- int destY;
- int sourceX;
- int sourceY;
- try
- {
- power = int.Parse(cPowerInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- horAngle = double.Parse(cHorAngleInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- verAngle = double.Parse(cVerAngleInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- destX = int.Parse(cResXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- destY = int.Parse(cResYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- sourceX = int.Parse(telepadXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- sourceY = int.Parse(telepadYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- }
- catch (System.FormatException exception)
- {
- statusDisplay.Text = "wrong entry format";
- return;
- }
- //Add new thing
- CalibrationData cd = new CalibrationData(power, horAngle, verAngle, destX, destY);
- calibrations.Add(cd);
- //Cull the offsets
- offsets.RemoveAll(off => !checkDestination(cd, sourceX, sourceY, off.HAOff, off.VAOff, off.PowOff));
- offsets.TrimExcess();
- updateCalibrationDisplay();
- }
- private void cResetButton_Click(object sender, EventArgs e)
- {
- resetOffsets();
- updateCalibrationDisplay();
- }
- private void computeButton_Click(object sender, EventArgs e)
- {
- //Check offsets
- if (offsets.Count != 1)
- {
- destVerdict.Text = "not accurate yet";
- statusDisplay.Text = "Likely inaccurate!";
- }
- Offsets off = offsets[0];
- //Get data
- int power;
- int destX;
- int destY;
- int sourceX;
- int sourceY;
- try
- {
- power = int.Parse(destPowInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- destX = int.Parse(destXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- destY = int.Parse(destYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- sourceX = int.Parse(telepadXInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- sourceY = int.Parse(telepadYInput.Text, System.Globalization.CultureInfo.InvariantCulture);
- }
- catch (System.FormatException exception)
- {
- statusDisplay.Text = "wrong entry format";
- destVerdict.Text = "wrong entry format";
- return;
- }
- //DeltaX/Y
- double deltaX = (double)destX - (double)sourceX;
- double deltaY = (double)destY - (double)sourceY;
- //Compute the distance we need
- double dist = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
- //Get the actual power
- double actPower = (double)power + (double)off.PowOff;
- //Get maximum distance
- double maxRange = actPower * actPower * Math.Sin(2 * toRadian(45.0)) / G;
- //Check
- if (dist > maxRange)
- {
- destVerdict.Text = "out of range!";
- return;
- }
- //Compute the angle we want
- double desHAngle = mod(Math.Atan2(deltaY, deltaX), Math.PI * 2.0);
- //Turn it into a bearing
- double desBearing = fromScientificDegree(fromRadian(desHAngle));
- //Round it
- double actBearing = Math.Round(desBearing, 2);
- //Modify it with the offset
- actBearing -= (double)off.HAOff;
- //Compute the vertical angle we want
- double desVAngle = 0.5 * Math.Asin(G * dist / (actPower * actPower));
- //Round it as well
- double actElevation = Math.Round(fromRadian(desVAngle), 1);
- //Modify it with the offset
- actElevation -= (double)off.VAOff;
- //Done, print the result
- destVerdict.Text = "Bearing: " + actBearing.ToString() + " Elevation: " + actElevation.ToString();
- }
- private void aboutButton_Click(object sender, EventArgs e)
- {
- statusDisplay.Text = "Braincake made this! Read the readme!";
- }
- private void ApplyOffsetButton_Click(object sender, EventArgs e)
- {
- //Get data
- int haoff;
- int powoff;
- try
- {
- haoff = int.Parse(HAOffsetIn.Text, System.Globalization.CultureInfo.InvariantCulture);
- powoff = int.Parse(POWOffsetIn.Text, System.Globalization.CultureInfo.InvariantCulture);
- }
- catch (System.FormatException exception)
- {
- statusDisplay.Text = "wrong entry format";
- return;
- }
- //Check
- if (haoff < HA_MIN_OFF || haoff > HA_MAX_OFF || powoff < POW_MIN_OFF || powoff > POW_MAX_OFF)
- {
- statusDisplay.Text = "Offsets out of range!";
- return;
- }
- //Apply
- offsets.Clear();
- Offsets newoff = new Offsets(haoff, VA_MIN_OFF, powoff);
- offsets.Add(newoff);
- //Update status
- updateCalibrationDisplay();
- cVerdictDisplay.Text = "Offsets applied!";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement