[三边定位] 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 ...
随机推荐
- Nginx + tomcat服务器 负载均衡
Nginx 反向代理初印象 Nginx (“engine x”) 是一个高性能的HTTP和反向代理 服务器,也是一个IMAP/POP3/SMTP服务器.其特点是占有内存少,并发能力强,事实上nginx ...
- 论文阅读笔记三十二:YOLOv3: An Incremental Improvement
论文源址:https://pjreddie.com/media/files/papers/YOLOv3.pdf 代码:https://github.com/qqwweee/keras-yolo3 摘要 ...
- Just Oj 2017C语言程序设计竞赛高级组A: 求近似值(矩阵快速幂)
A: 求近似值 时间限制: 1 s 内存限制: 128 MB 提交 我的状态 题目描述 求⌊(5–√+6–√)2n⌋⌊(5+6)2n⌋%9932017. 例如:n=1,(5–√+6–√)2( ...
- 安装和强行卸载fuse
先卸载 yum list fuse yum --setopt=tsflags=noscripts remove fuse.x86_64 安装 yum install automake gcc-c++ ...
- MySQL主从备份配置实例
转载自:https://www.cnblogs.com/ahaii/p/6307648.html MySQL主从备份配置实例 场景: 1.主服务器192.168.0.225.从服务器192.168.0 ...
- java web获取请求体内容
Java Web中如何获取请求体内容呢? 我们知道请求方式分为两种:Get,Post. /*** * Compatible with GET and POST * * @param request * ...
- Java基础知识➣环境搭建与类型整理(一)
概述 公司业务需要,产品既要有.NET又需要Java,没得选择,只能业余时间学习Java,整体觉得Java也.NET还是很相似的,只是语法有差别,差别也不是很大,这就将学习Java的基础知识整理下,以 ...
- ssm又乱码
以前用mybatis的时候是这样的 mysql.connection.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characte ...
- HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)
CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...
- eclipse发布后在tomcat下没有文件夹,服务器的Server locations不能修改
主要是因为Server locations中的deploy路径不正确,要修改这个路径,需要移除服务器中的所有项目,然后点击清空,这时再双击服务器,既可修改这个设置.