my question is on a java program i have added all instructio

my question is on a java program. i have added all instructions for this homeowrk assignment. from how the input is entered and what the output should read. im seeing if i can get a psuedo code setup for how the algorithm should be setup.

Build an Intersection Check program. User inputs two points as x-y coordinates in twodimension space. Two points represent two end points of a line segment. Then, user can input second two points for the second line segment. With the two line segments, the program checks whether or not the two line segments intersect. The program displays “1” if the two line segments are crossing. Otherwise, display “0”. Please implement your algorithm in Java but not use API. Hint: use slop and intercept

Input: Four x-y coordinates that represent two point for the first line segment and two points for the second line segments

Output: 0 or 1 that represents intersection or no intersection

Example

Input: 7 8 5 12 10 10 3 10

Output: 1

Do you want to test again? Y

Input: 5 10 5 2 2 1 8 7

Output: 1

Do you want to test again? Y

Input: 10 10 3 10 6 3 6 6

Output: 0

Do you want to test again? Y

Input: 5 4 8 6 10 6 5 1

Output: 0

Do you want to test again? N

Thank you!

Solution

Suppose line segment 1 connects (X1,y1), (x2,y2) and line segment 2 connects(x3,y3),(x4,y4).

If sg1 is perpendicular to x axis,we first check x3 and x4. If both values are larger or smaller than sg1\'s x (endx4coordinate,then sg1,sg2 certainly do not intersect. If not calculate the equation f(x)=mx+i for sg2. If point (X1,m*x1+i) at sg2 is also a point of sg1, then answer is true. Else, the answer is false, if sg2 is perpendicular to x axis ,simply replace sg1 and sg2 for this question and check again.

And if both segments are not perpendicular to the x axis, the equation for the line connecting (x1,y1)and (x2,y2) is:

Y={ [(y2-y1/x2-X1)] (x-x1) }+ y1

=[(y2-y1/x2-X1)]x +{( x2y1-y2x1)} /(x2-X1)-----------line (1)

Similarly the equation for the line connecting (x3,y3) and (x4,y4) is

Y= {(y4-y3/x4-x3)}(x-x3) + y3

= {(y4-y3/x4-x3)}x + {(x4y3-y4x3)}/(x4-x3)---------line(2)

If the slopes of two lines are equivalent, sg1 and sg2 do not intersect. If not, then the two lines(1) and(2) must have an intersection at somewhere on the xy coordinate plane.

Calculate the intersection and check whether the point is at the segment.

The x coordination of the intersection of lines 1 and 2 satisfies

0= [ {(y2-y1)/(x2-X1)} - {(y4-y3)/(x4-x3)} ] x + [ {( x2y1-y2x1)/(x2-X1) } - { (x4y3-y4x3)/(x4-x3) }

X = [( x4y3-y4x3)/(x4-x3)] -[ (x2y1-y2x1)/(x2-X1)] / [(y2-y1)/(x2-X1)] - [(y4-y3)/(x4-x3)]

Subsequently check whether this x value is within the range if X11 and x2. If it is, sg1 and sg2 do intersect otherwise they do not intersect.

Package com.pack9e;

Public class point

{

Public double x , y;

Public point(double x , doubley )

{ super();

This.x= x;

This.y= y;

}

}

Package com.packa9e;

Public class segment

{

Public final point start, end;

Public final Boolean is vertical;

Public final double slope, intercept;

Public segment( point start, point end)

{

This.start= start;

This.end=end;

//set isvertical,which indicates whether this line is vertical or not on the coordinate plane

If( start.x==end.x)

Is vertical= 0;

Else

Isvertiacal=1;

//set slope and intercept

If(is vertical){

Slope=(start.y- end.y)/(start.x-end.x)

Intercept=[(End.x*start.y-start.x*end.y)]/(start.x-end.x);}

Else{

Slope=Double.MAX_value;

Intercept=-double.MAX_VALUE;}}}

(Intersectionchecker.java)

Package com.pack9e;

Public class intersection checker{

Public final segment segment1,segment2;

Private Boolean hasintersection;

Public intersectionchecker(segment segment1,segment segment2){

This.segment1= segment1;

This.segment2=segment2;}

Public intersection checker(double X11,double y1, double x2, double y2,doublex3, doubley3 ,doublex4, doubley4){

Point start1= new point(X1,y1);

Point end1=new point (x2,y2);

Point start2=newpoint(x3,y3);

my question is on a java program. i have added all instructions for this homeowrk assignment. from how the input is entered and what the output should read. im
my question is on a java program. i have added all instructions for this homeowrk assignment. from how the input is entered and what the output should read. im
my question is on a java program. i have added all instructions for this homeowrk assignment. from how the input is entered and what the output should read. im

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site