I wrote and debugged this C# code after I found out that my 1989 vector calculator in Modula-2 for the Commadore Amiga 2000 was not working correctly.






// C# Three-Dimensional Cartesian Vector Calculator
// (c) September 24, 2023 by James Pate Williams, Jr.
// All Applicable Rights Reserved
using System;
using System.Windows.Forms;
namespace CSVectorCalculator
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private static double[] A = new double[3];
private static double[] B = new double[3];
private static double[] C = new double[3];
private void ValidateAB(
ref bool valid)
{
try
{
A[0] = double.Parse(textBox1.Text);
A[1] = double.Parse(textBox2.Text);
A[2] = double.Parse(textBox3.Text);
B[0] = double.Parse(textBox4.Text);
B[1] = double.Parse(textBox5.Text);
B[2] = double.Parse(textBox6.Text);
valid = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
valid = false;
}
}
private void FillC(double[] C)
{
textBox7.Text = C[0].ToString();
textBox8.Text = C[1].ToString();
textBox9.Text = C[2].ToString();
}
private void button1_Click(object sender, EventArgs e)
{
bool valid = true;
ValidateAB(ref valid);
if (valid)
{
C[0] = A[0] + B[0];
C[1] = A[1] + B[1];
C[2] = A[2] + B[2];
FillC(C);
}
}
private void button2_Click(object sender, EventArgs e)
{
bool valid = true;
ValidateAB(ref valid);
if (valid)
{
C[0] = A[0] - B[0];
C[1] = A[1] - B[1];
C[2] = A[2] - B[2];
FillC(C);
}
}
private void button3_Click(object sender, EventArgs e)
{
bool valid = true;
ValidateAB(ref valid);
if (valid)
{
C[0] = A[1] * B[2] - A[2] * B[1];
C[1] = A[0] * B[2] - A[2] * B[0];
C[2] = A[1] * B[0] - A[0] * B[1];
FillC(C);
}
}
private void button4_Click(object sender, EventArgs e)
{
bool valid = true;
ValidateAB(ref valid);
if (valid)
{
C[0] = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
C[1] = C[2] = 0.0;
FillC(C);
}
}
private void button5_Click(object sender, EventArgs e)
{
bool valid = true;
ValidateAB(ref valid);
if (valid)
{
C[0] = Math.Sqrt(A[0] * A[0] + A[1] * A[1] + A[2] * A[2]);
C[1] = C[2] = 0.0;
FillC(C);
}
}
private void button6_Click(object sender, EventArgs e)
{
bool valid = true;
ValidateAB(ref valid);
if (valid)
{
textBox1.Text = C[0].ToString();
textBox2.Text = C[1].ToString();
textBox3.Text = C[2].ToString();
C[0] = C[1] = C[2] = 0.0;
FillC(C);
}
}
}
}