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
相关推荐
yangminlei2 分钟前
Spring Boot3集成LiteFlow!轻松实现业务流程编排
java·spring boot·后端
计算机毕设VX:Fegn089510 分钟前
计算机毕业设计|基于springboot + vue医院设备管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
txinyu的博客10 分钟前
解析业务层的key冲突问题
开发语言·c++·分布式
J_liaty11 分钟前
Spring Boot整合Nacos:从入门到精通
java·spring boot·后端·nacos
码不停蹄Zzz16 分钟前
C语言第1章
c语言·开发语言
面汤放盐31 分钟前
后端系统设计文档模板
后端
行者961 小时前
Flutter跨平台开发在OpenHarmony上的评分组件实现与优化
开发语言·flutter·harmonyos·鸿蒙
阿蒙Amon1 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
SmartRadio1 小时前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
2***d8851 小时前
SpringBoot 集成 Activiti 7 工作流引擎
java·spring boot·后端