-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
114 lines (101 loc) · 3.13 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
# Bresenham's circle algorithm:
# http://www.daniweb.com/software-development/python/threads/321181/python-bresenham-circle-arc-algorithm#
# Returns set of points of the circle [(x,y),(x,y)...]
def circle(radius):
"Bresenham complete circle algorithm in Python"
# init vars
switch = 3 - (2 * radius)
points = set()
# first quarter/octant starts clockwise at 12 o'clock
x = 0
y = radius
while x <= y:
# first quarter first octant
points.add((x, -y))
# first quarter 2nd octant
points.add((y, -x))
# second quarter 3rd octant
points.add((y, x))
# second quarter 4.octant
points.add((x, y))
# third quarter 5.octant
points.add((-x, y))
# third quarter 6.octant
points.add((-y, x))
# fourth quarter 7.octant
points.add((-y, -x))
# fourth quarter 8.octant
points.add((-x, -y))
if switch < 0:
switch = switch + (4 * x) + 6
else:
switch = switch + (4 * (x - y)) + 10
y = y - 1
x = x + 1
return points
# Return all pixels found inside of a circle
# http://www.blitzbasic.com/Community/post.php?topic=67883&post=758200
def filled_circle(radius):
import math
coords = set()
x = 0
y = 0
for pos_x in range(x - radius, x + radius):
for pos_y in range(y - radius, y + radius):
if math.sqrt((pos_x - x) ** 2 + (pos_y - y) ** 2) <= radius + 1:
coords.add((pos_x, pos_y))
return coords
# Bresenham's line algorithm (calculates the pixels between 2 points):
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
# Code from:
# http://snipplr.com/view.php?codeview&id=22482
# Returns list of coords [(x,y),(x,y)...]
def bresenham_line((x, y), (x2, y2)):
"""Brensenham line algorithm"""
steep = 0
coords = []
dx = abs(x2 - x)
sx = 1 if (x2 - x) > 0 else -1
dy = abs(y2 - y)
sy = 1 if (y2 - y) > 0 else -1
if dy > dx:
steep = 1
x, y = y, x
dx, dy = dy, dx
sx, sy = sy, sx
d = (2 * dy) - dx
for i in range(0, dx):
if steep:
coords.append((y, x))
else:
coords.append((x, y))
while d >= 0:
y = y + sy
d = d - (2 * dx)
x = x + sx
d = d + (2 * dy)
coords.append((x2, y2))
return coords
# http://stackoverflow.com/a/4913653/1145332
# haversine formula for calculating distances between 2 coordinates
def haversine(lng1, lat1, lng2, lat2):
from math import radians, cos, sin, asin, sqrt
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lng1, lat1, lng2, lat2 = map(radians, [lng1, lat1, lng2, lat2])
# haversine formula
dlon = lng2 - lng1
dlat = lat2 - lat1
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
def update_status(percent):
""" Update status bar
"""
sys.stdout.write("\r%3d%%" % percent)
sys.stdout.flush()