소스 검색

endpunkte

Kevin Trometer 8 년 전
부모
커밋
79240b98fa
1개의 변경된 파일48개의 추가작업 그리고 18개의 파일을 삭제
  1. 48 18
      src/ui/view/UnitGraph.java

+ 48 - 18
src/ui/view/UnitGraph.java

@@ -32,6 +32,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private Point recSize = new Point(8, 8); // Point Size
 	private Graphics2D g2;
 	private CubicCurve2D c = new CubicCurve2D.Double();
+	private CubicCurve2D cr = new CubicCurve2D.Double();
+	private CubicCurve2D cl = new CubicCurve2D.Double();
 	private LinkedList<Point> pointList;
 	private double scaleX;
 	private double scaleY;
@@ -80,9 +82,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		graphCurve.reset();
 
 		if (arrayOfValue != null) {
-			for (int i = 0; i < arrayOfValue.length; i++) {
-				arrayOfValue[i] = convertToValueY(getYValueAt((int) (i * width / (model.getIterations() - 1))));
-			}
+			fillArrayofValue();
 		}
 
 		// Draw the Vertical Lines
@@ -128,14 +128,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			}
 		}
 		// Actual Iteration Point Visualization
-		/*
-		 * g2.setColor(Color.RED); if (arrayOfValue != null) { for (int i = 0; i
-		 * < arrayOfValue.length; i++) { g2.fillOval((int) (i * width /
-		 * (model.getIterations() - 1) * scaleX - recSize.getX() / 2), (int)
-		 * (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() /
-		 * 2), (int) recSize.getX(), (int) recSize.getY()); } }
-		 */
 
+		g2.setColor(Color.RED);
+		if (arrayOfValue != null) {
+			for (int i = 0; i < arrayOfValue.length; i++) {
+				g2.fillOval((int) (i * width / (model.getIterations() - 1) * scaleX - recSize.getX() / 2),
+						(int) (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() / 2),
+						(int) recSize.getX(), (int) recSize.getY());
+			}
+		}
 	}
 
 	@Override
@@ -152,7 +153,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				y = this.getHeight() / scaleY;
 			}
 			// x
-			if (tempP.getX() == 0 || tempP.getX() == this.getWidth() / scaleX || pointList.get(i + 1).getX() <= x
+			if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
 					|| pointList.get(i - 1).getX() >= x) {
 				x = tempP.getX();
 			}
@@ -289,7 +290,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @return the converted number
 	 */
 	public double convertToCanvasY(float d) {
-		System.out.println("" + (height + (d * (height / MAXIMUM))));
 		return (height - (d * (height / MAXIMUM)));
 	}
 
@@ -301,7 +301,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @return the converted number
 	 */
 	public float convertToValueY(double d) {
-		return Math.round(((float) ((height - (height * (d / height))) / (height / MAXIMUM)) * 10)) / 10;
+		return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
 	}
 
 	/**
@@ -347,30 +347,59 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		return c;
 	}
 
+	public void fillArrayofValue() {
+		for (int i = 0; i < arrayOfValue.length; i++) {
+			arrayOfValue[i] = convertToValueY(getYValueAt_2((int) (i * this.getWidth() / (model.getIterations() - 1))));
+		}
+	}
+	
 	/**
 	 * 
 	 * @param xVal,
 	 *            the x value for the y value
 	 * @return y, the value at x
 	 */
-	public int getYValueAt(int xVal) {
+	public float getYValueAt(int xVal) {
 		for (int i = 0; i < pointList.size() - 1; i++) {
 			// get the Points
-			if (xVal < pointList.get(i + 1).getX()) {
+			if (xVal <= pointList.get(i + 1).getX()) {
 				// Curve erstellen
 				Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
 						pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
 				Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
 
-				return (int) getIntersectionPoint(l1, l2).getY();
+				return (float) getIntersectionPoint(l1, l2).getY();
+			}
+		}
+		return 0;
+	}
+	
+	/**
+	 * 
+	 * @param xVal,
+	 *            the x value for the y value
+	 * @return y, the value at x
+	 */
+	public float getYValueAt_2(int xVal) {
+		for (int i = 0; i < pointList.size() - 1; i++) {
+			// get the Points
+			if (xVal <= pointList.get(i + 1).getX()) {
+				// Curve erstellen
+				c = buildCurve(pointList.get(i), pointList.get(i + 1));
+				for (int j = 0; j < height; j++) {
+					if (c.contains(xVal, j)) {
+						return j;
+					}
+				}
 			}
 		}
 		return 0;
 	}
 
 	public Point getIntersectionPoint(Line2D l1, Line2D l2) {
-		if (!l1.intersectsLine(l2))
+		if (!l1.intersectsLine(l2)) {
 			return null;
+		}
 		double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
 		double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
 
@@ -379,8 +408,9 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			return null;
 		} else {
 			double z = (sx * (qy - py) + sy * (px - qx)) / det;
-			if (z < 0 || z > 1)
+			if (z < 0 || z > 1) {
 				return new Point(0, 0); // intersection at end point!
+			}
 			return new Point((int) (px + z * rx), (int) (py + z * ry));
 		}
 	} // end intersection line-line