Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

wpf line intersect rectangle

    public static bool SegmentIntersectRectangle(
        double rectangleMinX,
        double rectangleMinY,
        double rectangleMaxX,
        double rectangleMaxY,
        double p1X,
        double p1Y,
        double p2X,
        double p2Y)
    {
        // Find min and max X for the segment
        double minX = p1X;
        double maxX = p2X;

        if (p1X > p2X)
        {
            minX = p2X;
            maxX = p1X;
        }

        // Find the intersection of the segment's and rectangle's x-projections
        if (maxX > rectangleMaxX)
        {
            maxX = rectangleMaxX;
        }

        if (minX < rectangleMinX)
        {
            minX = rectangleMinX;
        }

        if (minX > maxX) // If their projections do not intersect return false
        {
            return false;
        }

        // Find corresponding min and max Y for min and max X we found before
        double minY = p1Y;
        double maxY = p2Y;

        double dx = p2X - p1X;

        if (Math.Abs(dx) > 0.0000001)
        {
            double a = (p2Y - p1Y)/dx;
            double b = p1Y - a*p1X;
            minY = a*minX + b;
            maxY = a*maxX + b;
        }

        if (minY > maxY)
        {
            double tmp = maxY;
            maxY = minY;
            minY = tmp;
        }

        // Find the intersection of the segment's and rectangle's y-projections
        if (maxY > rectangleMaxY)
        {
            maxY = rectangleMaxY;
        }

        if (minY < rectangleMinY)
        {
            minY = rectangleMinY;
        }

        if (minY > maxY) // If Y-projections do not intersect return false
        {
            return false;
        }

        return true;
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: vb.net delete a line from text file 
Csharp :: Smooth Sentences c# 
Csharp :: Acrylic UWP Title bar C# 
Csharp :: asp.net core mvc not triggering client side validation 
Csharp :: How to compile just one file in c# 
Csharp :: universities in greece 
Csharp :: c# null coalescing operator 
Csharp :: c# hashset 
Csharp :: selenium webdriver what browser am i using? 
Csharp :: unity how to check object position 
Csharp :: c# datagridview filter 
Csharp :: app rating within game in unity 
Csharp :: how to check if time is between two timespans in c# 
Csharp :: populate combobox from array c# 
Csharp :: c# get pixel from bitmap click 
Csharp :: urp set postprocessing value 
Csharp :: wie macht man eine schleife in c# 
Csharp :: how to show error xtramessagebox in devexpress c# 
Csharp :: git change remote origin 
Html :: email regex html pattern 
Html :: whitespace in html 
Html :: meta author 
Html :: disable html form input autocomplete autofill 
Html :: fa fa globe 
Html :: enctype= multipart/form-data 
Html :: Making a Phone number a link 
Html :: dropdown first option not selectable 
Html :: how to move all html files from one directory to other using python 
Html :: auto update copyright year html 
Html :: How to add a browser tab icon (favicon)? 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =