【问题标题】:How to count scattered points in Julia如何在 Julia 中计算散点
【发布时间】:2021-06-29 08:48:21
【问题描述】:

我想计算圆圈内散落的红点。
我的代码是:

using PyPlot # Here I define the circle

    k = 100
    ϕ = range(0,stop=2*π,length=k)
    c =  cos.(ϕ)
    d =  sin.(ϕ)

# Here I defined the scattered points with the circle

function scatterpoints(x,y)
    n = 1000
    x = -n:n
    x = x /  n
    y = rand(2*n+1)

    scatter(-x, -y;c="red",s=1)
    scatter(x, y;c="red", s=1)
    plot(c,d)
end

scatterpoints(x,y)

我的方法(伪代码)是这样的:

using LinearAlgebra
if norm < radius of circle then
amount of points in circle = amount of points in circle + 1
end

很遗憾,我不确定如何在 Julia 中实现这一点。

【问题讨论】:

    标签: geometry julia linear-algebra scatter-plot


    【解决方案1】:

    你的伪代码几乎都在这里

    using LinearAlgebra
    
    n = 1000
    N = 2n+1
    
    x = range(-1, 1, length=N)
    y = rand(N)
    
    center = (0,0)
    radius = 1
    
    n_in_circle = 0
    
    for i in 1:N
        if norm((x[i], y[i]) .- center) < radius
            n_in_circle += 1
        end
    end
    
    println(n_in_circle) # 1565
    println(pi*N/4)      # 1571.581724958294
    

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 2022-01-23
      • 2016-09-22
      • 2016-12-16
      • 2023-03-12
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多