UnitGraph.java 19 KB

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