Skip to content

How to draw a 3D sphere? #438

Answered by KmolYuan
KmolYuan asked this question in Q&A
Discussion options

You must be logged in to vote

Alternative plan: Use spherical mesh.

// sr = the radius of the sphere
// Draw the sphere
{
    let t = (0..=500).map(|t| t as f64 / 500. * TAU);
    let z = t.clone().map(|t| sr * t.cos());
    let y = t.map(|t| sr * t.sin());
    const N: usize = 96;
    for i in 0..N {
        let phi = i as f64 / N as f64 * TAU;
        let x = z.clone().map(|z| z * phi.sin());
        let z = z.clone().map(|z| z * phi.cos());
        let iter = x.zip(y.clone()).zip(z).map(|((x, y), z)| (x, y, z));
        chart.draw_series(LineSeries::new(iter, BLACK.mix(0.1)))?;
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by KmolYuan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant