UnitGraph.java 19 KB

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