Professional Documents
Culture Documents
Lab Test (Set-E) : Introduction To Computer Programming (CSE1001)
Lab Test (Set-E) : Introduction To Computer Programming (CSE1001)
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){...}
Sample Runs:
Run1:
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 IntersectingLines.java
file provided to you.]