This commit is contained in:
2024-06-13 17:27:50 +02:00
parent d76f4aefa9
commit f82d02cd06
7 changed files with 115 additions and 178 deletions

View File

@@ -4,28 +4,52 @@ from typing import List
class Circle:
def __init__(self, center: Point2D, inner: int, outer: int):
def __init__(self, center: Point2D):
self.center = center
self.inner = inner
self.outer = outer
self.radius = None
self.coordinates = []
self.radius = None # Used with circle_points()
self.inner = None
self.outer = None
self.coordinates_thick = []
self.spaced_radius = None
self.spaced_coordinates = []
self.circle(self.center, self.inner, self.outer)
def __repr__(self):
return f"Circle(center: {self.center}, inner: {self.inner}, outer: {self.outer})"
return f"Circle(center: {self.center}, radius: {self.radius}, spaced_radius: {self.spaced_radius}, inner: {self.inner}, outer: {self.outer})"
def circle(self, center: Point2D, inner: int, outer: int) -> List[Point2D]:
def cirlce(self, radius: int) -> List[Point2D]:
self.radius = radius
center = self.center.copy()
x = -radius
y = 0
error = 2-2*radius
while (True):
self.coordinates.append(Point2D(center.x-x, center.y+y))
self.coordinates.append(Point2D(center.x-y, center.y-x))
self.coordinates.append(Point2D(center.x+x, center.y-y))
self.coordinates.append(Point2D(center.x+y, center.y+x))
r = error
if (r <= y):
y += 1
error += y*2+1
if (r > x or error > y):
x += 1
error += x*2+1
if (x < 0):
continue
else:
break
def circle_thick(self, inner: int, outer: int) -> List[Point2D]:
"""Compute discrete value of a 2d-circle with thickness.
https://stackoverflow.com/questions/27755514/circle-with-thickness-drawing-algorithm
From: https://stackoverflow.com/questions/27755514/circle-with-thickness-drawing-algorithm
Args:
center (Type[Point2D]): Center of the circle. Circles always have an odd diameter due to the central coordinate.
inner (int): The minimum radius at which the disc is filled (included).
outer (int): The maximum radius where disc filling stops (included).
@@ -34,8 +58,14 @@ class Circle:
>>> Circle(Point2D(0, 0), 5, 10)
"""
self.inner = inner
self.outer = outer
center = self.center.copy()
xo = outer
xi = inner
y = 0
erro = 1 - xo
erri = 1 - xi
@@ -66,21 +96,23 @@ class Circle:
else:
xi -= 1
erri += 2 * (y - xi + 1)
return self.coordinates
return self.coordinates_thick
def circle_points(self, number: int, radius: int) -> List[Point2D]:
def circle_spaced(self, number: int, radius: int) -> List[Point2D]:
"""Get evenly spaced coordinates of the circle.
https://stackoverflow.com/questions/8487893/generate-all-the-points-on-the-circumference-of-a-circle
From: https://stackoverflow.com/questions/8487893/generate-all-the-points-on-the-circumference-of-a-circle
Args:
number (int): Number of coordinates to be returned.
radius (int, optional): Radius of the circle. Defaults to self.inner.
radius (int): Radius of the circle.
Returns:
list(Point2D): List of evenly spaced 2d-coordinates forming the circle.
"""
print(self.center.x)
self.spaced_radius = radius
center = self.center
self.spaced_coordinates = [
Point2D(cos(2 * pi / number * i) * radius,
sin(2 * pi / number * i) * radius)
@@ -89,17 +121,17 @@ class Circle:
for i in range(len(self.spaced_coordinates)):
self.spaced_coordinates[i] = Point2D(
self.spaced_coordinates[i].x + self.center.x,
self.spaced_coordinates[i].y + self.center.y
self.spaced_coordinates[i].x + center.x,
self.spaced_coordinates[i].y + center.y
).round()
return self.spaced_coordinates
def _x_line(self, x1, x2, y):
while x1 <= x2:
self.coordinates.append(Point2D(x1, y))
self.coordinates_thick.append(Point2D(x1, y))
x1 += 1
def _y_line(self, x, y1, y2):
while y1 <= y2:
self.coordinates.append(Point2D(x, y1))
self.coordinates_thick.append(Point2D(x, y1))
y1 += 1