UnitGraph.java 19 KB

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