replaced 'int rotation' by 'Orientation'

This commit is contained in:
Patrick Haßel 2024-05-15 16:39:48 +02:00
parent c61317f705
commit f8e64b1280
7 changed files with 50 additions and 13 deletions

View File

@ -36,7 +36,7 @@ public class Circuit {
private void addAndRotate(final Part part, final int rotate) { private void addAndRotate(final Part part, final int rotate) {
for (int i = 0; i < rotate % 4; i++) { for (int i = 0; i < rotate % 4; i++) {
part.rotate(); part.clockwise();
} }
add(part); add(part);
} }

View File

@ -43,7 +43,7 @@ public class CircuitPanel extends JPanel {
break; break;
case BUTTON3: case BUTTON3:
partOptional.ifPresent(part -> { partOptional.ifPresent(part -> {
part.rotate(); part.clockwise();
repaint(); repaint();
}); });
break; break;

View File

@ -1,6 +1,7 @@
package de.ph87.electro.circuit.dto; package de.ph87.electro.circuit.dto;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import de.ph87.electro.circuit.part.Orientation;
import de.ph87.electro.circuit.part.Part; import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.impl.*; import de.ph87.electro.circuit.part.impl.*;
import lombok.Getter; import lombok.Getter;
@ -23,7 +24,7 @@ public class PartDto {
private int y; private int y;
private int rotation; private Orientation orientation;
private List<JunctionDto> junctions; private List<JunctionDto> junctions;
@ -32,7 +33,7 @@ public class PartDto {
this.name = part.getName(); this.name = part.getName();
this.x = part.getX(); this.x = part.getX();
this.y = part.getY(); this.y = part.getY();
this.rotation = part.getRotation(); this.orientation = part.getOrientation();
this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList(); this.junctions = part.getJunctions().stream().map(JunctionDto::new).toList();
} }

View File

@ -0,0 +1,37 @@
package de.ph87.electro.circuit.part;
import java.awt.geom.Point2D;
import java.util.function.Function;
import static java.lang.Math.PI;
@SuppressWarnings("SuspiciousNameCombination")
public enum Orientation {
R0(0, p -> new Point2D.Double(p.x, p.y)),
R1(1, p -> new Point2D.Double(1 - p.y, p.x)),
R2(2, p -> new Point2D.Double(1 - p.x, 1 - p.y)),
R3(3, p -> new Point2D.Double(p.y, 1 - p.x)),
;
private final Function<Point2D.Double, Point2D.Double> map;
private final int deg90;
Orientation(final int deg90, final Function<Point2D.Double, Point2D.Double> map) {
this.deg90 = deg90;
this.map = map;
}
public Point2D.Double map(final Point2D.Double p) {
return map.apply(p);
}
public Orientation clockwise() {
return values()[(ordinal() + 1) % values().length];
}
public double getTheta() {
return PI / 2 * deg90;
}
}

View File

@ -32,7 +32,7 @@ public abstract class Part {
protected int y; protected int y;
protected int rotation = 0; protected Orientation orientation = Orientation.R0;
protected final List<Junction> junctions = new ArrayList<>(); protected final List<Junction> junctions = new ArrayList<>();
@ -48,7 +48,7 @@ public abstract class Part {
this.name = dto.getName(); this.name = dto.getName();
this.x = dto.getX(); this.x = dto.getX();
this.y = dto.getY(); this.y = dto.getY();
this.rotation = dto.getRotation(); this.orientation = dto.getOrientation();
} }
public void link(final List<Junction> junctions) { public void link(final List<Junction> junctions) {
@ -65,8 +65,8 @@ public abstract class Part {
junctions.forEach(junction -> junction.paint(g)); junctions.forEach(junction -> junction.paint(g));
} }
public void rotate() { public void clockwise() {
rotation = (rotation + 1) % 4; orientation = orientation.clockwise();
junctions.forEach(Junction::rotate); junctions.forEach(Junction::rotate);
} }

View File

@ -12,7 +12,6 @@ import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import static de.ph87.electro.CONFIG.*; import static de.ph87.electro.CONFIG.*;
import static java.lang.Math.PI;
@Getter @Getter
@ToString(callSuper = true) @ToString(callSuper = true)
@ -56,8 +55,8 @@ public class PartBattery extends Part {
} }
@Override @Override
public void rotate() { public void clockwise() {
super.rotate(); super.clockwise();
} }
public void startPropagation() { public void startPropagation() {
@ -80,7 +79,7 @@ public class PartBattery extends Part {
final BufferedImage img = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB); final BufferedImage img = new BufferedImage(RASTER, RASTER, BufferedImage.TYPE_INT_ARGB);
final Graphics2D x = (Graphics2D) img.getGraphics(); final Graphics2D x = (Graphics2D) img.getGraphics();
x.translate(RASTER / 2, RASTER / 2); x.translate(RASTER / 2, RASTER / 2);
x.rotate(PI / 2 * rotation); x.rotate(orientation.getTheta());
x.translate(-RASTER / 2, -RASTER / 2); x.translate(-RASTER / 2, -RASTER / 2);
line(x, JUNCTION_LEFT, HALF, HALF - GAP / 2 - MINUS_W / 2, HALF, Color.BLACK, SYMBOL_STROKE); line(x, JUNCTION_LEFT, HALF, HALF - GAP / 2 - MINUS_W / 2, HALF, Color.BLACK, SYMBOL_STROKE);
line(x, HALF + GAP / 2 + PLUS_W / 2, HALF, JUNCTION_RIGHT, HALF, Color.BLACK, SYMBOL_STROKE); line(x, HALF + GAP / 2 + PLUS_W / 2, HALF, JUNCTION_RIGHT, HALF, Color.BLACK, SYMBOL_STROKE);

View File

@ -44,7 +44,7 @@ class CircuitServiceTest {
assertEquals(originalPart.getName(), reloadedPart.getName()); assertEquals(originalPart.getName(), reloadedPart.getName());
assertEquals(originalPart.getX(), reloadedPart.getX()); assertEquals(originalPart.getX(), reloadedPart.getX());
assertEquals(originalPart.getY(), reloadedPart.getY()); assertEquals(originalPart.getY(), reloadedPart.getY());
assertEquals(originalPart.getRotation(), reloadedPart.getRotation()); assertEquals(originalPart.getOrientation(), reloadedPart.getOrientation());
assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size()); assertEquals(originalPart.getJunctions().size(), reloadedPart.getJunctions().size());
for (final Junction originalJunction : originalPart.getJunctions()) { for (final Junction originalJunction : originalPart.getJunctions()) {
System.out.printf(" - Junction: %s\n", originalJunction.getUuid()); System.out.printf(" - Junction: %s\n", originalJunction.getUuid());