Observen el método intrianglecircleintrianglecircle
de la clase TriangleTriangle
, lo que deseo es que este método reciba como parámetro por defecto 0.05 * self.incircle.radius0.05 * self.incircle.radius
, como está implementado esta funcionando perfectamente pero mi pregunta es: Hay¿Hay otra manera de hacerlo que no sea esa que hice ? siSi es así, cómo¿cómo sería ?
Acá dejo el código...
Para la clase Circle
class Circle:
def __init__(self, center, radius):
self.center = center
self.radius = radius
Para la clase Triangle:
class Triangle:
def __init__(self, vertices):
import numpy as np
# (3, 2) Array of triangle vertices
self.v = np.array(vertices)
@property
def area(self):
import numpy as np
# area triangle
a = abs(np.linalg.det([self.v[1] - self.v[0],
self.v[2] - self.v[0]])) / 2
return a
@property
def lensides(self):
from scipy.spatial import distance as dst
# lengths three sides of triangle
sc = dst.euclidean(self.v[1], self.v[0])
sb = dst.euclidean(self.v[2], self.v[0])
sa = dst.euclidean(self.v[1], self.v[2])
return (sa, sb, sc)
@property
def perimeter(self):
# perimeter triangle
p = sum(self.lensides)
return p
@property
def incircle(self):
s = self.lensides
p = self.perimeter
a = self.area
# center of the inscribed circle
c = (s[0] * self.v[0] + s[1] * self.v[1] +
s[2] * self.v[2]) / p
# radius of the inscribed circle
r = 2 * a / p
return Circle(c, r)
def intrianglecircle(self, radius=None):
import numpy as np
if not radius:
self.radius = 0.05 * self.incircle.radius
else:
self.radius = radius
# random number [0, 1]
r1, r2 = np.random.random(), np.random.random()
# random x-coordinate
px = (1-np.sqrt(r1))*self.v[0][0]+(np.sqrt(r1)*(1-r2)) * \
self.v[1][0]+(np.sqrt(r1)*r2)*self.v[2][0]
# y-coordinate
py = (1-np.sqrt(r1))*self.v[0][1]+(np.sqrt(r1)*(1-r2)) * \
self.v[1][1]+(np.sqrt(r1)*r2)*self.v[2][1]
# random center circle
self.center = np.array([px, py])
return Circle(self.center, self.radius)
Para la clase
Circle
class Circle: def __init__(self, center, radius): self.center = center self.radius = radius
Para la clase
Triangle
class Triangle: def __init__(self, vertices): import numpy as np # (3, 2) Array of triangle vertices self.v = np.array(vertices) @property def area(self): import numpy as np # area triangle a = abs(np.linalg.det([self.v[1] - self.v[0], self.v[2] - self.v[0]])) / 2 return a @property def lensides(self): from scipy.spatial import distance as dst # lengths three sides of triangle sc = dst.euclidean(self.v[1], self.v[0]) sb = dst.euclidean(self.v[2], self.v[0]) sa = dst.euclidean(self.v[1], self.v[2]) return (sa, sb, sc) @property def perimeter(self): # perimeter triangle p = sum(self.lensides) return p @property def incircle(self): s = self.lensides p = self.perimeter a = self.area # center of the inscribed circle c = (s[0] * self.v[0] + s[1] * self.v[1] + s[2] * self.v[2]) / p # radius of the inscribed circle r = 2 * a / p return Circle(c, r) def intrianglecircle(self, radius=None): import numpy as np if not radius: self.radius = 0.05 * self.incircle.radius else: self.radius = radius # random number [0, 1] r1, r2 = np.random.random(), np.random.random() # random x-coordinate px = (1-np.sqrt(r1))*self.v[0][0]+(np.sqrt(r1)*(1-r2)) * \ self.v[1][0]+(np.sqrt(r1)*r2)*self.v[2][0] # y-coordinate py = (1-np.sqrt(r1))*self.v[0][1]+(np.sqrt(r1)*(1-r2)) * \ self.v[1][1]+(np.sqrt(r1)*r2)*self.v[2][1] # random center circle self.center = np.array([px, py]) return Circle(self.center, self.radius)
def intrianglecircle(self, radius= 0.05 * self.incircle.radius):