UnitGraph.java 21 KB

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