Added Line Iterator.

This commit is contained in:
Alexei KADIR 2024-04-04 22:48:53 +02:00
parent e33112dcb0
commit c21bcf2a80

View File

@ -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<Cell> {
/**
* 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;
}
}