计划用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# 演示程序的更多相关文章

  1. 【三边定位】 演示程序V0.1

    忙于工作,这个小东西一直没有空去弄, 最近简单修改了些算法, 精度还有待提高. 贴一张图片 坐上角的坐标是鼠标点(31,17),后面location 是三边定位算出来的(31,19),后面跟的erro ...

  2. 三边定位 c#

    MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分. 项目中用到三 ...

  3. DWM1000 多个基站定位讨论 --[蓝点无限]

    该篇是之前<DWM1000 多个标签定位讨论 --[蓝点无限]>的续篇 多基站定位也是定位必然,因为有些稍微大一点的场合,或者多个区域(厂区不同房间)定位,往往4个基站会严重不足. DWM ...

  4. 【机器学习】WIFI室内定位

    WIFI室内定位-指纹法 在A1区域内每个点上采集四个WiFi的信号数据(信号强度),五点.九点.十六点采样. 5*5=25区域*16数据=400样本,用来训练 样本数 R B G1  G2 1 2 ...

  5. 关于APIT定位算法的讨论

    关于APIT定位算法的讨论 [摘要]   无线传感器网络节点定位机制的研究中,基于距离无关的定位技术得到快速发展,其中基于重叠区域的APIT定位技术在实际环境中的定位精度高,被广泛研究和应用. [关键 ...

  6. LED室内定位算法:RSS,TOA,AOA,TDOA(转载)

    转载自:https://blog.csdn.net/baidu_38197452/article/details/77115935 基于LED的室内定位算法大致可以分为四类: 1. 几何测量法 这种方 ...

  7. Bphero-UWB 基站0 和 电脑串口数据格式定义

    基站0 通过串口将系统中测得的距离信息发送到电脑,电脑定位软件通过三边定位算法计算出TAG的坐标,基站0 和 定位软件之间的数据格式定义如下(对官方数据结构进行了简化) 更多UWB定位信息请参阅论坛b ...

  8. CC2431 代码分析④-衣锦还乡的CC2431

    我们在第二节就分析到了 finishCollection( void ),但是当我们分析完第三节后,整个系统才真正执行到这里,我们依然像第二节一样把这个函数全部贴出来 /*************** ...

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

    //SMG12232A2标准图形点阵型液晶显示模块的演示程序[C51编程语言][MCS51总线接口方式] //应用产品: SMG12232A2标准图形点阵型液晶显示模块 // 本演示程序适用于SMG1 ...

随机推荐

  1. pycharm提示This inspection detects any methods which may safely be made static.

    示例代码: class Car(object): # 未定义任何类属性 def move(self): # 方法会出现下划线提示This inspection detects any methods ...

  2. 修改ElementUI源码样式

    参考:https://segmentfault.com/a/1190000010932321

  3. eclipse检出SVN项目的正确步骤

    一.在工作空间新建工作目录:workspace-xf 二.在工作目录下workspace-xf 新建文件夹 tdvs ,进入该文件夹鼠标右键:SVN CheckOut  检出需要的项目 三.打开ecl ...

  4. 异常:Keyword not supported: 'data source'的解决办法

    将连接字符串中的&quot换为“'”,一个单引号即可. 详细解释:https://blogs.msdn.microsoft.com/rickandy/2008/12/09/explicit-c ...

  5. 饮冰三年-人工智能-Python-14Python基础之变量与函数

    1:函数:函数是逻辑结构化和过程化的一种编程方法.函数即变量 #参数组:**字典 *列表 def test(x,*args): print(args); print(args[0]); print(& ...

  6. 使用CSS选择器定位页面元素

    摘录:http://blog.csdn.net/defectfinder/article/details/51734690 CSS选择器也是一个非常好用的定位元素的方法,甚至比Xpath强大.在自动化 ...

  7. HDU 1247 Hat’s Words(字典树活用)

    Hat's Words Time Limit : 2000 / 1000 MS(Java / Others)    Memory Limit : 65536 / 32768 K(Java / Othe ...

  8. 2017-2018-2 20165328 实验三《敏捷开发与XP实践》实验报告

    一.实践-1: 要求:参考http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD安装alibaba插件,解决代码中的规范问题. 在ID ...

  9. models批量生成数据

    models批量生成数据 1.将数据生成为 列表序列,通过 bulk_create 将数据一次插入数据库中 def host(request): # 插入数据速度快消耗资源少 Hostlist=[] ...

  10. vs无法启动程序,操作在当前状态中是非法的

    问题的图片: 解决方案: 工具--选项--调试--常规--启用asp.net的JavaScript调试(chrome和ie)去掉勾选