Created
April 4, 2023 01:21
-
-
Save Atrix256/8a512cedce1dfda039e4513f4910b1db to your computer and use it in GitHub Desktop.
Python code to test cosine weighted hemispherical 2d vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import math | |
import random | |
numPoints = 10000 | |
transparency = 0.01 | |
def Normalize(x): | |
len = math.sqrt(x[0] * x[0] + x[1] * x[1]) | |
return [x[0] / len, x[1] / len] | |
def PointOnCircle(): | |
angle = math.pi * 2.0 * random.random() | |
radius = 1.0 | |
return [math.cos(angle) * radius, math.sin(angle) * radius] | |
def PointInCircle(): | |
angle = math.pi * 2.0 * random.random() | |
radius = math.sqrt(random.random()) | |
return [math.cos(angle) * radius, math.sin(angle) * radius] | |
fig, ax = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True) | |
ax[0].set_title("In Circle") | |
ax[1].set_title("On Circle") | |
for i in range(2): | |
ax[i].set_xlim(-1, 1) | |
ax[i].set_ylim(-0.5, 1.5) | |
ax[i].plot([-2, 2], [0, 0], 'g-') | |
for i in range(numPoints): | |
p = PointOnCircle() | |
p[1] = p[1] + 1.0 | |
p = Normalize(p) | |
ax[0].plot([0, p[0]], [0, p[1]], 'b-', alpha=transparency) | |
p = PointInCircle() | |
p[1] = p[1] + 1.0 | |
p = Normalize(p) | |
ax[1].plot([0, p[0]], [0, p[1]], 'b-', alpha=transparency) | |
fig.savefig("out.png", bbox_inches='tight') |
Author
Atrix256
commented
Apr 4, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment