1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Get x and y radius of a hexagon no matter the angle

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 09:42.

  1. Stack

    Stack Membro Participativo

    I'm far from being an expert in mathematics and English so sorry in advance.

    I created a hexagon blur that give this kind of results: [​IMG]

    To make the code a bit cleaner, I created a hexagon class. I'm now implementing different methods inside.

    My constructor look to this for now:

    def __init__(self, radius, center_x, center_y, angle=0):
    self._dmin = np.sqrt(3)/2

    self._radius = radius
    self._radius_min = radius * self.dmin
    self._center_x = center_x
    self._center_y = center_y

    self._angle = angle # Angle in degrees
    self._radian = np.radians(self.angle)

    self._period = 60


    I stuck on the methods radiusX and radiusY. This methods should give in output the distance from the center to the border based on the current hexagon center. For radiusX with a y at 0 and radiusY with a x at 0. [​IMG]

    I tried different implementations but the more accurate one I got until now is:

    def radiusX(self) -> float:
    return self.radius_min + Trigonometry.one2Zero2One(x=self.angle%self.period, period=self.period) * (self.radius - self.radius_min)

    def radiusY(self) -> float:
    return self.radius_min + Trigonometry.zero2One2Zero(x=self.angle%self.period, period=self.period) * (self.radius - self.radius_min)


    The methods one2Zero2One() and zero2One2Zero() are implemented like this:

    @staticmethod
    def one2Zero2One(x, period):
    return 0.5 + 0.5 * np.cos(x * np.pi / (period / 2))

    @staticmethod
    def zero2One2Zero(x, period):
    return 0.5 + 0.5 * np.sin(x * np.pi / (period / 2) - 1.58)


    They allow you to obtain a value on a bell between 0 and 1. zero2One2Zero look to this: [​IMG]

    In my class, a hexagon with an angle of 0 is in this direction: [​IMG]

    I know that my two methods are not good because when I visualise the results with plotly, I directly see that the radius doesn't follow the one2Zero2One and zero2One2Zero curves. In the examples below, above and on the right is have a green point which indicate the end of the x and y radius: [​IMG]

    [​IMG]

    [​IMG]

    [​IMG]

    [​IMG]

    [​IMG]

    [​IMG]

    This graphics are obtained with:

    h = Hexagon(radius=0.5, center_x=0.5, center_y=0.5, angle=j)
    fig = go.Figure()
    inside = {'x':[], 'y':[]}
    outside = {'x':[], 'y':[]}
    radius = {'x':[h.center_x + h.radiusX(), h.center_y], 'y':[h.center_y, h.center_y + h.radiusY()]}
    print(h.radiusX())
    for i in range(int(10e3)):
    # print(i,'/',int(10e3))
    x, y = random(), random()
    if h.isPointInside(x, y):
    inside['x'].append(x)
    inside['y'].append(y)
    else:
    outside['x'].append(x)
    outside['y'].append(y)

    fig.add_trace(go.Scatter(x=inside['x'], y=inside['y'], mode='markers', name='Inside'))
    fig.add_trace(go.Scatter(x=outside['x'], y=outside['y'], mode='markers', name='Outside'))
    fig.add_trace(go.Scatter(x=radius['x'], y=radius['y'], mode='markers', name='radius', marker={'size':10}))
    fig.show()


    I would like to get help to find the corrects equation for get the radiusX and radiusY based on the current hexagon angle.

    Continue reading...

Compartilhe esta Página