Browse Source

even better point recognition

Kevin Trometer 8 years ago
parent
commit
7f5d6efe16
1 changed files with 39 additions and 28 deletions
  1. 39 28
      src/ui/view/UnitGraph.java

+ 39 - 28
src/ui/view/UnitGraph.java

@@ -85,13 +85,11 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		// Draw the Vertical Lines
 		g2.setColor(Color.BLACK);
 		for (int i = 0; i <= this.getWidth(); i += 10) {
-			g2.drawLine(i, 0,
-					i, this.getHeight());
+			g2.drawLine(i, 0, i, this.getHeight());
 		}
 
 		for (int i = 0; i <= this.getHeight(); i += 5) {
-			g2.drawLine(0, i, this.getWidth(),
-					i);
+			g2.drawLine(0, i, this.getWidth(), i);
 		}
 
 		if (arrayOfValue != null) {
@@ -122,11 +120,10 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
 		// Iteration Value
 		if (arrayOfValue != null) {
-				g2.drawString("" + arrayOfValue[model.getCurIteration()],
-						(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) +2,
-						this.getHeight()-10);
+			g2.drawString("" + arrayOfValue[model.getCurIteration()],
+					(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
+					this.getHeight() - 10);
 		}
-
 		/*
 		 * // Actual Iteration Point Visualization g2.setColor(Color.RED); if
 		 * (arrayOfValue != null) { for (int i = 0; i < arrayOfValue.length;
@@ -135,6 +132,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		 * arrayOfValue[i]) * scaleY - recSize.getY() / 2), (int)
 		 * recSize.getX(), (int) recSize.getY()); } }
 		 */
+
 	}
 
 	@Override
@@ -204,7 +202,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					}
 				}
 			}
-			//New Point
+			// New Point
 			if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
 					&& e.getX() != this.getWidth() / scaleX) {
 				for (int i = 0; i < pointList.size(); i++) {
@@ -220,7 +218,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					}
 				}
 			}
-			//Delete a Point
+			// Delete a Point
 			if (deletePoint && tempP.getX() != 0
 					&& (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
 				pointList.remove(tempP);
@@ -239,7 +237,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	}
 
 	public void componentResized(ComponentEvent e) {
-		//Wenn ein anderes Element genommen wird
+		// Wenn ein anderes Element genommen wird
 		if (init) {
 			MAXIMUM = tempElement.getEnergy();
 			init = false;
@@ -251,15 +249,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 			scaleX = this.getWidth() / width;
 			scaleY = this.getHeight() / height;
-			
-			//First time clicked on the Element
+
+			// First time clicked on the Element
 			if (pointList.isEmpty()) {
 				pointList.addFirst(new Point(0, 0));
 				pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
 			}
 		}
-		
-		//Scale
+
+		// Scale
 		scaleX = this.getWidth() / width;
 		scaleY = this.getHeight() / height;
 		repaint();
@@ -348,8 +346,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		y1 = (int) p1.getY();
 		x2 = (int) p2.getX();
 		y2 = (int) p2.getY();
-		
-		//calculate the controllpoints
+
+		// calculate the controllpoints
 		ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
 		ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
 		if (y1 < y2) {
@@ -360,12 +358,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
 		}
 
-		//set the curve
+		// set the curve
 		c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
 				x2 * scaleX, y2 * scaleY);
 		return c;
 	}
 
+	/**
+	 * Fills the Arrays of each HolonElement
+	 */
 	public void fillArrayofValue() {
 		for (int i = 0; i < arrayOfValue.length; i++) {
 			arrayOfValue[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
@@ -386,7 +387,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				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 (float) getIntersectionPoint(l1, l2).getY();
+				return getIntersectionPoint(l1, l2);
 			}
 		}
 		return 0;
@@ -410,13 +411,13 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					c = cl;
 					// Kurve Links von "unten"
 					if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
-						for (int j = (int) (height - 1); j >= 0; j--) {
+						for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
 							}
 						}
 					} else {// Kurve Links von "oben"
-						for (int j = 0; j < height; j++) {
+						for (float j = 0; j < height; j += 0.1f) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
 							}
@@ -426,13 +427,13 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					c = cr;
 					// Kurve Links von "unten"
 					if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
-						for (int j = 0; j < height; j++) {
+						for (float j = 0; j < height; j += 0.1f) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
 							}
 						}
 					} else {// Kurve Links von "oben"
-						for (int j = (int) (height - 1); j >= 0; j--) {
+						for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
 							}
@@ -444,22 +445,32 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		return getYValueAt(xVal);
 	}
 
-	public Point getIntersectionPoint(Line2D l1, Line2D l2) {
+	/**
+	 * 
+	 * @param l1,
+	 *            the first Line
+	 * @param l2,
+	 *            the second Line
+	 * 
+	 * @return The Intersection Point
+	 */
+	public float getIntersectionPoint(Line2D l1, Line2D l2) {
 		if (!l1.intersectsLine(l2)) {
-			return null;
+			return 0;// 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;
 
 		double det = sx * ry - sy * rx;
 		if (det == 0) {
-			return null;
+			return 0;// null;
 		} else {
 			double z = (sx * (qy - py) + sy * (px - qx)) / det;
 			if (z < 0 || z > 1) {
-				return new Point(0, 0); // intersection at end point!
+				return 0;// new Point(0, 0); // intersection at end point!
 			}
-			return new Point((int) (px + z * rx), (int) (py + z * ry));
+			return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
+											// (py + z * ry));
 		}
 	} // end intersection line-line