-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.py
More file actions
42 lines (35 loc) · 973 Bytes
/
Problem3.py
File metadata and controls
42 lines (35 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import math
import random
import numpy as np
import time
count=0
trian_area=0 #set the summation of all triangle area
def calc_area(p1, p2, p3):
(x1, y1), (x2, y2), (x3, y3) = p1,p2,p3
return 0.5 * abs(x2 * y3 + x1 * y2 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3)
t_start=time.time()
#do 100000 times
while (count<100000):
ptcount=0
pt=[[],[],[]]
#1st step:choose 3 point
while(ptcount<=2):
v1=2*random.uniform(0,1)-1
v2=2*random.uniform(0,1)-1
if(v1**2+v2**2<=1):
pt[ptcount]=[v1,v2]
ptcount+=1
#2nd step:calculate triangle area and sum up all of them
k=calc_area(pt[0],pt[1],pt[2])
trian_area+=k
count+=1
#let p be the probability of triangle
p=4*trian_area/(count*np.pi)
t_end=time.time()
#print out resault
print('凹四邊形機率:')
print(p)
print('凸四邊形機率:')
print(1-p)
print('花費時間:')
print(t_end-t_start)