Created
October 27, 2020 03:53
-
-
Save define-private-public/f51ddbfc5e5c91dacbc73fa87517af23 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python3 | |
import sys | |
import random | |
import math | |
import matplotlib.pyplot as plt | |
class Vec3: | |
__slots__ = ( | |
'x', | |
'y', | |
'z' | |
) | |
def __init__(self, x, y, z): | |
self.x = x | |
self.y = y | |
self.z = z | |
def length_squared(self): | |
return (self.x * self.x) + \ | |
(self.y * self.y) + \ | |
(self.z * self.z) | |
def main(): | |
if len(sys.argv) < 2: | |
print('Please provide an integer for how many points to sample (e.g. 1000)') | |
sys.exit(0) | |
# seed the random | |
random.seed('asdf') | |
# number of points to sample | |
n = int(sys.argv[1]) | |
points = [] | |
for _ in range(0, n): | |
# Make sure we get a valid vector | |
while True: | |
# cartesian | |
x = random.uniform(-1, 1) | |
y = random.uniform(-1, 1) | |
# polar | |
# r = random.uniform(0, 1) | |
# th = random.uniform(0, 2 * math.pi) | |
# | |
# x = r * math.cos(th) | |
# y = r * math.sin(th) | |
v = Vec3(x, y, 0) | |
if v.length_squared() <= 1: | |
break | |
points.append(v) | |
# Transform it into a pyplot friendly format | |
x = [p.x for p in points] | |
y = [p.y for p in points] | |
plt.scatter(x, y) | |
plt.show() | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cartesian produces this:

This is polar:
