創作內容

2 GP

新聞 : 東大生藍芽耳機掉一隻「靠數學找回」!日網驚呆:根本GPS

作者:矮鵝│2024-05-07 20:11:51│巴幣:4│人氣:91




有感而發
因為之前在工廠幫認識的工程師寫模具量測的程式有用到這個原理


原理不難,但是能有這個發想靈活運用在日常生活,解決一般人解決不了的難題也是蠻天才的
不過新聞底下的留言好像不是很了解,可能記者沒寫清楚也有關係(ー_ー)



以下就是幫認識的工程師用windows form寫的小程式 :
利用這個原理去找出最接近設定半徑的三個點
和新聞報的東大生不同的是,這裡用的是空間座標,東大生應該是用平面座標

使用起來就像是這樣 :
輸入目標的半徑,以及各種3D座標,每一排3個數字表示x,y,z,各數字用逗點隔開,然後可以從一堆點位中求得最接近目標半徑的的三個點
及該圓的圓心






前陣子去了台中的漫畫博物館
裡面居然有豬股睦美老師的畫作 :








我覺得她負責的傳說系列比較活潑,有很多會心一笑的小劇場~~~


原始碼

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);

    }
}
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=5929615
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:東大|豬股睦美|時空幻境|C#|幾何座標

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

2喜歡★missionary 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:19天前Reddit上有...

追蹤私訊切換新版閱覽

作品資料夾

aaa1357932大家
各位有空可以來我家看看畫作或聽聽我的全創作專輯!看更多我要大聲說9小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】