Calculate the intersection of two line segments on the plane

Source: Internet
Author: User

Some of the image algorithm fragments that need to be used in GDI + are hard to find on the Internet. Therefore, they are stored on the Internet for your reference.

 

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. drawing;
Using system. Drawing. drawing2d;
Namespace drawlibrary
{
Public Enum endpoint: short
{
Start = 0,
End = 1
}
Public class line {
Point _ beginpoint;
Point _ endpoint;

Public line (point beginpoint, point endpoint)
{
_ Beginpoint = beginpoint;
_ Endpoint = endpoint;
}

Public point beginpoint
{
Get {return _ beginpoint ;}
}
Public point endpoint
{
Get {return _ endpoint ;}
Set
{
_ Endpoint = value;
}
}

}
Public class utils
{
Public static pointf pointtof (point P)
{
Return new pointf (P. X, p. y );
}

Public static point pointfromf (pointf P)
{
Return new point (INT) p. x, (INT) p. y );
}

Public static endpoint otherendpoint (endpoint which)
{
Return (which = endpoint. Start )? Endpoint. End: endpoint. Start;
}

Public static float distance (point P1, point P2)
{
Return (float) math. SQRT (math. Pow (p1.x-p2.x, 2) + math. Pow (p1.y-p2.y, 2 ));
}

Public static int area (rectangle rect)
{
Return rect. Width * rect. height;
}

Public static point centerofrectangle (rectangle rect)
{
Return new point (rect. Left + (rect. width/2), rect. Top + (rect. Height/2 ));
}

// Calculate the intersection of a line segment and a rectangle
Public static point calculateintersection (line, rectangle rect)
{
If (rect. isempty)
Throw new argumentexception ("cannot be calculated with an empty region .");

// Corners
Point topleft = new point (rect. Left, rect. Top );
Point topright = new point (rect. Right, rect. Top );
Point bottomleft = new point (rect. Left, rect. Bottom );
Point bottomright = new point (rect. Right, rect. Bottom );

// Boundaries
Line Top = new line (topleft, topright );
Line left = new line (topleft, bottomleft );
Line right = new line (topright, bottomright );
Line bottom = new line (bottomleft, bottomright );

// Calculate intersection between line and any boundary
Point intersection = new point ();
If (intersection. isempty) intersection = calculateintersection (line, top );
If (intersection. isempty) intersection = calculateintersection (line, left );
If (intersection. isempty) intersection = calculateintersection (line, right );
If (intersection. isempty) intersection = calculateintersection (line, bottom );
Return intersection;
}

// Calculate the intersection of two line segments
Public static point calculateintersection (line line1, line line2)
{
Point P1, P2, P3, P4;
P1 = normalizebeginpoint (line1.beginpoint, line1.endpoint );
P2 = normalizeendpoint (line1.beginpoint, line1.endpoint );
P3 = normalizebeginpoint (line2.beginpoint, line2.endpoint );
P4 = normalizeendpoint (line2.beginpoint, line2.endpoint );

Int ua_num = (p4.x-p3.x) * (p1.y-p3.y)-(p4.y-p3.y) * (p1.x-p3.x );
Int ub_num = (p2.x-p1.x) * (p1.y-p3.y)-(p2.y-p1.y) * (p1.x-p3.x );
Int denom = (p4.y-p3.y) * (p2.x-p1.x)-(p4.x-p3.x) * (p2.y-p1.y );

If (denom = 0)
{
If (ua_num = 0 & ub_num = 0)
{
Point begin1 = normalizebeginpoint (line1.beginpoint, line1.endpoint );
Point begin2 = normalizebeginpoint (line2.beginpoint, line2.endpoint );
Return normalizeendpoint (begin1, begin2 );
}
Else
{
Return Point. empty;
}
}


Double UA = ua_num/(double) denom;
Double UB = ub_num/(double) denom;
If (0 <= UA & UA <= 1 & // does line segment line1 contain intersecting PT?
0 <= UB & UB <= 1) // does line segment line2 contain intersecting PT?
{
// Line segments intersect
Point intersection = new point ();
Intersection. x = (INT) (p1.x + UA * (p2.x-p1.x ));
Intersection. Y = (INT) (p1.y + UA * (p2.y-p1.y ));
Return intersection;
}
Else
{
Return Point. empty;
}
}

Public static double calculateangleinradians (line)
{
If (line. beginpoint. isempty | line. endpoint. isempty) throw new argumentexception ("invalid line end points ");

Double Hyp = utils. Distance (line. beginpoint, line. endpoint );
If (HYP = 0) throw new argumentexception ("can't calculate angle for zero-length line ");

Double Theta = (float) math. asin (line. endpoint. Y-line. beginpoint. Y)/Hyp );
If (line. endpoint. x <line. beginpoint. X) Theta = math. Pi-Theta;
Return Theta;
}

Public static double degreestoradians (double degrees)
{
Return (degrees/360) * (2 * Math. Pi );
}

Public static double radianstodegrees (double radians)
{
Return 360*(radians/(2 * Math. Pi ));
}

Private Static line normalizeline (line)
{
Return New Line (
Normalizebeginpoint (line. beginpoint, line. endpoint ),
Normalizeendpoint (line. beginpoint, line. endpoint)
);
}

Private Static point normalizebeginpoint (point P1, point P2)
{
If (p1.x <p2.x) return P1;
If (p1.x> p2.x) return P2;
If (p1.y <p2.y) return P1;
If (p1.y> p2.y) return P2;
Return P1; // p1 = P2
}

Private Static point normalizeendpoint (point P1, point P2)
{
If (p1.x <p2.x) return P2;
If (p1.x> p2.x) return P1;
If (p1.y <p2.y) return P2;
If (p1.y> p2.y) return P1;
Return P2; // p1 = P2
}
}
}

 

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.