Skip to content

Commit

Permalink
#11: added sorting rectangles by vector module.
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh3 committed Mar 14, 2019
1 parent c6ae80a commit f919db6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/ua/comparison/image/ImageComparison.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import ua.comparison.image.model.Rectangle;

public class ImageComparison {
Expand Down Expand Up @@ -114,8 +116,9 @@ public BufferedImage compareImages() throws IOException {

//todo implement logic for overlapping.
private List<Rectangle> avoidOverlapping(List<Rectangle> rectangles) {

return rectangles;
rectangles.sort(Comparator.comparing(Rectangle::calculateVectorModule));
//todo removed this hotfix and investigate it in #32 issue.
return rectangles.stream().filter(it -> !it.equals(new Rectangle())).collect(Collectors.toList());
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/ua/comparison/image/model/Rectangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public class Rectangle {
private int maxX = Integer.MIN_VALUE;
private int maxY = Integer.MIN_VALUE;

/**
* Modul of the vector from the start of the matrix to point of the
* beginning {@link Rectangle} object.
*/
public int calculateVectorModule() {
return minX*minX + minY*minY;
}

public void setMinX(int minX) {
this.minX = minX;
}
Expand Down Expand Up @@ -39,4 +47,36 @@ public int getMinX() {
public int getWidth() { return maxY - minY; }

public int getHeight() { return maxX - minX; }

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Rectangle rectangle = (Rectangle) o;

if (minX != rectangle.minX) {
return false;
}
if (minY != rectangle.minY) {
return false;
}
if (maxX != rectangle.maxX) {
return false;
}
return maxY == rectangle.maxY;
}

@Override
public int hashCode() {
int result = minX;
result = 31 * result + minY;
result = 31 * result + maxX;
result = 31 * result + maxY;
return result;
}
}

0 comments on commit f919db6

Please sign in to comment.