Poj1410 cleverly turns a straight line into a line segment Intersection

Source: Internet
Author: User

This rough question is still considered to be the intersection of a line segment and a line segment, but the subject requires that the line segment is completely within the rectangle. Therefore, it can be changed to the intersection problem between the straight line of the target line segment a and each line segment of the rectangle, and then check whether the line segment a is within the rectangle range.

 

[Cpp]
# Include <iostream>
Using namespace std;
 
 
Struct Point
{
Point ()
{
X = 0.0f;
Y = 0.0f;
}
 
Point (double tx, double ty)
{
X = tx;
Y = ty;
}
 
Double x, y;
};
 
Struct Segment
{
Point point1, point2;
};
 
Double Min (double a, double B)
{
If (a <B)
Return;
Else
Return B;
}
 
Double Max (double a, double B)
{
If (a <B)
Return B;
Else
Return;
}
 
Double CrossProduct (Point vec1, Point vec2)
{
Return (vec1.x * vec2.y)-(vec1.y * vec2.x );
}
 
// Calculate the crossproduct of (P2-P1) * (p3-p1)
Double Direction (Point point1, Point point2, Point point3)
{
Point vec1;
Vec1.x = point2.x-point1.x;
Vec1.y = point2.y-point1.y;
 
Point vec2;
Vec2.x = point3.x-point1.x;
Vec2.y = point3.y-point1.y;
 
Return CrossProduct (vec1, vec2 );
}
 
// See if line interact the segment.
Bool LineInteractSegment (Segment line, Segment seg)
{
Point vec1;
Vec1.x = line. point2.x-line. point1.x;
Vec1.y = line. point2.y-line. point2.y;
 
Point vec2;
Vec2.x = seg. point2.x-seg. point1.x;
Vec2.y = seg. point2.y-seg. point2.y;
 
Double cross1 = CrossProduct (
Point (line. point2.x-line. point1.x, line. point2.y-line. point1.y ),
Point (seg. point1.x-line. point1.x, seg. point1.y-line. point1.y ));
 
Double cross2 = CrossProduct (
Point (line. point2.x-line. point1.x, line. point2.y-line. point1.y ),
Point (seg. point2.x-line. point1.x, seg. point2.y-line. point1.y ));
 
If (cross1 * cross2 <= 0)
Return true;
Else
Return false;
}
 
Bool SegmentInRect (Segment seg, Point leftTopPoint, Point rightBottomPoint)
{
Double minX = Min (seg. point1.x, seg. point2.x );
Double maxX = Max (seg. point1.x, seg. point2.x );
 
Double minY = Min (seg. point1.y, seg. point2.y );
Double maxY = Max (seg. point1.y, seg. point2.y );
 
If (minX> rightBottomPoint. x | maxX <leftTopPoint. x)
{
Return false;
}
Else
{
If (minY> leftTopPoint. y | maxY <rightBottomPoint. y)
{
Return false;
}
Else
{
Return true;
}
}
}
 
Void SwapData (double & a, double & B)
{
Double c =;
A = B;
B = c;
Return;
}
 
Int main ()
{
Bool HaveInteraction = false;
Segment seg;
Segment rectSeg [4];
Double leftTopX, leftTopY, rightBottomX, rightBottomY;
 
Int tCase;
// Cin> tCase;
Scanf ("% d", & tCase );
While (tCase> 0)
{
TCase --;
 
// Cin> seg. point1.x> seg. point1.y> seg. point2.x> seg. point2.y;
Scanf ("% lf", & seg. point1.x, & seg. point1.y, & seg. point2.x, & seg. point2.y );
 
// Cin> leftTopX> leftTopY> rightBottomX> rightBottomY;
Scanf ("% lf", & leftTopX, & leftTopY, & rightBottomX, & rightBottomY );
 
If (leftTopX> rightBottomX)
SwapData (leftTopX, rightBottomX );
If (rightBottomY> leftTopY)
SwapData (rightBottomY, leftTopY );
 
// Start from |, take turn in clockwise.
RectSeg [0]. point1 = Point (leftTopX, rightBottomY );
RectSeg [0]. point2 = Point (leftTopX, leftTopY );
 
RectSeg [1]. point1 = Point (leftTopX, leftTopY );
RectSeg [1]. point2 = Point (rightBottomX, leftTopY );
 
RectSeg [2]. point1 = Point (rightBottomX, leftTopY );
RectSeg [2]. point2 = Point (rightBottomX, rightBottomY );
 
RectSeg [3]. point1 = Point (rightBottomX, rightBottomY );
RectSeg [3]. point2 = Point (leftTopX, rightBottomY );
 
HaveInteraction = false;
For (int I = 0; I <4; I ++)
{
If (LineInteractSegment (seg, rectSeg [I]) // the line interact with the rectangle, see if the segment interact the rectangle too.
{
If (SegmentInRect (seg, Point (leftTopX, leftTopY), Point (rightBottomX, rightBottomY )))
{
// Cout <"T" <endl;
Printf ("T \ n ");
HaveInteraction = true;
}
Break;
}
}
 
If (HaveInteraction = false)
{
// Cout <"F" <endl;
Printf ("F \ n ");
}

}
 
Return 0;
}

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.