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
相关推荐
恋猫de小郭几秒前
如何查看项目是否支持最新 Android 16K Page Size 一文汇总
android·开发语言·javascript·kotlin
柚个朵朵41 分钟前
Spring的Validation,这是一套基于注解的权限校验框架
java·后端·spring
于壮士hoho43 分钟前
Python | Dashboard制作
开发语言·python
我重来不说话2 小时前
免费Ollama大模型集成系统——Golang
golang·gin·ollama·免费大模型
Asus.Blogs2 小时前
为什么go语言中返回的指针类型,不需要用*取值(解引用),就可以直接赋值呢?
开发语言·后端·golang
青瓦梦滋2 小时前
【语法】C++的多态
开发语言·c++
C_V_Better2 小时前
Java Spring Boot 控制器中处理用户数据详解
java·开发语言·spring boot·后端·spring
胡子洲2 小时前
Spring Boot 应用中实现基本的 SSE 功能
java·spring boot·后端
t198751282 小时前
基于Qt的OSG三维建模
java·开发语言
贰拾wan2 小时前
【Java-EE进阶】SpringBoot针对某个IP限流问题
java·spring boot·后端·idea