[三边定位] 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-;//误差,小于这个就当作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[], sin_value = new double[];
if (double_equals(circles[].Center.X, circles[].Center.X)
&& double_equals(circles[].Center.Y, circles[].Center.Y)
&& double_equals(circles[].Radius, circles[].Radius))
{
return -;
} d = distance(circles[].Center, circles[].Center);
if (d > circles[].Radius + circles[].Radius
|| d < Math.Abs(circles[].Radius - circles[].Radius))
{
return ;
} a = 2.0 * circles[].Radius * (circles[].Center.X - circles[].Center.X);
b = 2.0 * circles[].Radius * (circles[].Center.Y - circles[].Center.Y);
c = circles[].Radius * circles[].Radius - circles[].Radius * circles[].Radius
- distance_sqr(circles[].Center, circles[].Center);
p = a * a + b * b;
q = -2.0 * a * c;
if (double_equals(d, circles[].Radius + circles[].Radius)
|| double_equals(d, Math.Abs(circles[].Radius - circles[].Radius)))
{
cos_value[] = -q / p / 2.0;
sin_value[] = Math.Sqrt( - cos_value[] * cos_value[]); points[].X = circles[].Radius * cos_value[] + circles[].Center.X;
points[].Y = circles[].Radius * sin_value[] + circles[].Center.Y; if (!double_equals(distance_sqr(points[], circles[].Center),
circles[].Radius * circles[].Radius))
{
points[].Y = circles[].Center.Y - circles[].Radius * sin_value[];
}
return ;
} r = c * c - b * b;
cos_value[] = (Math.Sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
cos_value[] = (-Math.Sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
sin_value[] = Math.Sqrt( - cos_value[] * cos_value[]);
sin_value[] = Math.Sqrt( - cos_value[] * cos_value[]); points[].X = circles[].Radius * cos_value[] + circles[].Center.X;
points[].X = circles[].Radius * cos_value[] + circles[].Center.X;
points[].Y = circles[].Radius * sin_value[] + circles[].Center.Y;
points[].Y = circles[].Radius * sin_value[] + circles[].Center.Y; if (!double_equals(distance_sqr(points[], circles[].Center),
circles[].Radius * circles[].Radius))
{
points[].Y = circles[].Center.Y - circles[].Radius * sin_value[];
}
if (!double_equals(distance_sqr(points[], circles[].Center),
circles[].Radius * circles[].Radius))
{
points[].Y = circles[].Center.Y - circles[].Radius * sin_value[];
}
if (double_equals(points[].Y, points[].Y)
&& double_equals(points[].X, points[].X))
{
if (points[].Y > )
{
points[].Y = -points[].Y;
}
else
{
points[].Y = -points[].Y;
}
}
return ;
} void Draw_circle(Graphics grap, Circle circles)
{
// int iSeed = 10;
Random ro = new Random();
long tick = DateTime.Now.Ticks;
Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> )); int R = ran.Next();
int G = ran.Next();
int B = ran.Next();
B = (R + G > ) ? R + G - : B;//0 : 380 - R - G;
B = (B > ) ? : B; Pen pen = new Pen(Color.Red, );//画笔颜色
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(* circles.Radius), Convert.ToInt32(* 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[];
Point[] points = new Point[]; circles[].Center.X = ; ;
circles[].Center.Y = ;
circles[].Radius =; circles[].Center.X = ;
circles[].Center.Y = ;
circles[].Radius =; circles[].Center.X = ; ;
circles[].Center.Y = ;
circles[].Radius = ; Circle[] copy = (Circle[])circles.Clone(); //copy
// Draw_circle(gra, circles[0]);
Draw_circle(gra, circles[]);
Draw_circle(gra, circles[]);
insect(circles, points);// 0 1
Point Pac; double points_AC_0 = distance(points[], circles[].Center);
double points_AC_1 = distance(points[], circles[].Center);
if (points_AC_0 < points_AC_1)
{
Pac.X = points[].X;
Pac.Y = points[].Y;
}
else
{
Pac.X = points[].X;
Pac.Y = points[].Y;
}
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , );
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , ); circles[].Center.X = circles[].Center.X; ;
circles[].Center.Y = circles[].Center.Y;
circles[].Radius = circles[].Radius;
// Draw_circle(gra, circles[0]);
Draw_circle(gra, circles[]);
Draw_circle(gra, circles[]);
Draw_circle(gra, circles[]);
insect(circles, points);// 0 2 Point Pbc; points_AC_0 = distance(points[], copy[].Center);
points_AC_1 = distance(points[], copy[].Center);
if (points_AC_0 < points_AC_1)
{
Pbc.X = points[].X;
Pbc.Y = points[].Y;
}
else
{
Pbc.X = points[].X;
Pbc.Y = points[].Y;
}
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , );
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , );
circles = (Circle[])copy.Clone(); //copy circles[].Center.X = circles[].Center.X; ;
circles[].Center.Y = circles[].Center.Y;
circles[].Radius = circles[].Radius;
insect(circles, points); // 1 3 Point Pbd; points_AC_0 = distance(points[], copy[].Center);
points_AC_1 = distance(points[], copy[].Center);
if (points_AC_0 < points_AC_1)
{
Pbd.X = points[].X;
Pbd.Y = points[].Y;
}
else
{
Pbd.X = points[].X;
Pbd.Y = points[].Y;
}
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , );
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , ); 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), , ); gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , );
gra.FillRectangle(new SolidBrush(Color.Red), Convert.ToInt32(points[].X), Convert.ToInt32(points[].Y), , ); }
}
}
[三边定位] C# 演示程序的更多相关文章
- 【三边定位】 演示程序V0.1
忙于工作,这个小东西一直没有空去弄, 最近简单修改了些算法, 精度还有待提高. 贴一张图片 坐上角的坐标是鼠标点(31,17),后面location 是三边定位算出来的(31,19),后面跟的erro ...
- 三边定位 c#
MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分. 项目中用到三 ...
- DWM1000 多个基站定位讨论 --[蓝点无限]
该篇是之前<DWM1000 多个标签定位讨论 --[蓝点无限]>的续篇 多基站定位也是定位必然,因为有些稍微大一点的场合,或者多个区域(厂区不同房间)定位,往往4个基站会严重不足. DWM ...
- 【机器学习】WIFI室内定位
WIFI室内定位-指纹法 在A1区域内每个点上采集四个WiFi的信号数据(信号强度),五点.九点.十六点采样. 5*5=25区域*16数据=400样本,用来训练 样本数 R B G1 G2 1 2 ...
- 关于APIT定位算法的讨论
关于APIT定位算法的讨论 [摘要] 无线传感器网络节点定位机制的研究中,基于距离无关的定位技术得到快速发展,其中基于重叠区域的APIT定位技术在实际环境中的定位精度高,被广泛研究和应用. [关键 ...
- LED室内定位算法:RSS,TOA,AOA,TDOA(转载)
转载自:https://blog.csdn.net/baidu_38197452/article/details/77115935 基于LED的室内定位算法大致可以分为四类: 1. 几何测量法 这种方 ...
- Bphero-UWB 基站0 和 电脑串口数据格式定义
基站0 通过串口将系统中测得的距离信息发送到电脑,电脑定位软件通过三边定位算法计算出TAG的坐标,基站0 和 定位软件之间的数据格式定义如下(对官方数据结构进行了简化) 更多UWB定位信息请参阅论坛b ...
- CC2431 代码分析④-衣锦还乡的CC2431
我们在第二节就分析到了 finishCollection( void ),但是当我们分析完第三节后,整个系统才真正执行到这里,我们依然像第二节一样把这个函数全部贴出来 /*************** ...
- SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言]
//SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言][MCS51总线接口方式] //应用产品: SMG12232A2标准图形点阵型液晶显示模块 // 本演示程序适用于SMG1 ...
随机推荐
- 饮冰三年-人工智能-linux-04 vim编辑器
vim的三种模式:命令行模式.编辑模式.扩展模式 1:命令行模式下常见的操作 删除 a):dd 删除光标所在当前行 b):ndd 删除光标所在当前行后的n行 复制 c):yy 复制光标所在当前行 ...
- windows_agent 添加
一:复制windows agent文件和.exe文件到c:\zabbix\目录下 二:配置zabbix_agentd.win.conf文件 hostname:设置为自定义名称,但是要和zabbix-s ...
- RHEL7恢复root密码
RHEL7恢复root密码 首先关闭SELINUX [root@panda ~]# getenforce Disabled 然后重启,按↑↓键,进入如下界面,选择第一项,按下e键进行编辑 在此界面找到 ...
- 实现用VB.Net/(C#)开发K/3 BOS 插件的真正可行方法
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没有认真去找这个方法呀. https://blog.csdn.net/chzjxgd/arti ...
- python内置的魔术命令(builtin magic commands)
在ipython或者jupyter notebook中,会出现"%"开头并且一个很短的命令,例如交互式的matlablib绘图: %matplotlib inline 之前一直不知 ...
- Visual Studio 中使用万能头文件 #include <bits/stdc++.h>
最近开始使用VS,之前用的DEV C++软件可直接使用 #include <bits/stdc++.h> ,但VS中并没有,为了使用方便,可直接在VS中添加此头文件,方法如下: 1.在安 ...
- 【Android】修改Android 模拟器IMSI
修改Android 模拟器IMEI 在.....\android_sdk\tools文件下找到emulator-arm.exe,使用UltraEdit文本编辑器打开,搜索CIMI关键字,把310260 ...
- zabbix通过shell脚本安装异常问题定位
htxk-106主机信息现象如下: 通过zabbix_get命令 zabbix_get [7189]: Check access restrictions in Zabbix agent config ...
- 0day漏洞
0Day的概念最早用于软件和游戏破解,属于非盈利性和非商业化的组织行为,其基本内涵是“即时性”. Warez被许多人误认为是一个最大的软件破解组 织,而实际上,Warez如黑客一样,只是一种行为. 0 ...
- php5.6安装redis各个版本地址集合
igbinary扩展 http://windows.php.net/downloads/pecl/releases/igbinary/2.0.1/ redis扩展 http://windows.php ...