Extend and merge intersecting lines - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Extend and merge intersecting lines

Demo Code


//package com.java2s;

import java.awt.geom.Line2D;

import java.awt.geom.Point2D;

import java.util.List;

public class Main {
    /**//  w w  w .  j a v  a2  s .co  m
     * Extend and merge intersecting lines
     * 
     * @param intersectLines
     * @param start
     * @param end
     * @return
     */
    public static List<Line2D> extendLine(List<Line2D> intersectLines,
            Point2D start, Point2D end) {
        boolean addLine = false;
        for (Line2D il : intersectLines) {
            if (!il.intersectsLine(new Line2D.Double(start, end))) {
                addLine = true;
            }
            if (start.getY() <= il.getY1() && end.getY() >= il.getY1()
                    && end.getY() <= il.getY2()) {
                il.setLine(start, il.getP2());
                addLine = false;
                mergeLine(intersectLines);
                break;
            }

            else if (start.getY() >= il.getY1()
                    && start.getY() <= il.getY2()
                    && end.getY() >= il.getY2()) {
                il.setLine(il.getP1(), end);
                addLine = false;
                mergeLine(intersectLines);
                break;
            }

            else if (il.getY1() >= start.getY() && il.getY2() <= end.getY()) {
                il.setLine(start, end);
                addLine = false;
                mergeLine(intersectLines);
                break;
            }

            else if (!(il.getY2() < start.getY() || il.getY1() > end.getY())) {
                addLine = false;
            }

        }

        if (intersectLines.isEmpty() || addLine) {
            intersectLines.add(new Line2D.Double(start, end));
            mergeLine(intersectLines);
            // System.out.print(addLine+"\n");
        }

        return intersectLines;
    }

    /**
     * Merge overlapping lines
     * 
     * @param intersectLines
     * @return
     */
    public static List<Line2D> mergeLine(List<Line2D> intersectLines) {
        boolean merged = false;
        for (int i = 0; i < intersectLines.size();) {
            for (int j = i + 1; j < intersectLines.size(); j++) {
                Line2D l1 = intersectLines.get(i);
                Line2D l2 = intersectLines.get(j);
                if (l1.getY1() <= l2.getY1() && l1.getY2() >= l2.getY1()
                        && l1.getY2() <= l2.getY2()) {
                    l2.setLine(l1.getP1(), l2.getP2());
                    intersectLines.remove(i);
                    // System.out.print("remove : " + i + "\n");
                    merged = true;
                    break;
                }

                else if (l1.getY1() >= l2.getY1()
                        && l1.getY1() <= l2.getY2()
                        && l1.getY2() >= l2.getY2()) {
                    l2.setLine(l2.getP1(), l1.getP2());
                    intersectLines.remove(i);
                    // System.out.print("remove : " + i + "\n");
                    merged = true;
                    break;
                }

                else if (l2.getY1() >= l1.getY1()
                        && l2.getY2() <= l1.getY2()) {
                    l2.setLine(l1);
                    // mergeLine(intersectLines);
                    intersectLines.remove(i);
                    // System.out.print("remove : " + i + "\n");
                    merged = true;
                    break;
                }

                else if (l1.getY1() >= l2.getY1()
                        && l1.getY2() <= l2.getY2()) {
                    intersectLines.remove(i);
                    merged = true;
                    break;
                }
            }
            if (merged == true) {
                i = 0;
                merged = false;
            } else
                i++;
        }

        return intersectLines;
    }
}

Related Tutorials