From b8e375e11866bb2b80c35ba2d593f65c5e20f788 Mon Sep 17 00:00:00 2001 From: ruffdd Date: Thu, 22 Oct 2020 17:28:54 +0200 Subject: [PATCH 1/8] improve Line shape the Line shape can now go in all directions --- .../fius/jvk/provided/shapes/Line.java | 67 ++++++++----------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java index 1a42b7a3..2c0521e8 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java @@ -3,63 +3,52 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; - +import java.lang.Math; import de.unistuttgart.informatik.fius.icge.simulation.Position; + /** - * A finite length line shape that is always parallel to the coordinate axes. + * A finite length line shape. */ public class Line implements Shape { - + private List points; - + /** * Create a new line by specifying start and end position of the line. * - * @param start the start of the line - * @param end the end of the line; may not differ from the line start in both x and y direction + * @param start + * the start of the line + * @param end + * the end of the line; may not differ from the line start in both x and y direction */ public Line(Position start, Position end) { - Integer deltaX = start.getX() - end.getX(); - Integer deltaY = start.getY() - end.getY(); - - if (deltaX != 0 && deltaY != 0) { - throw new IllegalArgumentException("Only lines parallel to the grid are supported."); - } - + Integer deltaX = end.getX() - start.getX(); + Integer deltaY = end.getY() - start.getY(); + Float slope; //slope needs to have digits after the . ,so Float is used + List points = new ArrayList<>(); - points.add(start); - - Position currentPos = start; - while (deltaX != 0 || deltaY != 0) { - if (deltaX > 0) { - currentPos = new Position(currentPos.getX() - 1, currentPos.getY()); - points.add(currentPos); - deltaX -= 1; - } - if (deltaX < 0) { - currentPos = new Position(currentPos.getX() + 1, currentPos.getY()); - points.add(currentPos); - deltaX += 1; + + if (Math.abs(deltaX) > Math.abs(deltaY)) { + slope = deltaY.floatValue() / deltaX.floatValue(); + for (Integer x = start.getX(); (deltaX > 0) ? x <= end.getX() : x >= end.getX(); x += ((deltaX > 0 ? 1 : -1))) { + Integer y = Math.round(x.floatValue() * slope + start.getX()); + points.add(new Position(x, y)); } - if (deltaY > 0) { - currentPos = new Position(currentPos.getX(), currentPos.getY() - 1); - points.add(currentPos); - deltaY -= 1; - } - if (deltaY < 0) { - currentPos = new Position(currentPos.getX(), currentPos.getY() + 1); - points.add(currentPos); - deltaY += 1; + } + else{ + slope = deltaX.floatValue() / deltaY.floatValue(); + for(Integer y = start.getY(); (deltaY > 0) ? y <= end.getY() : y >= end.getY(); y += ((deltaY > 0 ? 1 : -1))){ + Integer x = Math.round(y.floatValue() * slope + start.getY()); + points.add(new Position(x, y)); } } - this.points = points; } - + @Override public Iterator iterator() { return this.points.iterator(); } - -} \ No newline at end of file + +} From b762944f01794ee17054b10e613ddd6a28e0a3f5 Mon Sep 17 00:00:00 2001 From: ruffdd Date: Fri, 23 Oct 2020 17:45:23 +0200 Subject: [PATCH 2/8] add circle shape --- .../fius/jvk/provided/shapes/Circle.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java new file mode 100644 index 00000000..24385978 --- /dev/null +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java @@ -0,0 +1,54 @@ +package de.unistuttgart.informatik.fius.jvk.provided.shapes; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.lang.Math; +import de.unistuttgart.informatik.fius.icge.simulation.Position; + + +/** + * A finite length line shape. + */ +public class Circle implements Shape { + + private List points; + + /** + * Create a new line by specifying start and end position of the line. + * + * @param start + * the start of the line + * @param end + * the end of the line; may not differ from the line start in both x and y direction + */ + public Circle(Position middle, Integer radius) { + + + List points = new ArrayList<>(); + List pointsAdd = new ArrayList<>(); + for(Integer x = 0; x < radius;x++){ + Double y = Math.sin(Math.acos(x.doubleValue()/radius))*radius; + points.add(new Position(x,(int)Math.round(y))); + } + for(Integer y = 0; y < radius;y++){ + Double x = Math.cos(Math.asin(y.doubleValue()/radius))*radius; + Position position = new Position((int)Math.round(x),y); + if(!points.contains(position)){ + points.add(position); + } + } + points.forEach(p -> pointsAdd.add(new Position(p.getX(), -1*p.getY()))); + pointsAdd.forEach(p -> {if(!points.contains(p))points.add(p);} ); + pointsAdd.clear(); + points.forEach(p -> pointsAdd.add(new Position(-1*p.getX(), p.getY()))); + pointsAdd.forEach(p -> {if(!points.contains(p))points.add(p);} ); + this.points=points; + } + + @Override + public Iterator iterator() { + return this.points.iterator(); + } + +} From bc5894ad932d11d36cda3f4c9b5dbd6f8feb0e49 Mon Sep 17 00:00:00 2001 From: ruffdd Date: Fri, 23 Oct 2020 18:00:06 +0200 Subject: [PATCH 3/8] fix circle positioning --- .../informatik/fius/jvk/provided/shapes/Circle.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java index 24385978..f7f7f890 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java @@ -23,7 +23,8 @@ public class Circle implements Shape { * the end of the line; may not differ from the line start in both x and y direction */ public Circle(Position middle, Integer radius) { - + Integer deltaX = middle.getX(); + Integer deltaY = middle.getY(); List points = new ArrayList<>(); List pointsAdd = new ArrayList<>(); @@ -43,7 +44,9 @@ public Circle(Position middle, Integer radius) { pointsAdd.clear(); points.forEach(p -> pointsAdd.add(new Position(-1*p.getX(), p.getY()))); pointsAdd.forEach(p -> {if(!points.contains(p))points.add(p);} ); - this.points=points; + pointsAdd.clear(); + points.forEach(p-> pointsAdd.add(new Position(p.getX()+middle.getX(), p.getY()+middle.getY()))); + this.points=pointsAdd; } @Override From c804cfdd926f15b3292ee98fe964708bf33e5eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20B=C3=BChler?= <17296905+buehlefs@users.noreply.github.com> Date: Fri, 23 Oct 2020 20:57:47 +0200 Subject: [PATCH 4/8] Add FilledShape that flood fills existing shapes --- .../fius/jvk/provided/shapes/FilledShape.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/FilledShape.java diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/FilledShape.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/FilledShape.java new file mode 100644 index 00000000..a5bcf970 --- /dev/null +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/FilledShape.java @@ -0,0 +1,118 @@ +package de.unistuttgart.informatik.fius.jvk.provided.shapes; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.Stack; + +import de.unistuttgart.informatik.fius.icge.simulation.Position; + +/** + * A shape created by filling out an existing shape. + */ +public class FilledShape implements Shape { + + private Set points; + + /** + * Create a new shape by filling out the given shape. + * + * @param shape the input shape + */ + public FilledShape(Shape shape) { + Set points = new HashSet<>(); + + points.addAll(shape); + + if (points.isEmpty()) { + this.points = points; + return; + } + + // calc bounding box... + Integer x1 = Integer.MAX_VALUE; + Integer x2 = Integer.MIN_VALUE; + Integer y1 = Integer.MAX_VALUE; + Integer y2 = Integer.MIN_VALUE; + for (Position pos : points) { + if (pos.getX() < x1) { + x1 = pos.getX(); + } + if (pos.getX() > x2) { + x2 = pos.getX(); + } + if (pos.getY() < y1) { + y1 = pos.getY(); + } + if (pos.getY() > y2) { + y2 = pos.getY(); + } + } + + Set outside = new HashSet<>(); + for (Integer y = y1; y <= y2; y++) { + outside.add(new Position(x1-1, y)); + outside.add(new Position(x2+1, y)); + } + for (Integer x = x1; x <= x2; x++) { + outside.add(new Position(x, y1-1)); + outside.add(new Position(x, y2+1)); + } + + Set visited = new HashSet<>(); + Stack stack = new Stack<>(); + + // flood fill outside + stack.push(new Position(x1-1, y1-1)); + while (!stack.isEmpty()) { + Position current = stack.pop(); + if (visited.contains(current)) { + continue; + } + visited.add(current); + if (current.getX() < x1 - 1) continue; + if (current.getX() > x2 + 1) continue; + if (current.getY() < y1 - 1) continue; + if (current.getY() > y2 + 1) continue; + if (!points.contains(current)) { + outside.add(current); + + stack.push(new Position(current.getX(), current.getY()-1)); + stack.push(new Position(current.getX()-1, current.getY())); + stack.push(new Position(current.getX(), current.getY()+1)); + stack.push(new Position(current.getX()+1, current.getY())); + } + } + + + // flood fill inside + visited.clear(); + for (Position pos : points) { + stack.push(pos); + } + while (!stack.isEmpty()) { + Position current = stack.pop(); + if (visited.contains(current)) { + continue; + } + visited.add(current); + if (!outside.contains(current)) { + points.add(current); + + stack.push(new Position(current.getX(), current.getY()-1)); + stack.push(new Position(current.getX()-1, current.getY())); + stack.push(new Position(current.getX(), current.getY()+1)); + stack.push(new Position(current.getX()+1, current.getY())); + } + } + + this.points = points; + } + + @Override + public Iterator iterator() { + return this.points.iterator(); + } + +} From 1727cfcb9e1bf4cf281bcc00f060f014abb40eeb Mon Sep 17 00:00:00 2001 From: ruffdd Date: Fri, 23 Oct 2020 21:16:31 +0200 Subject: [PATCH 5/8] improve constructor --- .../fius/jvk/provided/shapes/Circle.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java index f7f7f890..4a719904 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java @@ -15,7 +15,37 @@ public class Circle implements Shape { private List points; /** - * Create a new line by specifying start and end position of the line. + * Create a Circle, at the middle of the field. With the radius 1; + * + */ + public Circle() { + this(new Position(0, 0),1); + } + + /** + * Create a Circle, at the middle of the field + * + * @param radius + * the radius of the circle + */ + public Circle(Integer radius) { + this(new Position(0, 0),radius); + } + + /** + * Create a Circle + * + * @param start + * the start of the line + * @param end + * the end of the line; may not differ from the line start in both x and y direction + */ + public Circle(Position middle, Integer radius) { + + } + + /** + * Create a Circle * * @param start * the start of the line From c3a5275815aa4b9cae0a9029953976f47427a179 Mon Sep 17 00:00:00 2001 From: ruffdd Date: Fri, 30 Oct 2020 16:14:57 +0100 Subject: [PATCH 6/8] fix javadoc-mchange the old javadoc and removed unnecessary parameters --- .../fius/jvk/provided/shapes/Circle.java | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java index 4a719904..6cd3b01e 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java @@ -19,38 +19,27 @@ public class Circle implements Shape { * */ public Circle() { - this(new Position(0, 0),1); + this(new Position(0, 0), 1); } - + /** * Create a Circle, at the middle of the field * * @param radius - * the radius of the circle + * the radius of the circle */ public Circle(Integer radius) { - this(new Position(0, 0),radius); - } - - /** - * Create a Circle - * - * @param start - * the start of the line - * @param end - * the end of the line; may not differ from the line start in both x and y direction - */ - public Circle(Position middle, Integer radius) { - + this(new Position(0, 0), radius); } - + + /** * Create a Circle * - * @param start - * the start of the line - * @param end - * the end of the line; may not differ from the line start in both x and y direction + * @param middle + * the center of the circle + * @param radius + * the radius of the circle */ public Circle(Position middle, Integer radius) { Integer deltaX = middle.getX(); @@ -75,10 +64,16 @@ public Circle(Position middle, Integer radius) { points.forEach(p -> pointsAdd.add(new Position(-1*p.getX(), p.getY()))); pointsAdd.forEach(p -> {if(!points.contains(p))points.add(p);} ); pointsAdd.clear(); - points.forEach(p-> pointsAdd.add(new Position(p.getX()+middle.getX(), p.getY()+middle.getY()))); + points.forEach(p->{ + pointsAdd.add(new Position(p.getX()+middle.getX(), p.getY()+middle.getY())) + }); this.points=pointsAdd; } + /** + * + * @return an iterator over the pixels of the shape + */ @Override public Iterator iterator() { return this.points.iterator(); From 0a417b8396de87232fd3133e0c3e0da23ea82923 Mon Sep 17 00:00:00 2001 From: ruffdd Date: Fri, 30 Oct 2020 17:05:19 +0100 Subject: [PATCH 7/8] fix line calculation --- .../informatik/fius/jvk/provided/shapes/Circle.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java index 6cd3b01e..e89b75a4 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java @@ -65,7 +65,7 @@ public Circle(Position middle, Integer radius) { pointsAdd.forEach(p -> {if(!points.contains(p))points.add(p);} ); pointsAdd.clear(); points.forEach(p->{ - pointsAdd.add(new Position(p.getX()+middle.getX(), p.getY()+middle.getY())) + pointsAdd.add(new Position(p.getX()+middle.getX(), p.getY()+middle.getY())); }); this.points=pointsAdd; } From c9a32e7b0ca5b1f7d6f293593a2424dfc25c2704 Mon Sep 17 00:00:00 2001 From: ruffdd Date: Fri, 30 Oct 2020 17:13:51 +0100 Subject: [PATCH 8/8] fix line calculation and circle javadoc the last commit had the worng file --- .../informatik/fius/jvk/provided/shapes/Circle.java | 4 ---- .../informatik/fius/jvk/provided/shapes/Line.java | 11 +++++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java index e89b75a4..fa5236c4 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java @@ -70,10 +70,6 @@ public Circle(Position middle, Integer radius) { this.points=pointsAdd; } - /** - * - * @return an iterator over the pixels of the shape - */ @Override public Iterator iterator() { return this.points.iterator(); diff --git a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java index 2c0521e8..a0fd82a7 100644 --- a/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java +++ b/project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Line.java @@ -25,21 +25,20 @@ public class Line implements Shape { public Line(Position start, Position end) { Integer deltaX = end.getX() - start.getX(); Integer deltaY = end.getY() - start.getY(); - Float slope; //slope needs to have digits after the . ,so Float is used - + Double slope; //slope needs to have digits after the . ,so Float is used List points = new ArrayList<>(); if (Math.abs(deltaX) > Math.abs(deltaY)) { - slope = deltaY.floatValue() / deltaX.floatValue(); + slope = deltaY.doubleValue() / deltaX.doubleValue(); for (Integer x = start.getX(); (deltaX > 0) ? x <= end.getX() : x >= end.getX(); x += ((deltaX > 0 ? 1 : -1))) { - Integer y = Math.round(x.floatValue() * slope + start.getX()); + Integer y = (int)Math.round(x.doubleValue() * slope+start.getY()); points.add(new Position(x, y)); } } else{ - slope = deltaX.floatValue() / deltaY.floatValue(); + slope = deltaX.doubleValue() / deltaY.doubleValue(); for(Integer y = start.getY(); (deltaY > 0) ? y <= end.getY() : y >= end.getY(); y += ((deltaY > 0 ? 1 : -1))){ - Integer x = Math.round(y.floatValue() * slope + start.getY()); + Integer x = (int)Math.round(y.doubleValue() * slope +start.getX()); points.add(new Position(x, y)); } }