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
相关推荐
Code Warrior1 小时前
【每日算法】专题五_位运算
开发语言·c++
程序员张31 小时前
SpringBoot计时一次请求耗时
java·spring boot·后端
沐知全栈开发3 小时前
HTML DOM 访问
开发语言
脑袋大大的4 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
二进制person5 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6665 小时前
C++讲解---创建日期类
开发语言·c++·算法
码农不惑6 小时前
2025.06.27-14.44 C语言开发:Onvif(二)
c语言·开发语言
程序员岳焱7 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*7 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
Coding小公仔8 小时前
C++ bitset 模板类
开发语言·c++