File:Barnes-Hut tree construction.png

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Barnes-Hut_tree_construction.png(300 × 300 pixels, file size: 9 KB, MIME type: image/png, looped, 6 frames, 6.0 s)

Note: Due to technical limitations, thumbnails of this file will not be animated.

Captions

Captions

An animation of Barnes-Hut tree construction

Summary

[edit]
Description
English: An animation of Barnes-Hut tree construction for 48 particles distributed in a 2D box uniformly.
Date
Source Own work
Author Osanshouo

Source code

[edit]

This animation was created with the following Python source code.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

def cube_plot(ax, pos, level, idx):
    d = 1./2**level
    idx = [ idx[0]*d, idx[1]*d ]
    count = np.sum((pos[:,0] - idx[0] >= 0)*(pos[:,0] - idx[0] < d)*(pos[:,1] - idx[1] >= 0)*(pos[:,1] - idx[1] < d))
    if count == 0:
        return count
    
    color = cm.nipy_spectral(level/7.)
    ax.plot([idx[0]  , idx[0]+d], [idx[1]  , idx[1]  ], lw=1, color=color, alpha=1)
    ax.plot([idx[0]  , idx[0]  ], [idx[1]  , idx[1]+d], lw=1, color=color, alpha=1)
    ax.plot([idx[0]+d, idx[0]+d], [idx[1]  , idx[1]+d], lw=1, color=color, alpha=1)
    ax.plot([idx[0]  , idx[0]+d], [idx[1]+d, idx[1]+d], lw=1, color=color, alpha=1)
    return count

def plot_rec(ax, pos, Lmax, level, idx):
    count = cube_plot(ax, pos, level, idx)
    if level +1 == Lmax:
        return
    if count > 1:
        plot_rec(ax, pos, Lmax, level+1, [idx[0]*2, idx[1]*2])
        plot_rec(ax, pos, Lmax, level+1, [idx[0]*2+1, idx[1]*2])
        plot_rec(ax, pos, Lmax, level+1, [idx[0]*2, idx[1]*2+1])
        plot_rec(ax, pos, Lmax, level+1, [idx[0]*2+1, idx[1]*2+1])

n = 48
np.random.seed(123)

pos = np.random.rand(n, 2)

for Lmax in range(1, 7):
    fig = plt.figure( figsize=(3, 3) )
    plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
    ax = fig.add_subplot(111, aspect=1)
    ax.set_xlim([0, 1])
    ax.set_ylim([0, 1])
    plt.tick_params(axis='x',which='both',bottom=False,top=False,labelbottom=False)
    plt.tick_params(axis='y',which='both',left=False,right=False,labelleft=False)

    ax.scatter(pos[:,0], pos[:,1], c="indianred", s=5)
    plot_rec(ax, pos, Lmax, 0, [0,0])

    plt.savefig("bh{}.png".format(Lmax))
    plt.close()

Licensing

[edit]
I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution
This file is licensed under the Creative Commons Attribution 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current13:21, 25 May 2020Thumbnail for version as of 13:21, 25 May 2020300 × 300 (9 KB)Osanshouo (talk | contribs)shrink to 300px
06:18, 24 May 2020Thumbnail for version as of 06:18, 24 May 2020800 × 800 (23 KB)Osanshouo (talk | contribs)Uploaded own work with UploadWizard

There are no pages that use this file.

File usage on other wikis

The following other wikis use this file:

Metadata