using System;
using System.Diagnostics;
using System.Globalization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Windows.Forms;
using System.IO;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
int pointCounts;
int[] pointPositions = new int[3];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
calculate();
}
private void calculate()
{
string str;
char ch = ';';
char dot = ',';
str = textBox1.Text;
string[] sArray = str.Split(new char[2] { ';', ',' });
int chfreq = str.Count(f => (f == ch));
int dotfreq = str.Count(f => (f == dot));
float[,] coordinations = new float[90, 3];
for (int i = 0; i < sArray.Count(); i++)
{
Trace.WriteLine(sArray[i]);
}
if (sArray.Count() % 3 == 0 && textBox2.Text != null)
{
pointCounts = sArray.Count() / 3;
for (int i = 0; i < (sArray.Count() + 1) / 3; i++)
{
for (int j = 0; j <= 2; j++)
{
coordinations[i, j] = float.Parse(sArray[i * 3 + j], CultureInfo.InvariantCulture.NumberFormat);
//Trace.WriteLine("arrary count " + sArray.Count() + "array number : " + coordinations[i,j] + " i value : " + i + " j value : " + j);
}
}
radious(coordinations);
}
else
{
var result = MessageBox.Show("error", "error",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
}
//label1.Text = chfreq.ToString() + " " + dotfreq.ToString();
}
private void radious(float[,] inputArray)
{
double radiusGap = 100f;
float[] currentCircleCenter = new float[3];
double currentRadius;
float targetRadius = float.Parse(textBox2.Text);
double bestRadius = 0;
string textString = "";
foreach (float a in inputArray)
{
if (a != null) { }
//Trace.WriteLine(inputArray.Length);
}
for (int i = 0; i <= pointCounts - 2 - 1; i++)
{
for (int j = i + 1; j <= pointCounts - 1 - 1; j++)
{
for (int k = j + 1; k <= pointCounts - 1; k++)
{
float a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, factor0, factor1, factor2, factor3;
a1 = inputArray[i, 0] - inputArray[j, 0]; a2 = inputArray[i, 1] - inputArray[j, 1]; a3 = inputArray[i, 2] - inputArray[j, 2];
b1 = inputArray[j, 0] - inputArray[k, 0]; b2 = inputArray[j, 1] - inputArray[k, 1]; b3 = inputArray[j, 2] - inputArray[k, 2];
c1 = (inputArray[j, 1] - inputArray[i, 1]) * (inputArray[k, 2] - inputArray[i, 2]) - (inputArray[j, 2] - inputArray[i, 2]) * (inputArray[k, 1] - inputArray[i, 1]);
c2 = (inputArray[j, 2] - inputArray[i, 2]) * (inputArray[k, 0] - inputArray[i, 0]) - (inputArray[j, 0] - inputArray[i, 0]) * (inputArray[k, 2] - inputArray[i, 2]);
c3 = (inputArray[j, 0] - inputArray[i, 0]) * (inputArray[k, 1] - inputArray[i, 1]) - (inputArray[j, 1] - inputArray[i, 1]) * (inputArray[k, 0] - inputArray[i, 0]);
d1 = (inputArray[i, 0] * inputArray[i, 0] - inputArray[j, 0] * inputArray[j, 0] + inputArray[i, 1] * inputArray[i, 1] - inputArray[j, 1] * inputArray[j, 1] + inputArray[i, 2] * inputArray[i, 2] - inputArray[j, 2] * inputArray[j, 2]) / 2;
d2 = (inputArray[j, 0] * inputArray[j, 0] - inputArray[k, 0] * inputArray[k, 0] + inputArray[j, 1] * inputArray[j, 1] - inputArray[k, 1] * inputArray[k, 1] + inputArray[j, 2] * inputArray[j, 2] - inputArray[k, 2] * inputArray[k, 2]) / 2;
d3 = c1 * inputArray[i, 0] + c2 * inputArray[i, 1] + c3 * inputArray[i, 2];
factor0 = a1 * b2 * c3 + b1 * c2 * a3 + c1 * a2 * b3 - c1 * b2 * a3 - b1 * a2 * c3 - a1 * c2 * b3;
factor1 = d1 * b2 * c3 + d2 * c2 * a3 + d3 * a2 * b3 - d3 * b2 * a3 - d2 * a2 * c3 - d1 * c2 * b3;
factor2 = a1 * d2 * c3 + b1 * d3 * a3 + c1 * d1 * b3 - c1 * d2 * a3 - b1 * d1 * c3 - a1 * d3 * b3;
factor3 = a1 * b2 * d3 + b1 * c2 * d1 + c1 * a2 * d2 - c1 * b2 * d1 - b1 * a2 * d3 - a1 * c2 * d2;
currentCircleCenter[0] = factor1 / factor0; currentCircleCenter[1] = factor2 / factor0; currentCircleCenter[2] = factor3 / factor0;
currentRadius = Math.Pow(Math.Pow(inputArray[i, 0] - currentCircleCenter[0], 2) + Math.Pow(inputArray[i, 1] - currentCircleCenter[1], 2) + Math.Pow(inputArray[i, 2] - currentCircleCenter[2], 2), 0.5);
textString = textString + " _points : " + (i + 1).ToString() + " , " + (j + 1).ToString() + " , " + (k + 1).ToString() +
" _radius : " + currentRadius.ToString() + " rnrn ";
if (Math.Abs(currentRadius - targetRadius) < radiusGap)
{
radiusGap = Math.Abs(currentRadius - targetRadius);
bestRadius = currentRadius;
pointPositions[0] = i + 1; pointPositions[1] = j + 1; pointPositions[2] = k + 1;
Trace.WriteLine("radius : " + currentRadius + " points positions : " + i.ToString() + j.ToString() + k.ToString());
}
textBox3.Text = "radius : " + bestRadius + " , best points : " + pointPositions[0].ToString() + " , " + pointPositions[1].ToString() + " , " + pointPositions[2].ToString() + " rn rn " + textString;
}
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
//Console.WriteLine(item);
}
}