Merge overlapping lines - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Merge overlapping lines

Demo Code


//package com.java2s;

import java.awt.geom.Line2D;

import java.util.List;

public class Main {
    /**// w w  w  . j a  v  a2  s .  c o m
     * 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