UnitGraph.java 20 KB

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