private double calculateDistance(Node node1, Node node2) { double totalDistance = 0; double node1x = node1.getX(); double node1y = node1.getX(); double node2x = node2.getX(); double node2y = node2.getX(); for (double[][][] RADAR_LINE : RADARS) { for (double[][] LINE : RADAR_LINE) { if (Line2D.linesIntersect( LINE[POINT_1][X], LINE[POINT_1][Y], LINE[POINT_2][X], LINE[POINT_2][Y], node1x, node1y, node2x, node2y)) { totalDistance += FEE_IN_RADAR_RANGE; } } } double distanceX = (node1.getX() - node2.getX()); double distanceY = (node1.getY() - node2.getY()); double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY) * FEE_DISTANCE_FACTOR; return distance + totalDistance; }
/** * Finds the intersection of two line segments * * @return EastNorth null if no intersection was found, the EastNorth coordinates of the * intersection otherwise */ public static EastNorth getSegmentSegmentIntersection( EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) { double x1 = p1.getX(); double y1 = p1.getY(); double x2 = p2.getX(); double y2 = p2.getY(); double x3 = p3.getX(); double y3 = p3.getY(); double x4 = p4.getX(); double y4 = p4.getY(); // TODO: do this locally. if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null; // Convert line from (point, point) form to ax+by=c double a1 = y2 - y1; double b1 = x1 - x2; double c1 = x2 * y1 - x1 * y2; double a2 = y4 - y3; double b2 = x3 - x4; double c2 = x4 * y3 - x3 * y4; // Solve the equations double det = a1 * b2 - a2 * b1; if (det == 0) return null; // Lines are parallel double x = (b1 * c2 - b2 * c1) / det; double y = (a2 * c1 - a1 * c2) / det; return new EastNorth(x, y); }
private static Map<SgroupBracket, IBond> bracketBondPairs( Collection<SgroupBracket> brackets, Collection<IBond> bonds) { Map<SgroupBracket, IBond> pairs = new HashMap<>(); for (SgroupBracket bracket : brackets) { IBond crossingBond = null; for (IBond bond : bonds) { IAtom a1 = bond.getAtom(0); IAtom a2 = bond.getAtom(1); if (Line2D.linesIntersect( bracket.getFirstPoint().x, bracket.getFirstPoint().y, bracket.getSecondPoint().x, bracket.getSecondPoint().y, a1.getPoint2d().x, a1.getPoint2d().y, a2.getPoint2d().x, a2.getPoint2d().y)) { // more than one... not good if (crossingBond != null) return new HashMap<>(); crossingBond = bond; } } if (crossingBond == null) return new HashMap<>(); pairs.put(bracket, crossingBond); } return pairs; }
/** Determines if a line from start to end intersects a rectangle */ private boolean intersects(float x1, float y1, float x2, float y2, Rectangle rectangle) { double x = rectangle.min[0]; double y = rectangle.min[1]; if (x1 >= rectangle.min[0] && x1 <= rectangle.max[0] && y1 >= rectangle.min[1] && y1 <= rectangle.max[1]) return true; if (x2 >= rectangle.min[0] && x2 <= rectangle.max[0] && y2 >= rectangle.min[1] && y2 <= rectangle.max[1]) return true; double x3 = rectangle.max[0]; double y3 = rectangle.max[1]; return (Line2D.linesIntersect(x1, y1, x2, y2, x, y, x, y3) || Line2D.linesIntersect(x1, y1, x2, y2, x, y3, x3, y3) || Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x3, y) || Line2D.linesIntersect(x1, y1, x2, y2, x3, y, x, y)); }
public static boolean linesIntersect(Line line1, Line line2) { if (!(line1.equals(line2))) { double x1 = line1.getX1(); double y1 = line1.getY1(); double x2 = line1.getX2(); double y2 = line1.getY2(); double x3 = line2.getX1(); double y3 = line2.getY1(); double x4 = line2.getX2(); double y4 = line2.getY2(); return Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4); } else { return false; } }