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 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() != 0 && pointList.get(i).getY() != 0) {
  358. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) height - 1));
  359. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), 0));
  360. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), 0));
  361. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) height - 1));
  362. added = true;
  363. } else if (pointList.get(i - 1).getY() == 0 && pointList.get(i).getY() == 0) {
  364. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), 0));
  365. pointList.add(i, new Point((int) ((x + p2) / 2 + dist), (int) height - 1));
  366. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), (int) height - 1));
  367. pointList.add(i, new Point((int) ((x + p1) / 2 - dist), 0));
  368. added = true;
  369. }
  370. }
  371. }
  372. }
  373. // Delete a Point
  374. if (deletePoint && tempP.getX() != 0
  375. && (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
  376. int i = pointList.indexOf(tempP);
  377. if (tempP.getY() == 0) {
  378. pointList.remove(i);
  379. pointList.remove(i - 1);
  380. pointList.remove(i - 2);
  381. pointList.remove(i - 3);
  382. }
  383. if (tempP.getY() == height - 1) {
  384. pointList.remove(i + 2);
  385. pointList.remove(i + 1);
  386. pointList.remove(i);
  387. pointList.remove(i - 1);
  388. }
  389. }
  390. repaint();
  391. }
  392. // TODO Siwtch pressed zeugs hier hin
  393. }
  394. @Override
  395. public void mouseReleased(MouseEvent e) {
  396. if (pointDrag) {
  397. pointDrag = false;
  398. tempP = null;
  399. }
  400. /**
  401. * reset the dragInformation.
  402. */
  403. dragInformation = "";
  404. repaint();
  405. }
  406. /**
  407. * When the Component is Resized.
  408. *
  409. * @param e
  410. * ComponentEvent
  411. */
  412. public void componentResized(ComponentEvent e) {
  413. // Wenn ein anderes Element genommen wird
  414. if (init) {
  415. init = false;
  416. // for scale on the first initialisation
  417. if (width == -1 && height == -1) {
  418. width = this.getWidth();
  419. height = this.getHeight();
  420. }
  421. scaleX = this.getWidth() / width;
  422. scaleY = this.getHeight() / height;
  423. }
  424. // Scale
  425. scaleX = this.getWidth() / width;
  426. scaleY = this.getHeight() / height;
  427. repaint();
  428. }
  429. @Override
  430. public void componentHidden(ComponentEvent e) {
  431. }
  432. @Override
  433. public void componentMoved(ComponentEvent e) {
  434. }
  435. @Override
  436. public void componentShown(ComponentEvent e) {
  437. }
  438. /**
  439. * Empty the Graph.
  440. */
  441. public void empty() {
  442. pointList = null;
  443. tempElements = null;
  444. arrayOfFloats = null;
  445. arrayOfBooleans = null;
  446. isSwitch = false;
  447. isElement = false;
  448. repaint();
  449. }
  450. /**
  451. * Resets the Points for the Element.
  452. */
  453. public void reset() {
  454. pointList.removeAll(pointList);
  455. pointList.addFirst(new Point(0, 0));
  456. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  457. repaint();
  458. }
  459. /**
  460. * converts the number to fit the canvas.
  461. *
  462. * @param d
  463. * the number to convert
  464. * @return the converted number
  465. */
  466. public double convertToCanvasY(float d) {
  467. return (height - (d * (height / maximum)));
  468. }
  469. /**
  470. * converts the number to fit the value.
  471. *
  472. * @param d
  473. * the number to convert
  474. * @return the converted number
  475. */
  476. public float convertToValueY(double d) {
  477. return (float) Math.round(((height - (height * (d / height))) / (height / maximum)) * 10) / 10;
  478. }
  479. /**
  480. * Visualize the HolonElement on the Graph.
  481. *
  482. * @param selectedElement
  483. * which should be visualized
  484. */
  485. public void repaintWithNewElement(ArrayList<HolonElement> selectedElement) {
  486. arrayOfFloats = selectedElement.get(selectedElement.size() - 1).getEnergyAt();
  487. tempElements = selectedElement;
  488. pointList = selectedElement.get(selectedElement.size() - 1).getGraphPoints();
  489. isSwitch = false;
  490. isElement = true;
  491. maximum = selectedElement.get(selectedElement.size() - 1).getEnergy();
  492. // First time clicked on the Element
  493. if (pointList.isEmpty()) {
  494. pointList.addFirst(new Point(0, 0));
  495. pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
  496. }
  497. repaint();
  498. }
  499. /**
  500. * Visualize the Switch on the Graph.
  501. *
  502. * @param s
  503. * which should be visualized
  504. */
  505. public void repaintWithNewSwitch(HolonSwitch s) {
  506. arrayOfBooleans = s.getActiveAt();
  507. pointList = s.getGraphPoints();
  508. isSwitch = true;
  509. isElement = false;
  510. // First time clicked on the Element
  511. if (pointList.isEmpty()) {
  512. pointList.addFirst(new Point(0, 0));
  513. pointList.addLast(new Point((int) (width), 0));
  514. }
  515. repaint();
  516. }
  517. /**
  518. * Build a Curve for the Graph.
  519. *
  520. * @param p1
  521. * startpoint
  522. * @param p2
  523. * endpoint
  524. *
  525. * @return the CubicCurve2D for the Graph
  526. */
  527. public CubicCurve2D buildCurve(Point p1, Point p2) {
  528. x1 = (int) p1.getX();
  529. y1 = (int) p1.getY();
  530. x2 = (int) p2.getX();
  531. y2 = (int) p2.getY();
  532. // calculate the controllpoints
  533. ctrlx1 = (int) p1.getX() + ((int) p2.getX() - (int) p1.getX()) / 2;
  534. ctrlx2 = (int) p2.getX() - ((int) p2.getX() - (int) p1.getX()) / 2;
  535. if (y1 < y2) {
  536. ctrly1 = (int) p1.getY() + ((int) p2.getY() - (int) p1.getY()) / 10;
  537. ctrly2 = (int) p2.getY() - ((int) p2.getY() - (int) p1.getY()) / 10;
  538. } else {
  539. ctrly1 = (int) p1.getY() - ((int) p1.getY() - (int) p2.getY()) / 10;
  540. ctrly2 = (int) p2.getY() + ((int) p1.getY() - (int) p2.getY()) / 10;
  541. }
  542. // set the curve
  543. c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
  544. x2 * scaleX, y2 * scaleY);
  545. return c;
  546. }
  547. /**
  548. * Fills the Arrays with booleans.
  549. */
  550. public void fillArrayofBooleans() {
  551. for (int i = 0; i < arrayOfBooleans.length; i++) {
  552. int t = (int) getYValueAt2((int) (i * width / (model.getIterations() - 1)));
  553. if (t == 0) {
  554. arrayOfBooleans[i] = true;
  555. } else {
  556. arrayOfBooleans[i] = false;
  557. }
  558. }
  559. }
  560. /**
  561. * Fills the Arrays of each HolonElement.
  562. */
  563. @SuppressWarnings("unchecked")
  564. public void fillArrayofValue() {
  565. for (HolonElement he : tempElements) {
  566. maximum = he.getEnergy();
  567. he.setGraphPoints((LinkedList<Point>) pointList.clone());
  568. for (int i = 0; i < arrayOfFloats.length; i++) {
  569. he.getEnergyAt()[i] = convertToValueY(getYValueAt2((int) (i * width / (model.getIterations() - 1))));
  570. }
  571. arrayOfFloats = he.getEnergyAt();
  572. }
  573. }
  574. /**
  575. * Get the Y Value at the x Coordination.
  576. *
  577. * @param xVal
  578. * the x value for the y value
  579. * @return y, the value at x
  580. */
  581. public float getYValueAt(int xVal) {
  582. for (int i = 0; i < pointList.size() - 1; i++) {
  583. // get the Points
  584. if (xVal <= pointList.get(i + 1).getX()) {
  585. // Curve erstellen
  586. Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
  587. pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
  588. Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
  589. return getIntersectionPoint(l1, l2);
  590. }
  591. }
  592. return 0;
  593. }
  594. /**
  595. * Get y value at the x Coordination via curves.
  596. *
  597. * @param xVal
  598. * the x value for the y value
  599. * @return y value at x
  600. */
  601. public float getYValueAt2(int xVal) {
  602. for (int i = 0; i < pointList.size() - 1; i++) {
  603. // get the Points
  604. if (xVal >= pointList.get(i).getX()) {
  605. // Curve erstellen
  606. c = buildCurve(pointList.get(i), pointList.get(i + 1));
  607. c.subdivide(cl, cr);
  608. // Teil der Kurve aussuchen
  609. if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
  610. c = cl;
  611. // Kurve Links von "unten"
  612. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  613. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  614. if (c.contains(xVal * scaleX, j * scaleY)) {
  615. return (float) (j);
  616. }
  617. }
  618. } else {// Kurve Links von "oben"
  619. for (float j = 0; j < height; j += 0.1f) {
  620. if (c.contains(xVal * scaleX, j * scaleY)) {
  621. return (float) (j);
  622. }
  623. }
  624. }
  625. } else {
  626. c = cr;
  627. // Kurve Links von "unten"
  628. if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
  629. for (float j = 0; j < height; j += 0.1f) {
  630. if (c.contains(xVal * scaleX, j * scaleY)) {
  631. return (float) (j);
  632. }
  633. }
  634. } else {// Kurve Links von "oben"
  635. for (float j = (float) (height - 1); j >= 0; j -= 0.1f) {
  636. if (c.contains(xVal * scaleX, j * scaleY)) {
  637. return (float) (j);
  638. }
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return getYValueAt(xVal);
  645. }
  646. /**
  647. * Get the Intersection Point of 2 Lines.
  648. *
  649. * @param l1
  650. * the first Line
  651. * @param l2
  652. * the second Line
  653. *
  654. * @return The Intersection Point
  655. */
  656. public float getIntersectionPoint(Line2D l1, Line2D l2) {
  657. if (!l1.intersectsLine(l2)) {
  658. return 0;// null;
  659. }
  660. double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
  661. double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
  662. double det = sx * ry - sy * rx;
  663. if (det == 0) {
  664. return 0;// null;
  665. } else {
  666. double z = (sx * (qy - py) + sy * (px - qx)) / det;
  667. if (z < 0 || z > 1) {
  668. return 0;// new Point(0, 0); // intersection at end point!
  669. }
  670. return (float) (py + z * ry);// new Point((int) (px + z * rx), (int)
  671. // (py + z * ry));
  672. }
  673. } // end intersection line-line
  674. }