求两圆交点算法代码实现(Golang/Go语言)
2016-07-25 15:10
543 查看
数学求解原理请看:http://blog.csdn.net/qq245671051/article/details/52017403
下面是golang代码实现:
下面是golang代码实现:
package main import ( "math" ) //代表一个点,包含横纵坐标 type Point struct { X, Y float64 } //代表一个圆,包含横纵坐标及半径 type Circle struct { Point R float64 } //创建圆对象 func NewCircle(x, y, r float64) *Circle { return &Circle{Point{x, y}, r} } //求两圆相交的交点,交点个数可能有0,1,2 func Intersect(a *Circle, b *Circle) (p []Point) { dx, dy := b.X - a.X, b.Y - a.Y lr := a.R + b.R //半径和 dr := math.Abs(a.R - b.R) //半径差 ab := math.Sqrt(dx * dx + dy * dy) //圆心距 if ab <= lr && ab > dr { theta1 := math.Atan(dy / dx) ef := lr - ab ao := a.R - ef / 2 theta2 := math.Acos(ao / a.R) theta := theta1 + theta2 xc := a.X + a.R * math.Cos(theta) yc := a.Y + a.R * math.Sin(theta) p = append(p, Point{xc, yc}) if ab < lr { //两个交点 theta3 := math.Acos(ao / a.R) theta = theta3 - theta1 xd := a.X + a.R * math.Cos(theta) yd := a.Y - a.R * math.Sin(theta) p = append(p, Point{xd, yd}) } } return }
相关文章推荐
- Golang测试技术
- GoogLeNet
- Django安装配置及HelloWorld
- Navicat导入数据时发生了报错 --- 1153 - Got a packet bigger than 'max_allowed的处理办法
- How to play BreakerBall game on DragonBoard 410c ?
- pymongo方法
- Google推荐的图片加载库Glide介绍
- 使用USRP和GNU Radio实现GPS位置伪造
- Google Charts -- Timelines 使用说明
- Golang-简洁的并发
- Django新手学习总结整理记录
- go特性学习
- Sort Algorithm-->Select Sort
- 2.go开源groupcache项目——关于protobuf
- go中方法的接收者是值或者指针的区别
- Django集成的子框架
- Sort Algorithm-->Bubble Sort
- Google首席工程师Joshua Bloch谈如何设计优秀的API
- Algorithms for Detecting Significantly Mutated Pathways in Cancer
- Google推荐的图片加载库Glide介绍