Skip to content

Instantly share code, notes, and snippets.

@darktrojan
Created April 12, 2018 22:56
Show Gist options
  • Save darktrojan/69fc928d29c4da4bbd459614074064fb to your computer and use it in GitHub Desktop.
Save darktrojan/69fc928d29c4da4bbd459614074064fb to your computer and use it in GitHub Desktop.
Cartesian to polar coordinates converter for photo spheres
from math import atan, hypot, degrees
paths = [
[
[100, 0],
[0, 100],
[-100, 0],
[0, -100],
]
]
def interpolate_points(points):
def interpolate():
i = 0.0
while i < 0.991:
j = 1.0 - i
more_points.append([x1 * j + x2 * i, y1 * j + y2 * i])
i += 0.01
more_points = []
[x1, y1] = points[0]
for i in range(1, len(points)):
[x2, y2] = points[i]
interpolate()
x1 = x2
y1 = y2
[x2, y2] = points[0]
interpolate()
return more_points
def transform_xy(xy):
[x, y] = xy
if x == 0:
longitude = 270 if y < 0 else 90
else:
longitude = degrees(atan(y / x))
if x < 0:
longitude += 180
elif y < 0:
longitude += 360
radius = hypot(x, y)
latitude = 180 - degrees(atan(radius / 100))
return '%f,%f' % (longitude, latitude)
print '<svg xmlns="http://www.w3.org/2000/svg" width="360" height="180">'
for points in paths:
points = interpolate_points(points)
print '<path fill="#ff0" d="M ' + ' '.join(map(transform_xy, points)) + ' Z" />'
print '</svg>'
@darktrojan
Copy link
Author

This script converts the diamond shape specified in paths to what appears as a 2D square at the bottom of a photo sphere (think of it as the bottom face of a cube inside the sphere). You can also flip the results to create the top.

Change paths to one or more shapes of your choice. If one of your paths crosses the positive X axis, you'll need to move some of its points 360px to one side in the resulting SVG file, and make a copy 360px to the other side. I could fix this in the code, but I don't need it.

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