From 93dfb4744d1c66cbbc433762e37548f2f4b9b483 Mon Sep 17 00:00:00 2001 From: Valkryst Date: Thu, 28 Sep 2017 09:22:08 -0300 Subject: [PATCH] Rewrites most functions to use Point/Dimension objects. --- .../VTerminal/misc/ShapeAlgorithms.java | 114 +++++++++--------- 1 file changed, 54 insertions(+), 60 deletions(-) diff --git a/src/com/valkryst/VTerminal/misc/ShapeAlgorithms.java b/src/com/valkryst/VTerminal/misc/ShapeAlgorithms.java index c9cb2ce785..02df07103c 100644 --- a/src/com/valkryst/VTerminal/misc/ShapeAlgorithms.java +++ b/src/com/valkryst/VTerminal/misc/ShapeAlgorithms.java @@ -1,5 +1,6 @@ package com.valkryst.VTerminal.misc; +import java.awt.Dimension; import java.awt.Point; import java.util.ArrayList; import java.util.List; @@ -12,24 +13,23 @@ private ShapeAlgorithms() {} * Constructs a list, containing the outline, of an ellipse's points by using * the Bresenham algorithm, * - * @param x - * The x-axis (column) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * - * @param y - * The y-axis (row) coordinate of the top-left character. - * - * @param width - * The width. - * - * @param height - * The height. + * @param dimension + * The width/height. * * @return * The list of points. */ - public static List getEllipse(final int x, final int y, final int width, final int height) { + public static List getEllipse(final Point position, final Dimension dimension) { final List points = new ArrayList<>(); + final int x = position.x; + final int y = position.y; + final int width = dimension.width; + final int height = dimension.height; + int a2 = width * width; int b2 = height * height; int fa2 = 4 * a2; @@ -80,28 +80,23 @@ public static List getEllipse(final int x, final int y, final int width, /** * Constructs a list, containing the outline and fill, of an ellipse's points. * - * @param x - * The x-axis (column) coordinate of the top-left character. - * - * @param y - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * - * @param width - * The width. - * - * @param height - * The height. + * @param dimension + * The width/height. * * @return * The list of points. */ - public static List getFilledEllipse(final int x, final int y, final int width, final int height) { - final List points = getEllipse(x, y, width, height); + public static List getFilledEllipse(final Point position, final Dimension dimension) { + final List points = getEllipse(position, dimension); - final int xCenter = x + (width / 2); - final int yCenter = y + (height / 2); + final int xCenter = position.x + (dimension.width / 2); + final int yCenter = position.y + (dimension.height / 2); + position.setLocation(xCenter, yCenter); - return recursiveFill(points, xCenter, yCenter); + return recursiveFill(points, position); } /** @@ -177,24 +172,23 @@ public static List getLine(int fromX, int fromY, final int toX, final int /** * Constructs a list, containing the outline, of a rectangle's points. * - * @param x - * The x-axis (column) coordinate of the top-left character. - * - * @param y - * The y-axis (row) coordinate of the top-left character. - * - * @param width - * The width. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * - * @param height - * The height. + * @param dimension + * The width/height. * * @return * The list of points. */ - public static List getRectangle(final int x, final int y, final int width, final int height) { + public static List getRectangle(final Point position, final Dimension dimension) { final List points = new ArrayList<>(); + final int x = position.x; + final int y = position.y; + final int width = dimension.width; + final int height = dimension.height; + final int lastRow = y + height - 1; final int lastColumn = x + width - 1; @@ -222,26 +216,20 @@ public static List getRectangle(final int x, final int y, final int width /** * Constructs a list containing all of rectangle's points. * - * @param x - * The x-axis (column) coordinate of the top-left character. - * - * @param y - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * - * @param width - * The width. - * - * @param height - * The height. + * @param dimension + * The width/height. * * @return * The list of points. */ - public static List getFilledRectangle(final int x, final int y, final int width, final int height) { + public static List getFilledRectangle(final Point position, final Dimension dimension) { final List points = new ArrayList<>(); - for (int xCounter = x ; xCounter < width + x ; xCounter++) { - for (int yCounter = y ; yCounter < height + y ; yCounter++) { + for (int xCounter = position.x ; xCounter < dimension.width + position.x ; xCounter++) { + for (int yCounter = position.y ; yCounter < dimension.height + position.y ; yCounter++) { points.add(new Point(xCounter, yCounter)); } } @@ -255,17 +243,16 @@ public static List getFilledRectangle(final int x, final int y, final int * @param points * The border points. * - * @param x - * The x-axis (column) coordinate of the current point. - * - * @param y - * The y-axis (row) coordinate of the current point. + * @param position + * The x/y-axis (column/row) coordinates of the current point. * * @return * The list of filled points. */ - public static List recursiveFill(final List points, final int x, final int y) { + public static List recursiveFill(final List points, final Point position) { boolean pointExists = false; + int x = position.x; + int y = position.y; for (final Point point : points) { if (point.x == x && point.y == y) { @@ -277,10 +264,17 @@ public static List recursiveFill(final List points, final int x, f if (pointExists == false) { points.add(new Point(x, y)); - recursiveFill(points, x + 1, y); - recursiveFill(points, x - 1, y); - recursiveFill(points, x, y + 1); - recursiveFill(points, x, y - 1); + position.setLocation(x + 1, y); + recursiveFill(points, position); + + position.setLocation(x - 1, y); + recursiveFill(points, position); + + position.setLocation(x, y + 1); + recursiveFill(points, position); + + position.setLocation(x, y - 1); + recursiveFill(points, position); } return points;