Skip to content

Commit

Permalink
fixed line rendering in the wrong place...
Browse files Browse the repository at this point in the history
  • Loading branch information
PycraftDeveloper committed Oct 23, 2024
1 parent 8e95ffa commit 7ee6fb5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 97 deletions.
16 changes: 8 additions & 8 deletions experiments/drawing test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

events = pmma.Events()

"""

line = pmma.Line()
line.set_start((300, 300))
line.set_end((500, 300))
line.set_color([255, 0, 0])
line.set_start((10, 10))
line.set_end((200, 200))
line.set_color([255, 0, 255])
line.set_width(1)
"""
pixel = pmma.Pixel()
pixel.set_position((100, 100))
pixel.set_color([255, 255, 255])
Expand Down Expand Up @@ -70,16 +70,16 @@

display.clear([0, 0, 0])

radial_polygon.render()
#line.render()
#radial_polygon.render()
line.render()
#rectangle.render()
#pixel.render()
#arc.render()
#ellipse.render()
#polygon.render()
#radial_polygon.set_rotation((time.perf_counter()-s)*60)

#line.set_rotation((time.perf_counter()-s)*5)
line.set_rotation((time.perf_counter()-s)*5)
#line.set_end((500, 60*(time.perf_counter()-start)))
#rectangle.set_rotation((time.perf_counter()-s)*10)

Expand Down
19 changes: 12 additions & 7 deletions python_src/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,23 @@ def get_surface(self):

def _rotate_point_around_center(self, point, center, angle):
"""
Rotates a 2D point around a given center by the given angle in radians.
Rotates a 2D point around a given center by the given angle in radians, accounting for aspect ratio.
:param point: A list or tuple representing the x and y coordinates (x, y)
:param center: The center of rotation as (cx, cy)
:param angle: The angle to rotate by, in radians.
:return: The rotated point as a [x', y'] list.
"""
# Get the aspect ratio (width/height)
aspect_ratio = self._surface.get_width() / self._surface.get_height()

# Scale the point and center coordinates to account for aspect ratio
scaled_point = [point[0] * aspect_ratio, point[1]]
scaled_center = [center[0] * aspect_ratio, center[1]]

# Translate the point to the origin (relative to the center)
translated_x = point[0] - center[0]
translated_y = point[1] - center[1]
translated_x = scaled_point[0] - scaled_center[0]
translated_y = scaled_point[1] - scaled_center[1]

# Apply 2D rotation matrix
cos_angle = _numpy.cos(angle)
Expand All @@ -156,7 +163,8 @@ def _rotate_point_around_center(self, point, center, angle):
y_prime = sin_angle * translated_x + cos_angle * translated_y

# Translate the point back to its original position (relative to the center)
return [x_prime + center[0], y_prime + center[1]]
# Scale back the x-coordinate to remove aspect ratio effect
return [x_prime / aspect_ratio + center[0], y_prime + center[1]]

def _rotate_line(self, angle):
"""
Expand All @@ -179,9 +187,6 @@ def _rotate_line(self, angle):

def _update_buffers(self):
if self._vertices_changed:
if _Constants.DISPLAY_OBJECT in _Registry.pmma_module_spine:
self._program.set_shader_variable('aspect_ratio', _Registry.pmma_module_spine[_Constants.DISPLAY_OBJECT].get_aspect_ratio())

_Registry.number_of_render_updates += 1
rotated_line_points = self._rotate_line(self._rotation.get_angle(format=_Constants.RADIANS))

Expand Down
Loading

0 comments on commit 7ee6fb5

Please sign in to comment.