Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:23:49+00:00 2026-06-05T20:23:49+00:00

I have three points, for example: Start 194 171 Right 216 131 Left 216

  • 0

I have three points, for example:

Start 194 171
Right 216 131
Left  216 203

I want to get all the points within that triangle. How would I do that efficiently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:23:50+00:00Added an answer on June 5, 2026 at 8:23 pm

    see z3nth10n’s answer for better input validation

    Introduction:

    The general idea was to get the triangle’s edges (y-Wise) for every x in it’s range, and then you have all the y’s that exist within the triangle for every single x, which with simple conversion turns into all points within the triangle.

    You can look at it as if you cut the triangle into stripes, each being of width 1.
    So for X=0, on the line between A and B, the Y is 6, and on the line between A and C, the Y is -2, so you can see that the stripe of X=0 is between -2 and 6. Therefore, you can tell that (0, -2) (0, -1) (0, 0) … (0, 5) (0, 6) are all in the triangle. Doing that for X’s between the smallest and the largest within the triangle, and you have all the points in the triangle!

    Speed:

    For the triangle (0, 0) (1, 8) (4, 6) – found 16 points.

    Done 1,000,000 times in 3.68 seconds.

    Implementation:

    public IEnumerable<Point> PointsInTriangle(Point pt1, Point pt2, Point pt3)
    {
        if (pt1.Y == pt2.Y && pt1.Y == pt3.Y)
        {
            throw new ArgumentException("The given points must form a triangle.");
        }
    
        Point tmp;
    
        if (pt2.X < pt1.X)
        {
            tmp = pt1;
            pt1 = pt2;
            pt2 = tmp;
        }
    
        if (pt3.X < pt2.X)
        {
            tmp = pt2;
            pt2 = pt3;
            pt3 = tmp;
    
            if (pt2.X < pt1.X)
            {
                tmp = pt1;
                pt1 = pt2;
                pt2 = tmp;
            }
        }
    
        var baseFunc = CreateFunc(pt1, pt3);
        var line1Func = pt1.X == pt2.X ? (x => pt2.Y) : CreateFunc(pt1, pt2);
    
        for (var x = pt1.X; x < pt2.X; x++)
        {
            int maxY;
            int minY = GetRange(line1Func(x), baseFunc(x), out maxY);
    
            for (var y = minY; y <= maxY; y++)
            {
                yield return new Point(x, y);
            }
        }
    
        var line2Func = pt2.X == pt3.X ? (x => pt2.Y) : CreateFunc(pt2, pt3);
    
        for (var x = pt2.X; x <= pt3.X; x++)
        {
            int maxY;
            int minY = GetRange(line2Func(x), baseFunc(x), out maxY);
    
            for (var y = minY; y <= maxY; y++)
            {
                yield return new Point(x, y);
            }
        }
    }
    
    private int GetRange(double y1, double y2, out int maxY)
    {
        if (y1 < y2)
        {
            maxY = (int)Math.Floor(y2);
            return (int)Math.Ceiling(y1);
        }
    
        maxY = (int)Math.Floor(y1);
        return (int)Math.Ceiling(y2);
    }
    
    private Func<int, double> CreateFunc(Point pt1, Point pt2)
    {
        var y0 = pt1.Y;
    
        if (y0 == pt2.Y)
        {
            return x => y0;
        }
    
        var m = (double)(pt2.Y - y0) / (pt2.X - pt1.X);
    
        return x => m * (x - pt1.X) + y0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the coordinates of three points on a plane. Let's call them X1,Y1,
I have a dataset consisting of a large collection of points in three dimensional
Have three divs in a container that I want to float over a large
I have three activities that I can call A, B and C. I want
I have a simple Three.js scene around which I want to move the camera.
There have been several questions posted to SO about floating-point representation. For example, the
Basically I have two 2D points and there is a line between them. A
So I have some function that receives N random 2D points. Is there any
Is there a way for me to have eclipse start and point to a
Have three classes User, Group and Field. Many to many relationship on User /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.