Skip to content

Commit 69ec0d6

Browse files
committed
Add ability to graph area under plot
1 parent 666a4b5 commit 69ec0d6

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

displayio_cartesian.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,47 @@ def add_plot_line(self, x: int, y: int) -> None:
635635
self.plot_line_point[-1][1],
636636
1,
637637
)
638+
# Plot area under graph
639+
if self._fill_area:
640+
641+
delta_x = self.plot_line_point[-2][0] - self.plot_line_point[-1][0]
642+
delta_y = self.plot_line_point[-2][1] - self.plot_line_point[-1][1]
643+
delta_y_product = self.plot_line_point[-1][1] * self.plot_line_point[-2][1]
644+
645+
if delta_x == 0:
646+
return
647+
648+
slope = delta_y/delta_x
649+
above_x_axis = False if slope > 0 else True
650+
651+
if delta_y_product < 0:
652+
# TODO: Area needs to be split into two triangles
653+
654+
c = self.plot_line_point[-1][1]
655+
zero_point_x = (-1 * c)/slope
656+
657+
self._draw_area_under(self.plot_line_point[-2], (zero_point_x, 0))
658+
self._draw_area_under((zero_point_x, 0), self.plot_line_point[-1])
659+
660+
else:
661+
662+
point_to_check = self.plot_line_point[-1] if self.plot_line_point[-1][1] != 0 else self.plot_line_point[-2]
663+
above_x_axis = point_to_check[1] > 0
664+
665+
self._draw_area_under(self.plot_line_point[-2], self.plot_line_point[-1])
666+
667+
def _draw_area_under(self, xy0: Tuple[float, float], xy1: Tuple[float, float]) -> None:
668+
669+
_, plot_y_zero = self._calc_local_xy(0, 0)
670+
671+
delta_x = self.plot_line_point[-2][0] - self.plot_line_point[-1][0]
672+
delta_y = self.plot_line_point[-2][1] - self.plot_line_point[-1][1]
673+
slope = delta_y/delta_x
674+
675+
for pixel_x in range(xy0[0], xy1[0]+1):
676+
if pixel_x != xy1[0]:
677+
pixel_y = round(slope*(pixel_x-xy1[0]) + xy1[1])
678+
bitmaptools.draw_line(self._plot_bitmap, pixel_x, pixel_y, pixel_x, plot_y_zero, 1)
638679

639680
def clear_plot_lines(self, palette_index=5):
640681
"""clear_plot_lines function.

0 commit comments

Comments
 (0)