[三边定位] C# 演示程序

 计划用CC2530做定位,网上找了一些求圆交点的程序, 修改成3个圆求交点的质心,感觉算法还行。 粗略写了一下程序,结果还行。

  现在只能手动输入3个圆的信息。 后面需要再优化。

  

全部未优化的程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

        /* static void Main(string[] args)
         {
             Circle[] circles = new Circle[2];
             Point[] points = new Point[2];
             Console.Write("请输入两圆x,y,半径(以逗号分开):");
             char[] sep = new char[] { ‘ ‘, ‘,‘, ‘,‘ };
             string[] tempstr;
             while (true)
             {
                 tempstr = Console.ReadLine().Split(sep, StringSplitOptions.RemoveEmptyEntries);
                 if (tempstr.Length != 6)
                 {
                     Console.Write("输入有误\n按任意键退出...");
                     Console.ReadKey();
                     Environment.Exit(0);
                 }
                 circles[0].X = double.Parse(tempstr[0]);
                 circles[0].Y = double.Parse(tempstr[1]);
                 circles[0].Radius = double.Parse(tempstr[2]);
                 circles[1].X = double.Parse(tempstr[3]);
                 circles[1].Y = double.Parse(tempstr[4]);
                 circles[1].Radius = double.Parse(tempstr[5]);
                 switch (insect(circles, points))
                 {
                     case -1:
                         Console.Write("两圆相同\n");
                         break;
                     case 0:
                         Console.Write("不相交\n");
                         break;
                     case 1:
                         Console.WriteLine(points[0]);
                         break;
                     case 2:
                         Console.WriteLine(string.Join(" ", points));
                         break;
                 }
             }
         }*/

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        struct Point
        {
            public double X;
            public double Y;
            public override string ToString()
            {
                return string.Format("({0},{1})", X, Y);
            }
        }

        struct Circle
        {
            public Point Center;
            public double X { get { return Center.X; } set { Center.X = value; } }
            public double Y { get { return Center.Y; } set { Center.X = value; } }
            public double Radius;
        };
           const double ZERO = 1e-9;//误差,小于这个就当作0
        static bool double_equals(double a, double b)
        {
            return Math.Abs(a - b) < ZERO;
        }

        static double distance_sqr(Point a, Point b)
        {
            return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
        }

        static double distance(Point a, Point b)
        {
            return Math.Sqrt(distance_sqr(a, b));
        }

        int insect(Circle[] circles, Point[] points)
        {
            double d, a, b, c, p, q, r;
            double[] cos_value = new double[2], sin_value = new double[2];
            if (double_equals(circles[0].Center.X, circles[1].Center.X)
                && double_equals(circles[0].Center.Y, circles[1].Center.Y)
                && double_equals(circles[0].Radius, circles[1].Radius))
            {
                return -1;
            }

            d = distance(circles[0].Center, circles[1].Center);
            if (d > circles[0].Radius + circles[1].Radius
                || d < Math.Abs(circles[0].Radius - circles[1].Radius))
            {
                return 0;
            }

            a = 2.0 * circles[0].Radius * (circles[0].Center.X - circles[1].Center.X);
            b = 2.0 * circles[0].Radius * (circles[0].Center.Y - circles[1].Center.Y);
            c = circles[1].Radius * circles[1].Radius - circles[0].Radius * circles[0].Radius
                - distance_sqr(circles[0].Center, circles[1].Center);
            p = a * a + b * b;
            q = -2.0 * a * c;
            if (double_equals(d, circles[0].Radius + circles[1].Radius)
                || double_equals(d, Math.Abs(circles[0].Radius - circles[1].Radius)))
            {
                cos_value[0] = -q / p / 2.0;
                sin_value[0] = Math.Sqrt(1 - cos_value[0] * cos_value[0]);

                points[0].X = circles[0].Radius * cos_value[0] + circles[0].Center.X;
                points[0].Y = circles[0].Radius * sin_value[0] + circles[0].Center.Y;

                if (!double_equals(distance_sqr(points[0], circles[1].Center),
                                   circles[1].Radius * circles[1].Radius))
                {
                    points[0].Y = circles[0].Center.Y - circles[0].Radius * sin_value[0];
                }
                return 1;
            }

            r = c * c - b * b;
            cos_value[0] = (Math.Sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
            cos_value[1] = (-Math.Sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
            sin_value[0] = Math.Sqrt(1 - cos_value[0] * cos_value[0]);
            sin_value[1] = Math.Sqrt(1 - cos_value[1] * cos_value[1]);

            points[0].X = circles[0].Radius * cos_value[0] + circles[0].Center.X;
            points[1].X = circles[0].Radius * cos_value[1] + circles[0].Center.X;
            points[0].Y = circles[0].Radius * sin_value[0] + circles[0].Center.Y;
            points[1].Y = circles[0].Radius * sin_value[1] + circles[0].Center.Y;

            if (!double_equals(distance_sqr(points[0], circles[1].Center),
                               circles[1].Radius * circles[1].Radius))
            {
                points[0].Y = circles[0].Center.Y - circles[0].Radius * sin_value[0];
            }
            if (!double_equals(distance_sqr(points[1], circles[1].Center),
                               circles[1].Radius * circles[1].Radius))
            {
                points[1].Y = circles[0].Center.Y - circles[0].Radius * sin_value[1];
            }
            if (double_equals(points[0].Y, points[1].Y)
                && double_equals(points[0].X, points[1].X))
            {
                if (points[0].Y > 0)
                {
                    points[1].Y = -points[1].Y;
                }
                else
                {
                    points[0].Y = -points[0].Y;
                }
            }
            return 2;
        }

        void Draw_circle(Graphics grap, Circle circles)
        {
           // int iSeed = 10;
            Random ro = new Random(10);
            long tick = DateTime.Now.Ticks;
            Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));

            int R = ran.Next(255);
            int G = ran.Next(255);
            int B = ran.Next(255);
            B = (R + G > 400) ? R + G - 400 : B;//0 : 380 - R - G;
            B = (B > 255) ? 255 : B;

            Pen pen = new Pen(Color.Red, 5);//画笔颜色
            pen.Color = Color.FromArgb(R, G, B);
            grap.DrawEllipse(pen, Convert.ToInt32(circles.Center.X - circles.Radius), Convert.ToInt32(circles.Center.Y - circles.Radius), Convert.ToInt32(2* circles.Radius), Convert.ToInt32(2* circles.Radius));//画椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics gra = this.pictureBox1.CreateGraphics();
            gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
             Circle[] circles = new Circle[3];
             Point[] points = new Point[2];

             circles[0].Center.X = 130; ;
             circles[0].Center.Y = 130;
             circles[0].Radius =120;

             circles[1].Center.X = 400;
             circles[1].Center.Y = 100;
             circles[1].Radius =180;

             circles[2].Center.X = 320; ;
             circles[2].Center.Y = 350;
             circles[2].Radius = 180;

             Circle[] copy = (Circle[])circles.Clone(); //copy
             //  Draw_circle(gra, circles[0]);
             Draw_circle(gra, circles[0]);
             Draw_circle(gra, circles[1]);
             insect(circles, points);// 0 1
             Point Pac;

             double points_AC_0 = distance(points[0], circles[2].Center);
             double points_AC_1 = distance(points[1], circles[2].Center);
             if (points_AC_0 < points_AC_1)
             {
                 Pac.X = points[0].X;
                 Pac.Y = points[0].Y;
             }
             else
             {
                 Pac.X = points[1].X;
                 Pac.Y = points[1].Y;
             }
             gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[0].X), Convert.ToInt32(points[0].Y), 8, 8);
             gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[1].X), Convert.ToInt32(points[1].Y), 8, 8);

             circles[1].Center.X = circles[2].Center.X; ;
             circles[1].Center.Y = circles[2].Center.Y;
             circles[1].Radius = circles[2].Radius;
             //  Draw_circle(gra, circles[0]);
             Draw_circle(gra, circles[0]);
             Draw_circle(gra, circles[1]);
             Draw_circle(gra, circles[2]);
             insect(circles, points);// 0 2

             Point Pbc;

             points_AC_0 = distance(points[0], copy[1].Center);
             points_AC_1 = distance(points[1], copy[1].Center);
             if (points_AC_0 < points_AC_1)
             {
                 Pbc.X = points[0].X;
                 Pbc.Y = points[0].Y;
             }
             else
             {
                 Pbc.X = points[1].X;
                 Pbc.Y = points[1].Y;
             }
             gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[0].X), Convert.ToInt32(points[0].Y), 8, 8);
             gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[1].X), Convert.ToInt32(points[1].Y), 8, 8);
             circles = (Circle[])copy.Clone(); //copy

             circles[0].Center.X = circles[2].Center.X; ;
             circles[0].Center.Y = circles[2].Center.Y;
             circles[0].Radius = circles[2].Radius;
             insect(circles, points); // 1 3

             Point Pbd;

             points_AC_0 = distance(points[0], copy[2].Center);
             points_AC_1 = distance(points[1], copy[2].Center);
             if (points_AC_0 < points_AC_1)
             {
                 Pbd.X = points[0].X;
                 Pbd.Y = points[0].Y;
             }
             else
             {
                 Pbd.X = points[1].X;
                 Pbd.Y = points[1].Y;
             }
             gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[0].X), Convert.ToInt32(points[0].Y), 8, 8);
             gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[1].X), Convert.ToInt32(points[1].Y), 8, 8);

             double P_1, P_2;
             P_1 = (Pac.X + Pbc.X + Pbd.X) / 3.0;
             P_2 = (Pac.Y + Pbc.Y + Pbd.Y) / 3.0;
             gra.FillRectangle(new SolidBrush(Color.Blue), Convert.ToInt32(P_1), Convert.ToInt32(P_2), 20, 20);

            gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[0].X), Convert.ToInt32(points[0].Y), 4, 4);
            gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[1].X), Convert.ToInt32(points[1].Y), 4, 4);

        }
    }
}
时间: 2024-08-02 11:04:58

[三边定位] C# 演示程序的相关文章

三边定位 c#

MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分. 项目中用到三边定位,c#中没有MATLAB solve函数来解方程,只好人工去解方程了. class Program { static void Main(string[] args) { Point p1 = new Point() { X = 0, Y = 2, Distance = Math.Sqrt(5

室内定位(最全的总结:种类,方法,特点)

时间过得真快,从研究生开始到现在三年过去了. 也就是我做室内定位已经三年多了,不成器,没做出什么成果. 不过踩了不少坑,在这里做个总结,如果有人翻到这篇博客,就当科普也好,如果恰好你也是这个方向,能避免你踩些坑,有些方向的作用那也就值了. ps.可能持续更新... 1室内定位的主要种类 这里要讲的室内定位,主要针对我自己做过或者理解的内容,主要是基于手机平台的行人的室内定位.至于机器人的SLAM通过的激光雷达等部分暂时不敢妄议. 目前来看,从定位信号来源大致可以分为: 1.基于无线信号发射设备的

关于APIT定位算法的讨论

关于APIT定位算法的讨论 [摘要]   无线传感器网络节点定位机制的研究中,基于距离无关的定位技术得到快速发展,其中基于重叠区域的APIT定位技术在实际环境中的定位精度高,被广泛研究和应用. [关键词] 无线传感器网络:定位算法:APIT: [正文] 在传感网络中的许多应用中,用户一般都会关心一个重要问题,即特定时间发生的具体位置或区域.例如,目标跟踪,入侵检测,环境监控等,若不知道传感器自身的位置,感知的数据是没有意义的.因此,传感器网络及诶单必须知道自身所在的位置,才能够有效地说明被检测物

好好说一说室内定位技术

室内定位技术进过了几十年的发展,从未像今天这样引起大家的关注,这无疑得益于VR技术在这几年的蓬勃发展,52VR的编辑们今天就梳理下已有的定位技术和手段,有哪些点值得我们参考和注意呢? 那么传统的室内定位技术有哪些呢? 室内无线定位技术可以这样分成三类: 近邻法 三边(角)测量法 模式匹配法. 近邻法: 最简单的方式,直接选定那个信号强度最大的AP的位置.纠正一个很容易被误导的地方,目前大多数手机中的定位方式为(GPS/AGPS.基站定 位.WiFi定位),这里的WiFi定位并不是位置指纹法,而是

无线定位技术

实现室内地图定位导航,需要解决哪些技术难题? 2015-06-18 15:00:00 来源: 知乎每日精选 跟贴 0 条 手机看新闻 Wi-Fi指纹定位是什么原理? 室内定位中的位置指纹法,简单来说,就是事先把各个位置上的信号特征(各Wi-Fi的信号强度)测量一遍,存入指纹数据库.定位的时候,将当前的信号特征与指纹库中的进行匹配,从而确定位置.下图是我本科毕设时画的示意图(AP就是WiFi,RP是离线采集选取的参考点): 离线阶段:记录下每个RP处测到的信号强度,每组数据(指纹)包含4个信号强度

SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言]

//SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言][MCS51总线接口方式] //应用产品: SMG12232A2标准图形点阵型液晶显示模块 // 本演示程序适用于SMG12232A2液晶显示模块与MCS51系列单片机采用MCS51总线的 //硬件连线方式. // 本演示程序包括SED1520A兼容芯片的MCS51总线接口子程序集,SED1520A兼容芯片的12232 //液晶显示模块的基本子程序,12232系列绘图子程序集,12232系列图形子程序集,12232系列

STM32W108无线传感器网络节点定位技术

使用STM32W108无线节点完成基于接收信号强度指示(RSSI:Received SignalStrength Indication)的N次三边质心加权定位,对移动中的节点实时进行定位,并将定位结果发送到汇集中心.图15.1为定位实验的实际场景,所有实验在室内完成,共使用9个节点,1个移动节点,8个信标节点. 图15.1. 定位实际场景 基于第10章介绍的SimpleMac协议栈,对程序进行删减更改,下面给出更改部分代码: 文件solar-system.c相关内容: 函数processRxPa

微信实现定位城市并获取城市编码

最近在做一个项目是将用户的当前所在市县定位出来并展示在手机端页面,同时还要获取到该市县的城市编码从而进行数据过滤,这里重点讲定位城市及获取城市编码 前端页面代码: 首先引用腾讯地图的一个js <script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js" ></script> 同时在页面加载

div与 css--绝对定位和相对定位

<10-页面美化专题-div和css基础.avi> Border-topPadding-topMargin-topFloat Position #clear{Both:clear;}//浮动 float为了不影响后面的 语句#son2{position:relative; //没有脱离文档流left:60%;} position :absolute;left:0px;绝对定位 就脱离了文档 子的绝对定位:要看爸爸是否有相对定位:如果有就相对爸爸的边框,如果没有就相对浏览器的定位了: