From ad0a9750ede410d8ed802915da32f5fea22aca68 Mon Sep 17 00:00:00 2001 From: iotcat Date: Fri, 25 Oct 2019 15:37:03 +0100 Subject: [PATCH] part 2 --- func/getDisProb.asv | 21 +++++++++++++++++++++ func/getDisProb.m | 6 +++--- func/getDivByPos.asv | 10 ++++++++++ func/getDivByPos.m | 13 +++++++++++++ func/isGoalKept.asv | 13 +++++++++++++ func/isGoalKept.m | 13 +++++++++++++ func/normrnd_circle.m | 14 +++++++++----- func/uniform_5case.asv | 4 ++++ func/uniform_5case.m | 5 +++++ func/uniform_5case_plus.m | 9 +++++++++ t10_8.m | 26 ++++++++++++++++++++++++++ t10_9.m | 26 ++++++++++++++++++++++++++ t8.m | 26 ++++++++++++++++++++++++++ t9.m | 26 ++++++++++++++++++++++++++ 14 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 func/getDisProb.asv create mode 100644 func/getDivByPos.asv create mode 100644 func/getDivByPos.m create mode 100644 func/isGoalKept.asv create mode 100644 func/isGoalKept.m create mode 100644 func/uniform_5case.asv create mode 100644 func/uniform_5case.m create mode 100644 func/uniform_5case_plus.m create mode 100644 t10_8.m create mode 100644 t10_9.m create mode 100644 t8.m create mode 100644 t9.m diff --git a/func/getDisProb.asv b/func/getDisProb.asv new file mode 100644 index 0000000..988d8ae --- /dev/null +++ b/func/getDisProb.asv @@ -0,0 +1,21 @@ +% compute distributation +function [score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, isPlot, disMethod, goalkeepMethod) +score = 0; +totalTimes = 0; +for m = 1 : R + for n = 1 : N + [x, y] = disMethod(Radius); + totalTimes = totalTimes + 1; + if isInRect(Length/2, Width/2, x, y) && (nargin<8 || isGoalKept()) + score = score + 1; + if isPlot + d_rx = plot(x, y, 'rx'); + end + else + if isPlot + d_bo = plot(x, y, 'bo'); + end + end + end + prs = score / totalTimes; +end diff --git a/func/getDisProb.m b/func/getDisProb.m index 86d58a5..97dee41 100644 --- a/func/getDisProb.m +++ b/func/getDisProb.m @@ -1,12 +1,12 @@ % compute distributation -function [score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, isPlot, method) +function [score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, isPlot, disMethod, goalkeepMethod) score = 0; totalTimes = 0; for m = 1 : R for n = 1 : N - [x, y] = method(Radius); + [x, y] = disMethod(Radius); totalTimes = totalTimes + 1; - if isInRect(Length/2, Width/2, x, y) + if isInRect(Length/2, Width/2, x, y) && (nargin<8 || ~isGoalKept(Length, Width, x, y, goalkeepMethod)) score = score + 1; if isPlot d_rx = plot(x, y, 'rx'); diff --git a/func/getDivByPos.asv b/func/getDivByPos.asv new file mode 100644 index 0000000..155a302 --- /dev/null +++ b/func/getDivByPos.asv @@ -0,0 +1,10 @@ +function res = getDivByPos(Length, Width, x, y) + res = 0; + ux=Length/4; + uy=Width/2; + + x = x + Length/2; + y = y + Width/2; + + px = ceil(x/ux); +end \ No newline at end of file diff --git a/func/getDivByPos.m b/func/getDivByPos.m new file mode 100644 index 0000000..430deda --- /dev/null +++ b/func/getDivByPos.m @@ -0,0 +1,13 @@ +function res = getDivByPos(Length, Width, x, y) + + ux=Length/4; + uy=Width/2; + + x = x + Length/2; + y = y + Width/2; + + px = ceil(x/ux); + py = floor(y/uy); + + res = px + py*4; +end \ No newline at end of file diff --git a/func/isGoalKept.asv b/func/isGoalKept.asv new file mode 100644 index 0000000..fb8efa0 --- /dev/null +++ b/func/isGoalKept.asv @@ -0,0 +1,13 @@ +function res = isGoalKept(Length, Width, x, y, goalkeepMethod) + div = getDivByPos(Length, Width, x, y); + act = goalkeepMethod(); + + pattern = [2, 3, 6, 7; ]; + + res = false; + + % case 1 + if act == 1 && + + +end \ No newline at end of file diff --git a/func/isGoalKept.m b/func/isGoalKept.m new file mode 100644 index 0000000..aa77d0a --- /dev/null +++ b/func/isGoalKept.m @@ -0,0 +1,13 @@ +function res = isGoalKept(Length, Width, x, y, goalkeepMethod) + div = getDivByPos(Length, Width, x, y); + act = goalkeepMethod(); + + pattern = [2, 3, 6, 7; 2, 5, 0, 0; 3, 8, 0, 0; 1, 2, 0, 0; 3, 4, 0, 0]; + + if ismember(div, pattern(act, :)) + res = true; + else + res = false; + end + +end \ No newline at end of file diff --git a/func/normrnd_circle.m b/func/normrnd_circle.m index dc1ec19..fe5649a 100644 --- a/func/normrnd_circle.m +++ b/func/normrnd_circle.m @@ -1,9 +1,13 @@ % get random position in circle function [res_x, res_y] = normrnd_circle(r) - ang = 2*pi*rand(1); - t_r = normrnd(0, r); - res_x = t_r*cos(ang); - res_y = t_r*sin(ang); - + while true + ang = 2*pi*rand(1); + t_r = normrnd(0, r); + res_x = t_r*cos(ang); + res_y = t_r*sin(ang); + if sqrt(res_x^2 + res_y^2) < r + break; + end + end diff --git a/func/uniform_5case.asv b/func/uniform_5case.asv new file mode 100644 index 0000000..e89f8f4 --- /dev/null +++ b/func/uniform_5case.asv @@ -0,0 +1,4 @@ +function res = uniform_5case() + + +end \ No newline at end of file diff --git a/func/uniform_5case.m b/func/uniform_5case.m new file mode 100644 index 0000000..7add1d4 --- /dev/null +++ b/func/uniform_5case.m @@ -0,0 +1,5 @@ +function res = uniform_5case() + + res = randi(5, 1, 1); + +end \ No newline at end of file diff --git a/func/uniform_5case_plus.m b/func/uniform_5case_plus.m new file mode 100644 index 0000000..6815541 --- /dev/null +++ b/func/uniform_5case_plus.m @@ -0,0 +1,9 @@ +function res = uniform_5case_plus() + + if rand(1) <= 0.9 + res = randi([4, 5], 1, 1); + else + res = randi(3, 1, 1); + end + +end \ No newline at end of file diff --git a/t10_8.m b/t10_8.m new file mode 100644 index 0000000..5dc45fb --- /dev/null +++ b/t10_8.m @@ -0,0 +1,26 @@ + +% init +clear +addpath(genpath('./func/')); + +% declear const +Radius = sqrt(5); +Width = 2; +Length = 4; + +% params input +N = input("Shots Times: "); +R = input("Repeating Times: "); + +% draw background +drawBackGround(Radius, Width, Length); + +% calculate prs +[score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, true, @unifrnd_circle, @uniform_5case_plus); + +% add legend +legend([d_rx, d_bo], {'scored shot', 'missed shot'}); +t_s = sprintf('Scatter plot for N=%d and R=%d. prs=%d%%', N, R, round(prs*100)); +title(t_s); + +display(prs); diff --git a/t10_9.m b/t10_9.m new file mode 100644 index 0000000..2ea04a6 --- /dev/null +++ b/t10_9.m @@ -0,0 +1,26 @@ + +% init +clear +addpath(genpath('./func/')); + +% declear const +Radius = sqrt(5); +Width = 2; +Length = 4; + +% params input +N = input("Shots Times: "); +R = input("Repeating Times: "); + +% draw background +drawBackGround(Radius, Width, Length); + +% calculate prs +[score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, true, @normrnd_circle, @uniform_5case_plus); + +% add legend +legend([d_rx, d_bo], {'scored shot', 'missed shot'}); +t_s = sprintf('Scatter plot for N=%d and R=%d. prs=%d%%', N, R, round(prs*100)); +title(t_s); + +display(prs); diff --git a/t8.m b/t8.m new file mode 100644 index 0000000..2be3a12 --- /dev/null +++ b/t8.m @@ -0,0 +1,26 @@ + +% init +clear +addpath(genpath('./func/')); + +% declear const +Radius = sqrt(5); +Width = 2; +Length = 4; + +% params input +N = input("Shots Times: "); +R = input("Repeating Times: "); + +% draw background +drawBackGround(Radius, Width, Length); + +% calculate prs +[score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, true, @unifrnd_circle, @uniform_5case); + +% add legend +legend([d_rx, d_bo], {'scored shot', 'missed shot'}); +t_s = sprintf('Scatter plot for N=%d and R=%d. prs=%d%%', N, R, prs*100); +title(t_s); + +display(prs); diff --git a/t9.m b/t9.m new file mode 100644 index 0000000..e92544a --- /dev/null +++ b/t9.m @@ -0,0 +1,26 @@ + +% init +clear +addpath(genpath('./func/')); + +% declear const +Radius = sqrt(5); +Width = 2; +Length = 4; + +% params input +N = input("Shots Times: "); +R = input("Repeating Times: "); + +% draw background +drawBackGround(Radius, Width, Length); + +% calculate prs +[score, totalTimes, prs, d_rx, d_bo] = getDisProb(R, N, Radius, Length, Width, true, @normrnd_circle, @uniform_5case); + +% add legend +legend([d_rx, d_bo], {'scored shot', 'missed shot'}); +t_s = sprintf('Scatter plot for N=%d and R=%d. prs=%d%%', N, R, prs*100); +title(t_s); + +display(prs);