Adds support for line-drawing with a set of points.
@@ -3,6 +3,8 @@ package com.valkryst.VTerminal.printer;
import com.valkryst.VTerminal.Panel;
import com.valkryst.VTerminal.component.Screen;
+import java.awt.*;
+
public class LinePrinter {
/**
* Prints a line on the screen of a panel.
@@ -28,7 +30,7 @@ public class LinePrinter {
/**
- * Prints a line on a screen using the Bresenham algorithm.
+ * Prints a line on a screen.
*
* @param screen
* The screen.
@@ -46,6 +48,79 @@ public class LinePrinter {
* The y-axis (row) coordinate of the end point of the line.
*/
public void print(final Screen screen, int fromX, int fromY, final int toX, final int toY) {
+ printLine(screen, fromX, fromY, toX, toY);
+ }
+
+ /**
+ * Prints one or more lines on the screen of a panel using a set of points.
+ *
+ * The algorithm requires a minimum of two points.
+ *
+ * The algorithm draws a line from the first point to the second point,
+ * then from the second point to the third point, and so on.
+ *
+ * @param panel
+ * The panel.
+ *
+ * @param points
+ * The points.
+ *
+ * @throws IllegalArgumentException
+ * If there are fewer than two points.
+ */
+ public void print(final Panel panel, final Point[] points) {
+ print(panel.getCurrentScreen(), points);
+ }
+
+ /**
+ * Prints one or more lines on a screen using a set of points.
+ *
+ * The algorithm requires a minimum of two points.
+ *
+ * The algorithm draws a line from the first point to the second point,
+ * then from the second point to the third point, and so on.
+ *
+ * @param screen
+ * The screen.
+ *
+ * @param points
+ * The points.
+ *
+ * @throws IllegalArgumentException
+ * If there are fewer than two points.
+ */
+ public void print(final Screen screen, final Point[] points) {
+ if (points.length < 2) {
+ throw new IllegalArgumentException("A line requires at-least two points to be drawn.");
+ }
+
+ for (int i = 1 ; i < points.length ; i++) {
+ final Point previous = points[i - 1];
+ final Point current = points[i];
+
+ print(screen, previous.x, previous.y, current.x, current.y);
+ }
+ }
+
+ /**
+ * Prints a line on a screen using the Bresenham algorithm.
+ *
+ * @param screen
+ * The screen.
+ *
+ * @param fromX
+ * The x-axis (column) coordinate of the start point of the line.
+ *
+ * @param fromY
+ * The y-axis (row) coordinate of the start point of the line.
+ *
+ * @param toX
+ * The x-axis (column) coordinate of the end point of the line.
+ *
+ * @param toY
+ * The y-axis (row) coordinate of the end point of the line.
+ */
+ private void printLine(final Screen screen, int fromX, int fromY, final int toX, final int toY) {
// delta of exact value and rounded value of the dependant variable
int d = 0;
