From c21bcf2a8057548a27a4cebcda74fe49bcd126d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Thu, 4 Apr 2024 22:48:53 +0200 Subject: [PATCH] Added Line Iterator. --- .../java/fr/iutfbleau/sae/LineIterator.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/src/main/java/fr/iutfbleau/sae/LineIterator.java diff --git a/app/src/main/java/fr/iutfbleau/sae/LineIterator.java b/app/src/main/java/fr/iutfbleau/sae/LineIterator.java new file mode 100644 index 0000000..237373b --- /dev/null +++ b/app/src/main/java/fr/iutfbleau/sae/LineIterator.java @@ -0,0 +1,79 @@ +package fr.iutfbleau.sae; + +import android.graphics.Point; + +import java.util.Iterator; + +/** + * {@code LineIterator} is an iterator that iterates over the {@code Cell}s of a + * {@code Line}. + * + * @autor Alexeï Kadir, Lyanis Souidi, Hugo Dimitrijevic + * @version 1.0 + */ +public class LineIterator implements Iterator { + /** + * The {@code Line} to iterate over. + */ + private final Line line; + + /** + * The difference between the x-coordinates of the start and end {@code Cell}s + * of the {@code Line}. + */ + private final int dx; + + /** + * The difference between the y-coordinates of the start and end {@code Cell}s + * of the {@code Line}. + */ + private final int dy; + + /** + * The index of the next {@code Cell} to return. + */ + private int index; + + /** + * Constructs a new {@code LineIterator} that iterates over the specified + * {@code Line}. + * + * @param line The {@code Line} to iterate over. + */ + public LineIterator(Line line) { + this.line = line; + + this.dx = this.line.getEnd().x - this.line.getStart().x; + this.dy = this.line.getEnd().y - this.line.getStart().y; + + this.index = 0; + } + + /** + * Checks if there is a next {@code Cell} to return. + * + * @return {@code true} if there is a next {@code Cell} to return, {@code false} + * otherwise. + */ + @Override + public boolean hasNext() { + return this.index < 5; + } + + /** + * Returns the next {@code Cell} of the {@code Line}. + * + * @return The next {@code Cell} of the {@code Line}. + */ + @Override + public Cell next() { + Cell cell = this.line.getStart(); + + cell.x += Integer.signum(this.dx) * this.index; + cell.y += Integer.signum(this.dy) * this.index; + + this.index++; + + return cell; + } +}