Java version determines whether two lines are intersecting

Source: Internet
Author: User
Tags gety

/**
* Determine whether two lines intersection a line segment 1 start coordinate B line 1 end coordinate C line 2 start coordinate D line 2 end coordinate intersection Coordinate
* Reutrn intersection: 0: two lines parallel-1: not parallel and not intersection 1: two lines Intersection
*/
Public int segintersect (point a, point B, point C, point D ){
Point intersection = new point ();

If (math. ABS (B. gety ()-. gety () + math. ABS (B. getx ()-. getx () + math. ABS (D. gety ()-C. gety ())
+ Math. Abs (D. getx ()-C. getx () = 0 ){
If (C. getx ()-A. getx () + (C. Gety ()-A. Gety () = 0 ){
System. Out. println ("ABCD is the same point! ");
} Else {
System. Out. println ("AB is a vertex, CD is a vertex, And the AC is different! ");
}
Return 0;
}

If (math. Abs (B. Gety ()-A. Gety () + math. Abs (B. getx ()-A. getx () = 0 ){
If (. getx ()-D. getx () * (C. gety ()-D. gety ()-(. gety ()-D. gety () * (C. getx ()-D. getx () = 0 ){
System. Out. println ("A, B is a point, and it is on the CD line segment! ");
} Else {
System. Out. println ("A, B is a point and is not on the CD line segment! ");
}
Return 0;
}
If (math. Abs (D. Gety ()-C. Gety () + math. Abs (D. getx ()-C. getx () = 0 ){
If (D. getx ()-B. getx () * (. gety ()-B. gety ()-(D. gety ()-B. gety () * (. getx ()-B. getx () = 0 ){
System. Out. println ("C, D is a point, and on the AB Line Segment! ");
} Else {
System. Out. println ("C, D is a point and is not on the AB Line Segment! ");
}
Return 0;
}

If (B. gety ()-. gety () * (C. getx ()-D. getx ()-(B. getx ()-. getx () * (C. gety ()-D. gety () = 0 ){
// System. Out. println ("parallel line segments, no intersection point! ");
Return 0;
}

Intersection
. Setx (B. getx ()-A. getx () * (C. getx ()-D. getx ())
* (C. Gety ()-A. Gety ()-C. getx ()
* (B. getx ()-A. getx () * (C. Gety ()-D. Gety () +
. Getx () * (B. Gety ()-A. Gety () * (C. getx ()-D. getx ()))
/(B. Gety ()-A. Gety () * (C. getx ()-D. getx ()-(B
. Getx ()-A. getx () * (C. Gety ()-D. Gety ())));
Intersection
. Sety (B. Gety ()-A. Gety () * (C. Gety ()-D. Gety ())
* (C. getx ()-A. getx ()-C. Gety ()
* (B. Gety ()-A. Gety () * (C. getx ()-D. getx () +
. Gety () * (B. getx ()-A. getx () * (C. Gety ()-D. Gety ()))
/(B. getx ()-A. getx () * (C. Gety ()-D. Gety ()-(B
. Gety ()-A. Gety () * (C. getx ()-D. getx ())));

If (intersection. getx ()-A. getx () * (intersection. getx ()-B. getx () <= 0
& (Intersection. getx ()-C. getx ())
* (Intersection. getx ()-D. getx () <= 0
& (Intersection. Gety ()-A. Gety ())
* (Intersection. Gety ()-B. Gety () <= 0
& (Intersection. Gety ()-C. Gety ())
* (Intersection. Gety ()-D. Gety () <= 0 ){

If (. getx () = C. getx () &. gety () = C. gety () | (. getx () = D. getx () &. gety () = D. gety ())
| (B. getx () = C. getx () & B. gety () = C. gety () | (B. getx () = D. getx () & B. gety () = D. gety ())){

System. Out. println ("intersection of line segments on the endpoint ");
Return 2;

} Else {
System. Out. println ("line segments intersect with points (" + intersection. getx () + ","
+ Intersection. Gety () + ")! ");
Return 1; // 'intersection
}

} Else {
// System. Out. println ("the line segments intersect with virtual intersections (" + intersection. getx () + ","
// + Intersection. Gety () + ")! ");
Return-1; // 'intersection but not on the Line Segment
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.