golang点类圆类求pi值

go 复制代码
package Shape

import "math"

type Point struct{
	X float64
	Y float64
}

func (point *Point)GetDistance(other *Point) float64{
	return  math.Sqrt(math.Pow((point.X-other.X),2)+math.Pow((point.Y-other.Y),2))
}
go 复制代码
package Circle

import "awesomeProject/Shape"

type Circle struct {
	Heart  Shape.Point
	Radius float64
}

func (circle *Circle)IsPointInCircle(point *Shape.Point) bool{
	return  circle.Heart.GetDistance(point) <= circle.Radius
}
go 复制代码
package main

import (
	"awesomeProject/Circle"
	"awesomeProject/Shape"
	"fmt"
	"math/rand"
	"time"
)

const total = 1000000
func main() {
	point := Shape.Point{1, 1}
	circle := Circle.Circle{point, 1}
	rand.Seed(time.Now().UnixNano())

	count := 0
	for i := 0; i < total;i++{
		x := rand.Float64()*2
		y := rand.Float64()*2
		pointTmp := Shape.Point{x, y}
		if(circle.IsPointInCircle(&pointTmp)){
			count++
		}
	}
	fmt.Println(float64(count * 4.0) / total)
}

go.mod

复制代码
module awesomeProject

go 1.12
相关推荐
kylezhao201913 分钟前
C#通过HSLCommunication库操作PLC用法
开发语言·c#
短剑重铸之日1 小时前
SpringBoot声明式事务的源码解析
java·后端·spring·springboot
JIngJaneIL1 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
期待のcode1 小时前
Java的抽象类和接口
java·开发语言
wadesir2 小时前
Go语言中高效读取数据(详解io包的ReadAll函数用法)
开发语言·后端·golang
千寻技术帮2 小时前
10422_基于Springboot的教务管理系统
java·spring boot·后端·vue·教务管理
Victor3562 小时前
Hibernate(12)什么是Hibernate的实体类?
后端
Victor3562 小时前
Hibernate(11)什么是Hibernate的持久化上下文?
后端
小高不明2 小时前
前缀和一维/二维-复习篇
开发语言·算法
龘龍龙2 小时前
Python基础(八)
开发语言·python