-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Shapes #121
Open
ruffdd
wants to merge
9
commits into
master
Choose a base branch
from
feature/better-better-better-shapes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improve Shapes #121
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b8e375e
improve Line shape
ruffdd b762944
add circle shape
ruffdd bc5894a
fix circle positioning
ruffdd c804cfd
Add FilledShape that flood fills existing shapes
buehlefs 1727cfc
improve constructor
ruffdd c418957
Merge branch 'feature/better-better-better-shapes' of github.com:FIUS…
ruffdd c3a5275
fix javadoc-mchange the old javadoc and removed unnecessary parameters
ruffdd 0a417b8
fix line calculation
ruffdd c9a32e7
fix line calculation and circle javadoc
ruffdd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/Circle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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<Position> points; | ||
|
||
/** | ||
* 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 middle | ||
* the center of the circle | ||
* @param radius | ||
* the radius of the circle | ||
*/ | ||
public Circle(Position middle, Integer radius) { | ||
Integer deltaX = middle.getX(); | ||
Integer deltaY = middle.getY(); | ||
|
||
List<Position> points = new ArrayList<>(); | ||
List<Position> 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);} ); | ||
pointsAdd.clear(); | ||
points.forEach(p->{ | ||
pointsAdd.add(new Position(p.getX()+middle.getX(), p.getY()+middle.getY())); | ||
}); | ||
this.points=pointsAdd; | ||
} | ||
|
||
@Override | ||
public Iterator<Position> iterator() { | ||
return this.points.iterator(); | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
project/src/main/java/de/unistuttgart/informatik/fius/jvk/provided/shapes/FilledShape.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Position> points; | ||
|
||
/** | ||
* Create a new shape by filling out the given shape. | ||
* | ||
* @param shape the input shape | ||
*/ | ||
public FilledShape(Shape shape) { | ||
Set<Position> 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<Position> 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<Position> visited = new HashSet<>(); | ||
Stack<Position> 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<Position> iterator() { | ||
return this.points.iterator(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.