计划用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. vue :基本语法格式

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Vu ...

  2. oracle数据库无法连接 The Network Adapter could not establish

    Caused by: java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection 这个错误 ...

  3. A.Ocean的礼物线段树

    A: Ocean的礼物  Time Limit: 2 s Memory Limit: 128 MB  Submit My Status  Problem Description  皇家理工存在一段很神 ...

  4. 005-Python字典

    Python字典(dict) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号{}中: ...

  5. 据说是Flord算法

    贵有恒,何必三更起五更眠:最无益,莫过一日曝十日寒. 问题 C: Restoring Road Network 问题 C: Restoring Road Network 时间限制: 1 Sec  内存 ...

  6. Python自定义排序

    比如自定义了一个class,并且实例化了这个类的很多个实例,并且组成一个数组.这个数组要排序,是通过这个class的某个字段来排序的.怎么排序呢? 有两种做法: 第一种是定义__cmp__( )方法: ...

  7. How to disable Microsoft Compatibility Telemetry

    Issue: How to disable Microsoft Compatibility Telemetry (CompatTelRunner.exe)?   Option : Disable Mi ...

  8. java运算符-逻辑、三元运算符

    1.逻辑运算符 逻辑运算符,它是用于布尔值进行运算的,运算的最终结果为布尔值true或false. 运算符 运算规则 范例 结果 & 与 false&true False | 或 fa ...

  9. AtCoder Regular Contest 102 (ARC102) D All Your Paths are Different Lengths 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/ARC102D.html 题目传送门 - ARC102D 题意 给定 $L$,请你构造一个节点个数为 $n$ ,边 ...

  10. day76 auth模块 用户验证,

    概要: form组件回顾: (1) 创建form组件对应的类,比如LoginForm (2) views.login: if get请求: form_obj=LoginForm() return re ...