Skip to content

Instantly share code, notes, and snippets.

@define-private-public
Created October 27, 2020 03:53
Show Gist options
  • Save define-private-public/f51ddbfc5e5c91dacbc73fa87517af23 to your computer and use it in GitHub Desktop.
Save define-private-public/f51ddbfc5e5c91dacbc73fa87517af23 to your computer and use it in GitHub Desktop.
#!/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()
@define-private-public
Copy link
Author

Cartesian produces this:
dist_cartesian

This is polar:
dist_polar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment