* InnerConnection -> Resistance
* Wire extends Resistance + Voltmeter: Meter, Amperemeter, Voltmeter
This commit is contained in:
parent
04708e5835
commit
04fe180334
@ -7,9 +7,9 @@ import static java.lang.Math.round;
|
||||
@SuppressWarnings("unused")
|
||||
public class CONFIG {
|
||||
|
||||
public static final double NO_RESISTANCE = 1e-12;
|
||||
public static final double RESISTANCE_MIN = 1e-12;
|
||||
|
||||
public static final double MAX_RESISTANCE = 1 / NO_RESISTANCE;
|
||||
public static final double RESISTANCE_MAX = 1 / RESISTANCE_MIN;
|
||||
|
||||
public static final double VOLTAGE_HIGH_MIN = 0.1;
|
||||
|
||||
|
||||
@ -24,10 +24,6 @@ import static de.ph87.electro.CONFIG.ALIGN;
|
||||
@Slf4j
|
||||
public class Circuit {
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private File file = null;
|
||||
|
||||
@NonNull
|
||||
@Getter
|
||||
private final String created;
|
||||
@ -39,6 +35,10 @@ public class Circuit {
|
||||
@NonNull
|
||||
private final List<Wire> wires = new ArrayList<>();
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private File file = null;
|
||||
|
||||
public Circuit() {
|
||||
this.created = ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
}
|
||||
|
||||
@ -34,6 +34,8 @@ public class CircuitPanelDropTarget extends AbstractDropTarget {
|
||||
circuitPanel.getCircuit().addPart(new Poti(aligned));
|
||||
} else if (data.equals(Voltmeter.class.getSimpleName())) {
|
||||
circuitPanel.getCircuit().addPart(new Voltmeter(aligned));
|
||||
} else if (data.equals(Amperemeter.class.getSimpleName())) {
|
||||
circuitPanel.getCircuit().addPart(new Amperemeter(aligned));
|
||||
} else {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import de.ph87.electro.circuit.part.parts.ConnectorSub;
|
||||
import de.ph87.electro.circuit.part.parts.Connector;
|
||||
import de.ph87.electro.circuit.wire.Wire;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -144,7 +144,6 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
circuitPanel.repaint();
|
||||
}
|
||||
|
||||
@ -192,7 +191,7 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
||||
}
|
||||
|
||||
private void wireBendToNewConnector() {
|
||||
final ConnectorSub newConnector = circuitPanel.getCircuit().addPart(new ConnectorSub(draggingSub));
|
||||
final Connector newConnector = circuitPanel.getCircuit().addPart(new Connector(draggingSub));
|
||||
connectIntermediate(newConnector.getNode());
|
||||
}
|
||||
|
||||
@ -269,7 +268,7 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
||||
}
|
||||
|
||||
private boolean mouseClickedAction(@NonNull final MouseEvent event) {
|
||||
if (node != null && node.getPart() instanceof ConnectorSub) {
|
||||
if (node != null && node.getPart() instanceof Connector) {
|
||||
switch (event.getButton()) {
|
||||
case BUTTON3:
|
||||
circuitPanel.getCircuit().removePart(node.getPart());
|
||||
|
||||
55
src/main/java/de/ph87/electro/circuit/Resistance.java
Normal file
55
src/main/java/de/ph87/electro/circuit/Resistance.java
Normal file
@ -0,0 +1,55 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class Resistance {
|
||||
|
||||
@NonNull
|
||||
public final Node a;
|
||||
|
||||
@NonNull
|
||||
public final Node b;
|
||||
|
||||
@Setter
|
||||
protected double resistance;
|
||||
|
||||
@Setter
|
||||
protected double current = Double.NaN;
|
||||
|
||||
public Resistance(@NonNull final Node a, @NonNull final Node b, final double resistance) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.resistance = resistance;
|
||||
}
|
||||
|
||||
public Node getOpposite(final Node node) {
|
||||
if (a == node) {
|
||||
return b;
|
||||
} else if (b == node) {
|
||||
return a;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
public double getCurrent(final Node node) {
|
||||
return node == a ? current : -current;
|
||||
}
|
||||
|
||||
public Stream<Node> filterHasNode(final Node node) {
|
||||
if (a == node) {
|
||||
return Stream.of(b);
|
||||
} else if (b == node) {
|
||||
return Stream.of(a);
|
||||
}
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,7 +2,7 @@ package de.ph87.electro.circuit.calculation;
|
||||
|
||||
import de.ph87.electro.CONFIG;
|
||||
import de.ph87.electro.circuit.Circuit;
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import de.ph87.electro.circuit.part.parts.Battery;
|
||||
@ -12,7 +12,10 @@ import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.math3.linear.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
|
||||
@ -72,14 +75,14 @@ public class Calculation {
|
||||
|
||||
private void fromSchematic() {
|
||||
wires.forEach(
|
||||
wire -> addResistor(wire.getA(), wire.getB(), CONFIG.NO_RESISTANCE)
|
||||
wire -> addResistor(wire.getA(), wire.getB(), CONFIG.RESISTANCE_MIN)
|
||||
);
|
||||
parts.forEach(part -> {
|
||||
for (final InnerConnection innerConnection : part.getInnerConnections()) {
|
||||
addResistor(innerConnection.a, innerConnection.b, innerConnection.resistance);
|
||||
for (final Resistance resistance : part.getResistances()) {
|
||||
addResistor(resistance.a, resistance.b, resistance.getResistance());
|
||||
}
|
||||
if (part instanceof final Battery battery) {
|
||||
final double current = battery.getVoltage() / battery.getResistance();
|
||||
final double current = battery.getVoltage() / battery.getInnerResistance().getResistance();
|
||||
final int indexMinus = getNodeIndex(battery.getMinus());
|
||||
final int indexPlus = getNodeIndex(battery.getPlus());
|
||||
addCurrentSource(indexMinus, indexPlus, current);
|
||||
@ -90,12 +93,15 @@ public class Calculation {
|
||||
private void toSchematic() {
|
||||
wires.forEach(wire -> wire.setCurrent(getCurrent(wire)));
|
||||
parts.forEach(part -> {
|
||||
for (final Resistance resistance : part.getResistances()) {
|
||||
resistance.setCurrent(getCurrent(resistance));
|
||||
}
|
||||
part.getNodes().forEach(node -> node.setVoltage(getPotential(node)));
|
||||
part.postCalculate();
|
||||
});
|
||||
}
|
||||
|
||||
private double getPotential(final @NonNull Node node) {
|
||||
private double getPotential(@NonNull final Node node) {
|
||||
final int index = getNodeIndex(node);
|
||||
if (index < 0) {
|
||||
return 0; // per definition
|
||||
@ -106,11 +112,11 @@ public class Calculation {
|
||||
return potentials.getEntry(index);
|
||||
}
|
||||
|
||||
private double getCurrent(final Wire wire) {
|
||||
final int indexA = getNodeIndex(wire.getA());
|
||||
final int indexB = getNodeIndex(wire.getB());
|
||||
final double conductance = indexA >= 0 && indexB >= 0 ? matrix.getEntry(indexA, indexB) : 1 / CONFIG.NO_RESISTANCE;
|
||||
final double potentialDifference = getPotential(wire.getB()) - getPotential(wire.getA());
|
||||
private double getCurrent(@NonNull final Resistance resistance) {
|
||||
final int indexA = getNodeIndex(resistance.getA());
|
||||
final int indexB = getNodeIndex(resistance.getB());
|
||||
final double conductance = indexA >= 0 && indexB >= 0 ? matrix.getEntry(indexA, indexB) : 1 / CONFIG.RESISTANCE_MIN;
|
||||
final double potentialDifference = getPotential(resistance.getB()) - getPotential(resistance.getA());
|
||||
return conductance * potentialDifference;
|
||||
}
|
||||
|
||||
|
||||
@ -17,8 +17,8 @@ public class Demos {
|
||||
final Voltmeter voltmeter = circuit.addPart(new Voltmeter(RASTER(1, 4)));
|
||||
circuit.connect(battery.getMinus(), poti.getCommon());
|
||||
circuit.connect(battery.getPlus(), poti.getEnd());
|
||||
circuit.connect(poti.getCommon(), voltmeter.getA());
|
||||
circuit.connect(poti.getMiddle(), voltmeter.getB());
|
||||
circuit.connect(poti.getCommon(), voltmeter.a);
|
||||
circuit.connect(poti.getMiddle(), voltmeter.b);
|
||||
return circuit;
|
||||
}
|
||||
|
||||
|
||||
@ -1,30 +0,0 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class InnerConnection {
|
||||
|
||||
public final Node a;
|
||||
|
||||
public final Node b;
|
||||
|
||||
public final double resistance;
|
||||
|
||||
public InnerConnection(final Node a, final Node b, final double resistance) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.resistance = resistance;
|
||||
}
|
||||
|
||||
public Stream<Node> filter(final Node node) {
|
||||
if (a == node) {
|
||||
return Stream.of(b);
|
||||
} else if (b == node) {
|
||||
return Stream.of(a);
|
||||
}
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.electro.circuit.part;
|
||||
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import de.ph87.electro.circuit.part.node.NodeDto;
|
||||
import de.ph87.electro.circuit.part.parts.*;
|
||||
@ -63,12 +64,15 @@ public abstract class Part {
|
||||
return switch (abstractDto) {
|
||||
case final BatteryDto dto -> new Battery(dto);
|
||||
case final LightDto dto -> new Light(dto);
|
||||
case final ConnectorSubDto dto -> new ConnectorSub(dto);
|
||||
case final ConnectorDto dto -> new Connector(dto);
|
||||
case final Switch1x1Dto dto -> new Switch1x1(dto);
|
||||
case final Switch1x2Dto dto -> new Switch1x2(dto);
|
||||
case final SwitchCrossDto dto -> new SwitchCross(dto);
|
||||
case final PotiDto dto -> new Poti(dto);
|
||||
case final VoltmeterDto dto -> new Voltmeter(dto);
|
||||
case final MeterDto dto -> switch (dto.getType()) {
|
||||
case VOLT -> new Voltmeter(dto);
|
||||
case AMPERE -> new Amperemeter(dto);
|
||||
};
|
||||
case null, default -> throw new RuntimeException();
|
||||
};
|
||||
}
|
||||
@ -141,7 +145,7 @@ public abstract class Part {
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
public List<Resistance> getResistances() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
@ -33,12 +33,13 @@ public abstract class PartDto {
|
||||
return switch (abstractPart) {
|
||||
case final Battery part -> new BatteryDto(part);
|
||||
case final Light part -> new LightDto(part);
|
||||
case final ConnectorSub part -> new ConnectorSubDto(part);
|
||||
case final Connector part -> new ConnectorDto(part);
|
||||
case final Switch1x1 part -> new Switch1x1Dto(part);
|
||||
case final Switch1x2 part -> new Switch1x2Dto(part);
|
||||
case final SwitchCross part -> new SwitchCrossDto(part);
|
||||
case final Poti part -> new PotiDto(part);
|
||||
case final Voltmeter part -> new VoltmeterDto(part);
|
||||
case final Voltmeter part -> new MeterDto(part);
|
||||
case final Amperemeter part -> new MeterDto(part);
|
||||
case null, default -> throw new RuntimeException();
|
||||
};
|
||||
}
|
||||
|
||||
@ -38,7 +38,6 @@ public class Node {
|
||||
@NonNull
|
||||
protected Point position;
|
||||
|
||||
@NonNull
|
||||
@ToString.Include
|
||||
private double voltage = Double.NaN;
|
||||
|
||||
@ -50,7 +49,7 @@ public class Node {
|
||||
this.inside = inside;
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
this.name = name;
|
||||
this.position = part.transform(inside);
|
||||
this.position = calculateAbsolutePosition();
|
||||
}
|
||||
|
||||
public Node(@NonNull final Part part, @NonNull final NodeDto dto, @NonNull final Point inside) {
|
||||
@ -58,14 +57,14 @@ public class Node {
|
||||
this.name = dto.getName();
|
||||
this.part = part;
|
||||
this.inside = inside;
|
||||
this.position = part.transform(inside);
|
||||
this.position = calculateAbsolutePosition();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ToString.Include
|
||||
@SuppressWarnings("unused") // lombok toString
|
||||
public List<String> destinations() {
|
||||
return wires.stream().map(wire -> wire.getOpposite(this)).map(Node::getUuid).toList();
|
||||
return wires.stream().map(connection -> connection.getOpposite(this)).map(Node::getUuid).toList();
|
||||
}
|
||||
|
||||
public void setVoltage(final double voltage) {
|
||||
@ -74,7 +73,12 @@ public class Node {
|
||||
}
|
||||
|
||||
public void positionChanged() {
|
||||
position = part.transform(inside);
|
||||
position = calculateAbsolutePosition();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Point calculateAbsolutePosition() {
|
||||
return part.transform(inside);
|
||||
}
|
||||
|
||||
public void draw(@NonNull final Graphics2D g) {
|
||||
@ -96,7 +100,7 @@ public class Node {
|
||||
}
|
||||
connected.add(this);
|
||||
wires.forEach(wire -> wire.getOpposite(this).collectConnectedNodes(connected));
|
||||
part.getInnerConnections().stream().flatMap(innerConnection -> innerConnection.filter(this)).forEach(opposite -> opposite.collectConnectedNodes(connected));
|
||||
part.getResistances().stream().flatMap(resistance -> resistance.filterHasNode(this)).forEach(opposite -> opposite.collectConnectedNodes(connected));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@Getter
|
||||
public class Amperemeter extends Meter {
|
||||
|
||||
public Amperemeter(@NonNull final Point position) {
|
||||
super(MeterType.AMPERE, position);
|
||||
}
|
||||
|
||||
public Amperemeter(@NonNull final MeterDto dto) {
|
||||
super(dto);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getValue() {
|
||||
final double current = innerResistance.getCurrent();
|
||||
return Double.isNaN(current) ? 0.0 : current;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,9 +1,10 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@ -19,6 +20,10 @@ import static java.lang.Math.round;
|
||||
@ToString(callSuper = true, onlyExplicitlyIncluded = true)
|
||||
public class Battery extends Part {
|
||||
|
||||
public static final double DEFAULT_INNER_RESISTANCE = 0.05;
|
||||
|
||||
public static final int DEFAULT_VOLTAGE = 3;
|
||||
|
||||
private static final int MINUS_W = (int) round(0.1 * RASTER);
|
||||
|
||||
private static final int MINUS_H = (int) round(0.3 * RASTER);
|
||||
@ -29,34 +34,38 @@ public class Battery extends Part {
|
||||
|
||||
private static final int PLUS_H = (int) round(0.6 * RASTER);
|
||||
|
||||
@NonNull
|
||||
private final Point size = new Point(RASTER, RASTER);
|
||||
|
||||
@NonNull
|
||||
private final Node minus;
|
||||
|
||||
@NonNull
|
||||
private final Node plus;
|
||||
|
||||
@Setter
|
||||
@ToString.Include
|
||||
private double voltage = 3;
|
||||
private final Resistance innerResistance;
|
||||
|
||||
@Setter
|
||||
@ToString.Include
|
||||
private double resistance = 0.05;
|
||||
private double voltage = DEFAULT_VOLTAGE;
|
||||
|
||||
public Battery(final Point position) {
|
||||
public Battery(@NonNull final Point position) {
|
||||
super("Batterie", position);
|
||||
minus = addNode("MINUS", P10, P50);
|
||||
plus = addNode("PLUS", P90, P50);
|
||||
innerResistance = new Resistance(minus, plus, DEFAULT_INNER_RESISTANCE);
|
||||
}
|
||||
|
||||
public Battery(final BatteryDto dto) {
|
||||
public Battery(@NonNull final BatteryDto dto) {
|
||||
super(dto);
|
||||
minus = addNode(dto.getMinus(), P10, P50);
|
||||
plus = addNode(dto.getPlus(), P90, P50);
|
||||
voltage = dto.getVoltage();
|
||||
innerResistance = new Resistance(minus, plus, dto.getResistance());
|
||||
}
|
||||
|
||||
public static Stream<Battery> filterCast(final Part part) {
|
||||
@NonNull
|
||||
public static Stream<Battery> filterCast(@NonNull final Part part) {
|
||||
if (part instanceof final Battery battery) {
|
||||
return Stream.of(battery);
|
||||
}
|
||||
@ -64,12 +73,7 @@ public class Battery extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
return List.of(new InnerConnection(minus, plus, resistance));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _render(final Graphics2D g) {
|
||||
protected void _render(@NonNull final Graphics2D g) {
|
||||
drawLine(g, P10, P50, P50 - GAP / 2 - MINUS_W / 2, P50, Color.BLACK, SYMBOL_STROKE);
|
||||
drawLine(g, P50 + GAP / 2 + PLUS_W / 2, P50, P90, P50, Color.BLACK, SYMBOL_STROKE);
|
||||
fillRect(g, P50 - MINUS_W - GAP / 2, P50 - MINUS_H / 2, MINUS_W, MINUS_H, Color.BLACK);
|
||||
@ -77,8 +81,14 @@ public class Battery extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _labels(final Graphics2D g) {
|
||||
protected void _labels(@NonNull final Graphics2D g) {
|
||||
drawText(g, LABEL_FONT, "%.1fV".formatted(voltage), P50, P10, Color.BLACK, orientation);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<Resistance> getResistances() {
|
||||
return List.of(innerResistance);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,11 +17,14 @@ public class BatteryDto extends PartDto {
|
||||
|
||||
private double voltage;
|
||||
|
||||
private double resistance;
|
||||
|
||||
public BatteryDto(final Battery battery) {
|
||||
super(battery);
|
||||
this.minus = new NodeDto(battery.getMinus());
|
||||
this.plus = new NodeDto(battery.getPlus());
|
||||
this.voltage = battery.getVoltage();
|
||||
this.resistance = battery.getInnerResistance().getResistance();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package de.ph87.electro.circuit.part.parts;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
@ -11,18 +12,20 @@ import static de.ph87.electro.CONFIG.SUB_RASTER;
|
||||
|
||||
@Getter
|
||||
@ToString(callSuper = true)
|
||||
public class ConnectorSub extends Part {
|
||||
public class Connector extends Part {
|
||||
|
||||
@NonNull
|
||||
private final Node node;
|
||||
|
||||
@NonNull
|
||||
private final Point size = new Point(SUB_RASTER, SUB_RASTER);
|
||||
|
||||
public ConnectorSub(final Point position) {
|
||||
public Connector(@NonNull final Point position) {
|
||||
super("", position);
|
||||
node = addNode("", SUB_RASTER / 2, SUB_RASTER / 2);
|
||||
}
|
||||
|
||||
public ConnectorSub(final ConnectorSubDto dto) {
|
||||
public Connector(@NonNull final ConnectorDto dto) {
|
||||
super(dto);
|
||||
node = addNode(dto.getNode(), SUB_RASTER / 2, SUB_RASTER / 2);
|
||||
}
|
||||
@ -9,11 +9,11 @@ import lombok.ToString;
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class ConnectorSubDto extends PartDto {
|
||||
public class ConnectorDto extends PartDto {
|
||||
|
||||
private NodeDto node;
|
||||
|
||||
public ConnectorSubDto(final ConnectorSub part) {
|
||||
public ConnectorDto(final Connector part) {
|
||||
node = new NodeDto(part.getNode());
|
||||
}
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Orientation;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
@ -24,13 +25,15 @@ public class Light extends Part {
|
||||
|
||||
private static final Color BULB_OFF_COLOR = Color.DARK_GRAY;
|
||||
|
||||
private static final double DEFAULT_INNER_RESISTANCE = 15.0;
|
||||
|
||||
private final Point size = new Point(RASTER, RASTER);
|
||||
|
||||
private final Node a;
|
||||
|
||||
private final Node b;
|
||||
|
||||
private double resistance = 15;
|
||||
private final Resistance innerResistance;
|
||||
|
||||
private double maxVoltage = 3;
|
||||
|
||||
@ -40,19 +43,20 @@ public class Light extends Part {
|
||||
|
||||
private Color color = BULB_OFF_COLOR;
|
||||
|
||||
public Light(final Point position) {
|
||||
public Light(@NonNull final Point position) {
|
||||
super("Licht", position);
|
||||
a = addNode("A", P10, P50);
|
||||
b = addNode("B", P90, P50);
|
||||
innerResistance = new Resistance(a, b, DEFAULT_INNER_RESISTANCE);
|
||||
}
|
||||
|
||||
public Light(final LightDto dto) {
|
||||
public Light(@NonNull final LightDto dto) {
|
||||
super(dto);
|
||||
a = addNode(dto.getB(), P10, P50);
|
||||
b = addNode(dto.getA(), P90, P50);
|
||||
resistance = dto.getResistance();
|
||||
maxVoltage = dto.getMaxVoltage();
|
||||
defect = dto.isDefect();
|
||||
innerResistance = new Resistance(a, b, dto.getResistance());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -73,7 +77,7 @@ public class Light extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _render(final Graphics2D g) {
|
||||
protected void _render(@NonNull final Graphics2D g) {
|
||||
drawLine(g, a, b, Color.BLACK, SYMBOL_STROKE);
|
||||
drawCircle(g, P50, P50, BULB_RADIUS, Color.BLACK, SYMBOL_STROKE, color, 0, 360);
|
||||
|
||||
@ -83,7 +87,7 @@ public class Light extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _labels(final Graphics2D g) {
|
||||
protected void _labels(@NonNull final Graphics2D g) {
|
||||
final int x = RASTER / 2;
|
||||
final int y0;
|
||||
final int y1;
|
||||
@ -101,12 +105,13 @@ public class Light extends Part {
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
public List<Resistance> getResistances() {
|
||||
if (defect) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return List.of(new InnerConnection(a, b, resistance));
|
||||
return List.of(innerResistance);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ public class LightDto extends PartDto {
|
||||
this.a = new NodeDto(light.getB());
|
||||
this.b = new NodeDto(light.getA());
|
||||
this.defect = light.isDefect();
|
||||
this.resistance = light.getResistance();
|
||||
this.resistance = light.getInnerResistance().getResistance();
|
||||
this.maxVoltage = light.getMaxVoltage();
|
||||
}
|
||||
|
||||
|
||||
108
src/main/java/de/ph87/electro/circuit/part/parts/Meter.java
Normal file
108
src/main/java/de/ph87/electro/circuit/part/parts/Meter.java
Normal file
@ -0,0 +1,108 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import de.ph87.electro.common.RotationMatrix;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.math3.linear.ArrayRealVector;
|
||||
import org.apache.commons.math3.linear.RealVector;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static de.ph87.electro.circuit.CircuitPainter.*;
|
||||
import static java.lang.Math.round;
|
||||
|
||||
public abstract class Meter extends Part {
|
||||
|
||||
private static final int FINGER_LENGTH = P65;
|
||||
|
||||
private static final RealVector ANCHOR = new ArrayRealVector(new double[]{P50, P75});
|
||||
|
||||
private static final RealVector FINGER = new ArrayRealVector(new double[]{FINGER_LENGTH - P05, 0});
|
||||
|
||||
private static final Stroke SCALE_STROKE = new BasicStroke(1);
|
||||
|
||||
private static final double DEGREES_RANGE = 90;
|
||||
|
||||
public final MeterType type;
|
||||
|
||||
@Getter
|
||||
public final Point size = new Point(RASTER, RASTER);
|
||||
|
||||
public final Node a;
|
||||
|
||||
public final Node b;
|
||||
|
||||
public final String unit;
|
||||
|
||||
public final Resistance innerResistance;
|
||||
|
||||
@Setter
|
||||
public double min;
|
||||
|
||||
@Setter
|
||||
public double max;
|
||||
|
||||
public Meter(@NonNull MeterType type, @NonNull final Point position) {
|
||||
super(type.name, position);
|
||||
this.type = type;
|
||||
this.unit = type.unit;
|
||||
this.min = type.min;
|
||||
this.max = type.max;
|
||||
a = addNode("A", P10, P90);
|
||||
b = addNode("B", P90, P90);
|
||||
innerResistance = new Resistance(a, b, type.resistance);
|
||||
}
|
||||
|
||||
public Meter(@NonNull final MeterDto dto) {
|
||||
super(dto);
|
||||
this.type = dto.getType();
|
||||
this.unit = dto.getUnit();
|
||||
this.min = dto.getMin();
|
||||
this.max = dto.getMax();
|
||||
a = addNode("A", P10, P90);
|
||||
b = addNode("B", P90, P90);
|
||||
innerResistance = new Resistance(a, b, dto.getResistance());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Point P(@NonNull final RealVector anchor) {
|
||||
return new Point((int) round(anchor.getEntry(0)), (int) round(anchor.getEntry(1)));
|
||||
}
|
||||
|
||||
public abstract double getValue();
|
||||
|
||||
@Override
|
||||
public void _render(@NonNull final Graphics2D g) {
|
||||
// scale
|
||||
final double DEGREES_OFFSET = 45;
|
||||
drawCircle(g, P(ANCHOR), FINGER_LENGTH, DEGREES_OFFSET, DEGREES_RANGE, Color.BLACK, SCALE_STROKE, Color.white);
|
||||
|
||||
// finger
|
||||
final double ratio = (getValue() - min) / (max - min);
|
||||
final double degrees = -(DEGREES_OFFSET + DEGREES_RANGE * (1 - ratio));
|
||||
final RealVector finger = RotationMatrix.rotateDegrees(FINGER, degrees);
|
||||
final RealVector tip = ANCHOR.add(finger);
|
||||
drawLine(g, P(ANCHOR), P(tip), Color.BLACK, SCALE_STROKE);
|
||||
|
||||
// anchor
|
||||
drawCircle(g, P(ANCHOR), P03, 0, 360, null, null, Color.black);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _labels(@NonNull final Graphics2D g) {
|
||||
drawText(g, LABEL_FONT, "%.2f %s".formatted(getValue(), unit), P50, P90, Color.BLACK, orientation);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<Resistance> getResistances() {
|
||||
return List.of(innerResistance);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartDto;
|
||||
import de.ph87.electro.circuit.part.node.NodeDto;
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class MeterDto extends PartDto {
|
||||
|
||||
private MeterType type;
|
||||
|
||||
private NodeDto a;
|
||||
|
||||
private NodeDto b;
|
||||
|
||||
@Setter
|
||||
private double min;
|
||||
|
||||
@Setter
|
||||
private double max;
|
||||
|
||||
@NonNull
|
||||
@Setter
|
||||
private String unit;
|
||||
|
||||
@Setter
|
||||
private double resistance;
|
||||
|
||||
public MeterDto(@NonNull final Meter meter) {
|
||||
type = meter.type;
|
||||
a = new NodeDto(meter.a);
|
||||
b = new NodeDto(meter.b);
|
||||
min = meter.min;
|
||||
max = meter.max;
|
||||
unit = meter.unit;
|
||||
resistance = meter.innerResistance.getResistance();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import static de.ph87.electro.CONFIG.RESISTANCE_MAX;
|
||||
import static de.ph87.electro.CONFIG.RESISTANCE_MIN;
|
||||
|
||||
public enum MeterType {
|
||||
VOLT("Voltmeter", "V", -3, 3, RESISTANCE_MAX),
|
||||
AMPERE("Amperemeter", "A", -1, 1, RESISTANCE_MIN);
|
||||
|
||||
public final String name;
|
||||
|
||||
public final String unit;
|
||||
|
||||
public final double min;
|
||||
|
||||
public final double max;
|
||||
|
||||
public final double resistance;
|
||||
|
||||
MeterType(final String name, final String unit, final double min, final double max, final double resistance) {
|
||||
this.name = name;
|
||||
this.unit = unit;
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.resistance = resistance;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
@ -30,6 +30,10 @@ public class Poti extends Part {
|
||||
|
||||
private final Point size = new Point(RASTER, RASTER);
|
||||
|
||||
private final Resistance resistance0;
|
||||
|
||||
private final Resistance resistance1;
|
||||
|
||||
@Setter
|
||||
private double resistance = 10;
|
||||
|
||||
@ -40,6 +44,8 @@ public class Poti extends Part {
|
||||
common = addNode("C", P10, P50);
|
||||
middle = addNode("M", P50, P10);
|
||||
end = addNode("E", P90, P50);
|
||||
resistance0 = new Resistance(common, middle, max(RESISTANCE_MIN, resistance * ratio));
|
||||
resistance1 = new Resistance(middle, end, max(RESISTANCE_MIN, resistance * (1 - ratio)));
|
||||
}
|
||||
|
||||
public Poti(@NonNull final PotiDto dto) {
|
||||
@ -47,6 +53,8 @@ public class Poti extends Part {
|
||||
common = addNode(dto.getCommon().getName(), P10, P50);
|
||||
middle = addNode(dto.getMiddle().getName(), P50, P10);
|
||||
end = addNode(dto.getEnd().getName(), P90, P50);
|
||||
resistance0 = new Resistance(common, middle, max(RESISTANCE_MIN, resistance * ratio));
|
||||
resistance1 = new Resistance(middle, end, max(RESISTANCE_MIN, resistance * (1 - ratio)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -56,6 +64,8 @@ public class Poti extends Part {
|
||||
} else {
|
||||
ratio += 0.1;
|
||||
}
|
||||
resistance0.setResistance(max(RESISTANCE_MIN, resistance * ratio));
|
||||
resistance1.setResistance(max(RESISTANCE_MIN, resistance * (1 - ratio)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -63,11 +73,12 @@ public class Poti extends Part {
|
||||
drawText(g, LABEL_FONT, "%3.0f%%".formatted(ratio * 100), P50, P50, Color.BLACK, orientation);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
public List<Resistance> getResistances() {
|
||||
return List.of(
|
||||
new InnerConnection(common, middle, max(NO_RESISTANCE, resistance * ratio)),
|
||||
new InnerConnection(middle, end, max(NO_RESISTANCE, resistance * (1 - ratio)))
|
||||
resistance0,
|
||||
resistance1
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
@ -24,22 +26,26 @@ public class Switch1x1 extends Part {
|
||||
|
||||
private final Node output;
|
||||
|
||||
private final Resistance innerResistance;
|
||||
|
||||
private boolean state = false;
|
||||
|
||||
public Switch1x1(final Point position) {
|
||||
public Switch1x1(@NonNull final Point position) {
|
||||
super("Ausschalter", position);
|
||||
common = addNode("C", P10, P50);
|
||||
output = addNode("O", P90, P50);
|
||||
innerResistance = new Resistance(common, output, RESISTANCE_MIN);
|
||||
}
|
||||
|
||||
public Switch1x1(final Switch1x1Dto dto) {
|
||||
public Switch1x1(@NonNull final Switch1x1Dto dto) {
|
||||
super(dto);
|
||||
common = addNode(dto.getCommon(), P10, P50);
|
||||
output = addNode(dto.getOutput(), P90, P50);
|
||||
state = dto.isState();
|
||||
innerResistance = new Resistance(common, output, RESISTANCE_MIN);
|
||||
}
|
||||
|
||||
public void setState(final boolean state) {
|
||||
public void setState(@NonNull final boolean state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@ -49,7 +55,7 @@ public class Switch1x1 extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _render(final Graphics2D g) {
|
||||
protected void _render(@NonNull final Graphics2D g) {
|
||||
if (!state) {
|
||||
drawLine(g, common, END, common.getColor(), SWITCH_STROKE);
|
||||
} else {
|
||||
@ -57,12 +63,13 @@ public class Switch1x1 extends Part {
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
public List<Resistance> getResistances() {
|
||||
if (state) {
|
||||
return List.of(new InnerConnection(common, output, NO_RESISTANCE));
|
||||
return List.of(innerResistance);
|
||||
}
|
||||
return super.getInnerConnections();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
@ -24,24 +25,32 @@ public class Switch1x2 extends Part {
|
||||
|
||||
private final Node output1;
|
||||
|
||||
private final Resistance innerResistance0;
|
||||
|
||||
private final Resistance innerResistance1;
|
||||
|
||||
private boolean state = false;
|
||||
|
||||
public Switch1x2(final Point position) {
|
||||
public Switch1x2(@NonNull final Point position) {
|
||||
super("Wechselschalter", position);
|
||||
common = addNode("C", P10, P50);
|
||||
output0 = addNode("O0", P90, P25);
|
||||
output1 = addNode("O1", P90, P75);
|
||||
innerResistance0 = new Resistance(common, output0, RESISTANCE_MIN);
|
||||
innerResistance1 = new Resistance(common, output1, RESISTANCE_MIN);
|
||||
}
|
||||
|
||||
public Switch1x2(final Switch1x2Dto dto) {
|
||||
public Switch1x2(@NonNull final Switch1x2Dto dto) {
|
||||
super(dto);
|
||||
common = addNode(dto.getCommon(), P10, P50);
|
||||
output0 = addNode(dto.getOutput0(), P90, P25);
|
||||
output1 = addNode(dto.getOutput1(), P90, P75);
|
||||
state = dto.isState();
|
||||
innerResistance0 = new Resistance(common, output0, RESISTANCE_MIN);
|
||||
innerResistance1 = new Resistance(common, output1, RESISTANCE_MIN);
|
||||
}
|
||||
|
||||
public void setState(final boolean state) {
|
||||
public void setState(@NonNull final boolean state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@ -51,7 +60,7 @@ public class Switch1x2 extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _render(final Graphics2D g) {
|
||||
protected void _render(@NonNull final Graphics2D g) {
|
||||
if (!state) {
|
||||
drawLine(g, common, output0, common.getColor(), SWITCH_STROKE);
|
||||
} else {
|
||||
@ -60,11 +69,11 @@ public class Switch1x2 extends Part {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
public List<Resistance> getResistances() {
|
||||
if (state) {
|
||||
return List.of(new InnerConnection(common, output1, NO_RESISTANCE));
|
||||
return List.of(innerResistance1);
|
||||
}
|
||||
return List.of(new InnerConnection(common, output0, NO_RESISTANCE));
|
||||
return List.of(innerResistance0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.awt.*;
|
||||
@ -26,6 +27,14 @@ public class SwitchCross extends Part {
|
||||
|
||||
private final Node output1;
|
||||
|
||||
private final Resistance innerResistance01;
|
||||
|
||||
private final Resistance innerResistance10;
|
||||
|
||||
private final Resistance innerResistance00;
|
||||
|
||||
private final Resistance innerResistance11;
|
||||
|
||||
private boolean state = false;
|
||||
|
||||
public SwitchCross(final Point position) {
|
||||
@ -34,6 +43,10 @@ public class SwitchCross extends Part {
|
||||
common1 = addNode("C1", P10, P75);
|
||||
output0 = addNode("O0", P90, P25);
|
||||
output1 = addNode("O1", P90, P75);
|
||||
innerResistance01 = new Resistance(common0, output1, RESISTANCE_MIN);
|
||||
innerResistance10 = new Resistance(common1, output0, RESISTANCE_MIN);
|
||||
innerResistance00 = new Resistance(common0, output0, RESISTANCE_MIN);
|
||||
innerResistance11 = new Resistance(common1, output1, RESISTANCE_MIN);
|
||||
}
|
||||
|
||||
public SwitchCross(final SwitchCrossDto dto) {
|
||||
@ -43,6 +56,10 @@ public class SwitchCross extends Part {
|
||||
output0 = addNode(dto.getOutput0(), P90, P25);
|
||||
output1 = addNode(dto.getOutput1(), P90, P75);
|
||||
state = dto.isState();
|
||||
innerResistance01 = new Resistance(common0, output1, RESISTANCE_MIN);
|
||||
innerResistance10 = new Resistance(common1, output0, RESISTANCE_MIN);
|
||||
innerResistance00 = new Resistance(common0, output0, RESISTANCE_MIN);
|
||||
innerResistance11 = new Resistance(common1, output1, RESISTANCE_MIN);
|
||||
}
|
||||
|
||||
public void setState(final boolean state) {
|
||||
@ -65,17 +82,18 @@ public class SwitchCross extends Part {
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
public List<Resistance> getResistances() {
|
||||
if (state) {
|
||||
return List.of(
|
||||
new InnerConnection(common0, output1, NO_RESISTANCE),
|
||||
new InnerConnection(common1, output0, NO_RESISTANCE)
|
||||
innerResistance01,
|
||||
innerResistance10
|
||||
);
|
||||
}
|
||||
return List.of(
|
||||
new InnerConnection(common0, output0, NO_RESISTANCE),
|
||||
new InnerConnection(common1, output1, NO_RESISTANCE)
|
||||
innerResistance00,
|
||||
innerResistance11
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -1,98 +1,24 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.InnerConnection;
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.PartDto;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import de.ph87.electro.common.RotationMatrix;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.math3.linear.ArrayRealVector;
|
||||
import org.apache.commons.math3.linear.RealVector;
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static de.ph87.electro.circuit.CircuitPainter.*;
|
||||
import static java.lang.Math.round;
|
||||
|
||||
@Getter
|
||||
public class Voltmeter extends Part {
|
||||
public class Voltmeter extends Meter {
|
||||
|
||||
private static final int FINGER_LENGTH = P65;
|
||||
|
||||
private static final RealVector ANCHOR = new ArrayRealVector(new double[]{P50, P75});
|
||||
|
||||
private static final RealVector FINGER = new ArrayRealVector(new double[]{FINGER_LENGTH - P05, 0});
|
||||
|
||||
private static final Stroke SCALE_STROKE = new BasicStroke(1);
|
||||
|
||||
private static final double DEGREES_RANGE = 90;
|
||||
|
||||
private final Point size = new Point(RASTER, RASTER);
|
||||
|
||||
private final Node a;
|
||||
|
||||
private final Node b;
|
||||
|
||||
double x = 0;
|
||||
|
||||
@Setter
|
||||
private double min = -3;
|
||||
|
||||
@Setter
|
||||
private double max = 3;
|
||||
|
||||
private RealVector tip;
|
||||
|
||||
public Voltmeter(final Point position) {
|
||||
super("Voltmeter", position);
|
||||
a = addNode("A", P10, P90);
|
||||
b = addNode("B", P90, P90);
|
||||
postCalculate(); // TODO remove
|
||||
public Voltmeter(@NonNull final Point position) {
|
||||
super(MeterType.VOLT, position);
|
||||
}
|
||||
|
||||
public Voltmeter(final PartDto dto) {
|
||||
public Voltmeter(@NonNull final MeterDto dto) {
|
||||
super(dto);
|
||||
a = addNode("A", P10, P90);
|
||||
b = addNode("B", P90, P90);
|
||||
postCalculate(); // TODO remove
|
||||
}
|
||||
|
||||
public static Point P(final RealVector anchor) {
|
||||
return new Point((int) round(anchor.getEntry(0)), (int) round(anchor.getEntry(1)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void _render(final Graphics2D g) {
|
||||
// scale
|
||||
final double DEGREES_OFFSET = 45;
|
||||
drawCircle(g, P(ANCHOR), FINGER_LENGTH, DEGREES_OFFSET, DEGREES_RANGE, Color.BLACK, SCALE_STROKE, Color.white);
|
||||
|
||||
// finger
|
||||
final double ratio = (getVoltage() - min) / (max - min);
|
||||
final double degrees = -(DEGREES_OFFSET + DEGREES_RANGE * (1 - ratio));
|
||||
final RealVector finger = RotationMatrix.rotateDegrees(FINGER, degrees);
|
||||
tip = ANCHOR.add(finger);
|
||||
drawLine(g, P(ANCHOR), P(tip), Color.BLACK, SCALE_STROKE);
|
||||
|
||||
// anchor
|
||||
drawCircle(g, P(ANCHOR), P03, 0, 360, null, null, Color.black);
|
||||
}
|
||||
|
||||
private double getVoltage() {
|
||||
public double getValue() {
|
||||
return !Double.isNaN(b.getVoltage()) && !Double.isNaN(a.getVoltage()) ? b.getVoltage() - a.getVoltage() : 0.0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _labels(final Graphics2D g) {
|
||||
drawText(g, LABEL_FONT, "%.2f V".formatted(getVoltage()), P50, P90, Color.BLACK, orientation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<InnerConnection> getInnerConnections() {
|
||||
return List.of(new InnerConnection(a, b, MAX_RESISTANCE));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
package de.ph87.electro.circuit.part.parts;
|
||||
|
||||
import de.ph87.electro.circuit.part.PartDto;
|
||||
import de.ph87.electro.circuit.part.node.NodeDto;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class VoltmeterDto extends PartDto {
|
||||
|
||||
private NodeDto a;
|
||||
|
||||
private NodeDto b;
|
||||
|
||||
@Setter
|
||||
private double min;
|
||||
|
||||
@Setter
|
||||
private double max;
|
||||
|
||||
public VoltmeterDto(final Voltmeter voltmeter) {
|
||||
a = new NodeDto(voltmeter.getA());
|
||||
b = new NodeDto(voltmeter.getB());
|
||||
min = voltmeter.getMin();
|
||||
max = voltmeter.getMax();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.electro.circuit.wire;
|
||||
|
||||
import de.ph87.electro.circuit.Resistance;
|
||||
import de.ph87.electro.circuit.part.node.Node;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
@ -14,23 +15,13 @@ import static java.lang.Math.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class Wire {
|
||||
|
||||
@NonNull
|
||||
private final Node a;
|
||||
|
||||
@NonNull
|
||||
private final Node b;
|
||||
|
||||
@Setter
|
||||
private double current = Double.NaN;
|
||||
public class Wire extends Resistance {
|
||||
|
||||
@Setter
|
||||
private boolean ghost = false;
|
||||
|
||||
public Wire(@NonNull final Node a, @NonNull final Node b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
super(a, b, RESISTANCE_MIN);
|
||||
a.getWires().add(this);
|
||||
b.getWires().add(this);
|
||||
}
|
||||
@ -56,19 +47,6 @@ public class Wire {
|
||||
return distance <= WIRE_HOVER_STROKE_BACK.getLineWidth();
|
||||
}
|
||||
|
||||
public Node getOpposite(final Node node) {
|
||||
if (a == node) {
|
||||
return b;
|
||||
} else if (b == node) {
|
||||
return a;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
public double getCurrent(final Node node) {
|
||||
return node == a ? current : -current;
|
||||
}
|
||||
|
||||
public void draw(final Graphics2D g) {
|
||||
final Color color = ghost ? new Color(a.getColor().getRed(), a.getColor().getGreen(), a.getColor().getBlue(), 64) : a.getColor();
|
||||
|
||||
|
||||
@ -29,6 +29,7 @@ public class Sidebar extends JPanel {
|
||||
addPart(new SwitchCross(ZERO));
|
||||
addPart(new Poti(ZERO));
|
||||
addPart(new Voltmeter(ZERO));
|
||||
addPart(new Amperemeter(ZERO));
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user