0% found this document useful (0 votes)
4 views3 pages

Line Intersection and Overlap Analysis

This document provides an algorithm to determine if two lines are collinear, parallel, or intersecting using their slope-intercept equations. It describes: 1) Computing the slopes (m1, m2) and y-intercepts (b1, b2) of the two lines from input points 2) Checking if the lines are collinear by comparing slopes and intercepts 3) Checking if the lines are parallel by comparing only slopes 4) If intersecting, solving the equations simultaneously to find the intersection point The algorithm is implemented in a Java program that takes point inputs, computes the line equations, and outputs the relationship between the lines along with any relevant values like the intersection point or overlapping length.

Uploaded by

Saurav Somesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Line Intersection and Overlap Analysis

This document provides an algorithm to determine if two lines are collinear, parallel, or intersecting using their slope-intercept equations. It describes: 1) Computing the slopes (m1, m2) and y-intercepts (b1, b2) of the two lines from input points 2) Checking if the lines are collinear by comparing slopes and intercepts 3) Checking if the lines are parallel by comparing only slopes 4) If intersecting, solving the equations simultaneously to find the intersection point The algorithm is implemented in a Java program that takes point inputs, computes the line equations, and outputs the relationship between the lines along with any relevant values like the intersection point or overlapping length.

Uploaded by

Saurav Somesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Computer Programming(CSE1001)

Lab Test (Set- E)


Problem Statement: To test if two straight lines are co-linear, parallel or intersecting. If they
are intersecting lines then we need to find their intersection point, and if lines are co-linear then
find the length of overlapping portion.

Description:
The general equation of a line is usually written as y=mx + b where m is the slope of the line and
b is the intercept. Slope is how much the vertical (y) coordinate changes for each unit of change
in the horizontal direction (x). Intercept is where the line crosses the y-axis, i.e. the value of y
when x=0.
If we know two points on a line (x1,y1) and (x2,y2), we can determine m and b as follows. From
its definition, change in y divided by change in x =
m = (y2-y1)/(x2-x1).
And since b=y-mx for any point on the line, we can compute
b1=y1-mx1 and b2=y2-mx2
With two line segments, we can derive an equation for each, say
y=m1x+b1 and y=m2x+b2.
The x and y values at the intersection point must satisfy both, so
y = m1x+b1=m2x+b2.
solving for x, we get m1x+b1=m2x+b2
=> (m1-m2)x=b2-b1
=> x=(b2-b1)/(m1-m2).
Putting x in y=m1x+b1 we can get
y=m1*((b2-b1)/(m1-m2)) + b1
Notice that when m1 equals m2, the lines are parallel and they will never intersect. (unless b1
also equals b2 in which case the lines are co-linear).

co-linear lines

intersecting lines

parallel lines

Algorithm Development:
In this assignment you need to do the following things:

[1 points] Input two points on line1 (x1,y1) and (x2,y2) and two points on line2 (u1,v1)
and (u2,v2)
[2 points] Find slopes m1 and m2 using a method findSlope(). Header of the method can
be
public static double findSlope(int x1,int y1,int x2,int y2){...}

[2 points] Find b1 and b2 using a method findB(). Header of the method can be
public static double findB(int x1,int y1,double m){...}

[4 points] check if lines are co-linear


check if lines are overlapping
find co-ordinates of two end points of the overlapping portion
find length of the overlapping portion using method findLength()
public static double findLength(int x1,int y1,int x2,int y2){...}
[2 points] check if lines are parallel
[4 points] otherwise lines are intersecting,
find intersecting point (x,y) using the above method described.

Sample Runs:
Run1:

Input 2 points on line1 (x1,y1),(x2,y2)


4 2 2 0

Input 2 points on line2 (u1,v1),(u2,v2)


0 4 4 0
Lines are intersecting and
intersection point is (3,1)

Run2:
Input 2 points on line1 (x1,y1),(x2,y2)
0 0 1 1
Input 2 points on line2 (u1,v1),(u2,v2)
2 2 3 3
Lines are co-linear and
length of overlapping portion is 0.0

Run3:
Input 2 points on line1 (x1,y1),(x2,y2)
1 1 2 2
Input 2 points on line2 (u1,v1),(u2,v2)
1 2 2 3
Lines are parallel

[In this assignment you need to modify and complete the [Link]
file provided to you.]

You might also like