Menu
Question Index
...


Write a program to get the intersection areas between two lines. If the points of the line are {x1, y1}, {x2, y2}, {x3, y3} then line is given as {x1, y1, x2, y2, x3, y3....}. The intersection areas should also be given as lines. It is assumed the last point in the line is joined to the first point of the line to form an area.



The above image shows the points corresponding to the first example in the requirements table below. The grey coloured areas in the image are the intersection areas.

Input (int[], int[]) Output (int[][])
{5, 200, 10, 143, 15, 204, 20, 267, 25, 260, 30, 100, 35, 178}, {5, 153, 10, 75, 15, 302, 20, 456, 25, 155, 30, 200, 35, 54} {{12, 167, 15, 204, 20, 267, 23, 262, 20, 456, 15, 302},
{23, 262, 25, 260, 27, 178, 25, 155},
{27, 178, 30, 100, 32, 134, 30, 200}}
{5, 200, 10, 143, 15, 204, 20, 267} and {5, 255, 10, 75, 15, 302, 20, 100} {{7, 174, 10, 143, 12, 167, 10, 75},
{12, 167, 15, 204, 16, 227, 15, 302}}
{5, 200, 10, 143, 25, 260, 30, 10, 35, 178} and {5, 153, 10, 75, 15, 302, 20, 456, 25, 155, 35, 54} {{11, 157, 23, 247, 20, 456, 15, 302},
{23, 247, 25, 260, 27, 128, 25, 155},
{27, 128, 30, 10, 32, 82}}
{1000, 30, 1006, 20, 1014, 40, 1016, 20} and {1000, 20, 1002, 40, 1004, 20, 1006, 10, 1008, 30, 1010, 40, 1012, 20, 1014, 50} {{1000, 28, 1003, 24, 1002, 40},
{1003, 24, 1006, 20, 1007, 23, 1006, 10, 1004, 20},
{1007, 23, 1010, 32, 1008, 30},
{1010, 32, 1013, 38, 1012, 20}}
{50, 50, 100, 100, 150, 50} and {50, 100, 100, 50, 150, 100} {{75, 75, 100, 100, 125, 75, 100, 50}}
{50, 50, 100, 100, 150, 50} and {50, 50, 100, 25, 150, 50} {{50, 50, 100, 100, 150, 50, 100, 25}}
{10, 50, 100, 50} and {15, 10, 20, 75, 25, 120, 27, 150, 29, 55, 30, 250, 60, 60, 75, -20} {{18, 50, 61, 50, 60, 60, 30, 250, 29, 55, 27, 150, 25, 120, 20, 75}}

class GetLineIntersections

{    public static void main(String s[])
    {
        int line1[] = {5, 200, 10, 143, 15, 204, 20, 267, 25, 260, 30, 100, 35, 178};
        int line2[] = {5, 153, 10, 75, 15, 302, 20, 456, 25, 155, 30, 200, 35, 54};
        int[][] intersections = getIntersections(line1, line2);
        if (intersections != null) {
            for (int i = 0; i < intersections.length; i++) {
                System.out.print("Intersection area " + (i + 1) + " :- ");
                for (int j = 0; j < intersections[i].length; j += 2) {
                    System.out.print(intersections[i][j] + "~" + intersections[i][j + 1] + ", ");
                }
                System.out.println();
            }
        }

    }


public static int[][] getIntersections(int[] line1, int[] line2) {
//Write code here to get the intersection areas between two lines.
}

//If required write any additional methods here
}
//Define any additional classes here

Doubts

Problems

Topic: Other Advanced Topics In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App