UnitGraph.java 21 KB

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