Skip to content

Commit

Permalink
Added example operations to quickstart.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatoom committed Mar 21, 2021
1 parent a59835b commit fa58b9e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Visualizer(voronoi, canvas_offset=1)\
### Calculate the shell size for each point
```python
for point in v.sites:
print(f"{(point.xd, point.yd)} \t {point.cell_size()}")
print(f"{(point.xd, point.yd)} \t {point.area()}")
```
Output:
```
Expand Down
42 changes: 30 additions & 12 deletions examples/quickstart.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from voronoi import Voronoi, Polygon, Visualizer
from typing import List
from voronoi import Voronoi, Polygon, Visualizer, Point
from voronoi.graph import HalfEdge, Vertex

# Define some points (a.k.a sites or cell points)
points = [
Expand All @@ -24,14 +26,30 @@
.show()

# Some examples of how to access properties from the Voronoi diagram:
edges = v.edges # A list of all edges
vertices = v.vertices # A list of all vertices
sites = v.sites # A list of all cell points (a.k.a. sites)
v.sites[0].cell_size() # Calculate cell size for a cell point
v.sites[0].get_coordinates() # Get the coordinates of the borders around a cell point
v.sites[0].get_borders() # Get the borders around the cell point
v.sites[0].get_vertices() # Get the vertices of the borders
v.vertices[0].connected_edges() # Get all the edges that are connected to this vertex
position = v.vertices[0].position # Get the position of this vertex
origin = v.edges[0].origin # Get the vertex that the edge originates in
target = v.edges[0].target # Get the vertex that the edge points to
edges: List[HalfEdge] = v.edges # A list of all edges
vertices: List[Vertex] = v.vertices # A list of all vertices
sites: List[Point] = v.sites # A list of all cell points (a.k.a. sites)

edge, vertex, site = edges[0], vertices[0], sites[0]

# Edge operations
origin: Vertex = edge.origin # The vertex in which the edge originates
target: Vertex = edge.twin.origin # The twin is the edge that goes in the other direction
target_alt: Vertex = edge.target # Same as above, but more convenient
twin: HalfEdge = edge.twin # Get the twin of this edge
next: HalfEdge = edge.next # Get the next edge
prev: HalfEdge = edge.twin.next # Get the previous edge
prev_alt: HalfEdge = edge.prev # Same as above, but more convenient

# Site operations
size: float = site.area() # The area of the cell
borders: List[HalfEdge] = site.borders() # A list of all the borders that surround this cell point
vertices: List[Vertex] = site.vertices() # A list of all the vertices around this cell point
site_x: float = site.x # X-coordinate of the site
site_xy: [float, float] = site.xy # (x, y)-coordinates of the site

# Vertex operations
connected_edges: List[HalfEdge] = vertex.connected_edges # A list of all edges that are connected to this vertex
vertex_x: float = vertex.x # x-coordinate of the vertex
vertex_xy: [float, float] = vertex.xy # (x, y)-coordinates of the vertex
print()
4 changes: 2 additions & 2 deletions examples/random_bounding_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
if print_input:
print("points = [")
for point in points:
print(f" Point({point.x}, {point.y}),")
print(f" Point({point.xd}, {point.yd}),")
print("]")

points = [
Expand Down Expand Up @@ -66,7 +66,7 @@

# Start the procedure
v.create_diagram(
points=[(p.x, p.y) for p in points],
points=[(p.xd, p.yd) for p in points],
)


Expand Down
28 changes: 14 additions & 14 deletions voronoi/contrib/bounding_circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from voronoi import Polygon
from voronoi.algorithm import Algorithm
from voronoi.graph import Point, DecimalCoordinate, Vertex
from voronoi.graph import Point, Coordinate, Vertex

DEBUG = True

Expand All @@ -18,7 +18,7 @@ def __init__(self, x, y, radius):
self.y = Decimal(str(y))
self.radius = Decimal(str(radius))
self.polygon_vertices = []
self.center = DecimalCoordinate(self.x, self.y)
self.center = Coordinate(self.x, self.y)
self.max_x = self.x + 2 * self.radius
self.min_x = self.x - 2 * self.radius
self.max_y = self.y + 2 * self.radius
Expand All @@ -37,7 +37,7 @@ def __init__(self, x, y, radius):
""")

def inside(self, point):
return (self.x - point.x) ** 2 + (self.y - point.y) ** 2 < self.radius ** 2
return (self.x - point.xd) ** 2 + (self.y - point.yd) ** 2 < self.radius ** 2

def finish_edges(self, edges, vertices=None, points=None, event_queue=None):
resulting_edges = []
Expand Down Expand Up @@ -115,25 +115,25 @@ def trim_edge(self, edge, twisted=False):
return True

def get_line(self, A, B):
if (B.y - A.y) == 0:
if (B.yd - A.yd) == 0:
a = Decimal("0")
b = Decimal("1")
c = A.y
elif (B.x - A.x) == 0:
c = A.yd
elif (B.xd - A.xd) == 0:
a = Decimal("1")
b = Decimal("0")
c = A.x
c = A.xd
else:
a = -(B.y - A.y) / (B.x - A.x)
a = -(B.yd - A.yd) / (B.xd - A.xd)
b = Decimal("1")
c = A.x * a + A.y
c = A.xd * a + A.yd

return a, b, c

def get_ray(self, edge):
A = edge.origin.breakpoint[0]
B = edge.origin.breakpoint[1]
center = Point(x=(A.x + B.x) / 2, y=(A.y + B.y) / 2)
center = Point(x=(A.xd + B.xd) / 2, y=(A.yd + B.yd) / 2)

if edge.twin.get_origin() is None:
ray_start = center
Expand All @@ -153,8 +153,8 @@ def on_line(self, A, B, C):
def is_on(a, b, c):
"Return true iff point c intersects the line segment from a to b."
# (or the degenerate case that all 3 points are coincident)
return ((within(a.x, c.x, b.x) if a.x != b.x else
within(a.y, c.y, b.y)))
return ((within(a.xd, c.xd, b.xd) if a.xd != b.xd else
within(a.yd, c.yd, b.yd)))

def within(p, q, r):
"Return true iff q is between p and r (inclusive)."
Expand Down Expand Up @@ -185,8 +185,8 @@ def cut_line(self, edge):
elif len(points) == 1:
return points[0]
else:
dist_0 = (A.x - points[0].x) ** 2 + (A.y - points[0].y) ** 2
dist_1 = (A.x - points[1].x) ** 2 + (A.y - points[1].y) ** 2
dist_0 = (A.xd - points[0].xd) ** 2 + (A.yd - points[0].yd) ** 2
dist_1 = (A.xd - points[1].xd) ** 2 + (A.yd - points[1].yd) ** 2
if dist_0 < dist_1:
return points[0]
else:
Expand Down
27 changes: 6 additions & 21 deletions voronoi/graph/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __repr__(self):
return f"P{self.name}"
return f"Point({self.xd:.2f}, {self.xd:.2f})"

def cell_size(self, digits=None):
def area(self, digits=None):
"""
Calculate cell size if the point is a site.
:param digits: (int) number of digits to round to
Expand All @@ -40,9 +40,9 @@ def cell_size(self, digits=None):
if digits is not None:
return round(self._shoelace(x, y), digits)

return self._shoelace(x, y)
return float(self._shoelace(x, y))

def get_borders(self):
def borders(self):
if self.first_edge is None:
return None
edge = self.first_edge
Expand All @@ -54,29 +54,14 @@ def get_borders(self):
edges.append(edge)
return edges

def get_vertices(self):
borders = self.get_borders()
def vertices(self):
borders = self.borders()
if borders is None:
return None
return [border.origin for border in borders if isinstance(border.origin, Vertex)]

# def get_coordinates(self):
# borders = self.get_borders()
# if borders is None:
# return None
# coordinates = []
# for border in borders:
#
# # During construction, not all origins are vertices yet.
# origin = border.get_origin()
# if origin is None:
# return None
#
# coordinates.append(origin)
# return coordinates

def _get_xy(self):
coordinates = self.get_vertices()
coordinates = self.vertices()
if coordinates is None:
return [], []
x = [coordinate.x for coordinate in coordinates]
Expand Down
2 changes: 1 addition & 1 deletion voronoi/tests/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _triangle(x, y):
def _execute(polygon, points, sizes):
v = Algorithm(polygon)
v.create_diagram(points=points)
calculated = [p.cell_size(2) for p in v.sites]
calculated = [p.area(2) for p in v.sites]
assert sizes == calculated, f"\nResult=\n{calculated}\n\n Expected=\n{sizes}"


Expand Down
2 changes: 1 addition & 1 deletion voronoi/visualization/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def plot_sites(self, points=None, show_labels=True, color=Colors.CELL_POINTS, zo
# Add descriptions
if show_labels:
for point in points:
self.canvas.text(point.xd, point.yd, s=f"{point} (A={point.cell_size(digits=2)})", zorder=15)
self.canvas.text(point.xd, point.yd, s=f"{point} (A={point.area(digits=2)})", zorder=15)

return self

Expand Down

0 comments on commit fa58b9e

Please sign in to comment.