From: hama Date: Thu, 7 Jul 2016 22:08:55 +0000 (+0200) Subject: Diagram shows functions X-Git-Tag: v0.1~12 X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=ebae20a82dafc32f7b7fdd88bb3218cf57588ae2;p=jpinet Diagram shows functions --- diff --git a/src/main/java/de/republib/gui/Diagram.java b/src/main/java/de/republib/gui/Diagram.java index 9e2f0f2..f538ea5 100644 --- a/src/main/java/de/republib/gui/Diagram.java +++ b/src/main/java/de/republib/gui/Diagram.java @@ -10,7 +10,9 @@ import java.util.List; import javax.swing.JPanel; +import de.republib.util.DoublePair; import de.republib.util.IPairData; +import de.republib.util.PairData; /** * A diagram sheet. @@ -53,7 +55,7 @@ public class Diagram extends JPanel { * the pair data for one line * @return the instance (for chaining) */ - Diagram addData(IPairData data) { + public Diagram addData(IPairData data) { this.data.add(data); return this; } @@ -77,8 +79,13 @@ public class Diagram extends JPanel { final double y0 = this.realY0; final double realHeight = this.realHeight; for (final IPairData data : this.data) { + final DoublePair minMax = PairData.minMaxY(data, null); this.realX0 = data.getX(0); this.realWidth = data.getX(data.getSteps() - 1) - this.realX0; + this.realY0 = minMax.getValue1(); + final int steps = data.getSteps(); + final double maxY = minMax.getValue2(); + this.realHeight = maxY + (maxY - this.realY0) / steps; int lastX = 0, lastY = 0; for (int ix = 0; ix < data.getSteps(); ix++) { final int x = transformX(data.getX(ix)); @@ -104,10 +111,14 @@ public class Diagram extends JPanel { */ protected void paintScale(Graphics graphics) { graphics.setColor(this.colorScale); - graphics.drawLine(this.hScaleDistance, this.vScaleDistance, this.getWidth() - this.hScaleDistance, - this.vScaleDistance); - graphics.drawLine(this.hScaleDistance, this.vScaleDistance, this.hScaleDistance, - this.getHeight() - this.vScaleDistance); + final int y0 = this.getHeight() - this.vScaleDistance; + final int y1 = this.vScaleDistance; + final int x0 = this.hScaleDistance; + final int x1 = this.getWidth() - this.hScaleDistance; + // vertical scale: + graphics.drawLine(x0, y0, x0, y1); + // horizontal scale: + graphics.drawLine(x0, y0, x1, y0); } @@ -142,11 +153,11 @@ public class Diagram extends JPanel { final double yPixel = (y - this.realY0) * (getHeight() - 2 * this.vScaleDistance); int rc; if (yPixel < 0) { - rc = -1; - } else if (yPixel > getHeight()) { rc = getHeight(); + } else if (yPixel > getHeight()) { + rc = -1; } else { - rc = (int) Math.round(yPixel); + rc = getHeight() - (int) Math.round(yPixel); } return rc; } diff --git a/src/main/java/de/republib/pinet/gui/ControlCenter.java b/src/main/java/de/republib/pinet/gui/ControlCenter.java index 18dfd5b..a08d123 100644 --- a/src/main/java/de/republib/pinet/gui/ControlCenter.java +++ b/src/main/java/de/republib/pinet/gui/ControlCenter.java @@ -12,7 +12,9 @@ import javax.swing.JTabbedPane; import javax.swing.JTextField; import de.republib.gui.Diagram; +import de.republib.util.FunctionPairData; import de.republib.util.I18N; +import de.republib.util.MathFunction; /** * Created by hm on 03.07.16. @@ -23,7 +25,7 @@ public class ControlCenter { private JPanel panelOutput; private JPanel panelLog; private JLabel labelStatusLine; - private JPanel panelData; + private Diagram panelData; private JPanel panelHead; private JTextField textFieldServer; private JTextField textFieldPort; @@ -48,6 +50,14 @@ public class ControlCenter { this.panelCenter.add(this.tabbedPane = new JTabbedPane()); this.tabbedPane.addTab(I18N.tr("Output"), this.panelOutput = new JPanel()); this.tabbedPane.addTab(I18N.tr("Diagram"), this.panelData = new Diagram()); + FunctionPairData function = new FunctionPairData(MathFunction.X, 0.0, 10.0, 790); + function.setFactor(9.0); + function.setOffset(1.0); + this.panelData.addData(function); + function = new FunctionPairData(MathFunction.SIN, 0.0, 10.0, 790); + function.setFactor(4.0); + function.setOffset(5.0); + this.panelData.addData(function); this.tabbedPane.addTab(I18N.tr("Log"), this.panelLog = new JPanel()); } diff --git a/src/main/java/de/republib/util/DoublePair.java b/src/main/java/de/republib/util/DoublePair.java new file mode 100644 index 0000000..b9615ef --- /dev/null +++ b/src/main/java/de/republib/util/DoublePair.java @@ -0,0 +1,62 @@ +/** + * + */ +package de.republib.util; + +/** + * Stores two double values. + * + * @author hm + * + */ +public class DoublePair { + /** + * first value. + */ + public double value1; + /** + * second value. + */ + public double value2; + + /** + * Constructor. + * + * @param value1 + * @param value2 + */ + DoublePair(double value1, double value2) { + this.value1 = value1; + this.value2 = value2; + } + + /** + * @return the value1 + */ + public double getValue1() { + return this.value1; + } + + /** + * @return the value2 + */ + public double getValue2() { + return this.value2; + } + + /** + * @param value1 + * the value1 to set + */ + public void setValue1(double value1) { + this.value1 = value1; + } + + /** + * @param value2 + * the value2 to set + */ + public void setValue2(double value2) { + this.value2 = value2; + } +} diff --git a/src/main/java/de/republib/util/PairData.java b/src/main/java/de/republib/util/PairData.java index 6d3c517..e100aaf 100644 --- a/src/main/java/de/republib/util/PairData.java +++ b/src/main/java/de/republib/util/PairData.java @@ -8,6 +8,13 @@ package de.republib.util; * */ public class PairData { + /** + * Returns the maximal y value of all pairs. + * + * @param data + * data to inspect + * @return the maximum of the y values + */ public static double maxY(IPairData data) { double rc = data.getY(0); for (int ix = data.getSteps() - 1; ix > 0; ix--) { @@ -19,11 +26,45 @@ public class PairData { return rc; } + /** + * Returns the minimal y value of all pairs. + * + * @param data + * data to inspect + * @return the minimal of the y values + */ + public static DoublePair minMaxY(IPairData data, DoublePair minMax) { + double min = data.getY(0); + double max = min; + for (int ix = data.getSteps() - 1; ix > 0; ix--) { + final double current = data.getY(ix); + if (current < min) { + min = current; + } else if (current > max) { + max = current; + } + } + if (minMax == null) { + minMax = new DoublePair(min, max); + } else { + minMax.value1 = min; + minMax.value2 = max; + } + return minMax; + } + + /** + * Returns the minimal y value of all pairs. + * + * @param data + * data to inspect + * @return the minimal of the y values + */ public static double minY(IPairData data) { double rc = data.getY(0); for (int ix = data.getSteps() - 1; ix > 0; ix--) { final double current = data.getY(ix); - if (current > rc) { + if (current < rc) { rc = current; } } diff --git a/src/test/java/de/republib/util/PairDataTest.java b/src/test/java/de/republib/util/PairDataTest.java new file mode 100644 index 0000000..8a62a20 --- /dev/null +++ b/src/test/java/de/republib/util/PairDataTest.java @@ -0,0 +1,46 @@ +package de.republib.util; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class PairDataTest { + + @Test + public void maxY() { + final BytePairData data = new BytePairData(4, 4, -2.0, 2.0, 1); + for (byte value = -4; value < 0; value++) { + data.append(value); + } + Assert.assertEquals(PairData.maxY(data), -1.0); + Assert.assertEquals(PairData.minY(data), -4.0); + final DoublePair pair = PairData.minMaxY(data, new DoublePair(0.0, 0.0)); + Assert.assertEquals(pair.getValue2(), -1.0); + Assert.assertEquals(pair.getValue1(), -4.0); + } + + @Test + public void minMaxY() { + final BytePairData data = new BytePairData(4, 4, -2.0, 2.0, 8); + final long value = 0x12345678abcdefL; + final double value2 = value; + data.appendLittleEndian(value, 8); + Assert.assertEquals(PairData.maxY(data), value2); + Assert.assertEquals(PairData.minY(data), value2); + final DoublePair pair = PairData.minMaxY(data, new DoublePair(0.0, 0.0)); + Assert.assertEquals(pair.getValue2(), value2); + Assert.assertEquals(pair.getValue1(), value2); + } + + @Test + public void minY() { + final BytePairData data = new BytePairData(4, 4, -2.0, 2.0, 4); + for (int value = 4; value < 8; value++) { + data.appendLittleEndian(value, 4); + } + Assert.assertEquals(PairData.maxY(data), 7.0); + Assert.assertEquals(PairData.minY(data), 4.0); + final DoublePair pair = PairData.minMaxY(data, new DoublePair(0.0, 0.0)); + Assert.assertEquals(pair.getValue2(), 7.0); + Assert.assertEquals(pair.getValue1(), 4.0); + } +}