解决node先console.log不能用
pnpm install -D tslib @types/node
tsconfig.json
{"compilerOptions": {"target": "es2020","module": "commonjs","lib": ["es2020"],"sourceMap": true,"outDir": "./dist","rootDir": "./src","strict": true,"moduleResolution": "node","baseUrl": "src","esModuleInterop": true,"experimentalDecorators": true,"emitDecoratorMetadata": true,"skipLibCheck": true,"forceConsistentCasingInFileNames": true},"include": ["src/**/*"],"exclude": ["dist", "node_modules"],"compileOnSave": false
}
Point
export class TPoint { x: number = 0 ; y: number = 0 ; constructor ( x: number , y: number ) { this . x = x; this . y = y; } getDistance ( other: TPoint) : number { let xDis = Math. abs ( this . x - other. x) ; let yDis = Math. abs ( this . y - other. y) ; return Math. sqrt ( xDis* xDis + yDis* yDis) ; }
}
Circle
import { TPoint } from "./tspoint" ; class TCircle { radius: number = 0 ; point: TPoint; constructor ( point: TPoint, radius: number ) { this . radius = radius; this . point = point; } isInCircle ( point: TPoint) : boolean { let dis: number = this . point. getDistance ( point) return dis <= this . radius; }
} let point: TPoint = new TPoint ( 1 , 1 )
let circle: TCircle = new TCircle ( point, 1 ) ;
let total = 10000000 ;
let count = 0 ;
for ( let i = 0 ; i < total; i++ ) { let x = Math. random ( ) * 2 ; let y = Math. random ( ) * 2 ; let pointTmp: TPoint = new TPoint ( x, y) ; if ( circle. isInCircle ( pointTmp) ) { count++ ; }
} console . log ( count* 4.0 / total) ;