UnitGraph.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.event.ComponentEvent;
  8. import java.awt.event.ComponentListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.event.MouseMotionListener;
  12. import java.awt.geom.CubicCurve2D;
  13. import java.awt.geom.GeneralPath;
  14. import java.awt.geom.Line2D;
  15. import java.util.ArrayList;
  16. import java.util.LinkedList;
  17. import java.awt.Point;
  18. import javax.swing.JPanel;
  19. import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
  20. import classes.HolonElement;
  21. import ui.controller.Control;
  22. import ui.model.Model;
  23. import classes.HolonSwitch;
  24. import java.awt.Cursor;
  25. class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  26. private static final long serialVersionUID = 1L;
  27. private float MAXIMUM = 0;
  28. private Point recSize = new Point(8, 8); // Point Size
  29. private Graphics2D g2;
  30. private CubicCurve2D c = new CubicCurve2D.Double();
  31. private CubicCurve2D cr = new CubicCurve2D.Double();
  32. private CubicCurve2D cl = new CubicCurve2D.Double();
  33. private LinkedList<Point> pointList;
  34. private double scaleX;
  35. private double scaleY;
  36. private float[] arrayOfFloats = null;
  37. private boolean[] arrayOfBooleans = null;
  38. private double width = -1;
  39. private double height = -1;
  40. private boolean isElement = false;
  41. private boolean isSwitch = false;
  42. private ArrayList<HolonElement> tempElements = new ArrayList<>();
  43. private HolonSwitch tempSwitch;
  44. private Model model;
  45. private Control controller;
  46. private Line2D.Double line = null;
  47. GeneralPath graphCurve = new GeneralPath();
  48. private boolean pointDrag = false;
  49. private boolean init = true;
  50. private Point tempP = null;
  51. private double x = 0, y = 0;
  52. private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
  53. public UnitGraph(final Model model, Control control) {
  54. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  55. this.controller = control;
  56. this.model = model;
  57. this.pointList = new LinkedList<>();
  58. this.setBackground(Color.WHITE);
  59. this.addMouseListener(this);
  60. this.addMouseMotionListener(this);
  61. this.addComponentListener(this);
  62. }
  63. /**
  64. * Paints all Components on the Canvas
  65. *
  66. * @param Graphics
  67. *
  68. */
  69. public void paintComponent(Graphics g) {
  70. super.paintComponent(g);
  71. g2 = (Graphics2D) g;
  72. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  73. g2.setRenderingHints(rh);
  74. g2.setStroke(new BasicStroke(0));
  75. graphCurve.reset();
  76. // Draw the Vertical Lines
  77. g2.setColor(Color.BLACK);
  78. for (int i = 0; i <= this.getWidth(); i += 10) {
  79. g2.drawLine(i, 0, i, this.getHeight());
  80. }
  81. for (int i = 0; i <= this.getHeight(); i += 5) {
  82. g2.drawLine(0, i, this.getWidth(), i);
  83. }
  84. if (isElement) {
  85. if (arrayOfFloats != null) {
  86. // Draw the Lines
  87. g2.setStroke(new BasicStroke(2));
  88. g2.setColor(Color.BLACK);
  89. for (int i = 0; i < pointList.size() - 1; i++) {
  90. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  91. graphCurve.append(c, true);
  92. }
  93. g2.draw(graphCurve);
  94. // Draw the Points
  95. g2.setColor(Color.BLUE);
  96. for (int i = 0; i < pointList.size() - 0; i++) {
  97. g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  98. (int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
  99. (int) recSize.getY());
  100. }
  101. // Iteration Value
  102. if (arrayOfFloats != null) {
  103. g2.drawString("" + arrayOfFloats[model.getCurIteration()],
  104. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  105. this.getHeight() - 10);
  106. }
  107. }
  108. /*
  109. * // Actual Iteration Point Visualization g2.setColor(Color.RED);
  110. * if (arrayOfValue != null) { for (int i = 0; i <
  111. * arrayOfValue.length; i++) { g2.fillOval((int) (i * width /
  112. * (model.getIterations() - 1) * scaleX - recSize.getX() / 2), (int)
  113. * (convertToCanvasY((int) arrayOfValue[i]) * scaleY -
  114. * recSize.getY() / 2), (int) recSize.getX(), (int) recSize.getY());
  115. * } }
  116. */
  117. } else if (isSwitch) {
  118. if (arrayOfBooleans != null) {
  119. // array fillen
  120. fillArrayofBooleans();
  121. // Draw the Lines
  122. g2.setStroke(new BasicStroke(2));
  123. g2.setColor(Color.BLACK);
  124. for (int i = 0; i < pointList.size() - 1; i++) {
  125. line = new Line2D.Double(pointList.get(i).getX() * scaleX, pointList.get(i).getY() * scaleY,
  126. pointList.get(i + 1).getX() * scaleX, pointList.get(i + 1).getY() * scaleY);
  127. graphCurve.append(line, true);
  128. }
  129. g2.draw(graphCurve);
  130. /*
  131. * // Draw the Points g2.setColor(Color.BLUE); for (int i = 0; i
  132. * < pointList.size() - 0; i++) { g2.fillOval((int)
  133. * (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
  134. * (int) (pointList.get(i).getY() * scaleY - recSize.getY() /
  135. * 2), (int) recSize.getX(), (int) recSize.getY()); }
  136. */
  137. // Iteration Value
  138. if (arrayOfBooleans != null) {
  139. g2.drawString("" + arrayOfBooleans[model.getCurIteration()],
  140. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1) + 2,
  141. this.getHeight() - 10);
  142. }
  143. }
  144. }
  145. // Iteration Line
  146. g2.setColor(Color.BLUE);
  147. g2.setStroke(new BasicStroke(1));
  148. g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
  149. (model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
  150. }
  151. @Override
  152. public void mouseDragged(MouseEvent e) {
  153. if (isElement) {
  154. elementDragged(e);
  155. } else if (isSwitch) {
  156. switchDragged(e);
  157. }
  158. }
  159. /**
  160. * Wenn ein Punkt bei einem HolonElement gedragged wird
  161. *
  162. * @param e
  163. */
  164. public void elementDragged(MouseEvent e) {
  165. if (pointDrag && tempP != null) {
  166. // Out of Bounds verhindern
  167. int i = pointList.indexOf(tempP);
  168. x = e.getX() / scaleX;
  169. y = e.getY() / scaleY;
  170. // y
  171. if (e.getY() <= 0) {
  172. y = 0 / scaleY;
  173. } else if (this.getHeight() <= e.getY()) {
  174. y = this.getHeight() / scaleY;
  175. }
  176. // x
  177. if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
  178. || pointList.get(i - 1).getX() >= x) {
  179. x = tempP.getX();
  180. }
  181. tempP.setLocation(x, y);
  182. repaint();
  183. }
  184. }
  185. /**
  186. * Wenn ein Punkt bei einem CpsSwitch gedragged wird
  187. *
  188. * @param e
  189. */
  190. public void switchDragged(MouseEvent e) {
  191. if (pointDrag && tempP != null && tempP != pointList.getFirst() && tempP != pointList.getLast()) {
  192. int i = pointList.indexOf(tempP);
  193. x = e.getX() / scaleX;
  194. if (pointList.get(i + 1).getY() == tempP.getY()) {
  195. // x
  196. if (pointList.get(i + 1).getX() <= x + 1 || pointList.get(i - 2).getX() >= x - 1) {
  197. x = tempP.getX();
  198. }
  199. pointList.get(i - 1).setLocation(x, pointList.get(i - 1).getY());
  200. } else {
  201. // x
  202. if (pointList.get(i + 2).getX() <= x + 1 || pointList.get(i - 1).getX() >= x - 1) {
  203. x = tempP.getX();
  204. }
  205. pointList.get(i + 1).setLocation(x, pointList.get(i + 1).getY());
  206. }
  207. tempP.setLocation(x, tempP.getY());
  208. repaint();
  209. }
  210. }
  211. @Override
  212. public void mouseMoved(MouseEvent e) {
  213. }
  214. @Override
  215. public void mouseClicked(MouseEvent e) {
  216. }
  217. @Override
  218. public void mouseEntered(MouseEvent e) {
  219. }
  220. @Override
  221. public void mouseExited(MouseEvent e) {
  222. }
  223. @Override
  224. public void mousePressed(MouseEvent e) {
  225. if (isElement) {
  226. elementPressed(e);
  227. } else if (isSwitch) {
  228. switchPressed(e);
  229. }
  230. }
  231. /**
  232. * Wenn ein Punkt von einem Element gedrueckt wird
  233. *
  234. * @param e
  235. */
  236. public void elementPressed(MouseEvent e) {
  237. boolean added = false;
  238. boolean deletePoint = false;
  239. double x = e.getX() / scaleX;
  240. double y = e.getY() / scaleY;
  241. // Click on Point
  242. tempP = null;
  243. if (pointList != null) {
  244. // look if a point was clicked
  245. for (Point p : pointList) {
  246. if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
  247. && x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
  248. if (e.getButton() == MouseEvent.BUTTON3) {
  249. tempP = p;
  250. deletePoint = true;
  251. } else {
  252. pointDrag = true;
  253. tempP = p;
  254. }
  255. }
  256. }
  257. // New Point
  258. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
  259. && e.getX() != this.getWidth() / scaleX) {
  260. for (int i = 0; i < pointList.size(); i++) {
  261. if (x < pointList.get(i).getX() && !added) {
  262. if (e.getY() <= 0) {
  263. pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
  264. } else {
  265. pointList.add(i, new Point((int) (x), (int) y));
  266. }
  267. added = true;
  268. pointDrag = true;
  269. tempP = pointList.get(i);
  270. }
  271. }
  272. }
  273. // Delete a Point
  274. if (deletePoint && tempP.getX() != 0
  275. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  276. pointList.remove(tempP);
  277. }
  278. repaint();
  279. }
  280. }
  281. /**
  282. * Wenn ein Punkt von einem Switch gedr�ck wird
  283. *
  284. * @param e
  285. */
  286. public void switchPressed(MouseEvent e) {
  287. boolean added = false;
  288. boolean deletePoint = false;
  289. double x = e.getX() / scaleX;
  290. double y = e.getY() / scaleY;
  291. // Halbe Iterations Distanz
  292. double dist = (width / (model.getIterations() - 1)) / 2;
  293. // Click on Point
  294. tempP = null;
  295. if (pointList != null) {
  296. for (Point p : pointList) {
  297. if (x >= p.getX() - dist*2 && x <= p.getX() + dist*2) {
  298. if (e.getButton() == MouseEvent.BUTTON3) {
  299. tempP = p;
  300. deletePoint = true;
  301. } else {
  302. pointDrag = true;
  303. tempP = p;
  304. }
  305. }
  306. }
  307. // New Point
  308. if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && x != 0 && x != width) {
  309. for (int i = 0; i < pointList.size() && !added; i++) {
  310. if (x < pointList.get(i).getX() - dist) {
  311. // Punkte hinzufügen, je nachdem ob true oder false
  312. double p1 = pointList.get(i - 1).getX();
  313. double p2 = pointList.get(i).getX();
  314. if (pointList.get(i - 1).getY() != 0 && pointList.get(i).getY() != 0) {
  315. pointList.add(i, new Point((int) ((x+p2)/2 + dist), (int) height - 1));
  316. pointList.add(i, new Point((int) ((x+p2)/2 + dist), 0));
  317. pointList.add(i, new Point((int) ((x+p1)/2 - dist), 0));
  318. pointList.add(i, new Point((int) ((x+p1)/2- dist), (int) height - 1));
  319. added = true;
  320. } else if (pointList.get(i - 1).getY() == 0 && pointList.get(i).getY() == 0) {
  321. pointList.add(i, new Point((int) ((x+p2)/2 + dist), 0));
  322. pointList.add(i, new Point((int) ((x+p2)/2 + dist), (int) height - 1));
  323. pointList.add(i, new Point((int) ((x+p1)/2 - dist), (int) height - 1));
  324. pointList.add(i, new Point((int) ((x+p1)/2 - dist), 0));
  325. added = true;
  326. }
  327. }
  328. }
  329. }
  330. // Delete a Point
  331. if (deletePoint && tempP.getX() != 0
  332. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  333. int i = pointList.indexOf(tempP);
  334. if (tempP.getY() == 0) {
  335. pointList.remove(i);
  336. pointList.remove(i-1);
  337. pointList.remove(i-2);
  338. pointList.remove(i-3);
  339. }
  340. if (tempP.getY() == height-1) {
  341. pointList.remove(i+2);
  342. pointList.remove(i+1);
  343. pointList.remove(i);
  344. pointList.remove(i-1);
  345. }
  346. }
  347. repaint();
  348. }
  349. // TODO Siwtch pressed zeugs hier hin
  350. }
  351. @Override
  352. public void mouseReleased(MouseEvent e) {
  353. if (pointDrag) {
  354. pointDrag = false;
  355. tempP = null;
  356. }
  357. if (isElement) {
  358. // array fillen
  359. fillArrayofValue();
  360. }
  361. }
  362. public void componentResized(ComponentEvent e) {
  363. // Wenn ein anderes Element genommen wird
  364. if (init) {
  365. init = false;
  366. // for scale on the first initialisation
  367. if (width == -1 && height == -1) {
  368. width = this.getWidth();
  369. height = this.getHeight();
  370. }
  371. scaleX = this.getWidth() / width;
  372. scaleY = this.getHeight() / height;
  373. }
  374. // Scale
  375. scaleX = this.getWidth() / width;
  376. scaleY = this.getHeight() / height;
  377. repaint();
  378. }
  379. @Override
  380. public void componentHidden(ComponentEvent e) {
  381. }
  382. @Override
  383. public void componentMoved(ComponentEvent e) {
  384. }
  385. @Override
  386. public void componentShown(ComponentEvent e) {
  387. }
  388. /*
  389. * Emptys the Graph
  390. */
  391. public void empty() {
  392. pointList = null;
  393. tempElements = null;
  394. tempSwitch = null;
  395. arrayOfFloats = null;
  396. arrayOfBooleans = null;
  397. isSwitch = false;
  398. isElement = false;
  399. repaint();
  400. }
  401. /*
  402. * Resets the Points for the Element
  403. */
  404. public void reset() {
  405. pointList.removeAll(pointList);
  406. pointList.addFirst(new Point(0, 0));
  407. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  408. repaint();
  409. }
  410. /**
  411. * converts the number to fit the canvas
  412. *
  413. * @param double
  414. * d, the number to convert
  415. * @return the converted number
  416. */
  417. public double convertToCanvasY(float d) {
  418. return (height - (d * (height / MAXIMUM)));
  419. }
  420. /**
  421. * converts the number to fit the value
  422. *
  423. * @param double
  424. * d, the number to convert
  425. * @return the converted number
  426. */
  427. public float convertToValueY(double d) {
  428. return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
  429. }
  430. /**
  431. * Visualize the HolonElement on the Graph
  432. *
  433. * @param HolonElement
  434. * ele, which should be visualized
  435. */
  436. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  437. arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getEnergyAt();
  438. tempElements = selectedElement;
  439. pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  440. isSwitch = false;
  441. isElement = true;
  442. MAXIMUM = selectedElement.get(selectedElement.size() - 1).getEnergy();
  443. // First time clicked on the Element
  444. if (pointList.isEmpty()) {
  445. pointList.addFirst(new Point(0, 0));
  446. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  447. }
  448. repaint();
  449. }
  450. /**
  451. * Visualize the HolonElement on the Graph
  452. *
  453. * @param HolonElement
  454. * ele, which should be visualized
  455. */
  456. public void repaintWithNewSwitch(HolonSwitch s) {
  457. arrayOfBooleans = s.getActiveAt();
  458. tempSwitch = s;
  459. pointList = s.getGraphPoints();
  460. isSwitch = true;
  461. isElement = false;
  462. // First time clicked on the Element
  463. if (pointList.isEmpty()) {
  464. pointList.addFirst(new Point(0, 0));
  465. pointList.addLast(new Point((int) (width), 0));
  466. }
  467. repaint();
  468. }
  469. /**
  470. * Build a Curve for the Graph
  471. *
  472. * @param Point,Point
  473. * ,startpoint p1 and endpoint p2
  474. *
  475. * @return CubicCurve2D, c, the CubicCurve2D for the Graph
  476. */
  477. public CubicCurve2D buildCurve(Point p1, Point p2) {
  478. x1 = (int) p1.getX();
  479. y1 = (int) p1.getY();
  480. x2 = (int) p2.getX();
  481. y2 = (int) p2.getY();
  482. // calculate the controllpoints
  483. ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
  484. ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
  485. if (y1 < y2) {
  486. ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
  487. ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
  488. } else {
  489. ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
  490. ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
  491. }
  492. // set the curve
  493. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  494. x2 * scaleX, y2 * scaleY);
  495. return c;
  496. }
  497. /**
  498. * Fills the Arrays with booleans
  499. */
  500. public void fillArrayofBooleans() {
  501. for (int i = 0; i < arrayOfBooleans.length; i++) {
  502. int t = (int) getYValueAt_2((int) (i * width / (model.getIterations() - 1)));
  503. if (t == 0) {
  504. arrayOfBooleans[i] = true;
  505. } else {
  506. arrayOfBooleans[i] = false;
  507. }
  508. }
  509. }
  510. /**
  511. * Fills the Arrays of each HolonElement
  512. */
  513. @SuppressWarnings("unchecked")
  514. public void fillArrayofValue() {
  515. for (HolonElement he : tempElements) {
  516. MAXIMUM = he.getEnergy();
  517. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  518. for (int i = 0; i < arrayOfFloats.length; i++) {
  519. he.getEnergyAt()[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
  520. }
  521. arrayOfFloats = he.getEnergyAt();
  522. }
  523. }
  524. /**
  525. *
  526. * @param xVal,
  527. * the x value for the y value
  528. * @return y, the value at x
  529. */
  530. public float getYValueAt(int xVal) {
  531. for (int i = 0; i < pointList.size() - 1; i++) {
  532. // get the Points
  533. if (xVal <= pointList.get(i + 1).getX()) {
  534. // Curve erstellen
  535. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  536. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  537. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  538. return getIntersectionPoint(l1, l2);
  539. }
  540. }
  541. return 0;
  542. }
  543. /**
  544. *
  545. * @param xVal,
  546. * the x value for the y value
  547. * @return y, the value at x
  548. */
  549. public float getYValueAt_2(int xVal) {
  550. for (int i = 0; i < pointList.size() - 1; i++) {
  551. // get the Points
  552. if (xVal >= pointList.get(i).getX()) {
  553. // Curve erstellen
  554. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  555. c.subdivide(cl, cr);
  556. // Teil der Kurve aussuchen
  557. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  558. c = cl;
  559. // Kurve Links von "unten"
  560. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  561. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  562. if (c.contains(xVal * scaleX, j * scaleY)) {
  563. return (float) (j);
  564. }
  565. }
  566. } else {// Kurve Links von "oben"
  567. for (float j = 0; j < height; j += 0.1f) {
  568. if (c.contains(xVal * scaleX, j * scaleY)) {
  569. return (float) (j);
  570. }
  571. }
  572. }
  573. } else {
  574. c = cr;
  575. // Kurve Links von "unten"
  576. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  577. for (float j = 0; j < height; j += 0.1f) {
  578. if (c.contains(xVal * scaleX, j * scaleY)) {
  579. return (float) (j);
  580. }
  581. }
  582. } else {// Kurve Links von "oben"
  583. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  584. if (c.contains(xVal * scaleX, j * scaleY)) {
  585. return (float) (j);
  586. }
  587. }
  588. }
  589. }
  590. }
  591. }
  592. return getYValueAt(xVal);
  593. }
  594. /**
  595. *
  596. * @param l1,
  597. * the first Line
  598. * @param l2,
  599. * the second Line
  600. *
  601. * @return The Intersection Point
  602. */
  603. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  604. if (!l1.intersectsLine(l2)) {
  605. return 0;// null;
  606. }
  607. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  608. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  609. double det = sx * ry - sy * rx;
  610. if (det == 0) {
  611. return 0;// null;
  612. } else {
  613. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  614. if (z < 0 || z > 1) {
  615. return 0;// new Point(0, 0); // intersection at end point!
  616. }
  617. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  618. // (py + z * ry));
  619. }
  620. } // end intersection line-line
  621. }